blob: 466eede3527cf581724b19a80230f1bbd3abecb6 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner29219d92013-04-16 20:19:12 -070020import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070022import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Craig Mautner2420ead2013-04-01 17:13:20 -070023import static com.android.server.am.ActivityManagerService.localLOGV;
Craig Mautner23ac33b2013-04-01 16:26:35 -070024import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070025import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Craig Mautner6170f732013-04-02 13:05:23 -070027import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070029import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070030import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
31import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070032import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070033import static com.android.server.am.ActivityManagerService.TAG;
Craig Mautner84984fa2014-06-19 11:19:20 -070034import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
35import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070037
Craig Mautner2420ead2013-04-01 17:13:20 -070038import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080040import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityOptions;
42import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import android.app.IActivityContainer;
44import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070048import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070049import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070051import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040052import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040053import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070055import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070056import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070057import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.pm.ApplicationInfo;
61import android.content.pm.PackageManager;
62import android.content.pm.ResolveInfo;
63import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080064import android.graphics.Point;
65import android.hardware.display.DisplayManager;
66import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080067import android.hardware.display.DisplayManagerGlobal;
68import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080069import android.hardware.input.InputManager;
70import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070071import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070072import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070073import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070074import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070075import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070076import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070077import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070078import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070079import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070080import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070081import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040082import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070083import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070084import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040085import android.provider.Settings;
86import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070087import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070088import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070089import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070090import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080091import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070092
Craig Mautner4a1cb222013-12-04 16:14:06 -080093import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080094import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080095import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080096import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080097import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070098import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070099import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800100import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700101import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400102import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400103import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800104import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700105import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700106import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700107
justinzhang5286d3f2014-05-12 17:06:01 -0400108
Craig Mautner8d341ef2013-03-26 09:03:27 -0700109import java.io.FileDescriptor;
110import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700111import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700112import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700113import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700114
Craig Mautner4a1cb222013-12-04 16:14:06 -0800115public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700116 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
117 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
118 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700119 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700120 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700121 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700122 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
123 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner30bd3bb2014-11-13 00:55:53 +0000124 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700125 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700126
Craig Mautner2219a1b2013-03-25 09:44:30 -0700127 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700128
Craig Mautnerf3333272013-04-22 10:55:53 -0700129 /** How long we wait until giving up on the last activity telling us it is idle. */
130 static final int IDLE_TIMEOUT = 10*1000;
131
Craig Mautner0eea92c2013-05-16 13:35:39 -0700132 /** How long we can hold the sleep wake lock before giving up. */
133 static final int SLEEP_TIMEOUT = 5*1000;
134
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700135 // How long we can hold the launch wake lock before giving up.
136 static final int LAUNCH_TIMEOUT = 10*1000;
137
Craig Mautner05d29032013-05-03 13:40:13 -0700138 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
139 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
140 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700141 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700142 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800143 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
144 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
145 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700146 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400147 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
148 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700149 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700150 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700151 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800152
Craig Mautner4504de52013-12-20 09:06:56 -0800153 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700154
Jason Monk62515be2014-05-21 16:06:19 -0400155 private static final String LOCK_TASK_TAG = "Lock-to-App";
156
justinzhang5286d3f2014-05-12 17:06:01 -0400157 /** Status Bar Service **/
158 private IBinder mToken = new Binder();
159 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400160 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400161
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700162 // For debugging to make sure the caller when acquiring/releasing our
163 // wake lock is the system process.
164 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700165
Craig Mautner27084302013-03-25 08:05:25 -0700166 final ActivityManagerService mService;
167
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700168 final ActivityStackSupervisorHandler mHandler;
169
170 /** Short cut */
171 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800172 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700173
Craig Mautner8d341ef2013-03-26 09:03:27 -0700174 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700175 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700176
177 /** Task identifier that activities are currently being started in. Incremented each time a
178 * new task is created. */
179 private int mCurTaskId = 0;
180
Craig Mautner2420ead2013-04-01 17:13:20 -0700181 /** The current user */
182 private int mCurrentUser;
183
Craig Mautnere0a38842013-12-16 16:14:02 -0800184 /** The stack containing the launcher app. Assumed to always be attached to
185 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700186 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700187
Craig Mautnere0a38842013-12-16 16:14:02 -0800188 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700189 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700190
Craig Mautner4a1cb222013-12-04 16:14:06 -0800191 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
192 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800193 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800194 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700195
196 /** List of activities that are waiting for a new activity to become visible before completing
197 * whatever operation they are supposed to do. */
198 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
199
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700200 /** List of processes waiting to find out about the next visible activity. */
201 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
202 new ArrayList<IActivityManager.WaitResult>();
203
204 /** List of processes waiting to find out about the next launched activity. */
205 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
206 new ArrayList<IActivityManager.WaitResult>();
207
Craig Mautnerde4ef022013-04-07 19:01:33 -0700208 /** List of activities that are ready to be stopped, but waiting for the next activity to
209 * settle down before doing so. */
210 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
211
Craig Mautnerf3333272013-04-22 10:55:53 -0700212 /** List of activities that are ready to be finished, but waiting for the previous activity to
213 * settle down before doing so. It contains ActivityRecord objects. */
214 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
215
Craig Mautner0eea92c2013-05-16 13:35:39 -0700216 /** List of activities that are in the process of going to sleep. */
217 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
218
Craig Mautnerf3333272013-04-22 10:55:53 -0700219 /** Used on user changes */
220 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
221
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700222 /** Used to queue up any background users being started */
223 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
224
Craig Mautnerde4ef022013-04-07 19:01:33 -0700225 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
226 * is being brought in front of us. */
227 boolean mUserLeaving = false;
228
Craig Mautner0eea92c2013-05-16 13:35:39 -0700229 /** Set when we have taken too long waiting to go to sleep. */
230 boolean mSleepTimeout = false;
231
Jose Lima58e66d62014-05-27 20:00:27 -0700232 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
233 * setWindowManager is called. **/
234 private boolean mLeanbackOnlyDevice;
235
Craig Mautner0eea92c2013-05-16 13:35:39 -0700236 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700237 * We don't want to allow the device to go to sleep while in the process
238 * of launching an activity. This is primarily to allow alarm intent
239 * receivers to launch an activity and get that to run before the device
240 * goes back to sleep.
241 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700242 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700243
244 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700245 * Set when the system is going to sleep, until we have
246 * successfully paused the current activity and released our wake lock.
247 * At that point the system is allowed to actually sleep.
248 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700249 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700250
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700251 /** Stack id of the front stack when user switched, indexed by userId. */
252 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700253
Craig Mautner4504de52013-12-20 09:06:56 -0800254 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800255 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700256 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800257
258 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700259 private final SparseArray<ActivityDisplay> mActivityDisplays =
260 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800261
Jeff Brownca9bc702014-02-11 14:32:56 -0800262 InputManagerInternal mInputManagerInternal;
263
Craig Mautneraea74a52014-03-08 14:23:10 -0800264 /** If non-null then the task specified remains in front and no other tasks may be started
265 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400266 TaskRecord mLockTaskModeTask;
Jason Monka8f569c2014-07-07 12:15:21 -0400267 /** Whether lock task has been entered by an authorized app and cannot
268 * be exited. */
269 private boolean mLockTaskIsLocked;
Jason Monk62515be2014-05-21 16:06:19 -0400270 /**
271 * Notifies the user when entering/exiting lock-task.
272 */
273 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800274
Craig Mautneree36c772014-07-16 14:56:05 -0700275 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
276 = new ArrayList<PendingActivityLaunch>();
277
Craig Mautner42d04db2014-11-06 12:13:23 -0800278 /** Used to keep resumeTopActivityLocked() from being entered recursively */
279 boolean inResumeTopActivity;
280
Craig Mautneree36c772014-07-16 14:56:05 -0700281 /**
282 * Description of a request to start a new activity, which has been held
283 * due to app switches being disabled.
284 */
285 static class PendingActivityLaunch {
286 final ActivityRecord r;
287 final ActivityRecord sourceRecord;
288 final int startFlags;
289 final ActivityStack stack;
290
291 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
292 int _startFlags, ActivityStack _stack) {
293 r = _r;
294 sourceRecord = _sourceRecord;
295 startFlags = _startFlags;
296 stack = _stack;
297 }
298 }
299
Craig Mautner4a1cb222013-12-04 16:14:06 -0800300 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700301 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700302 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
303 }
304
305 /**
306 * At the time when the constructor runs, the power manager has not yet been
307 * initialized. So we initialize our wakelocks afterwards.
308 */
309 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800310 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700311 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700312 mLaunchingActivity =
313 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
314 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700315 }
316
justinzhang5286d3f2014-05-12 17:06:01 -0400317 // This function returns a IStatusBarService. The value is from ServiceManager.
318 // getService and is cached.
319 private IStatusBarService getStatusBarService() {
320 synchronized (mService) {
321 if (mStatusBarService == null) {
322 mStatusBarService = IStatusBarService.Stub.asInterface(
323 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
324 if (mStatusBarService == null) {
325 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
326 }
327 }
328 return mStatusBarService;
329 }
330 }
331
Jason Monk35c62a42014-06-17 10:24:47 -0400332 private IDevicePolicyManager getDevicePolicyManager() {
333 synchronized (mService) {
334 if (mDevicePolicyManager == null) {
335 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
336 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
337 if (mDevicePolicyManager == null) {
338 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
339 }
340 }
341 return mDevicePolicyManager;
342 }
343 }
344
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700345 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800346 synchronized (mService) {
347 mWindowManager = wm;
348
349 mDisplayManager =
350 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
351 mDisplayManager.registerDisplayListener(this, null);
352
353 Display[] displays = mDisplayManager.getDisplays();
354 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
355 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800356 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700357 if (activityDisplay.mDisplay == null) {
358 throw new IllegalStateException("Default Display does not exist");
359 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800360 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800361 }
362
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700363 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800364 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800365
366 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700367
368 // Initialize this here, now that we can get a valid reference to PackageManager.
369 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800370 }
Craig Mautner27084302013-03-25 08:05:25 -0700371 }
372
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200373 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700374 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200375 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700376 }
377
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700378 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700380 }
381
Craig Mautnerde4ef022013-04-07 19:01:33 -0700382 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700384 }
385
Craig Mautner4a1cb222013-12-04 16:14:06 -0800386 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
387 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700388 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800389 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
390 if (parent != null) {
391 stack = parent.task.stack;
392 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800393 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800394 if (stacks != null && !stacks.isEmpty()) {
395 return stack == stacks.get(stacks.size() - 1);
396 }
397 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700398 }
399
Craig Mautnerde4ef022013-04-07 19:01:33 -0700400 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800401 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800402 int topNdx = stacks.size() - 1;
403 if (topNdx <= 0) {
404 return;
405 }
406 ActivityStack topStack = stacks.get(topNdx);
407 final boolean homeInFront = topStack == mHomeStack;
408 if (homeInFront != toFront) {
409 mLastFocusedStack = topStack;
410 stacks.remove(mHomeStack);
411 stacks.add(toFront ? topNdx : 0, mHomeStack);
412 mFocusedStack = stacks.get(topNdx);
413 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
414 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700415 }
416 }
417
Craig Mautner84984fa2014-06-19 11:19:20 -0700418 void moveHomeStackTaskToTop(int homeStackTaskType) {
419 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
420 mWindowManager.showRecentApps();
421 return;
422 }
Craig Mautner69ada552013-04-18 13:51:51 -0700423 moveHomeStack(true);
Craig Mautner84984fa2014-06-19 11:19:20 -0700424 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700425 }
426
Craig Mautner84984fa2014-06-19 11:19:20 -0700427 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700428 if (!mService.mBooting && !mService.mBooted) {
429 // Not ready yet!
430 return false;
431 }
432
Craig Mautner84984fa2014-06-19 11:19:20 -0700433 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
434 mWindowManager.showRecentApps();
435 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700436 }
Craig Mautner84984fa2014-06-19 11:19:20 -0700437 moveHomeStackTaskToTop(homeStackTaskType);
438 if (prev != null) {
439 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
440 }
441
Craig Mautnera8a90e02013-06-28 15:24:50 -0700442 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700443 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000444 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700445 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700446 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700447 }
448 return mService.startHomeActivityLocked(mCurrentUser);
449 }
450
Craig Mautner8d341ef2013-03-26 09:03:27 -0700451 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800452 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800453 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800454 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800455 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
456 ActivityStack stack = stacks.get(stackNdx);
457 TaskRecord task = stack.taskForIdLocked(id);
458 if (task != null) {
459 return task;
460 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700461 }
462 }
463 return null;
464 }
465
Craig Mautner6170f732013-04-02 13:05:23 -0700466 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800467 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800468 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800469 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800470 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
471 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
472 if (r != null) {
473 return r;
474 }
Craig Mautner6170f732013-04-02 13:05:23 -0700475 }
476 }
477 return null;
478 }
479
Craig Mautneref73ee12014-04-23 11:45:37 -0700480 void setNextTaskId(int taskId) {
481 if (taskId > mCurTaskId) {
482 mCurTaskId = taskId;
483 }
484 }
485
Craig Mautner8d341ef2013-03-26 09:03:27 -0700486 int getNextTaskId() {
487 do {
488 mCurTaskId++;
489 if (mCurTaskId <= 0) {
490 mCurTaskId = 1;
491 }
492 } while (anyTaskForIdLocked(mCurTaskId) != null);
493 return mCurTaskId;
494 }
495
Craig Mautnerde4ef022013-04-07 19:01:33 -0700496 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700497 ActivityStack stack = getFocusedStack();
498 if (stack == null) {
499 return null;
500 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700501 ActivityRecord resumedActivity = stack.mResumedActivity;
502 if (resumedActivity == null || resumedActivity.app == null) {
503 resumedActivity = stack.mPausingActivity;
504 if (resumedActivity == null || resumedActivity.app == null) {
505 resumedActivity = stack.topRunningActivityLocked(null);
506 }
507 }
508 return resumedActivity;
509 }
510
Dianne Hackbornff072722014-09-24 10:56:28 -0700511 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700512 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800513 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800514 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
515 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800516 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
517 final ActivityStack stack = stacks.get(stackNdx);
518 if (!isFrontStack(stack)) {
519 continue;
520 }
521 ActivityRecord hr = stack.topRunningActivityLocked(null);
522 if (hr != null) {
523 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
524 && processName.equals(hr.processName)) {
525 try {
George Mount2c92c972014-03-20 09:38:23 -0700526 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800527 didSomething = true;
528 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700529 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800530 Slog.w(TAG, "Exception in new application when starting activity "
531 + hr.intent.getComponent().flattenToShortString(), e);
532 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700533 }
Craig Mautner20e72272013-04-01 13:45:53 -0700534 }
Craig Mautner20e72272013-04-01 13:45:53 -0700535 }
536 }
537 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700538 if (!didSomething) {
539 ensureActivitiesVisibleLocked(null, 0);
540 }
Craig Mautner20e72272013-04-01 13:45:53 -0700541 return didSomething;
542 }
543
544 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800545 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
546 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800547 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
548 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800549 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 continue;
551 }
552 final ActivityRecord resumedActivity = stack.mResumedActivity;
553 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800554 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
555 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800556 return false;
557 }
Craig Mautner20e72272013-04-01 13:45:53 -0700558 }
559 }
560 return true;
561 }
562
Craig Mautnerde4ef022013-04-07 19:01:33 -0700563 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800564 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
565 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800566 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
567 final ActivityStack stack = stacks.get(stackNdx);
568 if (isFrontStack(stack)) {
569 final ActivityRecord r = stack.mResumedActivity;
570 if (r != null && r.state != ActivityState.RESUMED) {
571 return false;
572 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700573 }
574 }
575 }
576 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800577 if (DEBUG_STACK) Slog.d(TAG,
578 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
579 mLastFocusedStack + " to=" + mFocusedStack);
580 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700581 return true;
582 }
583
584 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800585 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
586 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800587 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
588 final ActivityStack stack = stacks.get(stackNdx);
589 final ActivityRecord r = stack.mResumedActivity;
590 if (r != null && (!r.nowVisible || r.waitingVisible)) {
591 return false;
592 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700593 }
594 }
595 return true;
596 }
597
Craig Mautner2acc3892013-09-23 10:28:14 -0700598 /**
599 * Pause all activities in either all of the stacks or just the back stacks.
600 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700601 * @return true if any activity was paused as a result of this call.
602 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700603 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700604 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800605 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
606 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
608 final ActivityStack stack = stacks.get(stackNdx);
609 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
610 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
611 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700612 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
613 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800614 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700615 }
616 }
617 return someActivityPaused;
618 }
619
Craig Mautnerde4ef022013-04-07 19:01:33 -0700620 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700621 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800622 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
623 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800624 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
625 final ActivityStack stack = stacks.get(stackNdx);
626 final ActivityRecord r = stack.mPausingActivity;
627 if (r != null && r.state != ActivityState.PAUSED
628 && r.state != ActivityState.STOPPED
629 && r.state != ActivityState.STOPPING) {
630 if (DEBUG_STATES) {
631 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
632 pausing = false;
633 } else {
634 return false;
635 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700636 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700637 }
638 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700639 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700640 }
641
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700642 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
643 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500644 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800645 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
646 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
647 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
648 final ActivityStack stack = stacks.get(stackNdx);
649 if (stack.mResumedActivity != null &&
650 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700651 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800652 }
653 }
654 }
655 }
656
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700657 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700658 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700659 }
660
661 void sendWaitingVisibleReportLocked(ActivityRecord r) {
662 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700663 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700664 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700665 if (w.who == null) {
666 changed = true;
667 w.timeout = false;
668 if (r != null) {
669 w.who = new ComponentName(r.info.packageName, r.info.name);
670 }
671 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
672 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700673 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700674 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700675 if (changed) {
676 mService.notifyAll();
677 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700678 }
679
680 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
681 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700682 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700683 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700684 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700685 if (w.who == null) {
686 changed = true;
687 w.timeout = timeout;
688 if (r != null) {
689 w.who = new ComponentName(r.info.packageName, r.info.name);
690 }
691 w.thisTime = thisTime;
692 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700693 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700694 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700695 if (changed) {
696 mService.notifyAll();
697 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700698 }
699
Craig Mautner29219d92013-04-16 20:19:12 -0700700 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700701 final ActivityStack focusedStack = getFocusedStack();
702 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
703 if (r != null) {
704 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700705 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700706
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800708 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800709 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
710 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700711 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700712 r = stack.topRunningActivityLocked(null);
713 if (r != null) {
714 return r;
715 }
716 }
717 }
718 return null;
719 }
720
Dianne Hackborn09233282014-04-30 11:33:59 -0700721 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700722 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800723 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
724 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800725 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800727 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
729 final ActivityStack stack = stacks.get(stackNdx);
730 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
731 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700732 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700733 }
734 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700735
736 // The lists are already sorted from most recent to oldest. Just pull the most recent off
737 // each list and add it to list. Stop when all lists are empty or maxNum reached.
738 while (maxNum > 0) {
739 long mostRecentActiveTime = Long.MIN_VALUE;
740 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800741 final int numTaskLists = runningTaskLists.size();
742 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
743 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700744 if (!stackTaskList.isEmpty()) {
745 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
746 if (lastActiveTime > mostRecentActiveTime) {
747 mostRecentActiveTime = lastActiveTime;
748 selectedStackList = stackTaskList;
749 }
750 }
751 }
752 if (selectedStackList != null) {
753 list.add(selectedStackList.remove(0));
754 --maxNum;
755 } else {
756 break;
757 }
758 }
Craig Mautner20e72272013-04-01 13:45:53 -0700759 }
760
Craig Mautner23ac33b2013-04-01 16:26:35 -0700761 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700762 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700763 // Collect information about the target of the Intent.
764 ActivityInfo aInfo;
765 try {
766 ResolveInfo rInfo =
767 AppGlobals.getPackageManager().resolveIntent(
768 intent, resolvedType,
769 PackageManager.MATCH_DEFAULT_ONLY
770 | ActivityManagerService.STOCK_PM_FLAGS, userId);
771 aInfo = rInfo != null ? rInfo.activityInfo : null;
772 } catch (RemoteException e) {
773 aInfo = null;
774 }
775
776 if (aInfo != null) {
777 // Store the found target back into the intent, because now that
778 // we have it we never want to do this again. For example, if the
779 // user navigates back to this point in the history, we should
780 // always restart the exact same activity.
781 intent.setComponent(new ComponentName(
782 aInfo.applicationInfo.packageName, aInfo.name));
783
784 // Don't debug things in the system process
785 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
786 if (!aInfo.processName.equals("system")) {
787 mService.setDebugApp(aInfo.processName, true, false);
788 }
789 }
790
791 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
792 if (!aInfo.processName.equals("system")) {
793 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
794 }
795 }
796
Jeff Hao1b012d32014-08-20 10:35:34 -0700797 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700798 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700799 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700800 }
801 }
802 }
803 return aInfo;
804 }
805
Craig Mautner2219a1b2013-03-25 09:44:30 -0700806 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700807 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE);
Dianne Hackborn95465202014-09-15 16:21:55 -0700808 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
809 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700810 }
811
Craig Mautner23ac33b2013-04-01 16:26:35 -0700812 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700813 String callingPackage, Intent intent, String resolvedType,
814 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700815 IBinder resultTo, String resultWho, int requestCode, int startFlags,
816 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700817 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700818 // Refuse possible leaked file descriptors
819 if (intent != null && intent.hasFileDescriptors()) {
820 throw new IllegalArgumentException("File descriptors passed in Intent");
821 }
822 boolean componentSpecified = intent.getComponent() != null;
823
824 // Don't modify the client's object!
825 intent = new Intent(intent);
826
827 // Collect information about the target of the Intent.
828 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700829 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700830
Craig Mautnere0a38842013-12-16 16:14:02 -0800831 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700832 synchronized (mService) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700833 final int realCallingPid = Binder.getCallingPid();
834 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700835 int callingPid;
836 if (callingUid >= 0) {
837 callingPid = -1;
838 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700839 callingPid = realCallingPid;
840 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700841 } else {
842 callingPid = callingUid = -1;
843 }
844
Craig Mautnere0a38842013-12-16 16:14:02 -0800845 final ActivityStack stack;
846 if (container == null || container.mStack.isOnHomeDisplay()) {
847 stack = getFocusedStack();
848 } else {
849 stack = container.mStack;
850 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700851 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700852 && mService.mConfiguration.diff(config) != 0;
853 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700854 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700855
856 final long origId = Binder.clearCallingIdentity();
857
858 if (aInfo != null &&
859 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
860 // This may be a heavy-weight process! Check to see if we already
861 // have another, different heavy-weight process running.
862 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
863 if (mService.mHeavyWeightProcess != null &&
864 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
865 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700866 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700867 if (caller != null) {
868 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
869 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700870 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700871 } else {
872 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700873 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700874 + intent.toString());
875 ActivityOptions.abort(options);
876 return ActivityManager.START_PERMISSION_DENIED;
877 }
878 }
879
880 IIntentSender target = mService.getIntentSenderLocked(
881 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700882 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700883 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
884 | PendingIntent.FLAG_ONE_SHOT, null);
885
886 Intent newIntent = new Intent();
887 if (requestCode >= 0) {
888 // Caller is requesting a result.
889 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
890 }
891 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
892 new IntentSender(target));
893 if (mService.mHeavyWeightProcess.activities.size() > 0) {
894 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
895 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
896 hist.packageName);
897 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
898 hist.task.taskId);
899 }
900 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
901 aInfo.packageName);
902 newIntent.setFlags(intent.getFlags());
903 newIntent.setClassName("android",
904 HeavyWeightSwitcherActivity.class.getName());
905 intent = newIntent;
906 resolvedType = null;
907 caller = null;
908 callingUid = Binder.getCallingUid();
909 callingPid = Binder.getCallingPid();
910 componentSpecified = true;
911 try {
912 ResolveInfo rInfo =
913 AppGlobals.getPackageManager().resolveIntent(
914 intent, null,
915 PackageManager.MATCH_DEFAULT_ONLY
916 | ActivityManagerService.STOCK_PM_FLAGS, userId);
917 aInfo = rInfo != null ? rInfo.activityInfo : null;
918 aInfo = mService.getActivityInfoForUser(aInfo, userId);
919 } catch (RemoteException e) {
920 aInfo = null;
921 }
922 }
923 }
924 }
925
Dianne Hackborn91097de2014-04-04 18:02:06 -0700926 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
927 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700928 requestCode, callingPid, callingUid, callingPackage,
929 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700930 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700931
Craig Mautner85c11a82014-07-17 12:38:18 -0700932 Binder.restoreCallingIdentity(origId);
933
Craig Mautnerde4ef022013-04-07 19:01:33 -0700934 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700935 // If the caller also wants to switch to a new configuration,
936 // do so now. This allows a clean switch, as we are waiting
937 // for the current activity to pause (so we will not destroy
938 // it), and have not yet started the next activity.
939 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
940 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700941 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700942 if (DEBUG_CONFIGURATION) Slog.v(TAG,
943 "Updating to new configuration after starting activity.");
944 mService.updateConfigurationLocked(config, null, false, false);
945 }
946
Craig Mautner23ac33b2013-04-01 16:26:35 -0700947 if (outResult != null) {
948 outResult.result = res;
949 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700950 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700951 do {
952 try {
953 mService.wait();
954 } catch (InterruptedException e) {
955 }
956 } while (!outResult.timeout && outResult.who == null);
957 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700958 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700959 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700960 outResult.timeout = false;
961 outResult.who = new ComponentName(r.info.packageName, r.info.name);
962 outResult.totalTime = 0;
963 outResult.thisTime = 0;
964 } else {
965 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700966 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700967 do {
968 try {
969 mService.wait();
970 } catch (InterruptedException e) {
971 }
972 } while (!outResult.timeout && outResult.who == null);
973 }
974 }
975 }
976
977 return res;
978 }
979 }
980
981 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
982 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
983 Bundle options, int userId) {
984 if (intents == null) {
985 throw new NullPointerException("intents is null");
986 }
987 if (resolvedTypes == null) {
988 throw new NullPointerException("resolvedTypes is null");
989 }
990 if (intents.length != resolvedTypes.length) {
991 throw new IllegalArgumentException("intents are length different than resolvedTypes");
992 }
993
Craig Mautner23ac33b2013-04-01 16:26:35 -0700994
995 int callingPid;
996 if (callingUid >= 0) {
997 callingPid = -1;
998 } else if (caller == null) {
999 callingPid = Binder.getCallingPid();
1000 callingUid = Binder.getCallingUid();
1001 } else {
1002 callingPid = callingUid = -1;
1003 }
1004 final long origId = Binder.clearCallingIdentity();
1005 try {
1006 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001007 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001008 for (int i=0; i<intents.length; i++) {
1009 Intent intent = intents[i];
1010 if (intent == null) {
1011 continue;
1012 }
1013
1014 // Refuse possible leaked file descriptors
1015 if (intent != null && intent.hasFileDescriptors()) {
1016 throw new IllegalArgumentException("File descriptors passed in Intent");
1017 }
1018
1019 boolean componentSpecified = intent.getComponent() != null;
1020
1021 // Don't modify the client's object!
1022 intent = new Intent(intent);
1023
1024 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001025 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001026 // TODO: New, check if this is correct
1027 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1028
1029 if (aInfo != null &&
1030 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
1031 != 0) {
1032 throw new IllegalArgumentException(
1033 "FLAG_CANT_SAVE_STATE not supported here");
1034 }
1035
1036 Bundle theseOptions;
1037 if (options != null && i == intents.length-1) {
1038 theseOptions = options;
1039 } else {
1040 theseOptions = null;
1041 }
Craig Mautner6170f732013-04-02 13:05:23 -07001042 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001043 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1044 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001045 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001046 if (res < 0) {
1047 return res;
1048 }
1049
1050 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1051 }
1052 }
1053 } finally {
1054 Binder.restoreCallingIdentity(origId);
1055 }
1056
1057 return ActivityManager.START_SUCCESS;
1058 }
1059
Craig Mautner2420ead2013-04-01 17:13:20 -07001060 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001061 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001062 throws RemoteException {
1063
1064 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001065 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001066 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001067
1068 // schedule launch ticks to collect information about slow apps.
1069 r.startLaunchTickingLocked();
1070
1071 // Have the window manager re-evaluate the orientation of
1072 // the screen based on the new activity order. Note that
1073 // as a result of this, it can call back into the activity
1074 // manager with a new orientation. We don't care about that,
1075 // because the activity is not currently running so we are
1076 // just restarting it anyway.
1077 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001078 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001079 mService.mConfiguration,
1080 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1081 mService.updateConfigurationLocked(config, r, false, false);
1082 }
1083
1084 r.app = app;
1085 app.waitingToKill = null;
1086 r.launchCount++;
1087 r.lastLaunchTime = SystemClock.uptimeMillis();
1088
1089 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1090
1091 int idx = app.activities.indexOf(r);
1092 if (idx < 0) {
1093 app.activities.add(r);
1094 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001095 mService.updateLruProcessLocked(app, true, null);
1096 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001097
1098 final ActivityStack stack = r.task.stack;
1099 try {
1100 if (app.thread == null) {
1101 throw new RemoteException();
1102 }
1103 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001104 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001105 if (andResume) {
1106 results = r.results;
1107 newIntents = r.newIntents;
1108 }
1109 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1110 + " icicle=" + r.icicle
1111 + " with results=" + results + " newIntents=" + newIntents
1112 + " andResume=" + andResume);
1113 if (andResume) {
1114 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1115 r.userId, System.identityHashCode(r),
1116 r.task.taskId, r.shortComponentName);
1117 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001118 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001119 // Home process is the root process of the task.
1120 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001121 }
1122 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1123 r.sleeping = false;
1124 r.forceNewConfig = false;
1125 mService.showAskCompatModeDialogLocked(r);
1126 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1127 String profileFile = null;
1128 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001129 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1130 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1131 mService.mProfileProc = app;
1132 profileFile = mService.mProfileFile;
1133 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001134 }
1135 }
1136 app.hasShownUi = true;
1137 app.pendingUiClean = true;
1138 if (profileFd != null) {
1139 try {
1140 profileFd = profileFd.dup();
1141 } catch (IOException e) {
1142 if (profileFd != null) {
1143 try {
1144 profileFd.close();
1145 } catch (IOException o) {
1146 }
1147 profileFd = null;
1148 }
1149 }
1150 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001151
Jeff Hao1b012d32014-08-20 10:35:34 -07001152 ProfilerInfo profilerInfo = profileFile != null
1153 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1154 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001155 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001156 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001157 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001158 r.compat, r.launchedFromPackage, r.task.voiceInteractor, app.repProcState,
1159 r.icicle, r.persistentState, results, newIntents, !andResume,
1160 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001161
1162 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1163 // This may be a heavy-weight process! Note that the package
1164 // manager will ensure that only activity can run in the main
1165 // process of the .apk, which is the only thing that will be
1166 // considered heavy-weight.
1167 if (app.processName.equals(app.info.packageName)) {
1168 if (mService.mHeavyWeightProcess != null
1169 && mService.mHeavyWeightProcess != app) {
1170 Slog.w(TAG, "Starting new heavy weight process " + app
1171 + " when already running "
1172 + mService.mHeavyWeightProcess);
1173 }
1174 mService.mHeavyWeightProcess = app;
1175 Message msg = mService.mHandler.obtainMessage(
1176 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1177 msg.obj = r;
1178 mService.mHandler.sendMessage(msg);
1179 }
1180 }
1181
1182 } catch (RemoteException e) {
1183 if (r.launchFailed) {
1184 // This is the second time we failed -- finish activity
1185 // and give up.
1186 Slog.e(TAG, "Second failure launching "
1187 + r.intent.getComponent().flattenToShortString()
1188 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001189 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001190 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1191 "2nd-crash", false);
1192 return false;
1193 }
1194
1195 // This is the first time we failed -- restart process and
1196 // retry.
1197 app.activities.remove(r);
1198 throw e;
1199 }
1200
1201 r.launchFailed = false;
1202 if (stack.updateLRUListLocked(r)) {
1203 Slog.w(TAG, "Activity " + r
1204 + " being launched, but already in LRU list");
1205 }
1206
1207 if (andResume) {
1208 // As part of the process of launching, ActivityThread also performs
1209 // a resume.
1210 stack.minimalResumeActivityLocked(r);
1211 } else {
1212 // This activity is not starting in the resumed state... which
1213 // should look like we asked it to pause+stop (but remain visible),
1214 // and it has done so and reported back the current icicle and
1215 // other state.
1216 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1217 + " (starting in stopped state)");
1218 r.state = ActivityState.STOPPED;
1219 r.stopped = true;
1220 }
1221
1222 // Launch the new version setup screen if needed. We do this -after-
1223 // launching the initial activity (that is, home), so that it can have
1224 // a chance to initialize itself while in the background, making the
1225 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001226 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001227 mService.startSetupActivityLocked();
1228 }
1229
Dianne Hackborn465fa392014-09-14 14:21:18 -07001230 // Update any services we are bound to that might care about whether
1231 // their client may have activities.
1232 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1233
Craig Mautner2420ead2013-04-01 17:13:20 -07001234 return true;
1235 }
1236
Craig Mautnere79d42682013-04-01 19:01:53 -07001237 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001238 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001239 // Is this activity's application already running?
1240 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001241 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001242
1243 r.task.stack.setLaunchTime(r);
1244
1245 if (app != null && app.thread != null) {
1246 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001247 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1248 || !"android".equals(r.info.packageName)) {
1249 // Don't add this if it is a platform component that is marked
1250 // to run in multiple processes, because this is actually
1251 // part of the framework so doesn't make sense to track as a
1252 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001253 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1254 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001255 }
George Mount2c92c972014-03-20 09:38:23 -07001256 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001257 return;
1258 } catch (RemoteException e) {
1259 Slog.w(TAG, "Exception when starting activity "
1260 + r.intent.getComponent().flattenToShortString(), e);
1261 }
1262
1263 // If a dead object exception was thrown -- fall through to
1264 // restart the application.
1265 }
1266
1267 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001268 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001269 }
1270
Craig Mautner6170f732013-04-02 13:05:23 -07001271 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001272 Intent intent, String resolvedType, ActivityInfo aInfo,
1273 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1274 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001275 int callingPid, int callingUid, String callingPackage,
1276 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001277 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1278 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001279 int err = ActivityManager.START_SUCCESS;
1280
1281 ProcessRecord callerApp = null;
1282 if (caller != null) {
1283 callerApp = mService.getRecordForAppLocked(caller);
1284 if (callerApp != null) {
1285 callingPid = callerApp.pid;
1286 callingUid = callerApp.info.uid;
1287 } else {
1288 Slog.w(TAG, "Unable to find app for caller " + caller
1289 + " (pid=" + callingPid + ") when starting: "
1290 + intent.toString());
1291 err = ActivityManager.START_PERMISSION_DENIED;
1292 }
1293 }
1294
1295 if (err == ActivityManager.START_SUCCESS) {
1296 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1297 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001298 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001299 + " on display " + (container == null ? (mFocusedStack == null ?
1300 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1301 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1302 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001303 }
1304
1305 ActivityRecord sourceRecord = null;
1306 ActivityRecord resultRecord = null;
1307 if (resultTo != null) {
1308 sourceRecord = isInAnyStackLocked(resultTo);
1309 if (DEBUG_RESULTS) Slog.v(
1310 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1311 if (sourceRecord != null) {
1312 if (requestCode >= 0 && !sourceRecord.finishing) {
1313 resultRecord = sourceRecord;
1314 }
1315 }
1316 }
Craig Mautner6170f732013-04-02 13:05:23 -07001317
Dianne Hackborn91097de2014-04-04 18:02:06 -07001318 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001319
Dianne Hackborn95465202014-09-15 16:21:55 -07001320 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001321 // Transfer the result target from the source activity to the new
1322 // one being started, including any failures.
1323 if (requestCode >= 0) {
1324 ActivityOptions.abort(options);
1325 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1326 }
1327 resultRecord = sourceRecord.resultTo;
1328 resultWho = sourceRecord.resultWho;
1329 requestCode = sourceRecord.requestCode;
1330 sourceRecord.resultTo = null;
1331 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001332 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001333 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001334 if (sourceRecord.launchedFromUid == callingUid) {
1335 // The new activity is being launched from the same uid as the previous
1336 // activity in the flow, and asking to forward its result back to the
1337 // previous. In this case the activity is serving as a trampoline between
1338 // the two, so we also want to update its launchedFromPackage to be the
1339 // same as the previous activity. Note that this is safe, since we know
1340 // these two packages come from the same uid; the caller could just as
1341 // well have supplied that same package name itself. This specifially
1342 // deals with the case of an intent picker/chooser being launched in the app
1343 // flow to redirect to an activity picked by the user, where we want the final
1344 // activity to consider it to have been launched by the previous app activity.
1345 callingPackage = sourceRecord.launchedFromPackage;
1346 }
Craig Mautner6170f732013-04-02 13:05:23 -07001347 }
1348
1349 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1350 // We couldn't find a class that can handle the given Intent.
1351 // That's the end of that!
1352 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1353 }
1354
1355 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1356 // We couldn't find the specific class specified in the Intent.
1357 // Also the end of the line.
1358 err = ActivityManager.START_CLASS_NOT_FOUND;
1359 }
1360
Dianne Hackborn91097de2014-04-04 18:02:06 -07001361 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1362 && sourceRecord.task.voiceSession != null) {
1363 // If this activity is being launched as part of a voice session, we need
1364 // to ensure that it is safe to do so. If the upcoming activity will also
1365 // be part of the voice session, we can only launch it if it has explicitly
1366 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001367 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001368 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1369 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001370 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1371 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001372 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1373 }
1374 } catch (RemoteException e) {
1375 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1376 }
1377 }
1378 }
1379
1380 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1381 // If the caller is starting a new voice session, just make sure the target
1382 // is actually allowing it to run this way.
1383 try {
1384 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1385 intent, resolvedType)) {
1386 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1387 }
1388 } catch (RemoteException e) {
1389 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1390 }
1391 }
1392
louis_changcd5d1982014-08-01 10:09:08 +08001393 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1394
Craig Mautner6170f732013-04-02 13:05:23 -07001395 if (err != ActivityManager.START_SUCCESS) {
1396 if (resultRecord != null) {
1397 resultStack.sendActivityResultLocked(-1,
1398 resultRecord, resultWho, requestCode,
1399 Activity.RESULT_CANCELED, null);
1400 }
Craig Mautner6170f732013-04-02 13:05:23 -07001401 ActivityOptions.abort(options);
1402 return err;
1403 }
1404
1405 final int startAnyPerm = mService.checkPermission(
1406 START_ANY_ACTIVITY, callingPid, callingUid);
1407 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1408 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1409 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1410 if (resultRecord != null) {
1411 resultStack.sendActivityResultLocked(-1,
1412 resultRecord, resultWho, requestCode,
1413 Activity.RESULT_CANCELED, null);
1414 }
Craig Mautner6170f732013-04-02 13:05:23 -07001415 String msg;
1416 if (!aInfo.exported) {
1417 msg = "Permission Denial: starting " + intent.toString()
1418 + " from " + callerApp + " (pid=" + callingPid
1419 + ", uid=" + callingUid + ")"
1420 + " not exported from uid " + aInfo.applicationInfo.uid;
1421 } else {
1422 msg = "Permission Denial: starting " + intent.toString()
1423 + " from " + callerApp + " (pid=" + callingPid
1424 + ", uid=" + callingUid + ")"
1425 + " requires " + aInfo.permission;
1426 }
1427 Slog.w(TAG, msg);
1428 throw new SecurityException(msg);
1429 }
1430
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001431 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001432 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001433
Craig Mautner6170f732013-04-02 13:05:23 -07001434 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001435 try {
1436 // The Intent we give to the watcher has the extra data
1437 // stripped off, since it can contain private information.
1438 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001439 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001440 aInfo.applicationInfo.packageName);
1441 } catch (RemoteException e) {
1442 mService.mController = null;
1443 }
Ben Gruver5e207332013-04-03 17:41:37 -07001444 }
Craig Mautner6170f732013-04-02 13:05:23 -07001445
Ben Gruver5e207332013-04-03 17:41:37 -07001446 if (abort) {
1447 if (resultRecord != null) {
1448 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001449 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001450 }
Ben Gruver5e207332013-04-03 17:41:37 -07001451 // We pretend to the caller that it was really started, but
1452 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001453 ActivityOptions.abort(options);
1454 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001455 }
1456
1457 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001458 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001459 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001460 if (outActivity != null) {
1461 outActivity[0] = r;
1462 }
1463
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001464 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001465 if (voiceSession == null && (stack.mResumedActivity == null
1466 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001467 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1468 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001469 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001470 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001471 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001472 ActivityOptions.abort(options);
1473 return ActivityManager.START_SWITCHES_CANCELED;
1474 }
1475 }
1476
1477 if (mService.mDidAppSwitch) {
1478 // This is the second allowed switch since we stopped switches,
1479 // so now just generally allow switches. Use case: user presses
1480 // home (switches disabled, switch to home, mDidAppSwitch now true);
1481 // user taps a home icon (coming from home so allowed, we hit here
1482 // and now allow anyone to switch again).
1483 mService.mAppSwitchesAllowedTime = 0;
1484 } else {
1485 mService.mDidAppSwitch = true;
1486 }
1487
Craig Mautneree36c772014-07-16 14:56:05 -07001488 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001489
Dianne Hackborn91097de2014-04-04 18:02:06 -07001490 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001491 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001492
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001493 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001494 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001495 // activity start, but we are not actually doing an activity
1496 // switch... just dismiss the keyguard now, because we
1497 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001498 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001499 }
1500 return err;
1501 }
1502
Craig Mautner1b4bf852014-05-26 15:06:32 -07001503 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001504 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001505
1506 // On leanback only devices we should keep all activities in the same stack.
1507 if (!mLeanbackOnlyDevice &&
1508 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001509 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001510 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001511 if (taskStack.isOnHomeDisplay()) {
1512 if (mFocusedStack != taskStack) {
1513 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1514 "focused stack to r=" + r + " task=" + task);
1515 mFocusedStack = taskStack;
1516 } else {
1517 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1518 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1519 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001520 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001521 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001522 }
1523
Craig Mautnere0a38842013-12-16 16:14:02 -08001524 final ActivityContainer container = r.mInitialActivityContainer;
1525 if (container != null) {
1526 // The first time put it on the desired stack, after this put on task stack.
1527 r.mInitialActivityContainer = null;
1528 return container.mStack;
1529 }
1530
Craig Mautner1b4bf852014-05-26 15:06:32 -07001531 if (mFocusedStack != mHomeStack && (!newTask ||
1532 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001533 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1534 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1535 return mFocusedStack;
1536 }
1537
Craig Mautnere0a38842013-12-16 16:14:02 -08001538 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1539 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1540 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1541 if (!stack.isHomeStack()) {
1542 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1543 "adjustStackFocus: Setting focused stack=" + stack);
1544 mFocusedStack = stack;
1545 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001546 }
1547 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001548
Craig Mautner4a1cb222013-12-04 16:14:06 -08001549 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001550 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001551 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1552 " stackId=" + stackId);
1553 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001554 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001555 }
1556 return mHomeStack;
1557 }
1558
Craig Mautner29219d92013-04-16 20:19:12 -07001559 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001560 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001561 final TaskRecord task = r.task;
1562 boolean isHomeActivity = !r.isApplicationActivity();
1563 if (!isHomeActivity && task != null) {
1564 isHomeActivity = !task.isApplicationTask();
1565 }
1566 if (!isHomeActivity && task != null) {
1567 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1568 isHomeActivity = parent != null && parent.isHomeActivity();
1569 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001570 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001571 }
1572 }
1573
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001574 final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001575 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001576 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001577 final Intent intent = r.intent;
1578 final int callingUid = r.launchedFromUid;
1579
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001580 // In some flows in to this function, we retrieve the task record and hold on to it
1581 // without a lock before calling back in to here... so the task at this point may
1582 // not actually be in recents. Check for that, and if it isn't in recents just
1583 // consider it invalid.
1584 if (inTask != null && !inTask.inRecents) {
1585 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1586 inTask = null;
1587 }
1588
Craig Mautnerad400af2014-08-13 16:41:36 -07001589 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001590 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1591 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001592
Craig Mautnera228ae92014-07-09 05:44:55 -07001593 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001594 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001595 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001596 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1597 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1598 "\"singleInstance\" or \"singleTask\"");
1599 launchFlags &=
1600 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1601 } else {
1602 switch (r.info.documentLaunchMode) {
1603 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1604 break;
1605 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1606 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1607 break;
1608 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1609 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1610 break;
1611 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1612 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1613 break;
1614 }
1615 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001616
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001617 final boolean launchTaskBehind = r.mLaunchTaskBehind
1618 && !launchSingleTask && !launchSingleInstance
1619 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001620
1621 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1622 // For whatever reason this activity is being launched into a new
1623 // task... yet the caller has requested a result back. Well, that
1624 // is pretty messed up, so instead immediately send back a cancel
1625 // and let the new task continue launched as normal without a
1626 // dependency on its originator.
1627 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1628 r.resultTo.task.stack.sendActivityResultLocked(-1,
1629 r.resultTo, r.resultWho, r.requestCode,
1630 Activity.RESULT_CANCELED, null);
1631 r.resultTo = null;
1632 }
1633
1634 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1635 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1636 }
1637
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001638 // If we are actually going to launch in to a new task, there are some cases where
1639 // we further want to do multiple task.
1640 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1641 if (launchTaskBehind
1642 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1643 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1644 }
1645 }
1646
Craig Mautner8849a5e2013-04-02 16:41:03 -07001647 // We'll invoke onUserLeaving before onPause only if the launching
1648 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001649 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001650 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001651
1652 // If the caller has asked not to resume at this point, we make note
1653 // of this in the record so that we can skip it when trying to find
1654 // the top running activity.
1655 if (!doResume) {
1656 r.delayedResume = true;
1657 }
1658
Craig Mautnera254cd72014-05-25 16:47:39 -07001659 ActivityRecord notTop =
1660 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001661
1662 // If the onlyIfNeeded flag is set, then we can do this if the activity
1663 // being launched is the same as the one making the call... or, as
1664 // a special case, if we do not know the caller then we count the
1665 // current top activity as the caller.
1666 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1667 ActivityRecord checkedCaller = sourceRecord;
1668 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001669 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001670 }
1671 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1672 // Caller is not the same as launcher, so always needed.
1673 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1674 }
1675 }
1676
Craig Mautner8849a5e2013-04-02 16:41:03 -07001677 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001678 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001679
1680 // If the caller is not coming from another activity, but has given us an
1681 // explicit task into which they would like us to launch the new activity,
1682 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001683 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1684 final Intent baseIntent = inTask.getBaseIntent();
1685 final ActivityRecord root = inTask.getRootActivity();
1686 if (baseIntent == null) {
1687 ActivityOptions.abort(options);
1688 throw new IllegalArgumentException("Launching into task without base intent: "
1689 + inTask);
1690 }
1691
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001692 // If this task is empty, then we are adding the first activity -- it
1693 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001694 if (launchSingleInstance || launchSingleTask) {
1695 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1696 ActivityOptions.abort(options);
1697 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1698 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001699 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001700 if (root != null) {
1701 ActivityOptions.abort(options);
1702 throw new IllegalArgumentException("Caller with inTask " + inTask
1703 + " has root " + root + " but target is singleInstance/Task");
1704 }
1705 }
1706
1707 // If task is empty, then adopt the interesting intent launch flags in to the
1708 // activity being started.
1709 if (root == null) {
1710 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1711 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1712 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1713 launchFlags = (launchFlags&~flagsOfInterest)
1714 | (baseIntent.getFlags()&flagsOfInterest);
1715 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001716 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001717 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001718
Dianne Hackborn962d5352014-08-29 16:27:40 -07001719 // If the task is not empty and the caller is asking to start it as the root
1720 // of a new task, then we don't actually want to start this on the task. We
1721 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001722 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001723 addingToTask = false;
1724
1725 } else {
1726 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001727 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001728
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001729 reuseTask = inTask;
1730 } else {
1731 inTask = null;
1732 }
1733
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001734 if (inTask == null) {
1735 if (sourceRecord == null) {
1736 // This activity is not being started from another... in this
1737 // case we -always- start a new task.
1738 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1739 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1740 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1741 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1742 }
1743 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1744 // The original activity who is starting us is running as a single
1745 // instance... this new activity it is starting must go on its
1746 // own task.
1747 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1748 } else if (launchSingleInstance || launchSingleTask) {
1749 // The activity being started is a single instance... it always
1750 // gets launched into its own task.
1751 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1752 }
1753 }
1754
1755 ActivityInfo newTaskInfo = null;
1756 Intent newTaskIntent = null;
1757 ActivityStack sourceStack;
1758 if (sourceRecord != null) {
1759 if (sourceRecord.finishing) {
1760 // If the source is finishing, we can't further count it as our source. This
1761 // is because the task it is associated with may now be empty and on its way out,
1762 // so we don't want to blindly throw it in to that task. Instead we will take
1763 // the NEW_TASK flow and try to find a task for it. But save the task information
1764 // so it can be used when creating the new task.
1765 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1766 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1767 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1768 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1769 newTaskInfo = sourceRecord.info;
1770 newTaskIntent = sourceRecord.task.intent;
1771 }
1772 sourceRecord = null;
1773 sourceStack = null;
1774 } else {
1775 sourceStack = sourceRecord.task.stack;
1776 }
1777 } else {
1778 sourceStack = null;
1779 }
1780
1781 boolean movedHome = false;
1782 ActivityStack targetStack;
1783
1784 intent.setFlags(launchFlags);
1785
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001786 // We may want to try to place the new activity in to an existing task. We always
1787 // do this if the target activity is singleTask or singleInstance; we will also do
1788 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1789 // us to still place it in a new task: multi task, always doc mode, or being asked to
1790 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001791 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1792 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001793 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001794 // If bring to front is requested, and no result is requested and we have not
1795 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001796 // we can find a task that was started with this same
1797 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001798 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001799 // See if there is a task to bring to the front. If this is
1800 // a SINGLE_INSTANCE activity, there can be one and only one
1801 // instance of it in the history, and it is always in its own
1802 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001803 ActivityRecord intentActivity = !launchSingleInstance ?
1804 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001805 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001806 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001807 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001808 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001809 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1810 }
Craig Mautner29219d92013-04-16 20:19:12 -07001811 if (r.task == null) {
1812 r.task = intentActivity.task;
1813 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001814 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001815 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001816 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1817 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001818 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001819 if (intentActivity.task.intent == null) {
1820 // This task was started because of movement of
1821 // the activity based on affinity... now that we
1822 // are actually launching it, we can assign the
1823 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001824 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001825 }
1826 // If the target task is not in the front, then we need
1827 // to bring it to the front... except... well, with
1828 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1829 // to have the same behavior as if a new instance was
1830 // being started, which means not bringing it to the front
1831 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001832 final ActivityStack lastStack = getLastStack();
1833 ActivityRecord curTop = lastStack == null?
1834 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001835 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001836 if (curTop != null && (curTop.task != intentActivity.task ||
1837 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001838 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001839 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1840 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001841 // We really do want to push this one into the
1842 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001843 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001844 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1845 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001846 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001847 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001848 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001849 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1850 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001851 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001852 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001853 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001854 options = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001855 movedToFront = true;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001856 }
1857 }
1858 // If the caller has requested that the target task be
1859 // reset, then do so.
1860 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1861 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1862 }
1863 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1864 // We don't need to start a new activity, and
1865 // the client said not to do anything if that
1866 // is the case, so this is it! And for paranoia, make
1867 // sure we have correctly resumed the top activity.
1868 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001869 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001870
1871 // Make sure to notify Keyguard as well if we are not running an app
1872 // transition later.
1873 if (!movedToFront) {
1874 notifyActivityDrawnForKeyguard();
1875 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001876 } else {
1877 ActivityOptions.abort(options);
1878 }
1879 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1880 }
1881 if ((launchFlags &
1882 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1883 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1884 // The caller has requested to completely replace any
1885 // existing task with its new activity. Well that should
1886 // not be too hard...
1887 reuseTask = intentActivity.task;
1888 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001889 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001890 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001891 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001892 // In this situation we want to remove all activities
1893 // from the task up to the one being started. In most
1894 // cases this means we are resetting the task to its
1895 // initial state.
1896 ActivityRecord top =
1897 intentActivity.task.performClearTaskLocked(r, launchFlags);
1898 if (top != null) {
1899 if (top.frontOfTask) {
1900 // Activity aliases may mean we use different
1901 // intents for the top activity, so make sure
1902 // the task now has the identity of the new
1903 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001904 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001905 }
1906 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1907 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001908 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001909 } else {
1910 // A special case: we need to
1911 // start the activity because it is not currently
1912 // running, and the caller has asked to clear the
1913 // current task to have this activity at the top.
1914 addingToTask = true;
1915 // Now pretend like this activity is being started
1916 // by the top of its task, so it is put in the
1917 // right place.
1918 sourceRecord = intentActivity;
1919 }
1920 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1921 // In this case the top activity on the task is the
1922 // same as the one being launched, so we take that
1923 // as a request to bring the task to the foreground.
1924 // If the top activity in the task is the root
1925 // activity, deliver this new intent to it if it
1926 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001927 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001928 && intentActivity.realActivity.equals(r.realActivity)) {
1929 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1930 intentActivity.task);
1931 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001932 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001933 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001934 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
1935 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001936 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1937 // In this case we are launching the root activity
1938 // of the task, but with a different intent. We
1939 // should start a new instance on top.
1940 addingToTask = true;
1941 sourceRecord = intentActivity;
1942 }
1943 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1944 // In this case an activity is being launched in to an
1945 // existing task, without resetting that task. This
1946 // is typically the situation of launching an activity
1947 // from a notification or shortcut. We want to place
1948 // the new activity on top of the current task.
1949 addingToTask = true;
1950 sourceRecord = intentActivity;
1951 } else if (!intentActivity.task.rootWasReset) {
1952 // In this case we are launching in to an existing task
1953 // that has not yet been started from its front door.
1954 // The current task has been brought to the front.
1955 // Ideally, we'd probably like to place this new task
1956 // at the bottom of its stack, but that's a little hard
1957 // to do with the current organization of the code so
1958 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07001959 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001960 }
1961 if (!addingToTask && reuseTask == null) {
1962 // We didn't do anything... but it was needed (a.k.a., client
1963 // don't use that intent!) And for paranoia, make
1964 // sure we have correctly resumed the top activity.
1965 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001966 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001967 if (!movedToFront) {
1968 // Make sure to notify Keyguard as well if we are not running an app
1969 // transition later.
1970 notifyActivityDrawnForKeyguard();
1971 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001972 } else {
1973 ActivityOptions.abort(options);
1974 }
1975 return ActivityManager.START_TASK_TO_FRONT;
1976 }
1977 }
1978 }
1979 }
1980
1981 //String uri = r.intent.toURI();
1982 //Intent intent2 = new Intent(uri);
1983 //Slog.i(TAG, "Given intent: " + r.intent);
1984 //Slog.i(TAG, "URI is: " + uri);
1985 //Slog.i(TAG, "To intent: " + intent2);
1986
1987 if (r.packageName != null) {
1988 // If the activity being launched is the same as the one currently
1989 // at the top, then we need to check if it should only be launched
1990 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001991 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001992 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001993 if (top != null && r.resultTo == null) {
1994 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1995 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001996 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07001997 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001998 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1999 top.task);
2000 // For paranoia, make sure we have correctly
2001 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002002 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002003 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002004 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002005 }
2006 ActivityOptions.abort(options);
2007 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2008 // We don't need to start a new activity, and
2009 // the client said not to do anything if that
2010 // is the case, so this is it!
2011 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2012 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002013 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002014 return ActivityManager.START_DELIVERED_TO_TOP;
2015 }
2016 }
2017 }
2018 }
2019
2020 } else {
2021 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002022 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2023 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002024 }
2025 ActivityOptions.abort(options);
2026 return ActivityManager.START_CLASS_NOT_FOUND;
2027 }
2028
2029 boolean newTask = false;
2030 boolean keepCurTransition = false;
2031
Craig Mautnerbb742462014-07-07 15:28:55 -07002032 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002033 sourceRecord.task : null;
2034
Craig Mautner8849a5e2013-04-02 16:41:03 -07002035 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002036 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002037 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002038 if (isLockTaskModeViolation(reuseTask)) {
2039 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2040 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2041 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002042 newTask = true;
2043 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerbb742462014-07-07 15:28:55 -07002044 if (!launchTaskBehind) {
2045 targetStack.moveToFront();
2046 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002047 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002048 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2049 newTaskInfo != null ? newTaskInfo : r.info,
2050 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002051 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2052 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002053 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2054 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002055 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002056 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002057 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002058 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002059 if ((launchFlags &
2060 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2061 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2062 // Caller wants to appear on home activity, so before starting
2063 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002064 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002065 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002066 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002067 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002068 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002069 if (isLockTaskModeViolation(sourceTask)) {
2070 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2071 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2072 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002073 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07002074 targetStack.moveToFront();
Craig Mautner737fae22014-10-15 12:52:10 -07002075 final TaskRecord topTask = targetStack.topTask();
2076 if (topTask != sourceTask) {
2077 targetStack.moveTaskToFrontLocked(sourceTask, r, options);
Craig Mautner737fae22014-10-15 12:52:10 -07002078 } else {
2079 mWindowManager.moveTaskToTop(topTask.taskId);
2080 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002081 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002082 // In this case, we are adding the activity to an existing
2083 // task, but the caller has asked to clear that task if the
2084 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002085 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002086 keepCurTransition = true;
2087 if (top != null) {
2088 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002089 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002090 // For paranoia, make sure we have correctly
2091 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002092 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002093 if (doResume) {
2094 targetStack.resumeTopActivityLocked(null);
2095 }
2096 ActivityOptions.abort(options);
2097 return ActivityManager.START_DELIVERED_TO_TOP;
2098 }
2099 } else if (!addingToTask &&
2100 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2101 // In this case, we are launching an activity in our own task
2102 // that may already be running somewhere in the history, and
2103 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002104 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002105 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002106 final TaskRecord task = top.task;
2107 task.moveActivityToFrontLocked(top);
2108 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002109 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002110 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002111 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002112 if (doResume) {
2113 targetStack.resumeTopActivityLocked(null);
2114 }
2115 return ActivityManager.START_DELIVERED_TO_TOP;
2116 }
2117 }
2118 // An existing activity is starting this new activity, so we want
2119 // to keep the new one in the same task as the one that is starting
2120 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002121 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002122 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002123 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002124
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002125 } else if (inTask != null) {
2126 // The calling is asking that the new activity be started in an explicit
2127 // task it has provided to us.
2128 if (isLockTaskModeViolation(inTask)) {
2129 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2130 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2131 }
2132 targetStack = inTask.stack;
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002133 targetStack.moveTaskToFrontLocked(inTask, r, options);
Craig Mautner02a4aa22014-09-03 10:01:24 -07002134 targetStack.moveToFront();
Dianne Hackborn2c924fb2014-08-29 13:02:03 -07002135 mWindowManager.moveTaskToTop(inTask.taskId);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002136
2137 // Check whether we should actually launch the new activity in to the task,
2138 // or just reuse the current activity on top.
2139 ActivityRecord top = inTask.getTopActivity();
2140 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2141 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2142 || launchSingleTop || launchSingleTask) {
2143 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2144 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2145 // We don't need to start a new activity, and
2146 // the client said not to do anything if that
2147 // is the case, so this is it!
2148 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2149 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002150 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002151 return ActivityManager.START_DELIVERED_TO_TOP;
2152 }
2153 }
2154
Dianne Hackborn962d5352014-08-29 16:27:40 -07002155 if (!addingToTask) {
2156 // We don't actually want to have this activity added to the task, so just
2157 // stop here but still tell the caller that we consumed the intent.
2158 ActivityOptions.abort(options);
2159 return ActivityManager.START_TASK_TO_FRONT;
2160 }
2161
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002162 r.setTask(inTask, null);
2163 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2164 + " in explicit task " + r.task);
2165
Craig Mautner8849a5e2013-04-02 16:41:03 -07002166 } else {
2167 // This not being started from an existing activity, and not part
2168 // of a new task... just put it in the top task, though these days
2169 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002170 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07002171 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07002172 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002173 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002174 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002175 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002176 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2177 + " in new guessed " + r.task);
2178 }
2179
2180 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002181 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002182
Craig Mautner76e2a762014-06-24 09:05:43 -07002183 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2184 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2185 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002186 if (newTask) {
2187 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2188 }
2189 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002190 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002191 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002192 if (!launchTaskBehind) {
2193 // Don't set focus on an activity that's going to the back.
2194 mService.setFocusedActivityLocked(r);
2195 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196 return ActivityManager.START_SUCCESS;
2197 }
2198
Craig Mautneree36c772014-07-16 14:56:05 -07002199 final void doPendingActivityLaunchesLocked(boolean doResume) {
2200 while (!mPendingActivityLaunches.isEmpty()) {
2201 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002202 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002203 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002204 }
2205 }
2206
Craig Mautner7f13ed32014-07-28 14:00:35 -07002207 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002208 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2209 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002210 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002211 mPendingActivityLaunches.remove(palNdx);
2212 }
2213 }
2214 }
2215
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002216 void acquireLaunchWakelock() {
2217 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2218 throw new IllegalStateException("Calling must be system uid");
2219 }
2220 mLaunchingActivity.acquire();
2221 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2222 // To be safe, don't allow the wake lock to be held for too long.
2223 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2224 }
2225 }
2226
Craig Mautnerf3333272013-04-22 10:55:53 -07002227 // Checked.
2228 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2229 Configuration config) {
2230 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2231
Craig Mautnerf3333272013-04-22 10:55:53 -07002232 ArrayList<ActivityRecord> stops = null;
2233 ArrayList<ActivityRecord> finishes = null;
2234 ArrayList<UserStartedState> startingUsers = null;
2235 int NS = 0;
2236 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002237 boolean booting = false;
2238 boolean enableScreen = false;
2239 boolean activityRemoved = false;
2240
2241 ActivityRecord r = ActivityRecord.forToken(token);
2242 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002243 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2244 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002245 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2246 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002247 if (fromTimeout) {
2248 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002249 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002250
2251 // This is a hack to semi-deal with a race condition
2252 // in the client where it can be constructed with a
2253 // newer configuration from when we asked it to launch.
2254 // We'll update with whatever configuration it now says
2255 // it used to launch.
2256 if (config != null) {
2257 r.configuration = config;
2258 }
2259
2260 // We are now idle. If someone is waiting for a thumbnail from
2261 // us, we can now deliver.
2262 r.idle = true;
2263
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002264 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002265 if (isFrontStack(r.task.stack) || fromTimeout) {
2266 booting = mService.mBooting;
2267 mService.mBooting = false;
2268 if (!mService.mBooted) {
2269 mService.mBooted = true;
2270 enableScreen = true;
2271 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002272 }
2273 }
2274
2275 if (allResumedActivitiesIdle()) {
2276 if (r != null) {
2277 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002278 }
2279
2280 if (mLaunchingActivity.isHeld()) {
2281 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2282 if (VALIDATE_WAKE_LOCK_CALLER &&
2283 Binder.getCallingUid() != Process.myUid()) {
2284 throw new IllegalStateException("Calling must be system uid");
2285 }
2286 mLaunchingActivity.release();
2287 }
2288 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002289 }
2290
2291 // Atomically retrieve all of the other things to do.
2292 stops = processStoppingActivitiesLocked(true);
2293 NS = stops != null ? stops.size() : 0;
2294 if ((NF=mFinishingActivities.size()) > 0) {
2295 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2296 mFinishingActivities.clear();
2297 }
2298
Craig Mautnerf3333272013-04-22 10:55:53 -07002299 if (mStartingUsers.size() > 0) {
2300 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2301 mStartingUsers.clear();
2302 }
2303
Craig Mautnerf3333272013-04-22 10:55:53 -07002304 // Stop any activities that are scheduled to do so but have been
2305 // waiting for the next one to start.
2306 for (int i = 0; i < NS; i++) {
2307 r = stops.get(i);
2308 final ActivityStack stack = r.task.stack;
2309 if (r.finishing) {
2310 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2311 } else {
2312 stack.stopActivityLocked(r);
2313 }
2314 }
2315
2316 // Finish any activities that are scheduled to do so but have been
2317 // waiting for the next one to start.
2318 for (int i = 0; i < NF; i++) {
2319 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002320 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002321 }
2322
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002323 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002324 // Complete user switch
2325 if (startingUsers != null) {
2326 for (int i = 0; i < startingUsers.size(); i++) {
2327 mService.finishUserSwitch(startingUsers.get(i));
2328 }
2329 }
2330 // Complete starting up of background users
2331 if (mStartingBackgroundUsers.size() > 0) {
2332 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2333 mStartingBackgroundUsers.clear();
2334 for (int i = 0; i < startingUsers.size(); i++) {
2335 mService.finishUserBoot(startingUsers.get(i));
2336 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002337 }
2338 }
2339
2340 mService.trimApplications();
2341 //dump();
2342 //mWindowManager.dump();
2343
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002344 if (booting || enableScreen) {
2345 mService.postFinishBooting(booting, enableScreen);
Craig Mautnerf3333272013-04-22 10:55:53 -07002346 }
2347
2348 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002349 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002350 }
2351
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002352 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002353 }
2354
Craig Mautner8e569572013-10-11 17:36:59 -07002355 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002356 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002357 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2358 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002359 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2360 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2361 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002362 }
Craig Mautner19091252013-10-05 00:03:53 -07002363 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002364 }
2365
2366 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002367 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2368 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002369 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2370 stacks.get(stackNdx).closeSystemDialogsLocked();
2371 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002372 }
2373 }
2374
Craig Mautner93529a42013-10-04 15:03:13 -07002375 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002376 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002377 }
2378
Craig Mautner8d341ef2013-03-26 09:03:27 -07002379 /**
2380 * @return true if some activity was finished (or would have finished if doit were true).
2381 */
2382 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2383 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002384 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2385 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002386 final int numStacks = stacks.size();
2387 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2388 final ActivityStack stack = stacks.get(stackNdx);
2389 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2390 didSomething = true;
2391 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002392 }
2393 }
2394 return didSomething;
2395 }
2396
Dianne Hackborna413dc02013-07-12 12:02:55 -07002397 void updatePreviousProcessLocked(ActivityRecord r) {
2398 // Now that this process has stopped, we may want to consider
2399 // it to be the previous app to try to keep around in case
2400 // the user wants to return to it.
2401
2402 // First, found out what is currently the foreground app, so that
2403 // we don't blow away the previous app if this activity is being
2404 // hosted by the process that is actually still the foreground.
2405 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002406 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2407 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002408 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2409 final ActivityStack stack = stacks.get(stackNdx);
2410 if (isFrontStack(stack)) {
2411 if (stack.mResumedActivity != null) {
2412 fgApp = stack.mResumedActivity.app;
2413 } else if (stack.mPausingActivity != null) {
2414 fgApp = stack.mPausingActivity.app;
2415 }
2416 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002417 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002418 }
2419 }
2420
2421 // Now set this one as the previous process, only if that really
2422 // makes sense to.
2423 if (r.app != null && fgApp != null && r.app != fgApp
2424 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002425 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002426 mService.mPreviousProcess = r.app;
2427 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2428 }
2429 }
2430
Craig Mautner05d29032013-05-03 13:40:13 -07002431 boolean resumeTopActivitiesLocked() {
2432 return resumeTopActivitiesLocked(null, null, null);
2433 }
2434
2435 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2436 Bundle targetOptions) {
2437 if (targetStack == null) {
2438 targetStack = getFocusedStack();
2439 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002440 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002441 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002442 if (isFrontStack(targetStack)) {
2443 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2444 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002445 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2446 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002447 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2448 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002449 if (stack == targetStack) {
2450 // Already started above.
2451 continue;
2452 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002453 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002454 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002455 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002456 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002457 }
Craig Mautner05d29032013-05-03 13:40:13 -07002458 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002459 }
2460
Todd Kennedy539db512014-12-15 09:57:55 -08002461 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002462 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2463 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002464 final int numStacks = stacks.size();
2465 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2466 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002467 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002468 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002469 }
2470 }
2471
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002472 void finishVoiceTask(IVoiceInteractionSession session) {
2473 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2474 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2475 final int numStacks = stacks.size();
2476 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2477 final ActivityStack stack = stacks.get(stackNdx);
2478 stack.finishVoiceTask(session);
2479 }
2480 }
2481 }
2482
Craig Mautneraea74a52014-03-08 14:23:10 -08002483 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2484 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2485 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002486 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002487 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2488 // Caller wants the home activity moved with it. To accomplish this,
2489 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002490 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002491 }
2492 task.stack.moveTaskToFrontLocked(task, null, options);
2493 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2494 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002495 }
2496
Craig Mautner967212c2013-04-13 21:10:58 -07002497 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002498 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2499 if (activityContainer != null) {
2500 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002501 }
2502 return null;
2503 }
2504
Craig Mautner967212c2013-04-13 21:10:58 -07002505 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002506 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002507 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2508 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002509 }
2510 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002511 }
2512
Craig Mautner4a1cb222013-12-04 16:14:06 -08002513 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002514 ActivityRecord homeActivity = getHomeActivity();
2515 if (homeActivity != null) {
2516 return homeActivity.appToken;
2517 }
2518 return null;
2519 }
2520
2521 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002522 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2523 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2524 final TaskRecord task = tasks.get(taskNdx);
2525 if (task.isHomeTask()) {
2526 final ArrayList<ActivityRecord> activities = task.mActivities;
2527 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2528 final ActivityRecord r = activities.get(activityNdx);
2529 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002530 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002531 }
2532 }
2533 }
2534 }
2535 return null;
2536 }
2537
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002538 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002539 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002540 ActivityContainer activityContainer =
2541 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002542 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002543 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002544 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002545 return activityContainer;
2546 }
2547
Craig Mautner34b73df2014-01-12 21:11:08 -08002548 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002549 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2550 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2551 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002552 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2553 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002554 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002555 }
2556 }
2557
Craig Mautner95da1082014-02-24 17:54:35 -08002558 void deleteActivityContainer(IActivityContainer container) {
2559 ActivityContainer activityContainer = (ActivityContainer)container;
2560 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002561 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2562 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002563 final int stackId = activityContainer.mStackId;
2564 mActivityContainers.remove(stackId);
2565 mWindowManager.removeStack(stackId);
2566 }
2567 }
2568
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002569 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002570 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2571 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002572 return -1;
2573 }
2574
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002575 ActivityContainer activityContainer = new ActivityContainer(stackId);
2576 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002577 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002578 return stackId;
2579 }
2580
2581 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002582 while (true) {
2583 if (++mLastStackId <= HOME_STACK_ID) {
2584 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002585 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002586 if (getStack(mLastStackId) == null) {
2587 break;
2588 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002589 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002590 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002591 }
2592
Craig Mautneref73ee12014-04-23 11:45:37 -07002593 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2594 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2595 final ActivityStack stack = getStack(stackId);
2596 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2597 final TaskRecord task = tasks.get(taskNdx);
2598 stack.addTask(task, false, false);
2599 final int taskId = task.taskId;
2600 final ArrayList<ActivityRecord> activities = task.mActivities;
2601 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2602 final ActivityRecord r = activities.get(activityNdx);
2603 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2604 r.info.screenOrientation, r.fullscreen,
2605 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002606 r.userId, r.info.configChanges, task.voiceSession != null,
2607 r.mLaunchTaskBehind);
Craig Mautneref73ee12014-04-23 11:45:37 -07002608 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002609 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002610 }
2611
Craig Mautner8d341ef2013-03-26 09:03:27 -07002612 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002613 final TaskRecord task = anyTaskForIdLocked(taskId);
2614 if (task == null) {
2615 return;
2616 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002617 final ActivityStack stack = getStack(stackId);
2618 if (stack == null) {
2619 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2620 return;
2621 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002622 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002623 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002624 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002625 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002626 }
2627
Craig Mautnerac6f8432013-07-17 13:24:59 -07002628 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002629 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002630 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2631 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002632 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2633 final ActivityStack stack = stacks.get(stackNdx);
2634 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002635 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2636 continue;
2637 }
2638 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2639 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2640 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002641 continue;
2642 }
2643 final ActivityRecord ar = stack.findTaskLocked(r);
2644 if (ar != null) {
2645 return ar;
2646 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002647 }
2648 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002649 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002650 return null;
2651 }
2652
2653 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002654 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2655 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002656 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2657 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2658 if (ar != null) {
2659 return ar;
2660 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002661 }
2662 }
2663 return null;
2664 }
2665
Craig Mautner8d341ef2013-03-26 09:03:27 -07002666 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002667 scheduleSleepTimeout();
2668 if (!mGoingToSleep.isHeld()) {
2669 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002670 if (mLaunchingActivity.isHeld()) {
2671 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2672 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002673 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002674 mLaunchingActivity.release();
2675 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002676 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002677 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002678 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 }
2680
2681 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002682 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002683
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002684 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002685 final long endTime = System.currentTimeMillis() + timeout;
2686 while (true) {
2687 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002688 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2689 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002690 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2691 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2692 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002693 }
2694 if (cantShutdown) {
2695 long timeRemaining = endTime - System.currentTimeMillis();
2696 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002697 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002698 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002699 } catch (InterruptedException e) {
2700 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002701 } else {
2702 Slog.w(TAG, "Activity manager shutdown timed out");
2703 timedout = true;
2704 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002705 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002706 } else {
2707 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002708 }
2709 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002710
2711 // Force checkReadyForSleep to complete.
2712 mSleepTimeout = true;
2713 checkReadyForSleepLocked();
2714
Craig Mautner8d341ef2013-03-26 09:03:27 -07002715 return timedout;
2716 }
2717
2718 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002719 removeSleepTimeouts();
2720 if (mGoingToSleep.isHeld()) {
2721 mGoingToSleep.release();
2722 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002723 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2724 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002725 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2726 final ActivityStack stack = stacks.get(stackNdx);
2727 stack.awakeFromSleepingLocked();
2728 if (isFrontStack(stack)) {
2729 resumeTopActivitiesLocked();
2730 }
Craig Mautner5314a402013-09-26 12:40:16 -07002731 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002732 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002733 mGoingToSleepActivities.clear();
2734 }
2735
2736 void activitySleptLocked(ActivityRecord r) {
2737 mGoingToSleepActivities.remove(r);
2738 checkReadyForSleepLocked();
2739 }
2740
2741 void checkReadyForSleepLocked() {
2742 if (!mService.isSleepingOrShuttingDown()) {
2743 // Do not care.
2744 return;
2745 }
2746
2747 if (!mSleepTimeout) {
2748 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002749 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2750 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002751 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2752 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2753 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002754 }
2755
2756 if (mStoppingActivities.size() > 0) {
2757 // Still need to tell some activities to stop; can't sleep yet.
2758 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2759 + mStoppingActivities.size() + " activities");
2760 scheduleIdleLocked();
2761 dontSleep = true;
2762 }
2763
2764 if (mGoingToSleepActivities.size() > 0) {
2765 // Still need to tell some activities to sleep; can't sleep yet.
2766 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2767 + mGoingToSleepActivities.size() + " activities");
2768 dontSleep = true;
2769 }
2770
2771 if (dontSleep) {
2772 return;
2773 }
2774 }
2775
Craig Mautnere0a38842013-12-16 16:14:02 -08002776 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2777 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002778 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2779 stacks.get(stackNdx).goToSleep();
2780 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002781 }
2782
2783 removeSleepTimeouts();
2784
2785 if (mGoingToSleep.isHeld()) {
2786 mGoingToSleep.release();
2787 }
2788 if (mService.mShuttingDown) {
2789 mService.notifyAll();
2790 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002791 }
2792
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002793 boolean reportResumedActivityLocked(ActivityRecord r) {
2794 final ActivityStack stack = r.task.stack;
2795 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002796 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002797 }
2798 if (allResumedActivitiesComplete()) {
2799 ensureActivitiesVisibleLocked(null, 0);
2800 mWindowManager.executeAppTransition();
2801 return true;
2802 }
2803 return false;
2804 }
2805
Craig Mautner8d341ef2013-03-26 09:03:27 -07002806 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2808 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002809 final int numStacks = stacks.size();
2810 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2811 final ActivityStack stack = stacks.get(stackNdx);
2812 stack.handleAppCrashLocked(app);
2813 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002814 }
2815 }
2816
Jose Lima4b6c6692014-08-12 17:41:12 -07002817 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002818 final ActivityStack stack = r.task.stack;
2819 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002820 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2821 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002822 return false;
2823 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002824 final boolean isVisible = stack.hasVisibleBehindActivity();
2825 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2826 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002827
2828 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002829 if (top == null || top == r || (visible == isVisible)) {
2830 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2831 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002832 return true;
2833 }
2834
2835 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002836 if (visible && top.fullscreen) {
2837 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07002838 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07002839 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07002840 " top.app.thread=" + top.app.thread);
2841 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002842 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2843 // Only the activity set as currently visible behind should actively reset its
2844 // visible behind state.
2845 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
2846 + visible + " stack.getVisibleBehindActivity()=" +
2847 stack.getVisibleBehindActivity() + " r=" + r);
2848 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002849 }
2850
Jose Lima4b6c6692014-08-12 17:41:12 -07002851 stack.setVisibleBehindActivity(visible ? r : null);
2852 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002853 // Make the activity immediately above r opaque.
2854 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2855 if (next != null) {
2856 mService.convertFromTranslucent(next.appToken);
2857 }
2858 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002859 if (top.app != null && top.app.thread != null) {
2860 // Notify the top app of the change.
2861 try {
2862 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2863 } catch (RemoteException e) {
2864 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002865 }
2866 return true;
2867 }
2868
Craig Mautnerbb742462014-07-07 15:28:55 -07002869 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2870 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2871 r.mLaunchTaskBehind = false;
2872 final TaskRecord task = r.task;
2873 task.setLastThumbnail(task.stack.screenshotActivities(r));
2874 mService.addRecentTaskLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002875 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002876 mWindowManager.setAppVisibility(r.appToken, false);
2877 }
2878
2879 void scheduleLaunchTaskBehindComplete(IBinder token) {
2880 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2881 }
2882
Craig Mautnerde4ef022013-04-07 19:01:33 -07002883 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002884 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002885 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2886 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002887 final int topStackNdx = stacks.size() - 1;
2888 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2889 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002890 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002891 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002892 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002893 }
2894
2895 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002896 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2897 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002898 final int numStacks = stacks.size();
2899 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2900 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002901 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002902 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002903 }
2904 }
2905
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002906 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2907 // Examine all activities currently running in the process.
2908 TaskRecord firstTask = null;
2909 // Tasks is non-null only if two or more tasks are found.
2910 ArraySet<TaskRecord> tasks = null;
2911 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
2912 for (int i=0; i<app.activities.size(); i++) {
2913 ActivityRecord r = app.activities.get(i);
2914 // First, if we find an activity that is in the process of being destroyed,
2915 // then we just aren't going to do anything for now; we want things to settle
2916 // down before we try to prune more activities.
2917 if (r.finishing || r.state == ActivityState.DESTROYING
2918 || r.state == ActivityState.DESTROYED) {
2919 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
2920 return;
2921 }
2922 // Don't consider any activies that are currently not in a state where they
2923 // can be destroyed.
2924 if (r.visible || !r.stopped || !r.haveState
2925 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
2926 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
2927 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
2928 continue;
2929 }
2930 if (r.task != null) {
2931 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
2932 + " from " + r);
2933 if (firstTask == null) {
2934 firstTask = r.task;
2935 } else if (firstTask != r.task) {
2936 if (tasks == null) {
2937 tasks = new ArraySet<>();
2938 tasks.add(firstTask);
2939 }
2940 tasks.add(r.task);
2941 }
2942 }
2943 }
2944 if (tasks == null) {
2945 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
2946 return;
2947 }
2948 // If we have activities in multiple tasks that are in a position to be destroyed,
2949 // let's iterate through the tasks and release the oldest one.
2950 final int numDisplays = mActivityDisplays.size();
2951 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2952 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2953 // Step through all stacks starting from behind, to hit the oldest things first.
2954 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2955 final ActivityStack stack = stacks.get(stackNdx);
2956 // Try to release activities in this stack; if we manage to, we are done.
2957 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2958 return;
2959 }
2960 }
2961 }
2962 }
2963
Craig Mautner8d341ef2013-03-26 09:03:27 -07002964 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002965 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2966 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002967 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002968
Craig Mautner858d8a62013-04-23 17:08:34 -07002969 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002970 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2971 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002972 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002973 final ActivityStack stack = stacks.get(stackNdx);
2974 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002975 TaskRecord task = stack.topTask();
2976 if (task != null) {
2977 mWindowManager.moveTaskToTop(task.taskId);
2978 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002979 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002980 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002981
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002982 ActivityStack stack = getStack(restoreStackId);
2983 if (stack == null) {
2984 stack = mHomeStack;
2985 }
2986 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002987 if (stack.isOnHomeDisplay()) {
2988 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002989 TaskRecord task = stack.topTask();
2990 if (task != null) {
2991 mWindowManager.moveTaskToTop(task.taskId);
2992 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002993 } else {
2994 // Stack was moved to another display while user was swapped out.
Craig Mautner84984fa2014-06-19 11:19:20 -07002995 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002996 }
Craig Mautner93529a42013-10-04 15:03:13 -07002997 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002998 }
2999
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003000 /**
3001 * Add background users to send boot completed events to.
3002 * @param userId The user being started in the background
3003 * @param uss The state object for the user.
3004 */
3005 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3006 mStartingBackgroundUsers.add(uss);
3007 }
3008
Craig Mautnerde4ef022013-04-07 19:01:33 -07003009 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
3010 int N = mStoppingActivities.size();
3011 if (N <= 0) return null;
3012
3013 ArrayList<ActivityRecord> stops = null;
3014
3015 final boolean nowVisible = allResumedActivitiesVisible();
3016 for (int i=0; i<N; i++) {
3017 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07003018 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07003019 + nowVisible + " waitingVisible=" + s.waitingVisible
3020 + " finishing=" + s.finishing);
3021 if (s.waitingVisible && nowVisible) {
3022 mWaitingVisibleActivities.remove(s);
3023 s.waitingVisible = false;
3024 if (s.finishing) {
3025 // If this activity is finishing, it is sitting on top of
3026 // everyone else but we now know it is no longer needed...
3027 // so get rid of it. Otherwise, we need to go through the
3028 // normal flow and hide it once we determine that it is
3029 // hidden by the activities in front of it.
3030 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003031 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003032 }
3033 }
3034 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
3035 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3036 if (stops == null) {
3037 stops = new ArrayList<ActivityRecord>();
3038 }
3039 stops.add(s);
3040 mStoppingActivities.remove(i);
3041 N--;
3042 i--;
3043 }
3044 }
3045
3046 return stops;
3047 }
3048
Craig Mautnercf910b02013-04-23 11:23:27 -07003049 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003050 // FIXME
3051/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3052 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003053 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003054 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003055 if (isFrontStack(stack)) {
3056 if (r == null) {
3057 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3058 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003059 final ActivityRecord pausing = stack.mPausingActivity;
3060 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003061 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003062 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003063 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003064 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003065 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003066 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003067 }
3068 }
3069 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003070 final ActivityRecord resumed = stack.mResumedActivity;
3071 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003072 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003073 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003074 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003075 if (r != null && (state == ActivityState.INITIALIZING
3076 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003077 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003078 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003079 }
3080 }
3081 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003082*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003083 }
3084
Craig Mautner27084302013-03-25 08:05:25 -07003085 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003086 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003087 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003088 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3089 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3090 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003091 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003092 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003093
Craig Mautner20e72272013-04-01 13:45:53 -07003094 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003095 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003096 }
3097
Dianne Hackborn390517b2013-05-30 15:03:32 -07003098 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3099 boolean needSep, String prefix) {
3100 if (activity != null) {
3101 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3102 if (needSep) {
3103 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003104 }
3105 pw.print(prefix);
3106 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003107 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003108 }
3109 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003110 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003111 }
3112
Craig Mautner8d341ef2013-03-26 09:03:27 -07003113 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3114 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003115 boolean printed = false;
3116 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003117 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3118 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003119 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003120 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003121 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003122 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003123 final ActivityStack stack = stacks.get(stackNdx);
3124 StringBuilder stackHeader = new StringBuilder(128);
3125 stackHeader.append(" Stack #");
3126 stackHeader.append(stack.mStackId);
3127 stackHeader.append(":");
3128 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3129 needSep, stackHeader.toString());
3130 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3131 !dumpAll, false, dumpPackage, true,
3132 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003133
Craig Mautner4a1cb222013-12-04 16:14:06 -08003134 needSep = printed;
3135 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3136 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003137 if (pr) {
3138 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003139 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003140 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003141 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3142 " mResumedActivity: ");
3143 if (pr) {
3144 printed = true;
3145 needSep = false;
3146 }
3147 if (dumpAll) {
3148 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3149 " mLastPausedActivity: ");
3150 if (pr) {
3151 printed = true;
3152 needSep = true;
3153 }
3154 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3155 needSep, " mLastNoHistoryActivity: ");
3156 }
3157 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003158 }
3159 }
3160
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003161 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3162 false, dumpPackage, true, " Activities waiting to finish:", null);
3163 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3164 false, dumpPackage, true, " Activities waiting to stop:", null);
3165 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3166 false, dumpPackage, true, " Activities waiting for another to become visible:",
3167 null);
3168 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3169 false, dumpPackage, true, " Activities waiting to sleep:", null);
3170 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3171 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003172
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003173 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003174 }
3175
Dianne Hackborn390517b2013-05-30 15:03:32 -07003176 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003177 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003178 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003179 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003180 String innerPrefix = null;
3181 String[] args = null;
3182 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003183 for (int i=list.size()-1; i>=0; i--) {
3184 final ActivityRecord r = list.get(i);
3185 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3186 continue;
3187 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003188 if (innerPrefix == null) {
3189 innerPrefix = prefix + " ";
3190 args = new String[0];
3191 }
3192 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003193 final boolean full = !brief && (complete || !r.isInHistory());
3194 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003195 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003196 needNL = false;
3197 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003198 if (header1 != null) {
3199 pw.println(header1);
3200 header1 = null;
3201 }
3202 if (header2 != null) {
3203 pw.println(header2);
3204 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003205 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003206 if (lastTask != r.task) {
3207 lastTask = r.task;
3208 pw.print(prefix);
3209 pw.print(full ? "* " : " ");
3210 pw.println(lastTask);
3211 if (full) {
3212 lastTask.dump(pw, prefix + " ");
3213 } else if (complete) {
3214 // Complete + brief == give a summary. Isn't that obvious?!?
3215 if (lastTask.intent != null) {
3216 pw.print(prefix); pw.print(" ");
3217 pw.println(lastTask.intent.toInsecureStringWithClip());
3218 }
3219 }
3220 }
3221 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3222 pw.print(" #"); pw.print(i); pw.print(": ");
3223 pw.println(r);
3224 if (full) {
3225 r.dump(pw, innerPrefix);
3226 } else if (complete) {
3227 // Complete + brief == give a summary. Isn't that obvious?!?
3228 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3229 if (r.app != null) {
3230 pw.print(innerPrefix); pw.println(r.app);
3231 }
3232 }
3233 if (client && r.app != null && r.app.thread != null) {
3234 // flush anything that is already in the PrintWriter since the thread is going
3235 // to write to the file descriptor directly
3236 pw.flush();
3237 try {
3238 TransferPipe tp = new TransferPipe();
3239 try {
3240 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3241 r.appToken, innerPrefix, args);
3242 // Short timeout, since blocking here can
3243 // deadlock with the application.
3244 tp.go(fd, 2000);
3245 } finally {
3246 tp.kill();
3247 }
3248 } catch (IOException e) {
3249 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3250 } catch (RemoteException e) {
3251 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3252 }
3253 needNL = true;
3254 }
3255 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003256 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003257 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003258
Craig Mautnerf3333272013-04-22 10:55:53 -07003259 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003260 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003261 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3262 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003263 }
3264
3265 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003266 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003267 }
3268
3269 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003270 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003271 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3272 }
3273
Craig Mautner05d29032013-05-03 13:40:13 -07003274 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003275 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3276 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3277 }
Craig Mautner05d29032013-05-03 13:40:13 -07003278 }
3279
Craig Mautner0eea92c2013-05-16 13:35:39 -07003280 void removeSleepTimeouts() {
3281 mSleepTimeout = false;
3282 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3283 }
3284
3285 final void scheduleSleepTimeout() {
3286 removeSleepTimeouts();
3287 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3288 }
3289
Craig Mautner4a1cb222013-12-04 16:14:06 -08003290 @Override
3291 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003292 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003293 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3294 }
3295
3296 @Override
3297 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003298 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003299 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3300 }
3301
3302 @Override
3303 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003304 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003305 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3306 }
3307
3308 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003309 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003310 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003311 newDisplay = mActivityDisplays.get(displayId) == null;
3312 if (newDisplay) {
3313 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003314 if (activityDisplay.mDisplay == null) {
3315 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3316 return;
3317 }
Craig Mautner4504de52013-12-20 09:06:56 -08003318 mActivityDisplays.put(displayId, activityDisplay);
3319 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003320 }
Craig Mautner4504de52013-12-20 09:06:56 -08003321 if (newDisplay) {
3322 mWindowManager.onDisplayAdded(displayId);
3323 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003324 }
3325
3326 public void handleDisplayRemovedLocked(int displayId) {
3327 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003328 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3329 if (activityDisplay != null) {
3330 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003331 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003332 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003333 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003334 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003335 }
3336 }
3337 mWindowManager.onDisplayRemoved(displayId);
3338 }
3339
3340 public void handleDisplayChangedLocked(int displayId) {
3341 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003342 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3343 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003344 // TODO: Update the bounds.
3345 }
3346 }
3347 mWindowManager.onDisplayChanged(displayId);
3348 }
3349
3350 StackInfo getStackInfo(ActivityStack stack) {
3351 StackInfo info = new StackInfo();
3352 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3353 info.displayId = Display.DEFAULT_DISPLAY;
3354 info.stackId = stack.mStackId;
3355
3356 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3357 final int numTasks = tasks.size();
3358 int[] taskIds = new int[numTasks];
3359 String[] taskNames = new String[numTasks];
3360 for (int i = 0; i < numTasks; ++i) {
3361 final TaskRecord task = tasks.get(i);
3362 taskIds[i] = task.taskId;
3363 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3364 : task.realActivity != null ? task.realActivity.flattenToString()
3365 : task.getTopActivity() != null ? task.getTopActivity().packageName
3366 : "unknown";
3367 }
3368 info.taskIds = taskIds;
3369 info.taskNames = taskNames;
3370 return info;
3371 }
3372
3373 StackInfo getStackInfoLocked(int stackId) {
3374 ActivityStack stack = getStack(stackId);
3375 if (stack != null) {
3376 return getStackInfo(stack);
3377 }
3378 return null;
3379 }
3380
3381 ArrayList<StackInfo> getAllStackInfosLocked() {
3382 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003383 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3384 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003385 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3386 list.add(getStackInfo(stacks.get(ndx)));
3387 }
3388 }
3389 return list;
3390 }
3391
Jason Monka8f569c2014-07-07 12:15:21 -04003392 void showLockTaskToast() {
3393 mLockTaskNotify.showToast(mLockTaskIsLocked);
3394 }
3395
3396 void setLockTaskModeLocked(TaskRecord task, boolean isLocked) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003397 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003398 // Take out of lock task mode if necessary
3399 if (mLockTaskModeTask != null) {
3400 final Message lockTaskMsg = Message.obtain();
3401 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3402 lockTaskMsg.what = LOCK_TASK_END_MSG;
3403 mLockTaskModeTask = null;
3404 mHandler.sendMessage(lockTaskMsg);
3405 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003406 return;
3407 }
3408 if (isLockTaskModeViolation(task)) {
3409 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3410 return;
3411 }
3412 mLockTaskModeTask = task;
3413 findTaskToMoveToFrontLocked(task, 0, null);
3414 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003415
3416 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003417 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3418 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003419 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003420 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003421 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003422 }
3423
3424 boolean isLockTaskModeViolation(TaskRecord task) {
3425 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3426 }
3427
3428 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3429 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003430 final Message lockTaskMsg = Message.obtain();
3431 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3432 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003433 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003434 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003435 }
3436 }
3437
3438 boolean isInLockTaskMode() {
3439 return mLockTaskModeTask != null;
3440 }
3441
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003442 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003443
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003444 public ActivityStackSupervisorHandler(Looper looper) {
3445 super(looper);
3446 }
3447
Craig Mautnerf3333272013-04-22 10:55:53 -07003448 void activityIdleInternal(ActivityRecord r) {
3449 synchronized (mService) {
3450 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3451 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003452 }
3453
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003454 @Override
3455 public void handleMessage(Message msg) {
3456 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003457 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003458 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003459 if (mService.mDidDexOpt) {
3460 mService.mDidDexOpt = false;
3461 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3462 nmsg.obj = msg.obj;
3463 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3464 return;
3465 }
3466 // We don't at this point know if the activity is fullscreen,
3467 // so we need to be conservative and assume it isn't.
3468 activityIdleInternal((ActivityRecord)msg.obj);
3469 } break;
3470 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003471 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003472 activityIdleInternal((ActivityRecord)msg.obj);
3473 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003474 case RESUME_TOP_ACTIVITY_MSG: {
3475 synchronized (mService) {
3476 resumeTopActivitiesLocked();
3477 }
3478 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003479 case SLEEP_TIMEOUT_MSG: {
3480 synchronized (mService) {
3481 if (mService.isSleepingOrShuttingDown()) {
3482 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3483 mSleepTimeout = true;
3484 checkReadyForSleepLocked();
3485 }
3486 }
3487 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003488 case LAUNCH_TIMEOUT_MSG: {
3489 if (mService.mDidDexOpt) {
3490 mService.mDidDexOpt = false;
3491 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3492 return;
3493 }
3494 synchronized (mService) {
3495 if (mLaunchingActivity.isHeld()) {
3496 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3497 if (VALIDATE_WAKE_LOCK_CALLER
3498 && Binder.getCallingUid() != Process.myUid()) {
3499 throw new IllegalStateException("Calling must be system uid");
3500 }
3501 mLaunchingActivity.release();
3502 }
3503 }
3504 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003505 case HANDLE_DISPLAY_ADDED: {
3506 handleDisplayAddedLocked(msg.arg1);
3507 } break;
3508 case HANDLE_DISPLAY_CHANGED: {
3509 handleDisplayChangedLocked(msg.arg1);
3510 } break;
3511 case HANDLE_DISPLAY_REMOVED: {
3512 handleDisplayRemovedLocked(msg.arg1);
3513 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003514 case CONTAINER_CALLBACK_VISIBILITY: {
3515 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003516 final IActivityContainerCallback callback = container.mCallback;
3517 if (callback != null) {
3518 try {
3519 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3520 } catch (RemoteException e) {
3521 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003522 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003523 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003524 case LOCK_TASK_START_MSG: {
3525 // When lock task starts, we disable the status bars.
3526 try {
Jason Monk62515be2014-05-21 16:06:19 -04003527 if (mLockTaskNotify == null) {
3528 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003529 }
Jason Monk62515be2014-05-21 16:06:19 -04003530 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003531 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003532 if (getStatusBarService() != null) {
3533 int flags =
3534 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003535 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003536 flags ^= StatusBarManager.DISABLE_HOME
3537 | StatusBarManager.DISABLE_RECENT;
3538 }
3539 getStatusBarService().disable(flags, mToken,
3540 mService.mContext.getPackageName());
3541 }
3542 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003543 if (getDevicePolicyManager() != null) {
3544 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003545 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003546 }
justinzhang5286d3f2014-05-12 17:06:01 -04003547 } catch (RemoteException ex) {
3548 throw new RuntimeException(ex);
3549 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003550 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003551 case LOCK_TASK_END_MSG: {
3552 // When lock task ends, we enable the status bars.
3553 try {
Jason Monk62515be2014-05-21 16:06:19 -04003554 if (getStatusBarService() != null) {
3555 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3556 mService.mContext.getPackageName());
3557 }
3558 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003559 if (getDevicePolicyManager() != null) {
3560 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3561 msg.arg1);
3562 }
Jason Monk62515be2014-05-21 16:06:19 -04003563 if (mLockTaskNotify == null) {
3564 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3565 }
3566 mLockTaskNotify.show(false);
3567 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003568 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003569 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003570 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003571 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003572 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003573 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003574 new LockPatternUtils(mService.mContext)
3575 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003576 }
3577 } catch (SettingNotFoundException e) {
3578 // No setting, don't lock.
3579 }
justinzhang5286d3f2014-05-12 17:06:01 -04003580 } catch (RemoteException ex) {
3581 throw new RuntimeException(ex);
3582 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003583 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003584 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3585 final ActivityContainer container = (ActivityContainer) msg.obj;
3586 final IActivityContainerCallback callback = container.mCallback;
3587 if (callback != null) {
3588 try {
3589 callback.onAllActivitiesComplete(container.asBinder());
3590 } catch (RemoteException e) {
3591 }
3592 }
3593 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003594 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3595 synchronized (mService) {
3596 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3597 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003598 final ActivityContainer container = (ActivityContainer) msg.obj;
3599 container.mStack.finishAllActivitiesLocked(true);
3600 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003601 }
3602 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003603 case LAUNCH_TASK_BEHIND_COMPLETE: {
3604 synchronized (mService) {
3605 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3606 if (r != null) {
3607 handleLaunchTaskBehindCompleteLocked(r);
3608 }
3609 }
3610 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003611 }
3612 }
3613 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003614
Ying Wangb081a592014-04-22 15:20:16 -07003615 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003616 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003617 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003618 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003619 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003621 ActivityRecord mParentActivity = null;
3622 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003623
Craig Mautnere3a00d72014-04-16 08:31:19 -07003624 boolean mVisible = true;
3625
Craig Mautner4a1cb222013-12-04 16:14:06 -08003626 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003627 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003628
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003629 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3630 final static int CONTAINER_STATE_NO_SURFACE = 1;
3631 final static int CONTAINER_STATE_FINISHING = 2;
3632 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3633
3634 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003635 synchronized (mService) {
3636 mStackId = stackId;
3637 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003638 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003639 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003640 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003641 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003642
Craig Mautnere0a38842013-12-16 16:14:02 -08003643 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003644 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3645 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003646 mActivityDisplay = activityDisplay;
3647 mStack.mDisplayId = activityDisplay.mDisplayId;
3648 mStack.mStacks = activityDisplay.mStacks;
3649
3650 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003651 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003652 }
3653
3654 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003655 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003656 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003657 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3658 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003659 return;
3660 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003661 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003662 }
3663 }
3664
3665 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003666 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003667 synchronized (mService) {
3668 if (mActivityDisplay != null) {
3669 return mActivityDisplay.mDisplayId;
3670 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003671 }
3672 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003673 }
3674
Jeff Brownca9bc702014-02-11 14:32:56 -08003675 @Override
3676 public boolean injectEvent(InputEvent event) {
3677 final long origId = Binder.clearCallingIdentity();
3678 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003679 synchronized (mService) {
3680 if (mActivityDisplay != null) {
3681 return mInputManagerInternal.injectInputEvent(event,
3682 mActivityDisplay.mDisplayId,
3683 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3684 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003685 }
3686 return false;
3687 } finally {
3688 Binder.restoreCallingIdentity(origId);
3689 }
3690 }
3691
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003692 @Override
3693 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003694 synchronized (mService) {
3695 if (mContainerState == CONTAINER_STATE_FINISHING) {
3696 return;
3697 }
3698 mContainerState = CONTAINER_STATE_FINISHING;
3699
3700 final Message msg =
3701 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003702 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003703
3704 long origId = Binder.clearCallingIdentity();
3705 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003706 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003707 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003708 } finally {
3709 Binder.restoreCallingIdentity(origId);
3710 }
3711 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003712 }
3713
Craig Mautner60257702014-09-17 15:02:33 -07003714 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003715 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3716 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003717 if (mActivityDisplay != null) {
3718 mActivityDisplay.detachActivitiesLocked(mStack);
3719 mActivityDisplay = null;
3720 mStack.mDisplayId = -1;
3721 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003722 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003723 }
3724 }
3725
3726 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003727 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003728 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003729 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003730 Binder.getCallingUid(), mCurrentUser, false,
3731 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003732 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003733 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003734 String mimeType = intent.getType();
3735 if (mimeType == null && intent.getData() != null
3736 && "content".equals(intent.getData().getScheme())) {
3737 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3738 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003739 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0,
3740 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003741 }
3742
3743 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003744 public final int startActivityIntentSender(IIntentSender intentSender) {
3745 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3746
3747 if (!(intentSender instanceof PendingIntentRecord)) {
3748 throw new IllegalArgumentException("Bad PendingIntent object");
3749 }
3750
3751 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003752 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003753 }
3754
Craig Mautner247ab652014-04-25 10:09:00 -07003755 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3756 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003757 Binder.getCallingUid(), mCurrentUser, false,
3758 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003759 if (resolvedType == null) {
3760 resolvedType = intent.getType();
3761 if (resolvedType == null && intent.getData() != null
3762 && "content".equals(intent.getData().getScheme())) {
3763 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3764 }
3765 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003766 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003767 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003768 throw new SecurityException(
3769 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3770 }
3771 }
3772
3773 /** Throw a SecurityException if allowEmbedded is not true */
3774 @Override
3775 public final void checkEmbeddedAllowed(Intent intent) {
3776 checkEmbeddedAllowedInner(intent, null);
3777 }
3778
3779 /** Throw a SecurityException if allowEmbedded is not true */
3780 @Override
3781 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3782 if (!(intentSender instanceof PendingIntentRecord)) {
3783 throw new IllegalArgumentException("Bad PendingIntent object");
3784 }
3785 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3786 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3787 pendingIntent.key.requestResolvedType);
3788 }
3789
Craig Mautnerdf88d732014-01-27 09:21:32 -08003790 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003791 public IBinder asBinder() {
3792 return this;
3793 }
3794
Craig Mautner4504de52013-12-20 09:06:56 -08003795 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003796 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003797 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003798 }
3799
Craig Mautner4a1cb222013-12-04 16:14:06 -08003800 ActivityStackSupervisor getOuter() {
3801 return ActivityStackSupervisor.this;
3802 }
3803
Craig Mautnerd163e752014-06-13 17:18:47 -07003804 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003805 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806 }
3807
3808 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003809 synchronized (mService) {
3810 if (mActivityDisplay != null) {
3811 mActivityDisplay.getBounds(outBounds);
3812 } else {
3813 outBounds.set(0, 0);
3814 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 }
3816 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003817
Craig Mautner6985bad2014-04-21 15:22:06 -07003818 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003819 void setVisible(boolean visible) {
3820 if (mVisible != visible) {
3821 mVisible = visible;
3822 if (mCallback != null) {
3823 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3824 0 /* unused */, this).sendToTarget();
3825 }
3826 }
3827 }
3828
Craig Mautner6985bad2014-04-21 15:22:06 -07003829 void setDrawn() {
3830 }
3831
Craig Mautner1b4bf852014-05-26 15:06:32 -07003832 // You can always start a new task on a regular ActivityStack.
3833 boolean isEligibleForNewTasks() {
3834 return true;
3835 }
3836
Craig Mautnerd163e752014-06-13 17:18:47 -07003837 void onTaskListEmptyLocked() {
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003838 }
3839
Craig Mautner34b73df2014-01-12 21:11:08 -08003840 @Override
3841 public String toString() {
3842 return mIdString + (mActivityDisplay == null ? "N" : "A");
3843 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003844 }
3845
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003846 private class VirtualActivityContainer extends ActivityContainer {
3847 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003848 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003849
3850 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3851 super(getNextStackId());
3852 mParentActivity = parent;
3853 mCallback = callback;
3854 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003855 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003856 }
3857
3858 @Override
3859 public void setSurface(Surface surface, int width, int height, int density) {
3860 super.setSurface(surface, width, height, density);
3861
3862 synchronized (mService) {
3863 final long origId = Binder.clearCallingIdentity();
3864 try {
3865 setSurfaceLocked(surface, width, height, density);
3866 } finally {
3867 Binder.restoreCallingIdentity(origId);
3868 }
3869 }
3870 }
3871
3872 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3873 if (mContainerState == CONTAINER_STATE_FINISHING) {
3874 return;
3875 }
3876 VirtualActivityDisplay virtualActivityDisplay =
3877 (VirtualActivityDisplay) mActivityDisplay;
3878 if (virtualActivityDisplay == null) {
3879 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003880 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003881 mActivityDisplay = virtualActivityDisplay;
3882 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3883 attachToDisplayLocked(virtualActivityDisplay);
3884 }
3885
3886 if (mSurface != null) {
3887 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003888 }
3889
Craig Mautner6985bad2014-04-21 15:22:06 -07003890 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003891 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003892 mStack.resumeTopActivityLocked(null);
3893 } else {
3894 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003895 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003896 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003897 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003898 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003899 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003900
Craig Mautnerd163e752014-06-13 17:18:47 -07003901 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003902
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003903 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3904 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003905 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003906
Craig Mautner6985bad2014-04-21 15:22:06 -07003907 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003908 boolean isAttachedLocked() {
3909 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003910 }
3911
3912 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003913 void setDrawn() {
3914 synchronized (mService) {
3915 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003916 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003917 }
3918 }
3919
Craig Mautner1b4bf852014-05-26 15:06:32 -07003920 // Never start a new task on an ActivityView if it isn't explicitly specified.
3921 @Override
3922 boolean isEligibleForNewTasks() {
3923 return false;
3924 }
3925
Craig Mautner60257702014-09-17 15:02:33 -07003926 void onTaskListEmptyLocked() {
3927 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3928 detachLocked();
3929 deleteActivityContainer(this);
3930 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3931 }
3932
Craig Mautnerd163e752014-06-13 17:18:47 -07003933 private void setSurfaceIfReadyLocked() {
3934 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003935 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3936 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3937 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3938 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3939 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003940 }
3941 }
3942
Craig Mautner4a1cb222013-12-04 16:14:06 -08003943 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3944 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003945 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003946 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003947 int mDisplayId;
3948 Display mDisplay;
3949 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003950
Craig Mautner4a1cb222013-12-04 16:14:06 -08003951 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3952 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003953 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003954
Jose Lima4b6c6692014-08-12 17:41:12 -07003955 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003956
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003957 ActivityDisplay() {
3958 }
Craig Mautner4504de52013-12-20 09:06:56 -08003959
Craig Mautner1a70a162014-09-13 12:09:31 -07003960 // After instantiation, check that mDisplay is not null before using this. The alternative
3961 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003962 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003963 final Display display = mDisplayManager.getDisplay(displayId);
3964 if (display == null) {
3965 return;
3966 }
3967 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003968 }
3969
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003970 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003971 mDisplay = display;
3972 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003973 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003974 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003975
3976 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003977 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3978 + mDisplayId);
3979 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003980 }
3981
Craig Mautnere0a38842013-12-16 16:14:02 -08003982 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003983 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003984 + " from displayId=" + mDisplayId);
3985 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003986 }
3987
3988 void getBounds(Point bounds) {
3989 mDisplay.getDisplayInfo(mDisplayInfo);
3990 bounds.x = mDisplayInfo.appWidth;
3991 bounds.y = mDisplayInfo.appHeight;
3992 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003993
Jose Lima4b6c6692014-08-12 17:41:12 -07003994 void setVisibleBehindActivity(ActivityRecord r) {
3995 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003996 }
3997
Jose Lima4b6c6692014-08-12 17:41:12 -07003998 boolean hasVisibleBehindActivity() {
3999 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004000 }
4001
Craig Mautner34b73df2014-01-12 21:11:08 -08004002 @Override
4003 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004004 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4005 }
4006 }
4007
4008 class VirtualActivityDisplay extends ActivityDisplay {
4009 VirtualDisplay mVirtualDisplay;
4010
Craig Mautner6985bad2014-04-21 15:22:06 -07004011 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004012 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004013 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4014 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4015 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4016 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004017
4018 init(mVirtualDisplay.getDisplay());
4019
4020 mWindowManager.handleDisplayAdded(mDisplayId);
4021 }
4022
4023 void setSurface(Surface surface) {
4024 if (mVirtualDisplay != null) {
4025 mVirtualDisplay.setSurface(surface);
4026 }
4027 }
4028
4029 @Override
4030 void detachActivitiesLocked(ActivityStack stack) {
4031 super.detachActivitiesLocked(stack);
4032 if (mVirtualDisplay != null) {
4033 mVirtualDisplay.release();
4034 mVirtualDisplay = null;
4035 }
4036 }
4037
4038 @Override
4039 public String toString() {
4040 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004041 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004042 }
Jose Lima58e66d62014-05-27 20:00:27 -07004043
4044 private boolean isLeanbackOnlyDevice() {
4045 boolean onLeanbackOnly = false;
4046 try {
4047 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4048 PackageManager.FEATURE_LEANBACK_ONLY);
4049 } catch (RemoteException e) {
4050 // noop
4051 }
4052
4053 return onLeanbackOnly;
4054 }
Craig Mautner27084302013-03-25 08:05:25 -07004055}