blob: 8058c0524e42753d0d5aa9a3867c74cdcd413f8a [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner29219d92013-04-16 20:19:12 -070020import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070022import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Craig Mautner2420ead2013-04-01 17:13:20 -070023import static com.android.server.am.ActivityManagerService.localLOGV;
Craig Mautner23ac33b2013-04-01 16:26:35 -070024import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070025import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Craig Mautner6170f732013-04-02 13:05:23 -070027import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070029import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070030import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
31import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070032import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070033import static com.android.server.am.ActivityManagerService.TAG;
Craig Mautner84984fa2014-06-19 11:19:20 -070034import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
35import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070037
Craig Mautner2420ead2013-04-01 17:13:20 -070038import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080040import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityOptions;
42import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import android.app.IActivityContainer;
44import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.app.PendingIntent;
Craig Mautner20e72272013-04-01 13:45:53 -070048import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070050import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040051import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040052import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070054import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070056import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.pm.ApplicationInfo;
60import android.content.pm.PackageManager;
61import android.content.pm.ResolveInfo;
62import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080063import android.graphics.Point;
64import android.hardware.display.DisplayManager;
65import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080066import android.hardware.display.DisplayManagerGlobal;
67import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080068import android.hardware.input.InputManager;
69import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070070import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070071import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070072import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070073import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070074import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070075import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070076import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070078import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070079import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070080import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040081import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070082import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070083import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040084import android.provider.Settings;
85import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070086import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070087import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070088import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070089import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080090import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070091
Craig Mautner4a1cb222013-12-04 16:14:06 -080092import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080093import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080094import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080095import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080096import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070097import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070098import com.android.internal.app.IVoiceInteractor;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070099import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400100import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400101import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800102import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700103import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700104import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700105
justinzhang5286d3f2014-05-12 17:06:01 -0400106
Craig Mautner8d341ef2013-03-26 09:03:27 -0700107import java.io.FileDescriptor;
108import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700109import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700110import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700111import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700112
Craig Mautner4a1cb222013-12-04 16:14:06 -0800113public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700114 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
115 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
116 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700117 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700118 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700119 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700120 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
121 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
122 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700123 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700124
Craig Mautner2219a1b2013-03-25 09:44:30 -0700125 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700126
Craig Mautnerf3333272013-04-22 10:55:53 -0700127 /** How long we wait until giving up on the last activity telling us it is idle. */
128 static final int IDLE_TIMEOUT = 10*1000;
129
Craig Mautner0eea92c2013-05-16 13:35:39 -0700130 /** How long we can hold the sleep wake lock before giving up. */
131 static final int SLEEP_TIMEOUT = 5*1000;
132
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700133 // How long we can hold the launch wake lock before giving up.
134 static final int LAUNCH_TIMEOUT = 10*1000;
135
Craig Mautner05d29032013-05-03 13:40:13 -0700136 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
137 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
138 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700139 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700140 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800141 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
142 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
143 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700144 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400145 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
146 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700147 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700148 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700149 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800150
Craig Mautner4504de52013-12-20 09:06:56 -0800151 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700152
Jason Monk62515be2014-05-21 16:06:19 -0400153 private static final String LOCK_TASK_TAG = "Lock-to-App";
154
justinzhang5286d3f2014-05-12 17:06:01 -0400155 /** Status Bar Service **/
156 private IBinder mToken = new Binder();
157 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400158 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400159
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700160 // For debugging to make sure the caller when acquiring/releasing our
161 // wake lock is the system process.
162 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700163
Craig Mautner27084302013-03-25 08:05:25 -0700164 final ActivityManagerService mService;
165
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700166 final ActivityStackSupervisorHandler mHandler;
167
168 /** Short cut */
169 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800170 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700171
Craig Mautner8d341ef2013-03-26 09:03:27 -0700172 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700173 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700174
175 /** Task identifier that activities are currently being started in. Incremented each time a
176 * new task is created. */
177 private int mCurTaskId = 0;
178
Craig Mautner2420ead2013-04-01 17:13:20 -0700179 /** The current user */
180 private int mCurrentUser;
181
Craig Mautnere0a38842013-12-16 16:14:02 -0800182 /** The stack containing the launcher app. Assumed to always be attached to
183 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700184 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700185
Craig Mautnere0a38842013-12-16 16:14:02 -0800186 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700187 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700188
Craig Mautner4a1cb222013-12-04 16:14:06 -0800189 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
190 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800191 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800192 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700193
194 /** List of activities that are waiting for a new activity to become visible before completing
195 * whatever operation they are supposed to do. */
196 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
197
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700198 /** List of processes waiting to find out about the next visible activity. */
199 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
200 new ArrayList<IActivityManager.WaitResult>();
201
202 /** List of processes waiting to find out about the next launched activity. */
203 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
204 new ArrayList<IActivityManager.WaitResult>();
205
Craig Mautnerde4ef022013-04-07 19:01:33 -0700206 /** List of activities that are ready to be stopped, but waiting for the next activity to
207 * settle down before doing so. */
208 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
209
Craig Mautnerf3333272013-04-22 10:55:53 -0700210 /** List of activities that are ready to be finished, but waiting for the previous activity to
211 * settle down before doing so. It contains ActivityRecord objects. */
212 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
213
Craig Mautner0eea92c2013-05-16 13:35:39 -0700214 /** List of activities that are in the process of going to sleep. */
215 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
216
Craig Mautnerf3333272013-04-22 10:55:53 -0700217 /** Used on user changes */
218 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
219
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700220 /** Used to queue up any background users being started */
221 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
222
Craig Mautnerde4ef022013-04-07 19:01:33 -0700223 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
224 * is being brought in front of us. */
225 boolean mUserLeaving = false;
226
Craig Mautner0eea92c2013-05-16 13:35:39 -0700227 /** Set when we have taken too long waiting to go to sleep. */
228 boolean mSleepTimeout = false;
229
Jose Lima58e66d62014-05-27 20:00:27 -0700230 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
231 * setWindowManager is called. **/
232 private boolean mLeanbackOnlyDevice;
233
Craig Mautner0eea92c2013-05-16 13:35:39 -0700234 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700235 * We don't want to allow the device to go to sleep while in the process
236 * of launching an activity. This is primarily to allow alarm intent
237 * receivers to launch an activity and get that to run before the device
238 * goes back to sleep.
239 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700240 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700241
242 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700243 * Set when the system is going to sleep, until we have
244 * successfully paused the current activity and released our wake lock.
245 * At that point the system is allowed to actually sleep.
246 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700247 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700248
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700249 /** Stack id of the front stack when user switched, indexed by userId. */
250 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700251
Craig Mautner4504de52013-12-20 09:06:56 -0800252 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800253 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700254 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800255
256 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700257 private final SparseArray<ActivityDisplay> mActivityDisplays =
258 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800259
Jeff Brownca9bc702014-02-11 14:32:56 -0800260 InputManagerInternal mInputManagerInternal;
261
Craig Mautneraea74a52014-03-08 14:23:10 -0800262 /** If non-null then the task specified remains in front and no other tasks may be started
263 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400264 TaskRecord mLockTaskModeTask;
Jason Monka8f569c2014-07-07 12:15:21 -0400265 /** Whether lock task has been entered by an authorized app and cannot
266 * be exited. */
267 private boolean mLockTaskIsLocked;
Jason Monk62515be2014-05-21 16:06:19 -0400268 /**
269 * Notifies the user when entering/exiting lock-task.
270 */
271 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800272
Craig Mautneree36c772014-07-16 14:56:05 -0700273 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
274 = new ArrayList<PendingActivityLaunch>();
275
276 /**
277 * Description of a request to start a new activity, which has been held
278 * due to app switches being disabled.
279 */
280 static class PendingActivityLaunch {
281 final ActivityRecord r;
282 final ActivityRecord sourceRecord;
283 final int startFlags;
284 final ActivityStack stack;
285
286 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
287 int _startFlags, ActivityStack _stack) {
288 r = _r;
289 sourceRecord = _sourceRecord;
290 startFlags = _startFlags;
291 stack = _stack;
292 }
293 }
294
Craig Mautner4a1cb222013-12-04 16:14:06 -0800295 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700296 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700297 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
298 }
299
300 /**
301 * At the time when the constructor runs, the power manager has not yet been
302 * initialized. So we initialize our wakelocks afterwards.
303 */
304 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800305 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700306 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700307 mLaunchingActivity =
308 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
309 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700310 }
311
justinzhang5286d3f2014-05-12 17:06:01 -0400312 // This function returns a IStatusBarService. The value is from ServiceManager.
313 // getService and is cached.
314 private IStatusBarService getStatusBarService() {
315 synchronized (mService) {
316 if (mStatusBarService == null) {
317 mStatusBarService = IStatusBarService.Stub.asInterface(
318 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
319 if (mStatusBarService == null) {
320 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
321 }
322 }
323 return mStatusBarService;
324 }
325 }
326
Jason Monk35c62a42014-06-17 10:24:47 -0400327 private IDevicePolicyManager getDevicePolicyManager() {
328 synchronized (mService) {
329 if (mDevicePolicyManager == null) {
330 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
331 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
332 if (mDevicePolicyManager == null) {
333 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
334 }
335 }
336 return mDevicePolicyManager;
337 }
338 }
339
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700340 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800341 synchronized (mService) {
342 mWindowManager = wm;
343
344 mDisplayManager =
345 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
346 mDisplayManager.registerDisplayListener(this, null);
347
348 Display[] displays = mDisplayManager.getDisplays();
349 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
350 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800351 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
352 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800353 }
354
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700355 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800357
358 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700359
360 // Initialize this here, now that we can get a valid reference to PackageManager.
361 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800362 }
Craig Mautner27084302013-03-25 08:05:25 -0700363 }
364
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200365 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700366 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200367 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700368 }
369
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700370 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800371 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700372 }
373
Craig Mautnerde4ef022013-04-07 19:01:33 -0700374 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700376 }
377
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
379 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700380 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800381 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
382 if (parent != null) {
383 stack = parent.task.stack;
384 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800385 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800386 if (stacks != null && !stacks.isEmpty()) {
387 return stack == stacks.get(stacks.size() - 1);
388 }
389 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700390 }
391
Craig Mautnerde4ef022013-04-07 19:01:33 -0700392 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800393 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800394 int topNdx = stacks.size() - 1;
395 if (topNdx <= 0) {
396 return;
397 }
398 ActivityStack topStack = stacks.get(topNdx);
399 final boolean homeInFront = topStack == mHomeStack;
400 if (homeInFront != toFront) {
401 mLastFocusedStack = topStack;
402 stacks.remove(mHomeStack);
403 stacks.add(toFront ? topNdx : 0, mHomeStack);
404 mFocusedStack = stacks.get(topNdx);
405 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
406 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700407 }
408 }
409
Craig Mautner84984fa2014-06-19 11:19:20 -0700410 void moveHomeStackTaskToTop(int homeStackTaskType) {
411 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
412 mWindowManager.showRecentApps();
413 return;
414 }
Craig Mautner69ada552013-04-18 13:51:51 -0700415 moveHomeStack(true);
Craig Mautner84984fa2014-06-19 11:19:20 -0700416 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700417 }
418
Craig Mautner84984fa2014-06-19 11:19:20 -0700419 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev) {
420 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
421 mWindowManager.showRecentApps();
422 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700423 }
Craig Mautner84984fa2014-06-19 11:19:20 -0700424 moveHomeStackTaskToTop(homeStackTaskType);
425 if (prev != null) {
426 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
427 }
428
Craig Mautnera8a90e02013-06-28 15:24:50 -0700429 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700430 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000431 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700432 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700433 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700434 }
435 return mService.startHomeActivityLocked(mCurrentUser);
436 }
437
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200438 void keyguardWaitingForActivityDrawn() {
439 mWindowManager.keyguardWaitingForActivityDrawn();
Craig Mautner27084302013-03-25 08:05:25 -0700440 }
441
Craig Mautner8d341ef2013-03-26 09:03:27 -0700442 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800443 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800444 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800445 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800446 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
447 ActivityStack stack = stacks.get(stackNdx);
448 TaskRecord task = stack.taskForIdLocked(id);
449 if (task != null) {
450 return task;
451 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700452 }
453 }
454 return null;
455 }
456
Craig Mautner6170f732013-04-02 13:05:23 -0700457 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800458 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800459 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800460 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800461 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
462 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
463 if (r != null) {
464 return r;
465 }
Craig Mautner6170f732013-04-02 13:05:23 -0700466 }
467 }
468 return null;
469 }
470
Craig Mautneref73ee12014-04-23 11:45:37 -0700471 void setNextTaskId(int taskId) {
472 if (taskId > mCurTaskId) {
473 mCurTaskId = taskId;
474 }
475 }
476
Craig Mautner8d341ef2013-03-26 09:03:27 -0700477 int getNextTaskId() {
478 do {
479 mCurTaskId++;
480 if (mCurTaskId <= 0) {
481 mCurTaskId = 1;
482 }
483 } while (anyTaskForIdLocked(mCurTaskId) != null);
484 return mCurTaskId;
485 }
486
Craig Mautnerde4ef022013-04-07 19:01:33 -0700487 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700488 ActivityStack stack = getFocusedStack();
489 if (stack == null) {
490 return null;
491 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700492 ActivityRecord resumedActivity = stack.mResumedActivity;
493 if (resumedActivity == null || resumedActivity.app == null) {
494 resumedActivity = stack.mPausingActivity;
495 if (resumedActivity == null || resumedActivity.app == null) {
496 resumedActivity = stack.topRunningActivityLocked(null);
497 }
498 }
499 return resumedActivity;
500 }
501
Mike Lockwooded8902d2013-11-15 11:01:47 -0800502 boolean attachApplicationLocked(ProcessRecord app) throws Exception {
Craig Mautner20e72272013-04-01 13:45:53 -0700503 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800504 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800505 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
506 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800507 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
508 final ActivityStack stack = stacks.get(stackNdx);
509 if (!isFrontStack(stack)) {
510 continue;
511 }
512 ActivityRecord hr = stack.topRunningActivityLocked(null);
513 if (hr != null) {
514 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
515 && processName.equals(hr.processName)) {
516 try {
George Mount2c92c972014-03-20 09:38:23 -0700517 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800518 didSomething = true;
519 }
520 } catch (Exception e) {
521 Slog.w(TAG, "Exception in new application when starting activity "
522 + hr.intent.getComponent().flattenToShortString(), e);
523 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700524 }
Craig Mautner20e72272013-04-01 13:45:53 -0700525 }
Craig Mautner20e72272013-04-01 13:45:53 -0700526 }
527 }
528 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700529 if (!didSomething) {
530 ensureActivitiesVisibleLocked(null, 0);
531 }
Craig Mautner20e72272013-04-01 13:45:53 -0700532 return didSomething;
533 }
534
535 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800536 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
537 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800538 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
539 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800540 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800541 continue;
542 }
543 final ActivityRecord resumedActivity = stack.mResumedActivity;
544 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800545 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
546 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800547 return false;
548 }
Craig Mautner20e72272013-04-01 13:45:53 -0700549 }
550 }
551 return true;
552 }
553
Craig Mautnerde4ef022013-04-07 19:01:33 -0700554 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800555 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
556 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800557 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
558 final ActivityStack stack = stacks.get(stackNdx);
559 if (isFrontStack(stack)) {
560 final ActivityRecord r = stack.mResumedActivity;
561 if (r != null && r.state != ActivityState.RESUMED) {
562 return false;
563 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700564 }
565 }
566 }
567 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 if (DEBUG_STACK) Slog.d(TAG,
569 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
570 mLastFocusedStack + " to=" + mFocusedStack);
571 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700572 return true;
573 }
574
575 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800576 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
577 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800578 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
579 final ActivityStack stack = stacks.get(stackNdx);
580 final ActivityRecord r = stack.mResumedActivity;
581 if (r != null && (!r.nowVisible || r.waitingVisible)) {
582 return false;
583 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700584 }
585 }
586 return true;
587 }
588
Craig Mautner2acc3892013-09-23 10:28:14 -0700589 /**
590 * Pause all activities in either all of the stacks or just the back stacks.
591 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700592 * @return true if any activity was paused as a result of this call.
593 */
Craig Mautner5314a402013-09-26 12:40:16 -0700594 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700595 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800596 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
597 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800598 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
599 final ActivityStack stack = stacks.get(stackNdx);
600 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
601 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
602 " mResumedActivity=" + stack.mResumedActivity);
603 stack.startPausingLocked(userLeaving, false);
604 someActivityPaused = true;
605 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700606 }
607 }
608 return someActivityPaused;
609 }
610
Craig Mautnerde4ef022013-04-07 19:01:33 -0700611 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700612 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
614 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800615 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
616 final ActivityStack stack = stacks.get(stackNdx);
617 final ActivityRecord r = stack.mPausingActivity;
618 if (r != null && r.state != ActivityState.PAUSED
619 && r.state != ActivityState.STOPPED
620 && r.state != ActivityState.STOPPING) {
621 if (DEBUG_STATES) {
622 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
623 pausing = false;
624 } else {
625 return false;
626 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700627 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700628 }
629 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700630 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700631 }
632
Craig Mautnerdf88d732014-01-27 09:21:32 -0800633 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping) {
John Spurlock8a985d22014-02-25 09:40:05 -0500634 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800635 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
636 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
637 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
638 final ActivityStack stack = stacks.get(stackNdx);
639 if (stack.mResumedActivity != null &&
640 stack.mActivityContainer.mParentActivity == parent) {
641 stack.startPausingLocked(userLeaving, uiSleeping);
642 }
643 }
644 }
645 }
646
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700647 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700648 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700649 WaitResult w = mWaitingActivityVisible.get(i);
650 w.timeout = false;
651 if (r != null) {
652 w.who = new ComponentName(r.info.packageName, r.info.name);
653 }
654 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
655 w.thisTime = w.totalTime;
656 }
657 mService.notifyAll();
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200658 notifyActivityDrawnForKeyguard();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700659 }
660
661 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
662 long thisTime, long totalTime) {
663 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700664 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700665 w.timeout = timeout;
666 if (r != null) {
667 w.who = new ComponentName(r.info.packageName, r.info.name);
668 }
669 w.thisTime = thisTime;
670 w.totalTime = totalTime;
671 }
672 mService.notifyAll();
673 }
674
Craig Mautner29219d92013-04-16 20:19:12 -0700675 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700676 final ActivityStack focusedStack = getFocusedStack();
677 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
678 if (r != null) {
679 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700680 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700681
Craig Mautner4a1cb222013-12-04 16:14:06 -0800682 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800683 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800684 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
685 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700686 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700687 r = stack.topRunningActivityLocked(null);
688 if (r != null) {
689 return r;
690 }
691 }
692 }
693 return null;
694 }
695
Dianne Hackborn09233282014-04-30 11:33:59 -0700696 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700697 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800698 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
699 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800700 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800701 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800702 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
704 final ActivityStack stack = stacks.get(stackNdx);
705 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
706 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700707 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700708 }
709 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700710
711 // The lists are already sorted from most recent to oldest. Just pull the most recent off
712 // each list and add it to list. Stop when all lists are empty or maxNum reached.
713 while (maxNum > 0) {
714 long mostRecentActiveTime = Long.MIN_VALUE;
715 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800716 final int numTaskLists = runningTaskLists.size();
717 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
718 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700719 if (!stackTaskList.isEmpty()) {
720 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
721 if (lastActiveTime > mostRecentActiveTime) {
722 mostRecentActiveTime = lastActiveTime;
723 selectedStackList = stackTaskList;
724 }
725 }
726 }
727 if (selectedStackList != null) {
728 list.add(selectedStackList.remove(0));
729 --maxNum;
730 } else {
731 break;
732 }
733 }
Craig Mautner20e72272013-04-01 13:45:53 -0700734 }
735
Craig Mautner23ac33b2013-04-01 16:26:35 -0700736 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
737 String profileFile, ParcelFileDescriptor profileFd, int userId) {
738 // Collect information about the target of the Intent.
739 ActivityInfo aInfo;
740 try {
741 ResolveInfo rInfo =
742 AppGlobals.getPackageManager().resolveIntent(
743 intent, resolvedType,
744 PackageManager.MATCH_DEFAULT_ONLY
745 | ActivityManagerService.STOCK_PM_FLAGS, userId);
746 aInfo = rInfo != null ? rInfo.activityInfo : null;
747 } catch (RemoteException e) {
748 aInfo = null;
749 }
750
751 if (aInfo != null) {
752 // Store the found target back into the intent, because now that
753 // we have it we never want to do this again. For example, if the
754 // user navigates back to this point in the history, we should
755 // always restart the exact same activity.
756 intent.setComponent(new ComponentName(
757 aInfo.applicationInfo.packageName, aInfo.name));
758
759 // Don't debug things in the system process
760 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
761 if (!aInfo.processName.equals("system")) {
762 mService.setDebugApp(aInfo.processName, true, false);
763 }
764 }
765
766 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
767 if (!aInfo.processName.equals("system")) {
768 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
769 }
770 }
771
772 if (profileFile != null) {
773 if (!aInfo.processName.equals("system")) {
774 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
775 profileFile, profileFd,
776 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
777 }
778 }
779 }
780 return aInfo;
781 }
782
Craig Mautner2219a1b2013-03-25 09:44:30 -0700783 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700784 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700785 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null, 0,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700786 null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700787 }
788
Craig Mautner23ac33b2013-04-01 16:26:35 -0700789 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700790 String callingPackage, Intent intent, String resolvedType,
791 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
792 IBinder resultTo, String resultWho, int requestCode, int startFlags, String profileFile,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700793 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700794 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700795 // Refuse possible leaked file descriptors
796 if (intent != null && intent.hasFileDescriptors()) {
797 throw new IllegalArgumentException("File descriptors passed in Intent");
798 }
799 boolean componentSpecified = intent.getComponent() != null;
800
801 // Don't modify the client's object!
802 intent = new Intent(intent);
803
804 // Collect information about the target of the Intent.
805 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
806 profileFile, profileFd, userId);
807
Craig Mautnere0a38842013-12-16 16:14:02 -0800808 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700809 synchronized (mService) {
810 int callingPid;
811 if (callingUid >= 0) {
812 callingPid = -1;
813 } else if (caller == null) {
814 callingPid = Binder.getCallingPid();
815 callingUid = Binder.getCallingUid();
816 } else {
817 callingPid = callingUid = -1;
818 }
819
Craig Mautnere0a38842013-12-16 16:14:02 -0800820 final ActivityStack stack;
821 if (container == null || container.mStack.isOnHomeDisplay()) {
822 stack = getFocusedStack();
823 } else {
824 stack = container.mStack;
825 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700826 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700827 && mService.mConfiguration.diff(config) != 0;
828 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700829 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700830
831 final long origId = Binder.clearCallingIdentity();
832
833 if (aInfo != null &&
834 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
835 // This may be a heavy-weight process! Check to see if we already
836 // have another, different heavy-weight process running.
837 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
838 if (mService.mHeavyWeightProcess != null &&
839 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
840 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700841 int realCallingUid = callingUid;
842 if (caller != null) {
843 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
844 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700845 realCallingUid = callerApp.info.uid;
846 } else {
847 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700848 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700849 + intent.toString());
850 ActivityOptions.abort(options);
851 return ActivityManager.START_PERMISSION_DENIED;
852 }
853 }
854
855 IIntentSender target = mService.getIntentSenderLocked(
856 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
857 realCallingUid, userId, null, null, 0, new Intent[] { intent },
858 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
859 | PendingIntent.FLAG_ONE_SHOT, null);
860
861 Intent newIntent = new Intent();
862 if (requestCode >= 0) {
863 // Caller is requesting a result.
864 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
865 }
866 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
867 new IntentSender(target));
868 if (mService.mHeavyWeightProcess.activities.size() > 0) {
869 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
870 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
871 hist.packageName);
872 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
873 hist.task.taskId);
874 }
875 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
876 aInfo.packageName);
877 newIntent.setFlags(intent.getFlags());
878 newIntent.setClassName("android",
879 HeavyWeightSwitcherActivity.class.getName());
880 intent = newIntent;
881 resolvedType = null;
882 caller = null;
883 callingUid = Binder.getCallingUid();
884 callingPid = Binder.getCallingPid();
885 componentSpecified = true;
886 try {
887 ResolveInfo rInfo =
888 AppGlobals.getPackageManager().resolveIntent(
889 intent, null,
890 PackageManager.MATCH_DEFAULT_ONLY
891 | ActivityManagerService.STOCK_PM_FLAGS, userId);
892 aInfo = rInfo != null ? rInfo.activityInfo : null;
893 aInfo = mService.getActivityInfoForUser(aInfo, userId);
894 } catch (RemoteException e) {
895 aInfo = null;
896 }
897 }
898 }
899 }
900
Dianne Hackborn91097de2014-04-04 18:02:06 -0700901 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
902 voiceSession, voiceInteractor, resultTo, resultWho,
Craig Mautnere0a38842013-12-16 16:14:02 -0800903 requestCode, callingPid, callingUid, callingPackage, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700904 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700905
Craig Mautner85c11a82014-07-17 12:38:18 -0700906 Binder.restoreCallingIdentity(origId);
907
Craig Mautnerde4ef022013-04-07 19:01:33 -0700908 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700909 // If the caller also wants to switch to a new configuration,
910 // do so now. This allows a clean switch, as we are waiting
911 // for the current activity to pause (so we will not destroy
912 // it), and have not yet started the next activity.
913 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
914 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700915 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700916 if (DEBUG_CONFIGURATION) Slog.v(TAG,
917 "Updating to new configuration after starting activity.");
918 mService.updateConfigurationLocked(config, null, false, false);
919 }
920
Craig Mautner23ac33b2013-04-01 16:26:35 -0700921 if (outResult != null) {
922 outResult.result = res;
923 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700924 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700925 do {
926 try {
927 mService.wait();
928 } catch (InterruptedException e) {
929 }
930 } while (!outResult.timeout && outResult.who == null);
931 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700932 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700933 if (r.nowVisible) {
934 outResult.timeout = false;
935 outResult.who = new ComponentName(r.info.packageName, r.info.name);
936 outResult.totalTime = 0;
937 outResult.thisTime = 0;
938 } else {
939 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700940 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700941 do {
942 try {
943 mService.wait();
944 } catch (InterruptedException e) {
945 }
946 } while (!outResult.timeout && outResult.who == null);
947 }
948 }
949 }
950
951 return res;
952 }
953 }
954
955 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
956 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
957 Bundle options, int userId) {
958 if (intents == null) {
959 throw new NullPointerException("intents is null");
960 }
961 if (resolvedTypes == null) {
962 throw new NullPointerException("resolvedTypes is null");
963 }
964 if (intents.length != resolvedTypes.length) {
965 throw new IllegalArgumentException("intents are length different than resolvedTypes");
966 }
967
Craig Mautner23ac33b2013-04-01 16:26:35 -0700968
969 int callingPid;
970 if (callingUid >= 0) {
971 callingPid = -1;
972 } else if (caller == null) {
973 callingPid = Binder.getCallingPid();
974 callingUid = Binder.getCallingUid();
975 } else {
976 callingPid = callingUid = -1;
977 }
978 final long origId = Binder.clearCallingIdentity();
979 try {
980 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700981 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700982 for (int i=0; i<intents.length; i++) {
983 Intent intent = intents[i];
984 if (intent == null) {
985 continue;
986 }
987
988 // Refuse possible leaked file descriptors
989 if (intent != null && intent.hasFileDescriptors()) {
990 throw new IllegalArgumentException("File descriptors passed in Intent");
991 }
992
993 boolean componentSpecified = intent.getComponent() != null;
994
995 // Don't modify the client's object!
996 intent = new Intent(intent);
997
998 // Collect information about the target of the Intent.
999 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
1000 0, null, null, userId);
1001 // TODO: New, check if this is correct
1002 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1003
1004 if (aInfo != null &&
1005 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
1006 != 0) {
1007 throw new IllegalArgumentException(
1008 "FLAG_CANT_SAVE_STATE not supported here");
1009 }
1010
1011 Bundle theseOptions;
1012 if (options != null && i == intents.length-1) {
1013 theseOptions = options;
1014 } else {
1015 theseOptions = null;
1016 }
Craig Mautner6170f732013-04-02 13:05:23 -07001017 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn91097de2014-04-04 18:02:06 -07001018 aInfo, null, null, resultTo, null, -1, callingPid, callingUid, callingPackage,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001019 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001020 if (res < 0) {
1021 return res;
1022 }
1023
1024 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1025 }
1026 }
1027 } finally {
1028 Binder.restoreCallingIdentity(origId);
1029 }
1030
1031 return ActivityManager.START_SUCCESS;
1032 }
1033
Craig Mautner2420ead2013-04-01 17:13:20 -07001034 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001035 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001036 throws RemoteException {
1037
1038 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001039 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001041
1042 // schedule launch ticks to collect information about slow apps.
1043 r.startLaunchTickingLocked();
1044
1045 // Have the window manager re-evaluate the orientation of
1046 // the screen based on the new activity order. Note that
1047 // as a result of this, it can call back into the activity
1048 // manager with a new orientation. We don't care about that,
1049 // because the activity is not currently running so we are
1050 // just restarting it anyway.
1051 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001052 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001053 mService.mConfiguration,
1054 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1055 mService.updateConfigurationLocked(config, r, false, false);
1056 }
1057
1058 r.app = app;
1059 app.waitingToKill = null;
1060 r.launchCount++;
1061 r.lastLaunchTime = SystemClock.uptimeMillis();
1062
1063 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1064
1065 int idx = app.activities.indexOf(r);
1066 if (idx < 0) {
1067 app.activities.add(r);
1068 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001069 mService.updateLruProcessLocked(app, true, null);
1070 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001071
1072 final ActivityStack stack = r.task.stack;
1073 try {
1074 if (app.thread == null) {
1075 throw new RemoteException();
1076 }
1077 List<ResultInfo> results = null;
1078 List<Intent> newIntents = null;
1079 if (andResume) {
1080 results = r.results;
1081 newIntents = r.newIntents;
1082 }
1083 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1084 + " icicle=" + r.icicle
1085 + " with results=" + results + " newIntents=" + newIntents
1086 + " andResume=" + andResume);
1087 if (andResume) {
1088 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1089 r.userId, System.identityHashCode(r),
1090 r.task.taskId, r.shortComponentName);
1091 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001092 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001093 // Home process is the root process of the task.
1094 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001095 }
1096 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1097 r.sleeping = false;
1098 r.forceNewConfig = false;
1099 mService.showAskCompatModeDialogLocked(r);
1100 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1101 String profileFile = null;
1102 ParcelFileDescriptor profileFd = null;
1103 boolean profileAutoStop = false;
1104 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1105 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1106 mService.mProfileProc = app;
1107 profileFile = mService.mProfileFile;
1108 profileFd = mService.mProfileFd;
1109 profileAutoStop = mService.mAutoStopProfiler;
1110 }
1111 }
1112 app.hasShownUi = true;
1113 app.pendingUiClean = true;
1114 if (profileFd != null) {
1115 try {
1116 profileFd = profileFd.dup();
1117 } catch (IOException e) {
1118 if (profileFd != null) {
1119 try {
1120 profileFd.close();
1121 } catch (IOException o) {
1122 }
1123 profileFd = null;
1124 }
1125 }
1126 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001127
Dianne Hackborna413dc02013-07-12 12:02:55 -07001128 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001129 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1130 System.identityHashCode(r), r.info,
Craig Mautnera0026042014-04-23 11:45:37 -07001131 new Configuration(mService.mConfiguration), r.compat, r.task.voiceInteractor,
1132 app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume,
Craig Mautner233ceee2014-05-09 17:05:11 -07001133 mService.isNextTransitionForward(), profileFile, profileFd, profileAutoStop
1134 );
Craig Mautner2420ead2013-04-01 17:13:20 -07001135
1136 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1137 // This may be a heavy-weight process! Note that the package
1138 // manager will ensure that only activity can run in the main
1139 // process of the .apk, which is the only thing that will be
1140 // considered heavy-weight.
1141 if (app.processName.equals(app.info.packageName)) {
1142 if (mService.mHeavyWeightProcess != null
1143 && mService.mHeavyWeightProcess != app) {
1144 Slog.w(TAG, "Starting new heavy weight process " + app
1145 + " when already running "
1146 + mService.mHeavyWeightProcess);
1147 }
1148 mService.mHeavyWeightProcess = app;
1149 Message msg = mService.mHandler.obtainMessage(
1150 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1151 msg.obj = r;
1152 mService.mHandler.sendMessage(msg);
1153 }
1154 }
1155
1156 } catch (RemoteException e) {
1157 if (r.launchFailed) {
1158 // This is the second time we failed -- finish activity
1159 // and give up.
1160 Slog.e(TAG, "Second failure launching "
1161 + r.intent.getComponent().flattenToShortString()
1162 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001163 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001164 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1165 "2nd-crash", false);
1166 return false;
1167 }
1168
1169 // This is the first time we failed -- restart process and
1170 // retry.
1171 app.activities.remove(r);
1172 throw e;
1173 }
1174
1175 r.launchFailed = false;
1176 if (stack.updateLRUListLocked(r)) {
1177 Slog.w(TAG, "Activity " + r
1178 + " being launched, but already in LRU list");
1179 }
1180
1181 if (andResume) {
1182 // As part of the process of launching, ActivityThread also performs
1183 // a resume.
1184 stack.minimalResumeActivityLocked(r);
1185 } else {
1186 // This activity is not starting in the resumed state... which
1187 // should look like we asked it to pause+stop (but remain visible),
1188 // and it has done so and reported back the current icicle and
1189 // other state.
1190 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1191 + " (starting in stopped state)");
1192 r.state = ActivityState.STOPPED;
1193 r.stopped = true;
1194 }
1195
1196 // Launch the new version setup screen if needed. We do this -after-
1197 // launching the initial activity (that is, home), so that it can have
1198 // a chance to initialize itself while in the background, making the
1199 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001200 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001201 mService.startSetupActivityLocked();
1202 }
1203
1204 return true;
1205 }
1206
Craig Mautnere79d42682013-04-01 19:01:53 -07001207 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001208 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001209 // Is this activity's application already running?
1210 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001211 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001212
1213 r.task.stack.setLaunchTime(r);
1214
1215 if (app != null && app.thread != null) {
1216 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001217 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1218 || !"android".equals(r.info.packageName)) {
1219 // Don't add this if it is a platform component that is marked
1220 // to run in multiple processes, because this is actually
1221 // part of the framework so doesn't make sense to track as a
1222 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001223 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1224 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001225 }
George Mount2c92c972014-03-20 09:38:23 -07001226 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001227 return;
1228 } catch (RemoteException e) {
1229 Slog.w(TAG, "Exception when starting activity "
1230 + r.intent.getComponent().flattenToShortString(), e);
1231 }
1232
1233 // If a dead object exception was thrown -- fall through to
1234 // restart the application.
1235 }
1236
1237 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001238 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001239 }
1240
Craig Mautner6170f732013-04-02 13:05:23 -07001241 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001242 Intent intent, String resolvedType, ActivityInfo aInfo,
1243 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1244 IBinder resultTo, String resultWho, int requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001245 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001246 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1247 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001248 int err = ActivityManager.START_SUCCESS;
1249
1250 ProcessRecord callerApp = null;
1251 if (caller != null) {
1252 callerApp = mService.getRecordForAppLocked(caller);
1253 if (callerApp != null) {
1254 callingPid = callerApp.pid;
1255 callingUid = callerApp.info.uid;
1256 } else {
1257 Slog.w(TAG, "Unable to find app for caller " + caller
1258 + " (pid=" + callingPid + ") when starting: "
1259 + intent.toString());
1260 err = ActivityManager.START_PERMISSION_DENIED;
1261 }
1262 }
1263
1264 if (err == ActivityManager.START_SUCCESS) {
1265 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1266 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner9ef471f2014-02-07 13:11:47 -08001267 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)
1268 + " on display " + (container == null ? (mFocusedStack == null ?
1269 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1270 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1271 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001272 }
1273
1274 ActivityRecord sourceRecord = null;
1275 ActivityRecord resultRecord = null;
1276 if (resultTo != null) {
1277 sourceRecord = isInAnyStackLocked(resultTo);
1278 if (DEBUG_RESULTS) Slog.v(
1279 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1280 if (sourceRecord != null) {
1281 if (requestCode >= 0 && !sourceRecord.finishing) {
1282 resultRecord = sourceRecord;
1283 }
1284 }
1285 }
1286 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1287
Dianne Hackborn91097de2014-04-04 18:02:06 -07001288 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001289
1290 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1291 && sourceRecord != null) {
1292 // Transfer the result target from the source activity to the new
1293 // one being started, including any failures.
1294 if (requestCode >= 0) {
1295 ActivityOptions.abort(options);
1296 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1297 }
1298 resultRecord = sourceRecord.resultTo;
1299 resultWho = sourceRecord.resultWho;
1300 requestCode = sourceRecord.requestCode;
1301 sourceRecord.resultTo = null;
1302 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001303 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001304 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001305 if (sourceRecord.launchedFromUid == callingUid) {
1306 // The new activity is being launched from the same uid as the previous
1307 // activity in the flow, and asking to forward its result back to the
1308 // previous. In this case the activity is serving as a trampoline between
1309 // the two, so we also want to update its launchedFromPackage to be the
1310 // same as the previous activity. Note that this is safe, since we know
1311 // these two packages come from the same uid; the caller could just as
1312 // well have supplied that same package name itself. This specifially
1313 // deals with the case of an intent picker/chooser being launched in the app
1314 // flow to redirect to an activity picked by the user, where we want the final
1315 // activity to consider it to have been launched by the previous app activity.
1316 callingPackage = sourceRecord.launchedFromPackage;
1317 }
Craig Mautner6170f732013-04-02 13:05:23 -07001318 }
1319
1320 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1321 // We couldn't find a class that can handle the given Intent.
1322 // That's the end of that!
1323 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1324 }
1325
1326 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1327 // We couldn't find the specific class specified in the Intent.
1328 // Also the end of the line.
1329 err = ActivityManager.START_CLASS_NOT_FOUND;
1330 }
1331
Dianne Hackborn91097de2014-04-04 18:02:06 -07001332 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1333 && sourceRecord.task.voiceSession != null) {
1334 // If this activity is being launched as part of a voice session, we need
1335 // to ensure that it is safe to do so. If the upcoming activity will also
1336 // be part of the voice session, we can only launch it if it has explicitly
1337 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001338 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001339 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1340 try {
1341 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1342 intent, resolvedType)) {
1343 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1344 }
1345 } catch (RemoteException e) {
1346 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1347 }
1348 }
1349 }
1350
1351 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1352 // If the caller is starting a new voice session, just make sure the target
1353 // is actually allowing it to run this way.
1354 try {
1355 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1356 intent, resolvedType)) {
1357 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1358 }
1359 } catch (RemoteException e) {
1360 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1361 }
1362 }
1363
Craig Mautner6170f732013-04-02 13:05:23 -07001364 if (err != ActivityManager.START_SUCCESS) {
1365 if (resultRecord != null) {
1366 resultStack.sendActivityResultLocked(-1,
1367 resultRecord, resultWho, requestCode,
1368 Activity.RESULT_CANCELED, null);
1369 }
Craig Mautner6170f732013-04-02 13:05:23 -07001370 ActivityOptions.abort(options);
1371 return err;
1372 }
1373
1374 final int startAnyPerm = mService.checkPermission(
1375 START_ANY_ACTIVITY, callingPid, callingUid);
1376 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1377 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1378 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1379 if (resultRecord != null) {
1380 resultStack.sendActivityResultLocked(-1,
1381 resultRecord, resultWho, requestCode,
1382 Activity.RESULT_CANCELED, null);
1383 }
Craig Mautner6170f732013-04-02 13:05:23 -07001384 String msg;
1385 if (!aInfo.exported) {
1386 msg = "Permission Denial: starting " + intent.toString()
1387 + " from " + callerApp + " (pid=" + callingPid
1388 + ", uid=" + callingUid + ")"
1389 + " not exported from uid " + aInfo.applicationInfo.uid;
1390 } else {
1391 msg = "Permission Denial: starting " + intent.toString()
1392 + " from " + callerApp + " (pid=" + callingPid
1393 + ", uid=" + callingUid + ")"
1394 + " requires " + aInfo.permission;
1395 }
1396 Slog.w(TAG, msg);
1397 throw new SecurityException(msg);
1398 }
1399
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001400 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001401 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001402
Craig Mautner6170f732013-04-02 13:05:23 -07001403 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001404 try {
1405 // The Intent we give to the watcher has the extra data
1406 // stripped off, since it can contain private information.
1407 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001408 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001409 aInfo.applicationInfo.packageName);
1410 } catch (RemoteException e) {
1411 mService.mController = null;
1412 }
Ben Gruver5e207332013-04-03 17:41:37 -07001413 }
Craig Mautner6170f732013-04-02 13:05:23 -07001414
Ben Gruver5e207332013-04-03 17:41:37 -07001415 if (abort) {
1416 if (resultRecord != null) {
1417 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001418 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001419 }
Ben Gruver5e207332013-04-03 17:41:37 -07001420 // We pretend to the caller that it was really started, but
1421 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001422 ActivityOptions.abort(options);
1423 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001424 }
1425
1426 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001427 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001428 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001429 if (outActivity != null) {
1430 outActivity[0] = r;
1431 }
1432
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001433 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001434 if (voiceSession == null && (stack.mResumedActivity == null
1435 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Craig Mautner6170f732013-04-02 13:05:23 -07001436 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1437 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001438 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001439 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001440 ActivityOptions.abort(options);
1441 return ActivityManager.START_SWITCHES_CANCELED;
1442 }
1443 }
1444
1445 if (mService.mDidAppSwitch) {
1446 // This is the second allowed switch since we stopped switches,
1447 // so now just generally allow switches. Use case: user presses
1448 // home (switches disabled, switch to home, mDidAppSwitch now true);
1449 // user taps a home icon (coming from home so allowed, we hit here
1450 // and now allow anyone to switch again).
1451 mService.mAppSwitchesAllowedTime = 0;
1452 } else {
1453 mService.mDidAppSwitch = true;
1454 }
1455
Craig Mautneree36c772014-07-16 14:56:05 -07001456 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001457
Dianne Hackborn91097de2014-04-04 18:02:06 -07001458 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001459 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001460
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001461 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001462 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001463 // activity start, but we are not actually doing an activity
1464 // switch... just dismiss the keyguard now, because we
1465 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001466 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001467 }
1468 return err;
1469 }
1470
Craig Mautner1b4bf852014-05-26 15:06:32 -07001471 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001472 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001473
1474 // On leanback only devices we should keep all activities in the same stack.
1475 if (!mLeanbackOnlyDevice &&
1476 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001477 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001478 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001479 if (taskStack.isOnHomeDisplay()) {
1480 if (mFocusedStack != taskStack) {
1481 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1482 "focused stack to r=" + r + " task=" + task);
1483 mFocusedStack = taskStack;
1484 } else {
1485 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1486 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1487 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001488 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001489 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001490 }
1491
Craig Mautnere0a38842013-12-16 16:14:02 -08001492 final ActivityContainer container = r.mInitialActivityContainer;
1493 if (container != null) {
1494 // The first time put it on the desired stack, after this put on task stack.
1495 r.mInitialActivityContainer = null;
1496 return container.mStack;
1497 }
1498
Craig Mautner1b4bf852014-05-26 15:06:32 -07001499 if (mFocusedStack != mHomeStack && (!newTask ||
1500 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001501 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1502 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1503 return mFocusedStack;
1504 }
1505
Craig Mautnere0a38842013-12-16 16:14:02 -08001506 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1507 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1508 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1509 if (!stack.isHomeStack()) {
1510 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1511 "adjustStackFocus: Setting focused stack=" + stack);
1512 mFocusedStack = stack;
1513 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001514 }
1515 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001516
Craig Mautner4a1cb222013-12-04 16:14:06 -08001517 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001518 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001519 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1520 " stackId=" + stackId);
1521 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001522 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001523 }
1524 return mHomeStack;
1525 }
1526
Craig Mautner29219d92013-04-16 20:19:12 -07001527 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001528 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001529 final TaskRecord task = r.task;
1530 boolean isHomeActivity = !r.isApplicationActivity();
1531 if (!isHomeActivity && task != null) {
1532 isHomeActivity = !task.isApplicationTask();
1533 }
1534 if (!isHomeActivity && task != null) {
1535 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1536 isHomeActivity = parent != null && parent.isHomeActivity();
1537 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001538 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001539 }
1540 }
1541
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001542 final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001543 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001544 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001545 final Intent intent = r.intent;
1546 final int callingUid = r.launchedFromUid;
1547
Craig Mautnerad400af2014-08-13 16:41:36 -07001548 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001549 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1550 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001551
Craig Mautnera228ae92014-07-09 05:44:55 -07001552 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001553 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001554 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001555 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1556 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1557 "\"singleInstance\" or \"singleTask\"");
1558 launchFlags &=
1559 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1560 } else {
1561 switch (r.info.documentLaunchMode) {
1562 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1563 break;
1564 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1565 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1566 break;
1567 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1568 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1569 break;
1570 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1571 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1572 break;
1573 }
1574 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001575
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001576 final boolean launchTaskBehind = r.mLaunchTaskBehind
1577 && !launchSingleTask && !launchSingleInstance
1578 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001579
1580 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1581 // For whatever reason this activity is being launched into a new
1582 // task... yet the caller has requested a result back. Well, that
1583 // is pretty messed up, so instead immediately send back a cancel
1584 // and let the new task continue launched as normal without a
1585 // dependency on its originator.
1586 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1587 r.resultTo.task.stack.sendActivityResultLocked(-1,
1588 r.resultTo, r.resultWho, r.requestCode,
1589 Activity.RESULT_CANCELED, null);
1590 r.resultTo = null;
1591 }
1592
1593 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1594 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1595 }
1596
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001597 // If we are actually going to launch in to a new task, there are some cases where
1598 // we further want to do multiple task.
1599 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1600 if (launchTaskBehind
1601 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1602 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1603 }
1604 }
1605
Craig Mautner8849a5e2013-04-02 16:41:03 -07001606 // We'll invoke onUserLeaving before onPause only if the launching
1607 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001608 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001609 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001610
1611 // If the caller has asked not to resume at this point, we make note
1612 // of this in the record so that we can skip it when trying to find
1613 // the top running activity.
1614 if (!doResume) {
1615 r.delayedResume = true;
1616 }
1617
Craig Mautnera254cd72014-05-25 16:47:39 -07001618 ActivityRecord notTop =
1619 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001620
1621 // If the onlyIfNeeded flag is set, then we can do this if the activity
1622 // being launched is the same as the one making the call... or, as
1623 // a special case, if we do not know the caller then we count the
1624 // current top activity as the caller.
1625 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1626 ActivityRecord checkedCaller = sourceRecord;
1627 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001628 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001629 }
1630 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1631 // Caller is not the same as launcher, so always needed.
1632 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1633 }
1634 }
1635
1636 if (sourceRecord == null) {
1637 // This activity is not being started from another... in this
1638 // case we -always- start a new task.
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001639 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
Craig Mautner29219d92013-04-16 20:19:12 -07001640 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1641 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001642 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1643 }
1644 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1645 // The original activity who is starting us is running as a single
1646 // instance... this new activity it is starting must go on its
1647 // own task.
1648 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautnera228ae92014-07-09 05:44:55 -07001649 } else if (launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001650 // The activity being started is a single instance... it always
1651 // gets launched into its own task.
1652 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1653 }
1654
Craig Mautner88629292013-11-10 20:39:05 -08001655 ActivityInfo newTaskInfo = null;
1656 Intent newTaskIntent = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001657 ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001658 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001659 if (sourceRecord.finishing) {
1660 // If the source is finishing, we can't further count it as our source. This
1661 // is because the task it is associated with may now be empty and on its way out,
1662 // so we don't want to blindly throw it in to that task. Instead we will take
Craig Mautner88629292013-11-10 20:39:05 -08001663 // the NEW_TASK flow and try to find a task for it. But save the task information
1664 // so it can be used when creating the new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001665 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001666 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1667 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1668 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautner88629292013-11-10 20:39:05 -08001669 newTaskInfo = sourceRecord.info;
1670 newTaskIntent = sourceRecord.task.intent;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001671 }
1672 sourceRecord = null;
1673 sourceStack = null;
1674 } else {
1675 sourceStack = sourceRecord.task.stack;
1676 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001677 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001678 sourceStack = null;
1679 }
1680
Craig Mautner8849a5e2013-04-02 16:41:03 -07001681 boolean addingToTask = false;
1682 boolean movedHome = false;
1683 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001684 ActivityStack targetStack;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001685
1686 intent.setFlags(launchFlags);
1687
1688 // If the caller is not coming from another activity, but has given us an
1689 // explicit task into which they would like us to launch the new activity,
1690 // then let's see about doing that.
1691 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1692 // 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.
1694 if (inTask.getRootActivity() == null) {
1695 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
1696 && !launchSingleInstance && !launchSingleTask) {
1697 throw new IllegalStateException("Caller has inTask " + inTask
1698 + " but target is not a new task");
1699 } else if (inTask.getBaseIntent() == null || !intent.getComponent().equals(
1700 inTask.getBaseIntent().getComponent())) {
1701 throw new IllegalStateException("Caller requested " + inTask + " is component "
1702 + inTask.getBaseIntent() + " but starting " + intent);
1703 }
1704 inTask.setIntent(r);
1705
1706 // If the task is not empty, then we are going to add the new activity on top
1707 // of the task, so it can not be launching as a new task.
1708 } else {
1709 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1710 || launchSingleInstance || launchSingleTask) {
1711 throw new IllegalStateException("Caller has inTask " + inTask
1712 + " but target is a new task");
1713 }
1714 }
1715 sourceStack = inTask.stack;
1716 reuseTask = inTask;
1717 } else {
1718 inTask = null;
1719 }
1720
1721 // We may want to try to place the new activity in to an existing task. We always
1722 // do this if the target activity is singleTask or singleInstance; we will also do
1723 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1724 // us to still place it in a new task: multi task, always doc mode, or being asked to
1725 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001726 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1727 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001728 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001729 // If bring to front is requested, and no result is requested, and
1730 // we can find a task that was started with this same
1731 // component, then instead of launching bring that one to the front.
1732 if (r.resultTo == null) {
1733 // See if there is a task to bring to the front. If this is
1734 // a SINGLE_INSTANCE activity, there can be one and only one
1735 // instance of it in the history, and it is always in its own
1736 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001737 ActivityRecord intentActivity = !launchSingleInstance ?
1738 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001739 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001740 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001741 showLockTaskToast();
Craig Mautneraea74a52014-03-08 14:23:10 -08001742 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
1743 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1744 }
Craig Mautner29219d92013-04-16 20:19:12 -07001745 if (r.task == null) {
1746 r.task = intentActivity.task;
1747 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001748 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001749 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001750 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1751 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001752 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001753 if (intentActivity.task.intent == null) {
1754 // This task was started because of movement of
1755 // the activity based on affinity... now that we
1756 // are actually launching it, we can assign the
1757 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001758 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001759 }
1760 // If the target task is not in the front, then we need
1761 // to bring it to the front... except... well, with
1762 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1763 // to have the same behavior as if a new instance was
1764 // being started, which means not bringing it to the front
1765 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001766 final ActivityStack lastStack = getLastStack();
1767 ActivityRecord curTop = lastStack == null?
1768 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001769 if (curTop != null && (curTop.task != intentActivity.task ||
1770 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001771 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001772 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1773 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001774 // We really do want to push this one into the
1775 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001776 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001777 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1778 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001779 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001780 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001781 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001782 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1783 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001784 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001785 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001786 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001787 options = null;
1788 }
1789 }
1790 // If the caller has requested that the target task be
1791 // reset, then do so.
1792 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1793 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1794 }
1795 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1796 // We don't need to start a new activity, and
1797 // the client said not to do anything if that
1798 // is the case, so this is it! And for paranoia, make
1799 // sure we have correctly resumed the top activity.
1800 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001801 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001802 } else {
1803 ActivityOptions.abort(options);
1804 }
1805 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1806 }
1807 if ((launchFlags &
1808 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1809 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1810 // The caller has requested to completely replace any
1811 // existing task with its new activity. Well that should
1812 // not be too hard...
1813 reuseTask = intentActivity.task;
1814 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001815 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001816 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001817 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001818 // In this situation we want to remove all activities
1819 // from the task up to the one being started. In most
1820 // cases this means we are resetting the task to its
1821 // initial state.
1822 ActivityRecord top =
1823 intentActivity.task.performClearTaskLocked(r, launchFlags);
1824 if (top != null) {
1825 if (top.frontOfTask) {
1826 // Activity aliases may mean we use different
1827 // intents for the top activity, so make sure
1828 // the task now has the identity of the new
1829 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001830 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001831 }
1832 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1833 r, top.task);
1834 top.deliverNewIntentLocked(callingUid, r.intent);
1835 } else {
1836 // A special case: we need to
1837 // start the activity because it is not currently
1838 // running, and the caller has asked to clear the
1839 // current task to have this activity at the top.
1840 addingToTask = true;
1841 // Now pretend like this activity is being started
1842 // by the top of its task, so it is put in the
1843 // right place.
1844 sourceRecord = intentActivity;
1845 }
1846 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1847 // In this case the top activity on the task is the
1848 // same as the one being launched, so we take that
1849 // as a request to bring the task to the foreground.
1850 // If the top activity in the task is the root
1851 // activity, deliver this new intent to it if it
1852 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001853 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001854 && intentActivity.realActivity.equals(r.realActivity)) {
1855 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1856 intentActivity.task);
1857 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001858 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001859 }
1860 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1861 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1862 // In this case we are launching the root activity
1863 // of the task, but with a different intent. We
1864 // should start a new instance on top.
1865 addingToTask = true;
1866 sourceRecord = intentActivity;
1867 }
1868 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1869 // In this case an activity is being launched in to an
1870 // existing task, without resetting that task. This
1871 // is typically the situation of launching an activity
1872 // from a notification or shortcut. We want to place
1873 // the new activity on top of the current task.
1874 addingToTask = true;
1875 sourceRecord = intentActivity;
1876 } else if (!intentActivity.task.rootWasReset) {
1877 // In this case we are launching in to an existing task
1878 // that has not yet been started from its front door.
1879 // The current task has been brought to the front.
1880 // Ideally, we'd probably like to place this new task
1881 // at the bottom of its stack, but that's a little hard
1882 // to do with the current organization of the code so
1883 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07001884 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001885 }
1886 if (!addingToTask && reuseTask == null) {
1887 // We didn't do anything... but it was needed (a.k.a., client
1888 // don't use that intent!) And for paranoia, make
1889 // sure we have correctly resumed the top activity.
1890 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001891 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001892 } else {
1893 ActivityOptions.abort(options);
1894 }
1895 return ActivityManager.START_TASK_TO_FRONT;
1896 }
1897 }
1898 }
1899 }
1900
1901 //String uri = r.intent.toURI();
1902 //Intent intent2 = new Intent(uri);
1903 //Slog.i(TAG, "Given intent: " + r.intent);
1904 //Slog.i(TAG, "URI is: " + uri);
1905 //Slog.i(TAG, "To intent: " + intent2);
1906
1907 if (r.packageName != null) {
1908 // If the activity being launched is the same as the one currently
1909 // at the top, then we need to check if it should only be launched
1910 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001911 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001912 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001913 if (top != null && r.resultTo == null) {
1914 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1915 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001916 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07001917 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001918 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1919 top.task);
1920 // For paranoia, make sure we have correctly
1921 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001922 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001923 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001924 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001925 }
1926 ActivityOptions.abort(options);
1927 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1928 // We don't need to start a new activity, and
1929 // the client said not to do anything if that
1930 // is the case, so this is it!
1931 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1932 }
1933 top.deliverNewIntentLocked(callingUid, r.intent);
1934 return ActivityManager.START_DELIVERED_TO_TOP;
1935 }
1936 }
1937 }
1938 }
1939
1940 } else {
1941 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001942 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1943 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001944 }
1945 ActivityOptions.abort(options);
1946 return ActivityManager.START_CLASS_NOT_FOUND;
1947 }
1948
1949 boolean newTask = false;
1950 boolean keepCurTransition = false;
1951
Craig Mautnerbb742462014-07-07 15:28:55 -07001952 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07001953 sourceRecord.task : null;
1954
Craig Mautner8849a5e2013-04-02 16:41:03 -07001955 // Should this be considered a new task?
1956 if (r.resultTo == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001957 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001958 if (isLockTaskModeViolation(reuseTask)) {
1959 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1960 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1961 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001962 if (inTask == null) {
1963 // If we have an incoming task, we are just going to use that.
1964 newTask = true;
1965 targetStack = adjustStackFocus(r, newTask);
1966 } else {
1967 targetStack = inTask.stack;
1968 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001969 if (!launchTaskBehind) {
1970 targetStack.moveToFront();
1971 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001972 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08001973 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
1974 newTaskInfo != null ? newTaskInfo : r.info,
1975 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07001976 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
1977 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001978 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1979 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001980 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07001981 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001982 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001983 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001984 if ((launchFlags &
1985 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1986 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1987 // Caller wants to appear on home activity, so before starting
1988 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07001989 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001990 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001991 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001992 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001993 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08001994 if (isLockTaskModeViolation(sourceTask)) {
1995 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1996 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1997 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001998 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07001999 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07002000 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautnera228ae92014-07-09 05:44:55 -07002001 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002002 // In this case, we are adding the activity to an existing
2003 // task, but the caller has asked to clear that task if the
2004 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002005 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002006 keepCurTransition = true;
2007 if (top != null) {
2008 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
2009 top.deliverNewIntentLocked(callingUid, r.intent);
2010 // For paranoia, make sure we have correctly
2011 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002012 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002013 if (doResume) {
2014 targetStack.resumeTopActivityLocked(null);
2015 }
2016 ActivityOptions.abort(options);
2017 return ActivityManager.START_DELIVERED_TO_TOP;
2018 }
2019 } else if (!addingToTask &&
2020 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2021 // In this case, we are launching an activity in our own task
2022 // that may already be running somewhere in the history, and
2023 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002024 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002025 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002026 final TaskRecord task = top.task;
2027 task.moveActivityToFrontLocked(top);
2028 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002029 top.updateOptionsLocked(options);
2030 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07002031 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002032 if (doResume) {
2033 targetStack.resumeTopActivityLocked(null);
2034 }
2035 return ActivityManager.START_DELIVERED_TO_TOP;
2036 }
2037 }
2038 // An existing activity is starting this new activity, so we want
2039 // to keep the new one in the same task as the one that is starting
2040 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002041 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002042 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002043 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002044
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002045 } else if (inTask != null) {
2046 // The calling is asking that the new activity be started in an explicit
2047 // task it has provided to us.
2048 if (isLockTaskModeViolation(inTask)) {
2049 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2050 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2051 }
2052 targetStack = inTask.stack;
2053 targetStack.moveToFront();
2054 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
2055 r.setTask(inTask, null);
2056 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2057 + " in explicit task " + r.task);
2058
Craig Mautner8849a5e2013-04-02 16:41:03 -07002059 } else {
2060 // This not being started from an existing activity, and not part
2061 // of a new task... just put it in the top task, though these days
2062 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002063 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07002064 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07002065 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002066 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002067 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002068 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002069 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2070 + " in new guessed " + r.task);
2071 }
2072
2073 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002074 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002075
Craig Mautner76e2a762014-06-24 09:05:43 -07002076 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2077 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2078 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002079 if (newTask) {
2080 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2081 }
2082 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002083 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002084 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002085 if (!launchTaskBehind) {
2086 // Don't set focus on an activity that's going to the back.
2087 mService.setFocusedActivityLocked(r);
2088 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002089 return ActivityManager.START_SUCCESS;
2090 }
2091
Craig Mautneree36c772014-07-16 14:56:05 -07002092 final void doPendingActivityLaunchesLocked(boolean doResume) {
2093 while (!mPendingActivityLaunches.isEmpty()) {
2094 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002095 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002096 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002097 }
2098 }
2099
Craig Mautner7f13ed32014-07-28 14:00:35 -07002100 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002101 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2102 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002103 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002104 mPendingActivityLaunches.remove(palNdx);
2105 }
2106 }
2107 }
2108
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002109 void acquireLaunchWakelock() {
2110 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2111 throw new IllegalStateException("Calling must be system uid");
2112 }
2113 mLaunchingActivity.acquire();
2114 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2115 // To be safe, don't allow the wake lock to be held for too long.
2116 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2117 }
2118 }
2119
Craig Mautnerf3333272013-04-22 10:55:53 -07002120 // Checked.
2121 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2122 Configuration config) {
2123 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2124
Craig Mautnerf3333272013-04-22 10:55:53 -07002125 ArrayList<ActivityRecord> stops = null;
2126 ArrayList<ActivityRecord> finishes = null;
2127 ArrayList<UserStartedState> startingUsers = null;
2128 int NS = 0;
2129 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002130 boolean booting = false;
2131 boolean enableScreen = false;
2132 boolean activityRemoved = false;
2133
2134 ActivityRecord r = ActivityRecord.forToken(token);
2135 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002136 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2137 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002138 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2139 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002140 if (fromTimeout) {
2141 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002142 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002143
2144 // This is a hack to semi-deal with a race condition
2145 // in the client where it can be constructed with a
2146 // newer configuration from when we asked it to launch.
2147 // We'll update with whatever configuration it now says
2148 // it used to launch.
2149 if (config != null) {
2150 r.configuration = config;
2151 }
2152
2153 // We are now idle. If someone is waiting for a thumbnail from
2154 // us, we can now deliver.
2155 r.idle = true;
2156
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002157 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
2158 if (!mService.mBooted && isFrontStack(r.task.stack)) {
2159 mService.mBooted = true;
2160 enableScreen = true;
2161 }
2162 }
2163
2164 if (allResumedActivitiesIdle()) {
2165 if (r != null) {
2166 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002167 }
2168
2169 if (mLaunchingActivity.isHeld()) {
2170 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2171 if (VALIDATE_WAKE_LOCK_CALLER &&
2172 Binder.getCallingUid() != Process.myUid()) {
2173 throw new IllegalStateException("Calling must be system uid");
2174 }
2175 mLaunchingActivity.release();
2176 }
2177 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002178 }
2179
2180 // Atomically retrieve all of the other things to do.
2181 stops = processStoppingActivitiesLocked(true);
2182 NS = stops != null ? stops.size() : 0;
2183 if ((NF=mFinishingActivities.size()) > 0) {
2184 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2185 mFinishingActivities.clear();
2186 }
2187
Prashant Malani84b08ad2014-07-15 16:28:24 -07002188 booting = mService.mBooting;
2189 mService.mBooting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002190
2191 if (mStartingUsers.size() > 0) {
2192 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2193 mStartingUsers.clear();
2194 }
2195
Craig Mautnerf3333272013-04-22 10:55:53 -07002196 // Stop any activities that are scheduled to do so but have been
2197 // waiting for the next one to start.
2198 for (int i = 0; i < NS; i++) {
2199 r = stops.get(i);
2200 final ActivityStack stack = r.task.stack;
2201 if (r.finishing) {
2202 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2203 } else {
2204 stack.stopActivityLocked(r);
2205 }
2206 }
2207
2208 // Finish any activities that are scheduled to do so but have been
2209 // waiting for the next one to start.
2210 for (int i = 0; i < NF; i++) {
2211 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002212 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002213 }
2214
2215 if (booting) {
2216 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002217 } else {
2218 // Complete user switch
2219 if (startingUsers != null) {
2220 for (int i = 0; i < startingUsers.size(); i++) {
2221 mService.finishUserSwitch(startingUsers.get(i));
2222 }
2223 }
2224 // Complete starting up of background users
2225 if (mStartingBackgroundUsers.size() > 0) {
2226 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2227 mStartingBackgroundUsers.clear();
2228 for (int i = 0; i < startingUsers.size(); i++) {
2229 mService.finishUserBoot(startingUsers.get(i));
2230 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002231 }
2232 }
2233
2234 mService.trimApplications();
2235 //dump();
2236 //mWindowManager.dump();
2237
2238 if (enableScreen) {
Jeff Brown4b4971b2014-07-25 19:30:05 -07002239 mService.postEnableScreenAfterBootLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002240 }
2241
2242 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002243 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002244 }
2245
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002246 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002247 }
2248
Craig Mautner8e569572013-10-11 17:36:59 -07002249 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002250 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002251 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2252 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002253 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2254 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2255 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002256 }
Craig Mautner19091252013-10-05 00:03:53 -07002257 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002258 }
2259
2260 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002261 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2262 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002263 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2264 stacks.get(stackNdx).closeSystemDialogsLocked();
2265 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002266 }
2267 }
2268
Craig Mautner93529a42013-10-04 15:03:13 -07002269 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002270 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002271 }
2272
Craig Mautner8d341ef2013-03-26 09:03:27 -07002273 /**
2274 * @return true if some activity was finished (or would have finished if doit were true).
2275 */
2276 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2277 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002278 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2279 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002280 final int numStacks = stacks.size();
2281 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2282 final ActivityStack stack = stacks.get(stackNdx);
2283 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2284 didSomething = true;
2285 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002286 }
2287 }
2288 return didSomething;
2289 }
2290
Dianne Hackborna413dc02013-07-12 12:02:55 -07002291 void updatePreviousProcessLocked(ActivityRecord r) {
2292 // Now that this process has stopped, we may want to consider
2293 // it to be the previous app to try to keep around in case
2294 // the user wants to return to it.
2295
2296 // First, found out what is currently the foreground app, so that
2297 // we don't blow away the previous app if this activity is being
2298 // hosted by the process that is actually still the foreground.
2299 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002300 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2301 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002302 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2303 final ActivityStack stack = stacks.get(stackNdx);
2304 if (isFrontStack(stack)) {
2305 if (stack.mResumedActivity != null) {
2306 fgApp = stack.mResumedActivity.app;
2307 } else if (stack.mPausingActivity != null) {
2308 fgApp = stack.mPausingActivity.app;
2309 }
2310 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002311 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002312 }
2313 }
2314
2315 // Now set this one as the previous process, only if that really
2316 // makes sense to.
2317 if (r.app != null && fgApp != null && r.app != fgApp
2318 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002319 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002320 mService.mPreviousProcess = r.app;
2321 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2322 }
2323 }
2324
Craig Mautner05d29032013-05-03 13:40:13 -07002325 boolean resumeTopActivitiesLocked() {
2326 return resumeTopActivitiesLocked(null, null, null);
2327 }
2328
2329 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2330 Bundle targetOptions) {
2331 if (targetStack == null) {
2332 targetStack = getFocusedStack();
2333 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002334 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002335 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002336 if (isFrontStack(targetStack)) {
2337 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2338 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002339 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2340 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002341 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2342 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002343 if (stack == targetStack) {
2344 // Already started above.
2345 continue;
2346 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002347 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002348 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002349 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002350 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002351 }
Craig Mautner05d29032013-05-03 13:40:13 -07002352 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002353 }
2354
2355 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002356 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2357 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002358 final int numStacks = stacks.size();
2359 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2360 final ActivityStack stack = stacks.get(stackNdx);
2361 stack.finishTopRunningActivityLocked(app);
2362 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002363 }
2364 }
2365
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002366 void finishVoiceTask(IVoiceInteractionSession session) {
2367 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2368 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2369 final int numStacks = stacks.size();
2370 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2371 final ActivityStack stack = stacks.get(stackNdx);
2372 stack.finishVoiceTask(session);
2373 }
2374 }
2375 }
2376
Craig Mautneraea74a52014-03-08 14:23:10 -08002377 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2378 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2379 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002380 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002381 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2382 // Caller wants the home activity moved with it. To accomplish this,
2383 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002384 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002385 }
2386 task.stack.moveTaskToFrontLocked(task, null, options);
2387 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2388 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002389 }
2390
Craig Mautner967212c2013-04-13 21:10:58 -07002391 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002392 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2393 if (activityContainer != null) {
2394 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002395 }
2396 return null;
2397 }
2398
Craig Mautner967212c2013-04-13 21:10:58 -07002399 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002400 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002401 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2402 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002403 }
2404 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002405 }
2406
Craig Mautner4a1cb222013-12-04 16:14:06 -08002407 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002408 ActivityRecord homeActivity = getHomeActivity();
2409 if (homeActivity != null) {
2410 return homeActivity.appToken;
2411 }
2412 return null;
2413 }
2414
2415 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002416 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2417 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2418 final TaskRecord task = tasks.get(taskNdx);
2419 if (task.isHomeTask()) {
2420 final ArrayList<ActivityRecord> activities = task.mActivities;
2421 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2422 final ActivityRecord r = activities.get(activityNdx);
2423 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002424 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002425 }
2426 }
2427 }
2428 }
2429 return null;
2430 }
2431
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002432 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002433 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002434 ActivityContainer activityContainer =
2435 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002436 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002437 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002438 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002439 return activityContainer;
2440 }
2441
Craig Mautner34b73df2014-01-12 21:11:08 -08002442 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002443 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2444 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2445 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002446 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2447 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002448 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002449 }
2450 }
2451
Craig Mautner95da1082014-02-24 17:54:35 -08002452 void deleteActivityContainer(IActivityContainer container) {
2453 ActivityContainer activityContainer = (ActivityContainer)container;
2454 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002455 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2456 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002457 final int stackId = activityContainer.mStackId;
2458 mActivityContainers.remove(stackId);
2459 mWindowManager.removeStack(stackId);
2460 }
2461 }
2462
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002463 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002464 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2465 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002466 return -1;
2467 }
2468
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002469 ActivityContainer activityContainer = new ActivityContainer(stackId);
2470 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002471 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002472 return stackId;
2473 }
2474
2475 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002476 while (true) {
2477 if (++mLastStackId <= HOME_STACK_ID) {
2478 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002479 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002480 if (getStack(mLastStackId) == null) {
2481 break;
2482 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002483 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002484 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002485 }
2486
Craig Mautneref73ee12014-04-23 11:45:37 -07002487 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2488 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2489 final ActivityStack stack = getStack(stackId);
2490 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2491 final TaskRecord task = tasks.get(taskNdx);
2492 stack.addTask(task, false, false);
2493 final int taskId = task.taskId;
2494 final ArrayList<ActivityRecord> activities = task.mActivities;
2495 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2496 final ActivityRecord r = activities.get(activityNdx);
2497 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2498 r.info.screenOrientation, r.fullscreen,
2499 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002500 r.userId, r.info.configChanges, task.voiceSession != null,
2501 r.mLaunchTaskBehind);
Craig Mautneref73ee12014-04-23 11:45:37 -07002502 }
2503 mWindowManager.addTask(taskId, stackId, false);
2504 }
Craig Mautner84984fa2014-06-19 11:19:20 -07002505 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002506 }
2507
Craig Mautner8d341ef2013-03-26 09:03:27 -07002508 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002509 final TaskRecord task = anyTaskForIdLocked(taskId);
2510 if (task == null) {
2511 return;
2512 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002513 final ActivityStack stack = getStack(stackId);
2514 if (stack == null) {
2515 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2516 return;
2517 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002518 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002519 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002520 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002521 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002522 }
2523
Craig Mautnerac6f8432013-07-17 13:24:59 -07002524 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002525 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002526 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2527 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002528 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2529 final ActivityStack stack = stacks.get(stackNdx);
2530 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002531 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2532 continue;
2533 }
2534 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2535 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2536 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002537 continue;
2538 }
2539 final ActivityRecord ar = stack.findTaskLocked(r);
2540 if (ar != null) {
2541 return ar;
2542 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002543 }
2544 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002545 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002546 return null;
2547 }
2548
2549 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002550 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2551 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002552 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2553 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2554 if (ar != null) {
2555 return ar;
2556 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002557 }
2558 }
2559 return null;
2560 }
2561
Craig Mautner8d341ef2013-03-26 09:03:27 -07002562 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002563 scheduleSleepTimeout();
2564 if (!mGoingToSleep.isHeld()) {
2565 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002566 if (mLaunchingActivity.isHeld()) {
2567 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2568 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002569 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002570 mLaunchingActivity.release();
2571 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002572 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002573 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002574 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002575 }
2576
2577 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002578 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002579
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002580 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002581 final long endTime = System.currentTimeMillis() + timeout;
2582 while (true) {
2583 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002584 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2585 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002586 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2587 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2588 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002589 }
2590 if (cantShutdown) {
2591 long timeRemaining = endTime - System.currentTimeMillis();
2592 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002593 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002594 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002595 } catch (InterruptedException e) {
2596 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002597 } else {
2598 Slog.w(TAG, "Activity manager shutdown timed out");
2599 timedout = true;
2600 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002601 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002602 } else {
2603 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002604 }
2605 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002606
2607 // Force checkReadyForSleep to complete.
2608 mSleepTimeout = true;
2609 checkReadyForSleepLocked();
2610
Craig Mautner8d341ef2013-03-26 09:03:27 -07002611 return timedout;
2612 }
2613
2614 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002615 removeSleepTimeouts();
2616 if (mGoingToSleep.isHeld()) {
2617 mGoingToSleep.release();
2618 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002619 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2620 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002621 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2622 final ActivityStack stack = stacks.get(stackNdx);
2623 stack.awakeFromSleepingLocked();
2624 if (isFrontStack(stack)) {
2625 resumeTopActivitiesLocked();
2626 }
Craig Mautner5314a402013-09-26 12:40:16 -07002627 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002628 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002629 mGoingToSleepActivities.clear();
2630 }
2631
2632 void activitySleptLocked(ActivityRecord r) {
2633 mGoingToSleepActivities.remove(r);
2634 checkReadyForSleepLocked();
2635 }
2636
2637 void checkReadyForSleepLocked() {
2638 if (!mService.isSleepingOrShuttingDown()) {
2639 // Do not care.
2640 return;
2641 }
2642
2643 if (!mSleepTimeout) {
2644 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002645 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2646 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002647 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2648 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2649 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002650 }
2651
2652 if (mStoppingActivities.size() > 0) {
2653 // Still need to tell some activities to stop; can't sleep yet.
2654 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2655 + mStoppingActivities.size() + " activities");
2656 scheduleIdleLocked();
2657 dontSleep = true;
2658 }
2659
2660 if (mGoingToSleepActivities.size() > 0) {
2661 // Still need to tell some activities to sleep; can't sleep yet.
2662 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2663 + mGoingToSleepActivities.size() + " activities");
2664 dontSleep = true;
2665 }
2666
2667 if (dontSleep) {
2668 return;
2669 }
2670 }
2671
Craig Mautnere0a38842013-12-16 16:14:02 -08002672 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2673 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002674 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2675 stacks.get(stackNdx).goToSleep();
2676 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002677 }
2678
2679 removeSleepTimeouts();
2680
2681 if (mGoingToSleep.isHeld()) {
2682 mGoingToSleep.release();
2683 }
2684 if (mService.mShuttingDown) {
2685 mService.notifyAll();
2686 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002687 }
2688
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002689 boolean reportResumedActivityLocked(ActivityRecord r) {
2690 final ActivityStack stack = r.task.stack;
2691 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002692 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002693 }
2694 if (allResumedActivitiesComplete()) {
2695 ensureActivitiesVisibleLocked(null, 0);
2696 mWindowManager.executeAppTransition();
2697 return true;
2698 }
2699 return false;
2700 }
2701
Craig Mautner8d341ef2013-03-26 09:03:27 -07002702 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2704 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002705 final int numStacks = stacks.size();
2706 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2707 final ActivityStack stack = stacks.get(stackNdx);
2708 stack.handleAppCrashLocked(app);
2709 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002710 }
2711 }
2712
Jose Lima4b6c6692014-08-12 17:41:12 -07002713 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002714 final ActivityStack stack = r.task.stack;
2715 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002716 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2717 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002718 return false;
2719 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002720 final boolean isVisible = stack.hasVisibleBehindActivity();
2721 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2722 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002723
2724 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002725 if (top == null || top == r || (visible == isVisible)) {
2726 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2727 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002728 return true;
2729 }
2730
2731 // A non-top activity is reporting a visibility change.
Jose Lima34ff4922014-08-18 15:19:41 -07002732 if ((visible && (top.fullscreen || top.state != ActivityState.RESUMED)) ||
2733 top.app == null || top.app.thread == null) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002734 // Can't carry out this request.
Jose Lima4b6c6692014-08-12 17:41:12 -07002735 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07002736 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07002737 " top.app.thread=" + top.app.thread);
2738 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002739 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2740 // Only the activity set as currently visible behind should actively reset its
2741 // visible behind state.
2742 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
2743 + visible + " stack.getVisibleBehindActivity()=" +
2744 stack.getVisibleBehindActivity() + " r=" + r);
2745 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002746 }
2747
Jose Lima4b6c6692014-08-12 17:41:12 -07002748 stack.setVisibleBehindActivity(visible ? r : null);
2749 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002750 // Make the activity immediately above r opaque.
2751 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2752 if (next != null) {
2753 mService.convertFromTranslucent(next.appToken);
2754 }
2755 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002756 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002757 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002758 } catch (RemoteException e) {
2759 }
2760 return true;
2761 }
2762
Craig Mautnerbb742462014-07-07 15:28:55 -07002763 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2764 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2765 r.mLaunchTaskBehind = false;
2766 final TaskRecord task = r.task;
2767 task.setLastThumbnail(task.stack.screenshotActivities(r));
2768 mService.addRecentTaskLocked(task);
2769 mWindowManager.setAppVisibility(r.appToken, false);
2770 }
2771
2772 void scheduleLaunchTaskBehindComplete(IBinder token) {
2773 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2774 }
2775
Craig Mautnerde4ef022013-04-07 19:01:33 -07002776 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002777 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002778 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2779 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002780 final int topStackNdx = stacks.size() - 1;
2781 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2782 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002783 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002784 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002785 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002786 }
2787
2788 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002789 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2790 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002791 final int numStacks = stacks.size();
2792 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2793 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002794 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002795 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002796 }
2797 }
2798
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002799 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2800 // Examine all activities currently running in the process.
2801 TaskRecord firstTask = null;
2802 // Tasks is non-null only if two or more tasks are found.
2803 ArraySet<TaskRecord> tasks = null;
2804 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
2805 for (int i=0; i<app.activities.size(); i++) {
2806 ActivityRecord r = app.activities.get(i);
2807 // First, if we find an activity that is in the process of being destroyed,
2808 // then we just aren't going to do anything for now; we want things to settle
2809 // down before we try to prune more activities.
2810 if (r.finishing || r.state == ActivityState.DESTROYING
2811 || r.state == ActivityState.DESTROYED) {
2812 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
2813 return;
2814 }
2815 // Don't consider any activies that are currently not in a state where they
2816 // can be destroyed.
2817 if (r.visible || !r.stopped || !r.haveState
2818 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
2819 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
2820 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
2821 continue;
2822 }
2823 if (r.task != null) {
2824 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
2825 + " from " + r);
2826 if (firstTask == null) {
2827 firstTask = r.task;
2828 } else if (firstTask != r.task) {
2829 if (tasks == null) {
2830 tasks = new ArraySet<>();
2831 tasks.add(firstTask);
2832 }
2833 tasks.add(r.task);
2834 }
2835 }
2836 }
2837 if (tasks == null) {
2838 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
2839 return;
2840 }
2841 // If we have activities in multiple tasks that are in a position to be destroyed,
2842 // let's iterate through the tasks and release the oldest one.
2843 final int numDisplays = mActivityDisplays.size();
2844 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2845 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2846 // Step through all stacks starting from behind, to hit the oldest things first.
2847 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2848 final ActivityStack stack = stacks.get(stackNdx);
2849 // Try to release activities in this stack; if we manage to, we are done.
2850 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2851 return;
2852 }
2853 }
2854 }
2855 }
2856
Craig Mautner8d341ef2013-03-26 09:03:27 -07002857 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002858 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2859 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002860 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002861
Craig Mautner858d8a62013-04-23 17:08:34 -07002862 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002863 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2864 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002865 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002866 final ActivityStack stack = stacks.get(stackNdx);
2867 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002868 TaskRecord task = stack.topTask();
2869 if (task != null) {
2870 mWindowManager.moveTaskToTop(task.taskId);
2871 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002872 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002873 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002874
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002875 ActivityStack stack = getStack(restoreStackId);
2876 if (stack == null) {
2877 stack = mHomeStack;
2878 }
2879 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002880 if (stack.isOnHomeDisplay()) {
2881 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002882 TaskRecord task = stack.topTask();
2883 if (task != null) {
2884 mWindowManager.moveTaskToTop(task.taskId);
2885 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002886 } else {
2887 // Stack was moved to another display while user was swapped out.
Craig Mautner84984fa2014-06-19 11:19:20 -07002888 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002889 }
Craig Mautner93529a42013-10-04 15:03:13 -07002890 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002891 }
2892
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002893 /**
2894 * Add background users to send boot completed events to.
2895 * @param userId The user being started in the background
2896 * @param uss The state object for the user.
2897 */
2898 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2899 mStartingBackgroundUsers.add(uss);
2900 }
2901
Craig Mautnerde4ef022013-04-07 19:01:33 -07002902 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2903 int N = mStoppingActivities.size();
2904 if (N <= 0) return null;
2905
2906 ArrayList<ActivityRecord> stops = null;
2907
2908 final boolean nowVisible = allResumedActivitiesVisible();
2909 for (int i=0; i<N; i++) {
2910 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002911 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002912 + nowVisible + " waitingVisible=" + s.waitingVisible
2913 + " finishing=" + s.finishing);
2914 if (s.waitingVisible && nowVisible) {
2915 mWaitingVisibleActivities.remove(s);
2916 s.waitingVisible = false;
2917 if (s.finishing) {
2918 // If this activity is finishing, it is sitting on top of
2919 // everyone else but we now know it is no longer needed...
2920 // so get rid of it. Otherwise, we need to go through the
2921 // normal flow and hide it once we determine that it is
2922 // hidden by the activities in front of it.
2923 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002924 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002925 }
2926 }
2927 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2928 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2929 if (stops == null) {
2930 stops = new ArrayList<ActivityRecord>();
2931 }
2932 stops.add(s);
2933 mStoppingActivities.remove(i);
2934 N--;
2935 i--;
2936 }
2937 }
2938
2939 return stops;
2940 }
2941
Craig Mautnercf910b02013-04-23 11:23:27 -07002942 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002943 // FIXME
2944/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2945 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07002946 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002947 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002948 if (isFrontStack(stack)) {
2949 if (r == null) {
2950 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2951 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002952 final ActivityRecord pausing = stack.mPausingActivity;
2953 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002954 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002955 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002956 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002957 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002958 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002959 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002960 }
2961 }
2962 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002963 final ActivityRecord resumed = stack.mResumedActivity;
2964 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002965 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002966 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002967 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002968 if (r != null && (state == ActivityState.INITIALIZING
2969 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002970 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002971 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002972 }
2973 }
2974 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002975*/
Craig Mautner76ea2242013-05-15 11:40:05 -07002976 }
2977
Craig Mautner27084302013-03-25 08:05:25 -07002978 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002979 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002980 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002981 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2982 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2983 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002984 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07002985 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002986
Craig Mautner20e72272013-04-01 13:45:53 -07002987 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002988 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002989 }
2990
Dianne Hackborn390517b2013-05-30 15:03:32 -07002991 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2992 boolean needSep, String prefix) {
2993 if (activity != null) {
2994 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2995 if (needSep) {
2996 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002997 }
2998 pw.print(prefix);
2999 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003000 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003001 }
3002 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003003 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003004 }
3005
Craig Mautner8d341ef2013-03-26 09:03:27 -07003006 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3007 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003008 boolean printed = false;
3009 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003010 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3011 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003012 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
3013 pw.println(" (activities from bottom to top):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003014 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003015 final int numStacks = stacks.size();
3016 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3017 final ActivityStack stack = stacks.get(stackNdx);
3018 StringBuilder stackHeader = new StringBuilder(128);
3019 stackHeader.append(" Stack #");
3020 stackHeader.append(stack.mStackId);
3021 stackHeader.append(":");
3022 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3023 needSep, stackHeader.toString());
3024 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3025 !dumpAll, false, dumpPackage, true,
3026 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003027
Craig Mautner4a1cb222013-12-04 16:14:06 -08003028 needSep = printed;
3029 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3030 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003031 if (pr) {
3032 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003033 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003034 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003035 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3036 " mResumedActivity: ");
3037 if (pr) {
3038 printed = true;
3039 needSep = false;
3040 }
3041 if (dumpAll) {
3042 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3043 " mLastPausedActivity: ");
3044 if (pr) {
3045 printed = true;
3046 needSep = true;
3047 }
3048 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3049 needSep, " mLastNoHistoryActivity: ");
3050 }
3051 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003052 }
3053 }
3054
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003055 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3056 false, dumpPackage, true, " Activities waiting to finish:", null);
3057 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3058 false, dumpPackage, true, " Activities waiting to stop:", null);
3059 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3060 false, dumpPackage, true, " Activities waiting for another to become visible:",
3061 null);
3062 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3063 false, dumpPackage, true, " Activities waiting to sleep:", null);
3064 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3065 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003066
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003067 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003068 }
3069
Dianne Hackborn390517b2013-05-30 15:03:32 -07003070 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003071 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003072 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003073 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003074 String innerPrefix = null;
3075 String[] args = null;
3076 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003077 for (int i=list.size()-1; i>=0; i--) {
3078 final ActivityRecord r = list.get(i);
3079 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3080 continue;
3081 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003082 if (innerPrefix == null) {
3083 innerPrefix = prefix + " ";
3084 args = new String[0];
3085 }
3086 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003087 final boolean full = !brief && (complete || !r.isInHistory());
3088 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003089 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003090 needNL = false;
3091 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003092 if (header1 != null) {
3093 pw.println(header1);
3094 header1 = null;
3095 }
3096 if (header2 != null) {
3097 pw.println(header2);
3098 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003099 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003100 if (lastTask != r.task) {
3101 lastTask = r.task;
3102 pw.print(prefix);
3103 pw.print(full ? "* " : " ");
3104 pw.println(lastTask);
3105 if (full) {
3106 lastTask.dump(pw, prefix + " ");
3107 } else if (complete) {
3108 // Complete + brief == give a summary. Isn't that obvious?!?
3109 if (lastTask.intent != null) {
3110 pw.print(prefix); pw.print(" ");
3111 pw.println(lastTask.intent.toInsecureStringWithClip());
3112 }
3113 }
3114 }
3115 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3116 pw.print(" #"); pw.print(i); pw.print(": ");
3117 pw.println(r);
3118 if (full) {
3119 r.dump(pw, innerPrefix);
3120 } else if (complete) {
3121 // Complete + brief == give a summary. Isn't that obvious?!?
3122 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3123 if (r.app != null) {
3124 pw.print(innerPrefix); pw.println(r.app);
3125 }
3126 }
3127 if (client && r.app != null && r.app.thread != null) {
3128 // flush anything that is already in the PrintWriter since the thread is going
3129 // to write to the file descriptor directly
3130 pw.flush();
3131 try {
3132 TransferPipe tp = new TransferPipe();
3133 try {
3134 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3135 r.appToken, innerPrefix, args);
3136 // Short timeout, since blocking here can
3137 // deadlock with the application.
3138 tp.go(fd, 2000);
3139 } finally {
3140 tp.kill();
3141 }
3142 } catch (IOException e) {
3143 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3144 } catch (RemoteException e) {
3145 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3146 }
3147 needNL = true;
3148 }
3149 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003150 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003151 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003152
Craig Mautnerf3333272013-04-22 10:55:53 -07003153 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003154 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003155 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3156 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003157 }
3158
3159 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003160 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003161 }
3162
3163 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003164 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003165 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3166 }
3167
Craig Mautner05d29032013-05-03 13:40:13 -07003168 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003169 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3170 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3171 }
Craig Mautner05d29032013-05-03 13:40:13 -07003172 }
3173
Craig Mautner0eea92c2013-05-16 13:35:39 -07003174 void removeSleepTimeouts() {
3175 mSleepTimeout = false;
3176 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3177 }
3178
3179 final void scheduleSleepTimeout() {
3180 removeSleepTimeouts();
3181 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3182 }
3183
Craig Mautner4a1cb222013-12-04 16:14:06 -08003184 @Override
3185 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003186 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003187 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3188 }
3189
3190 @Override
3191 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003192 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003193 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3194 }
3195
3196 @Override
3197 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003198 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003199 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3200 }
3201
3202 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003203 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003204 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003205 newDisplay = mActivityDisplays.get(displayId) == null;
3206 if (newDisplay) {
3207 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
3208 mActivityDisplays.put(displayId, activityDisplay);
3209 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003210 }
Craig Mautner4504de52013-12-20 09:06:56 -08003211 if (newDisplay) {
3212 mWindowManager.onDisplayAdded(displayId);
3213 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003214 }
3215
3216 public void handleDisplayRemovedLocked(int displayId) {
3217 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003218 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3219 if (activityDisplay != null) {
3220 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003221 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003222 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003223 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003224 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003225 }
3226 }
3227 mWindowManager.onDisplayRemoved(displayId);
3228 }
3229
3230 public void handleDisplayChangedLocked(int displayId) {
3231 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003232 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3233 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003234 // TODO: Update the bounds.
3235 }
3236 }
3237 mWindowManager.onDisplayChanged(displayId);
3238 }
3239
3240 StackInfo getStackInfo(ActivityStack stack) {
3241 StackInfo info = new StackInfo();
3242 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3243 info.displayId = Display.DEFAULT_DISPLAY;
3244 info.stackId = stack.mStackId;
3245
3246 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3247 final int numTasks = tasks.size();
3248 int[] taskIds = new int[numTasks];
3249 String[] taskNames = new String[numTasks];
3250 for (int i = 0; i < numTasks; ++i) {
3251 final TaskRecord task = tasks.get(i);
3252 taskIds[i] = task.taskId;
3253 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3254 : task.realActivity != null ? task.realActivity.flattenToString()
3255 : task.getTopActivity() != null ? task.getTopActivity().packageName
3256 : "unknown";
3257 }
3258 info.taskIds = taskIds;
3259 info.taskNames = taskNames;
3260 return info;
3261 }
3262
3263 StackInfo getStackInfoLocked(int stackId) {
3264 ActivityStack stack = getStack(stackId);
3265 if (stack != null) {
3266 return getStackInfo(stack);
3267 }
3268 return null;
3269 }
3270
3271 ArrayList<StackInfo> getAllStackInfosLocked() {
3272 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003273 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3274 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003275 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3276 list.add(getStackInfo(stacks.get(ndx)));
3277 }
3278 }
3279 return list;
3280 }
3281
Jason Monka8f569c2014-07-07 12:15:21 -04003282 void showLockTaskToast() {
3283 mLockTaskNotify.showToast(mLockTaskIsLocked);
3284 }
3285
3286 void setLockTaskModeLocked(TaskRecord task, boolean isLocked) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003287 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003288 // Take out of lock task mode if necessary
3289 if (mLockTaskModeTask != null) {
3290 final Message lockTaskMsg = Message.obtain();
3291 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3292 lockTaskMsg.what = LOCK_TASK_END_MSG;
3293 mLockTaskModeTask = null;
3294 mHandler.sendMessage(lockTaskMsg);
3295 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003296 return;
3297 }
3298 if (isLockTaskModeViolation(task)) {
3299 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3300 return;
3301 }
3302 mLockTaskModeTask = task;
3303 findTaskToMoveToFrontLocked(task, 0, null);
3304 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003305
3306 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003307 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3308 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003309 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003310 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003311 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003312 }
3313
3314 boolean isLockTaskModeViolation(TaskRecord task) {
3315 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3316 }
3317
3318 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3319 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003320 final Message lockTaskMsg = Message.obtain();
3321 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3322 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003323 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003324 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003325 }
3326 }
3327
3328 boolean isInLockTaskMode() {
3329 return mLockTaskModeTask != null;
3330 }
3331
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003332 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003333
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003334 public ActivityStackSupervisorHandler(Looper looper) {
3335 super(looper);
3336 }
3337
Craig Mautnerf3333272013-04-22 10:55:53 -07003338 void activityIdleInternal(ActivityRecord r) {
3339 synchronized (mService) {
3340 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3341 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003342 }
3343
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003344 @Override
3345 public void handleMessage(Message msg) {
3346 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003347 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003348 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003349 if (mService.mDidDexOpt) {
3350 mService.mDidDexOpt = false;
3351 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3352 nmsg.obj = msg.obj;
3353 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3354 return;
3355 }
3356 // We don't at this point know if the activity is fullscreen,
3357 // so we need to be conservative and assume it isn't.
3358 activityIdleInternal((ActivityRecord)msg.obj);
3359 } break;
3360 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003361 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003362 activityIdleInternal((ActivityRecord)msg.obj);
3363 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003364 case RESUME_TOP_ACTIVITY_MSG: {
3365 synchronized (mService) {
3366 resumeTopActivitiesLocked();
3367 }
3368 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003369 case SLEEP_TIMEOUT_MSG: {
3370 synchronized (mService) {
3371 if (mService.isSleepingOrShuttingDown()) {
3372 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3373 mSleepTimeout = true;
3374 checkReadyForSleepLocked();
3375 }
3376 }
3377 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003378 case LAUNCH_TIMEOUT_MSG: {
3379 if (mService.mDidDexOpt) {
3380 mService.mDidDexOpt = false;
3381 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3382 return;
3383 }
3384 synchronized (mService) {
3385 if (mLaunchingActivity.isHeld()) {
3386 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3387 if (VALIDATE_WAKE_LOCK_CALLER
3388 && Binder.getCallingUid() != Process.myUid()) {
3389 throw new IllegalStateException("Calling must be system uid");
3390 }
3391 mLaunchingActivity.release();
3392 }
3393 }
3394 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003395 case HANDLE_DISPLAY_ADDED: {
3396 handleDisplayAddedLocked(msg.arg1);
3397 } break;
3398 case HANDLE_DISPLAY_CHANGED: {
3399 handleDisplayChangedLocked(msg.arg1);
3400 } break;
3401 case HANDLE_DISPLAY_REMOVED: {
3402 handleDisplayRemovedLocked(msg.arg1);
3403 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003404 case CONTAINER_CALLBACK_VISIBILITY: {
3405 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003406 final IActivityContainerCallback callback = container.mCallback;
3407 if (callback != null) {
3408 try {
3409 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3410 } catch (RemoteException e) {
3411 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003412 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003413 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003414 case LOCK_TASK_START_MSG: {
3415 // When lock task starts, we disable the status bars.
3416 try {
Jason Monk62515be2014-05-21 16:06:19 -04003417 if (mLockTaskNotify == null) {
3418 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003419 }
Jason Monk62515be2014-05-21 16:06:19 -04003420 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003421 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003422 if (getStatusBarService() != null) {
3423 int flags =
3424 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003425 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003426 flags ^= StatusBarManager.DISABLE_HOME
3427 | StatusBarManager.DISABLE_RECENT;
3428 }
3429 getStatusBarService().disable(flags, mToken,
3430 mService.mContext.getPackageName());
3431 }
3432 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003433 if (getDevicePolicyManager() != null) {
3434 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003435 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003436 }
justinzhang5286d3f2014-05-12 17:06:01 -04003437 } catch (RemoteException ex) {
3438 throw new RuntimeException(ex);
3439 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003440 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003441 case LOCK_TASK_END_MSG: {
3442 // When lock task ends, we enable the status bars.
3443 try {
Jason Monk62515be2014-05-21 16:06:19 -04003444 if (getStatusBarService() != null) {
3445 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3446 mService.mContext.getPackageName());
3447 }
3448 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003449 if (getDevicePolicyManager() != null) {
3450 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3451 msg.arg1);
3452 }
Jason Monk62515be2014-05-21 16:06:19 -04003453 if (mLockTaskNotify == null) {
3454 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3455 }
3456 mLockTaskNotify.show(false);
3457 try {
3458 boolean shouldLockKeyguard = Settings.System.getInt(
3459 mService.mContext.getContentResolver(),
3460 Settings.System.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003461 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003462 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003463 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003464 new LockPatternUtils(mService.mContext)
3465 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003466 }
3467 } catch (SettingNotFoundException e) {
3468 // No setting, don't lock.
3469 }
justinzhang5286d3f2014-05-12 17:06:01 -04003470 } catch (RemoteException ex) {
3471 throw new RuntimeException(ex);
3472 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003473 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003474 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3475 final ActivityContainer container = (ActivityContainer) msg.obj;
3476 final IActivityContainerCallback callback = container.mCallback;
3477 if (callback != null) {
3478 try {
3479 callback.onAllActivitiesComplete(container.asBinder());
3480 } catch (RemoteException e) {
3481 }
3482 }
3483 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003484 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3485 synchronized (mService) {
3486 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3487 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003488 final ActivityContainer container = (ActivityContainer) msg.obj;
3489 container.mStack.finishAllActivitiesLocked(true);
3490 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003491 }
3492 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003493 case LAUNCH_TASK_BEHIND_COMPLETE: {
3494 synchronized (mService) {
3495 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3496 if (r != null) {
3497 handleLaunchTaskBehindCompleteLocked(r);
3498 }
3499 }
3500 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003501 }
3502 }
3503 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003504
Ying Wangb081a592014-04-22 15:20:16 -07003505 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003506 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003507 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003508 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003509 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003510 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003511 ActivityRecord mParentActivity = null;
3512 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003513
Craig Mautnere3a00d72014-04-16 08:31:19 -07003514 boolean mVisible = true;
3515
Craig Mautner4a1cb222013-12-04 16:14:06 -08003516 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003517 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003518
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003519 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3520 final static int CONTAINER_STATE_NO_SURFACE = 1;
3521 final static int CONTAINER_STATE_FINISHING = 2;
3522 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3523
3524 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003525 synchronized (mService) {
3526 mStackId = stackId;
3527 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003528 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003529 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003531 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003532
Craig Mautnere0a38842013-12-16 16:14:02 -08003533 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003534 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3535 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003536 mActivityDisplay = activityDisplay;
3537 mStack.mDisplayId = activityDisplay.mDisplayId;
3538 mStack.mStacks = activityDisplay.mStacks;
3539
3540 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003541 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003542 }
3543
3544 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003545 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003546 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003547 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3548 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003549 return;
3550 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003551 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003552 }
3553 }
3554
3555 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003556 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003557 synchronized (mService) {
3558 if (mActivityDisplay != null) {
3559 return mActivityDisplay.mDisplayId;
3560 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003561 }
3562 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003563 }
3564
Jeff Brownca9bc702014-02-11 14:32:56 -08003565 @Override
3566 public boolean injectEvent(InputEvent event) {
3567 final long origId = Binder.clearCallingIdentity();
3568 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003569 synchronized (mService) {
3570 if (mActivityDisplay != null) {
3571 return mInputManagerInternal.injectInputEvent(event,
3572 mActivityDisplay.mDisplayId,
3573 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3574 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003575 }
3576 return false;
3577 } finally {
3578 Binder.restoreCallingIdentity(origId);
3579 }
3580 }
3581
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003582 @Override
3583 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003584 synchronized (mService) {
3585 if (mContainerState == CONTAINER_STATE_FINISHING) {
3586 return;
3587 }
3588 mContainerState = CONTAINER_STATE_FINISHING;
3589
3590 final Message msg =
3591 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003592 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003593
3594 long origId = Binder.clearCallingIdentity();
3595 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003596 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003597 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003598 } finally {
3599 Binder.restoreCallingIdentity(origId);
3600 }
3601 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003602 }
3603
Craig Mautner34b73df2014-01-12 21:11:08 -08003604 private void detachLocked() {
3605 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3606 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003607 if (mActivityDisplay != null) {
3608 mActivityDisplay.detachActivitiesLocked(mStack);
3609 mActivityDisplay = null;
3610 mStack.mDisplayId = -1;
3611 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003612 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003613 }
3614 }
3615
3616 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003617 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003618 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003619 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003620 Binder.getCallingUid(), mCurrentUser, false,
3621 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003622 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003623 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003624 String mimeType = intent.getType();
3625 if (mimeType == null && intent.getData() != null
3626 && "content".equals(intent.getData().getScheme())) {
3627 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3628 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003629 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0, 0, null,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003630 null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003631 }
3632
3633 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003634 public final int startActivityIntentSender(IIntentSender intentSender) {
3635 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3636
3637 if (!(intentSender instanceof PendingIntentRecord)) {
3638 throw new IllegalArgumentException("Bad PendingIntent object");
3639 }
3640
3641 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003642 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003643 }
3644
Craig Mautner247ab652014-04-25 10:09:00 -07003645 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3646 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003647 Binder.getCallingUid(), mCurrentUser, false,
3648 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003649 if (resolvedType == null) {
3650 resolvedType = intent.getType();
3651 if (resolvedType == null && intent.getData() != null
3652 && "content".equals(intent.getData().getScheme())) {
3653 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3654 }
3655 }
3656 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003657 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003658 throw new SecurityException(
3659 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3660 }
3661 }
3662
3663 /** Throw a SecurityException if allowEmbedded is not true */
3664 @Override
3665 public final void checkEmbeddedAllowed(Intent intent) {
3666 checkEmbeddedAllowedInner(intent, null);
3667 }
3668
3669 /** Throw a SecurityException if allowEmbedded is not true */
3670 @Override
3671 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3672 if (!(intentSender instanceof PendingIntentRecord)) {
3673 throw new IllegalArgumentException("Bad PendingIntent object");
3674 }
3675 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3676 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3677 pendingIntent.key.requestResolvedType);
3678 }
3679
Craig Mautnerdf88d732014-01-27 09:21:32 -08003680 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003681 public IBinder asBinder() {
3682 return this;
3683 }
3684
Craig Mautner4504de52013-12-20 09:06:56 -08003685 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003686 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003687 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003688 }
3689
Craig Mautner4a1cb222013-12-04 16:14:06 -08003690 ActivityStackSupervisor getOuter() {
3691 return ActivityStackSupervisor.this;
3692 }
3693
Craig Mautnerd163e752014-06-13 17:18:47 -07003694 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003695 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003696 }
3697
3698 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003699 synchronized (mService) {
3700 if (mActivityDisplay != null) {
3701 mActivityDisplay.getBounds(outBounds);
3702 } else {
3703 outBounds.set(0, 0);
3704 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003705 }
3706 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003707
Craig Mautner6985bad2014-04-21 15:22:06 -07003708 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003709 void setVisible(boolean visible) {
3710 if (mVisible != visible) {
3711 mVisible = visible;
3712 if (mCallback != null) {
3713 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3714 0 /* unused */, this).sendToTarget();
3715 }
3716 }
3717 }
3718
Craig Mautner6985bad2014-04-21 15:22:06 -07003719 void setDrawn() {
3720 }
3721
Craig Mautner1b4bf852014-05-26 15:06:32 -07003722 // You can always start a new task on a regular ActivityStack.
3723 boolean isEligibleForNewTasks() {
3724 return true;
3725 }
3726
Craig Mautnerd163e752014-06-13 17:18:47 -07003727 void onTaskListEmptyLocked() {
3728 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3729 if (!mStack.isHomeStack()) {
3730 detachLocked();
3731 deleteActivityContainer(this);
3732 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003733 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3734 }
3735
Craig Mautner34b73df2014-01-12 21:11:08 -08003736 @Override
3737 public String toString() {
3738 return mIdString + (mActivityDisplay == null ? "N" : "A");
3739 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003740 }
3741
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003742 private class VirtualActivityContainer extends ActivityContainer {
3743 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003744 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003745
3746 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3747 super(getNextStackId());
3748 mParentActivity = parent;
3749 mCallback = callback;
3750 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003751 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003752 }
3753
3754 @Override
3755 public void setSurface(Surface surface, int width, int height, int density) {
3756 super.setSurface(surface, width, height, density);
3757
3758 synchronized (mService) {
3759 final long origId = Binder.clearCallingIdentity();
3760 try {
3761 setSurfaceLocked(surface, width, height, density);
3762 } finally {
3763 Binder.restoreCallingIdentity(origId);
3764 }
3765 }
3766 }
3767
3768 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3769 if (mContainerState == CONTAINER_STATE_FINISHING) {
3770 return;
3771 }
3772 VirtualActivityDisplay virtualActivityDisplay =
3773 (VirtualActivityDisplay) mActivityDisplay;
3774 if (virtualActivityDisplay == null) {
3775 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003776 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003777 mActivityDisplay = virtualActivityDisplay;
3778 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3779 attachToDisplayLocked(virtualActivityDisplay);
3780 }
3781
3782 if (mSurface != null) {
3783 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003784 }
3785
Craig Mautner6985bad2014-04-21 15:22:06 -07003786 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003787 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003788 mStack.resumeTopActivityLocked(null);
3789 } else {
3790 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003791 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003792 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
3793 mStack.startPausingLocked(false, true);
3794 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003795 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003796
Craig Mautnerd163e752014-06-13 17:18:47 -07003797 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003798
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003799 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3800 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003801 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003802
Craig Mautner6985bad2014-04-21 15:22:06 -07003803 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003804 boolean isAttachedLocked() {
3805 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003806 }
3807
3808 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003809 void setDrawn() {
3810 synchronized (mService) {
3811 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003812 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003813 }
3814 }
3815
Craig Mautner1b4bf852014-05-26 15:06:32 -07003816 // Never start a new task on an ActivityView if it isn't explicitly specified.
3817 @Override
3818 boolean isEligibleForNewTasks() {
3819 return false;
3820 }
3821
Craig Mautnerd163e752014-06-13 17:18:47 -07003822 private void setSurfaceIfReadyLocked() {
3823 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003824 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3825 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3826 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3827 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3828 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003829 }
3830 }
3831
Craig Mautner4a1cb222013-12-04 16:14:06 -08003832 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3833 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003834 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003835 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003836 int mDisplayId;
3837 Display mDisplay;
3838 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003839
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3841 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003842 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003843
Jose Lima4b6c6692014-08-12 17:41:12 -07003844 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003845
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003846 ActivityDisplay() {
3847 }
Craig Mautner4504de52013-12-20 09:06:56 -08003848
Craig Mautnere0a38842013-12-16 16:14:02 -08003849 ActivityDisplay(int displayId) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003850 init(mDisplayManager.getDisplay(displayId));
Craig Mautner4504de52013-12-20 09:06:56 -08003851 }
3852
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003853 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003854 mDisplay = display;
3855 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003856 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003857 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003858
3859 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003860 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3861 + mDisplayId);
3862 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003863 }
3864
Craig Mautnere0a38842013-12-16 16:14:02 -08003865 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003866 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003867 + " from displayId=" + mDisplayId);
3868 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003869 }
3870
3871 void getBounds(Point bounds) {
3872 mDisplay.getDisplayInfo(mDisplayInfo);
3873 bounds.x = mDisplayInfo.appWidth;
3874 bounds.y = mDisplayInfo.appHeight;
3875 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003876
Jose Lima4b6c6692014-08-12 17:41:12 -07003877 void setVisibleBehindActivity(ActivityRecord r) {
3878 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003879 }
3880
Jose Lima4b6c6692014-08-12 17:41:12 -07003881 boolean hasVisibleBehindActivity() {
3882 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003883 }
3884
Craig Mautner34b73df2014-01-12 21:11:08 -08003885 @Override
3886 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003887 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3888 }
3889 }
3890
3891 class VirtualActivityDisplay extends ActivityDisplay {
3892 VirtualDisplay mVirtualDisplay;
3893
Craig Mautner6985bad2014-04-21 15:22:06 -07003894 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003895 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003896 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3897 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3898 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3899 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003900
3901 init(mVirtualDisplay.getDisplay());
3902
3903 mWindowManager.handleDisplayAdded(mDisplayId);
3904 }
3905
3906 void setSurface(Surface surface) {
3907 if (mVirtualDisplay != null) {
3908 mVirtualDisplay.setSurface(surface);
3909 }
3910 }
3911
3912 @Override
3913 void detachActivitiesLocked(ActivityStack stack) {
3914 super.detachActivitiesLocked(stack);
3915 if (mVirtualDisplay != null) {
3916 mVirtualDisplay.release();
3917 mVirtualDisplay = null;
3918 }
3919 }
3920
3921 @Override
3922 public String toString() {
3923 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003924 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003925 }
Jose Lima58e66d62014-05-27 20:00:27 -07003926
3927 private boolean isLeanbackOnlyDevice() {
3928 boolean onLeanbackOnly = false;
3929 try {
3930 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
3931 PackageManager.FEATURE_LEANBACK_ONLY);
3932 } catch (RemoteException e) {
3933 // noop
3934 }
3935
3936 return onLeanbackOnly;
3937 }
Craig Mautner27084302013-03-25 08:05:25 -07003938}