blob: 4a99ef334208960e2bb05f39eeb75f4e26102c24 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner29219d92013-04-16 20:19:12 -070020import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070022import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Craig Mautner2420ead2013-04-01 17:13:20 -070023import static com.android.server.am.ActivityManagerService.localLOGV;
Craig Mautner23ac33b2013-04-01 16:26:35 -070024import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070025import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Craig Mautner6170f732013-04-02 13:05:23 -070027import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070029import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070030import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
31import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070032import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070033import static com.android.server.am.ActivityManagerService.TAG;
Craig Mautner84984fa2014-06-19 11:19:20 -070034import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
35import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070037
Craig Mautner2420ead2013-04-01 17:13:20 -070038import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080040import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityOptions;
42import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import android.app.IActivityContainer;
44import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.app.PendingIntent;
Craig Mautner20e72272013-04-01 13:45:53 -070048import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070050import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040051import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040052import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070054import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070056import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.pm.ApplicationInfo;
60import android.content.pm.PackageManager;
61import android.content.pm.ResolveInfo;
62import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080063import android.graphics.Point;
64import android.hardware.display.DisplayManager;
65import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080066import android.hardware.display.DisplayManagerGlobal;
67import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080068import android.hardware.input.InputManager;
69import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070070import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070071import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070072import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070073import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070074import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070075import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070076import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070078import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070079import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070080import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040081import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070082import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070083import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040084import android.provider.Settings;
85import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070086import android.service.voice.IVoiceInteractionSession;
Craig Mautner2420ead2013-04-01 17:13:20 -070087import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070088import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080089import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070090
Craig Mautner4a1cb222013-12-04 16:14:06 -080091import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080092import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080093import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080094import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080095import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070096import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070097import com.android.internal.app.IVoiceInteractor;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070098import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040099import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400100import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800101import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700102import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700103import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104
justinzhang5286d3f2014-05-12 17:06:01 -0400105
Craig Mautner8d341ef2013-03-26 09:03:27 -0700106import java.io.FileDescriptor;
107import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700108import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700109import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700110import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700111
Craig Mautner4a1cb222013-12-04 16:14:06 -0800112public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700113 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
114 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
115 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700116 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700117 static final boolean DEBUG_IDLE = DEBUG || false;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700118 static final boolean DEBUG_MEDIA_VISIBILITY = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700119 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
120 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
121 static final boolean DEBUG_STATES = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700122
Craig Mautner2219a1b2013-03-25 09:44:30 -0700123 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700124
Craig Mautnerf3333272013-04-22 10:55:53 -0700125 /** How long we wait until giving up on the last activity telling us it is idle. */
126 static final int IDLE_TIMEOUT = 10*1000;
127
Craig Mautner0eea92c2013-05-16 13:35:39 -0700128 /** How long we can hold the sleep wake lock before giving up. */
129 static final int SLEEP_TIMEOUT = 5*1000;
130
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700131 // How long we can hold the launch wake lock before giving up.
132 static final int LAUNCH_TIMEOUT = 10*1000;
133
Craig Mautner05d29032013-05-03 13:40:13 -0700134 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
135 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
136 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700137 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700138 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800139 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
140 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
141 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700142 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400143 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
144 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700145 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700146 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700147 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800148
Craig Mautner4504de52013-12-20 09:06:56 -0800149 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700150
Jason Monk62515be2014-05-21 16:06:19 -0400151 private static final String LOCK_TASK_TAG = "Lock-to-App";
152
justinzhang5286d3f2014-05-12 17:06:01 -0400153 /** Status Bar Service **/
154 private IBinder mToken = new Binder();
155 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400156 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400157
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700158 // For debugging to make sure the caller when acquiring/releasing our
159 // wake lock is the system process.
160 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700161
Craig Mautner27084302013-03-25 08:05:25 -0700162 final ActivityManagerService mService;
163
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700164 final ActivityStackSupervisorHandler mHandler;
165
166 /** Short cut */
167 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800168 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700169
Craig Mautner27084302013-03-25 08:05:25 -0700170 /** Dismiss the keyguard after the next activity is displayed? */
Craig Mautner5314a402013-09-26 12:40:16 -0700171 boolean mDismissKeyguardOnNextActivity = false;
Craig Mautner27084302013-03-25 08:05:25 -0700172
Craig Mautner8d341ef2013-03-26 09:03:27 -0700173 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700174 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700175
176 /** Task identifier that activities are currently being started in. Incremented each time a
177 * new task is created. */
178 private int mCurTaskId = 0;
179
Craig Mautner2420ead2013-04-01 17:13:20 -0700180 /** The current user */
181 private int mCurrentUser;
182
Craig Mautnere0a38842013-12-16 16:14:02 -0800183 /** The stack containing the launcher app. Assumed to always be attached to
184 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700185 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700186
Craig Mautnere0a38842013-12-16 16:14:02 -0800187 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700188 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700189
Craig Mautner4a1cb222013-12-04 16:14:06 -0800190 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
191 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800192 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800193 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700194
195 /** List of activities that are waiting for a new activity to become visible before completing
196 * whatever operation they are supposed to do. */
197 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
198
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700199 /** List of processes waiting to find out about the next visible activity. */
200 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
201 new ArrayList<IActivityManager.WaitResult>();
202
203 /** List of processes waiting to find out about the next launched activity. */
204 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
205 new ArrayList<IActivityManager.WaitResult>();
206
Craig Mautnerde4ef022013-04-07 19:01:33 -0700207 /** List of activities that are ready to be stopped, but waiting for the next activity to
208 * settle down before doing so. */
209 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
210
Craig Mautnerf3333272013-04-22 10:55:53 -0700211 /** List of activities that are ready to be finished, but waiting for the previous activity to
212 * settle down before doing so. It contains ActivityRecord objects. */
213 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
214
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215 /** List of activities that are in the process of going to sleep. */
216 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
217
Craig Mautnerf3333272013-04-22 10:55:53 -0700218 /** Used on user changes */
219 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
220
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700221 /** Used to queue up any background users being started */
222 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
223
Craig Mautnerde4ef022013-04-07 19:01:33 -0700224 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
225 * is being brought in front of us. */
226 boolean mUserLeaving = false;
227
Craig Mautner0eea92c2013-05-16 13:35:39 -0700228 /** Set when we have taken too long waiting to go to sleep. */
229 boolean mSleepTimeout = false;
230
Jose Lima58e66d62014-05-27 20:00:27 -0700231 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
232 * setWindowManager is called. **/
233 private boolean mLeanbackOnlyDevice;
234
Craig Mautner0eea92c2013-05-16 13:35:39 -0700235 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700236 * We don't want to allow the device to go to sleep while in the process
237 * of launching an activity. This is primarily to allow alarm intent
238 * receivers to launch an activity and get that to run before the device
239 * goes back to sleep.
240 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700241 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700242
243 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700244 * Set when the system is going to sleep, until we have
245 * successfully paused the current activity and released our wake lock.
246 * At that point the system is allowed to actually sleep.
247 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700248 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700249
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700250 /** Stack id of the front stack when user switched, indexed by userId. */
251 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700252
Craig Mautner4504de52013-12-20 09:06:56 -0800253 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800254 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700255 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800256
257 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700258 private final SparseArray<ActivityDisplay> mActivityDisplays =
259 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800260
Jeff Brownca9bc702014-02-11 14:32:56 -0800261 InputManagerInternal mInputManagerInternal;
262
Craig Mautneraea74a52014-03-08 14:23:10 -0800263 /** If non-null then the task specified remains in front and no other tasks may be started
264 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400265 TaskRecord mLockTaskModeTask;
Jason Monka8f569c2014-07-07 12:15:21 -0400266 /** Whether lock task has been entered by an authorized app and cannot
267 * be exited. */
268 private boolean mLockTaskIsLocked;
Jason Monk62515be2014-05-21 16:06:19 -0400269 /**
270 * Notifies the user when entering/exiting lock-task.
271 */
272 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800273
Craig Mautneree36c772014-07-16 14:56:05 -0700274 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
275 = new ArrayList<PendingActivityLaunch>();
276
277 /**
278 * Description of a request to start a new activity, which has been held
279 * due to app switches being disabled.
280 */
281 static class PendingActivityLaunch {
282 final ActivityRecord r;
283 final ActivityRecord sourceRecord;
284 final int startFlags;
285 final ActivityStack stack;
286
287 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
288 int _startFlags, ActivityStack _stack) {
289 r = _r;
290 sourceRecord = _sourceRecord;
291 startFlags = _startFlags;
292 stack = _stack;
293 }
294 }
295
Craig Mautner4a1cb222013-12-04 16:14:06 -0800296 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700297 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700298 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
299 }
300
301 /**
302 * At the time when the constructor runs, the power manager has not yet been
303 * initialized. So we initialize our wakelocks afterwards.
304 */
305 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800306 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700307 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700308 mLaunchingActivity =
309 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
310 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700311 }
312
justinzhang5286d3f2014-05-12 17:06:01 -0400313 // This function returns a IStatusBarService. The value is from ServiceManager.
314 // getService and is cached.
315 private IStatusBarService getStatusBarService() {
316 synchronized (mService) {
317 if (mStatusBarService == null) {
318 mStatusBarService = IStatusBarService.Stub.asInterface(
319 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
320 if (mStatusBarService == null) {
321 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
322 }
323 }
324 return mStatusBarService;
325 }
326 }
327
Jason Monk35c62a42014-06-17 10:24:47 -0400328 private IDevicePolicyManager getDevicePolicyManager() {
329 synchronized (mService) {
330 if (mDevicePolicyManager == null) {
331 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
332 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
333 if (mDevicePolicyManager == null) {
334 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
335 }
336 }
337 return mDevicePolicyManager;
338 }
339 }
340
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700341 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800342 synchronized (mService) {
343 mWindowManager = wm;
344
345 mDisplayManager =
346 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
347 mDisplayManager.registerDisplayListener(this, null);
348
349 Display[] displays = mDisplayManager.getDisplays();
350 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
351 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800352 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
353 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800354 }
355
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700356 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800357 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800358
359 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700360
361 // Initialize this here, now that we can get a valid reference to PackageManager.
362 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800363 }
Craig Mautner27084302013-03-25 08:05:25 -0700364 }
365
366 void dismissKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700367 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner27084302013-03-25 08:05:25 -0700368 if (mDismissKeyguardOnNextActivity) {
369 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700370 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700371 }
372 }
373
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700374 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700376 }
377
Craig Mautnerde4ef022013-04-07 19:01:33 -0700378 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700380 }
381
Craig Mautner4a1cb222013-12-04 16:14:06 -0800382 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
383 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700384 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800385 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
386 if (parent != null) {
387 stack = parent.task.stack;
388 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800389 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800390 if (stacks != null && !stacks.isEmpty()) {
391 return stack == stacks.get(stacks.size() - 1);
392 }
393 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700394 }
395
Craig Mautnerde4ef022013-04-07 19:01:33 -0700396 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800397 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800398 int topNdx = stacks.size() - 1;
399 if (topNdx <= 0) {
400 return;
401 }
402 ActivityStack topStack = stacks.get(topNdx);
403 final boolean homeInFront = topStack == mHomeStack;
404 if (homeInFront != toFront) {
405 mLastFocusedStack = topStack;
406 stacks.remove(mHomeStack);
407 stacks.add(toFront ? topNdx : 0, mHomeStack);
408 mFocusedStack = stacks.get(topNdx);
409 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
410 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700411 }
412 }
413
Craig Mautner84984fa2014-06-19 11:19:20 -0700414 void moveHomeStackTaskToTop(int homeStackTaskType) {
415 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
416 mWindowManager.showRecentApps();
417 return;
418 }
Craig Mautner69ada552013-04-18 13:51:51 -0700419 moveHomeStack(true);
Craig Mautner84984fa2014-06-19 11:19:20 -0700420 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700421 }
422
Craig Mautner84984fa2014-06-19 11:19:20 -0700423 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev) {
424 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
425 mWindowManager.showRecentApps();
426 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700427 }
Craig Mautner84984fa2014-06-19 11:19:20 -0700428 moveHomeStackTaskToTop(homeStackTaskType);
429 if (prev != null) {
430 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
431 }
432
Craig Mautnera8a90e02013-06-28 15:24:50 -0700433 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700434 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000435 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700436 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700437 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700438 }
439 return mService.startHomeActivityLocked(mCurrentUser);
440 }
441
Craig Mautner27084302013-03-25 08:05:25 -0700442 void setDismissKeyguard(boolean dismiss) {
Craig Mautner5314a402013-09-26 12:40:16 -0700443 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen(" dismiss=" + dismiss);
Craig Mautner27084302013-03-25 08:05:25 -0700444 mDismissKeyguardOnNextActivity = dismiss;
445 }
446
Craig Mautner8d341ef2013-03-26 09:03:27 -0700447 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800448 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800449 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800450 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800451 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
452 ActivityStack stack = stacks.get(stackNdx);
453 TaskRecord task = stack.taskForIdLocked(id);
454 if (task != null) {
455 return task;
456 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700457 }
458 }
459 return null;
460 }
461
Craig Mautner6170f732013-04-02 13:05:23 -0700462 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800463 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800464 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800465 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800466 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
467 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
468 if (r != null) {
469 return r;
470 }
Craig Mautner6170f732013-04-02 13:05:23 -0700471 }
472 }
473 return null;
474 }
475
Craig Mautneref73ee12014-04-23 11:45:37 -0700476 void setNextTaskId(int taskId) {
477 if (taskId > mCurTaskId) {
478 mCurTaskId = taskId;
479 }
480 }
481
Craig Mautner8d341ef2013-03-26 09:03:27 -0700482 int getNextTaskId() {
483 do {
484 mCurTaskId++;
485 if (mCurTaskId <= 0) {
486 mCurTaskId = 1;
487 }
488 } while (anyTaskForIdLocked(mCurTaskId) != null);
489 return mCurTaskId;
490 }
491
Craig Mautnerde4ef022013-04-07 19:01:33 -0700492 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700493 ActivityStack stack = getFocusedStack();
494 if (stack == null) {
495 return null;
496 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700497 ActivityRecord resumedActivity = stack.mResumedActivity;
498 if (resumedActivity == null || resumedActivity.app == null) {
499 resumedActivity = stack.mPausingActivity;
500 if (resumedActivity == null || resumedActivity.app == null) {
501 resumedActivity = stack.topRunningActivityLocked(null);
502 }
503 }
504 return resumedActivity;
505 }
506
Mike Lockwooded8902d2013-11-15 11:01:47 -0800507 boolean attachApplicationLocked(ProcessRecord app) throws Exception {
Craig Mautner20e72272013-04-01 13:45:53 -0700508 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800509 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800510 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
511 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800512 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
513 final ActivityStack stack = stacks.get(stackNdx);
514 if (!isFrontStack(stack)) {
515 continue;
516 }
517 ActivityRecord hr = stack.topRunningActivityLocked(null);
518 if (hr != null) {
519 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
520 && processName.equals(hr.processName)) {
521 try {
George Mount2c92c972014-03-20 09:38:23 -0700522 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800523 didSomething = true;
524 }
525 } catch (Exception e) {
526 Slog.w(TAG, "Exception in new application when starting activity "
527 + hr.intent.getComponent().flattenToShortString(), e);
528 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700529 }
Craig Mautner20e72272013-04-01 13:45:53 -0700530 }
Craig Mautner20e72272013-04-01 13:45:53 -0700531 }
532 }
533 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700534 if (!didSomething) {
535 ensureActivitiesVisibleLocked(null, 0);
536 }
Craig Mautner20e72272013-04-01 13:45:53 -0700537 return didSomething;
538 }
539
540 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800541 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
542 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800543 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
544 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800545 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800546 continue;
547 }
548 final ActivityRecord resumedActivity = stack.mResumedActivity;
549 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800550 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
551 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800552 return false;
553 }
Craig Mautner20e72272013-04-01 13:45:53 -0700554 }
555 }
556 return true;
557 }
558
Craig Mautnerde4ef022013-04-07 19:01:33 -0700559 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800560 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
561 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800562 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
563 final ActivityStack stack = stacks.get(stackNdx);
564 if (isFrontStack(stack)) {
565 final ActivityRecord r = stack.mResumedActivity;
566 if (r != null && r.state != ActivityState.RESUMED) {
567 return false;
568 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700569 }
570 }
571 }
572 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800573 if (DEBUG_STACK) Slog.d(TAG,
574 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
575 mLastFocusedStack + " to=" + mFocusedStack);
576 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700577 return true;
578 }
579
580 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800581 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
582 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800583 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
584 final ActivityStack stack = stacks.get(stackNdx);
585 final ActivityRecord r = stack.mResumedActivity;
586 if (r != null && (!r.nowVisible || r.waitingVisible)) {
587 return false;
588 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700589 }
590 }
591 return true;
592 }
593
Craig Mautner2acc3892013-09-23 10:28:14 -0700594 /**
595 * Pause all activities in either all of the stacks or just the back stacks.
596 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700597 * @return true if any activity was paused as a result of this call.
598 */
Craig Mautner5314a402013-09-26 12:40:16 -0700599 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700600 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800601 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
602 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800603 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
604 final ActivityStack stack = stacks.get(stackNdx);
605 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
606 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
607 " mResumedActivity=" + stack.mResumedActivity);
608 stack.startPausingLocked(userLeaving, false);
609 someActivityPaused = true;
610 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700611 }
612 }
613 return someActivityPaused;
614 }
615
Craig Mautnerde4ef022013-04-07 19:01:33 -0700616 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700617 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800618 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
619 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800620 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
621 final ActivityStack stack = stacks.get(stackNdx);
622 final ActivityRecord r = stack.mPausingActivity;
623 if (r != null && r.state != ActivityState.PAUSED
624 && r.state != ActivityState.STOPPED
625 && r.state != ActivityState.STOPPING) {
626 if (DEBUG_STATES) {
627 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
628 pausing = false;
629 } else {
630 return false;
631 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700632 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700633 }
634 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700635 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700636 }
637
Craig Mautnerdf88d732014-01-27 09:21:32 -0800638 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping) {
John Spurlock8a985d22014-02-25 09:40:05 -0500639 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800640 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
641 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
642 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
643 final ActivityStack stack = stacks.get(stackNdx);
644 if (stack.mResumedActivity != null &&
645 stack.mActivityContainer.mParentActivity == parent) {
646 stack.startPausingLocked(userLeaving, uiSleeping);
647 }
648 }
649 }
650 }
651
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700652 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700653 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700654 WaitResult w = mWaitingActivityVisible.get(i);
655 w.timeout = false;
656 if (r != null) {
657 w.who = new ComponentName(r.info.packageName, r.info.name);
658 }
659 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
660 w.thisTime = w.totalTime;
661 }
662 mService.notifyAll();
663 dismissKeyguard();
664 }
665
666 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
667 long thisTime, long totalTime) {
668 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700669 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700670 w.timeout = timeout;
671 if (r != null) {
672 w.who = new ComponentName(r.info.packageName, r.info.name);
673 }
674 w.thisTime = thisTime;
675 w.totalTime = totalTime;
676 }
677 mService.notifyAll();
678 }
679
Craig Mautner29219d92013-04-16 20:19:12 -0700680 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700681 final ActivityStack focusedStack = getFocusedStack();
682 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
683 if (r != null) {
684 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700685 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700686
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800688 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800689 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
690 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700691 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700692 r = stack.topRunningActivityLocked(null);
693 if (r != null) {
694 return r;
695 }
696 }
697 }
698 return null;
699 }
700
Dianne Hackborn09233282014-04-30 11:33:59 -0700701 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700702 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
704 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800705 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800707 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800708 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
709 final ActivityStack stack = stacks.get(stackNdx);
710 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
711 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700712 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700713 }
714 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700715
716 // The lists are already sorted from most recent to oldest. Just pull the most recent off
717 // each list and add it to list. Stop when all lists are empty or maxNum reached.
718 while (maxNum > 0) {
719 long mostRecentActiveTime = Long.MIN_VALUE;
720 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800721 final int numTaskLists = runningTaskLists.size();
722 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
723 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700724 if (!stackTaskList.isEmpty()) {
725 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
726 if (lastActiveTime > mostRecentActiveTime) {
727 mostRecentActiveTime = lastActiveTime;
728 selectedStackList = stackTaskList;
729 }
730 }
731 }
732 if (selectedStackList != null) {
733 list.add(selectedStackList.remove(0));
734 --maxNum;
735 } else {
736 break;
737 }
738 }
Craig Mautner20e72272013-04-01 13:45:53 -0700739 }
740
Craig Mautner23ac33b2013-04-01 16:26:35 -0700741 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
742 String profileFile, ParcelFileDescriptor profileFd, int userId) {
743 // Collect information about the target of the Intent.
744 ActivityInfo aInfo;
745 try {
746 ResolveInfo rInfo =
747 AppGlobals.getPackageManager().resolveIntent(
748 intent, resolvedType,
749 PackageManager.MATCH_DEFAULT_ONLY
750 | ActivityManagerService.STOCK_PM_FLAGS, userId);
751 aInfo = rInfo != null ? rInfo.activityInfo : null;
752 } catch (RemoteException e) {
753 aInfo = null;
754 }
755
756 if (aInfo != null) {
757 // Store the found target back into the intent, because now that
758 // we have it we never want to do this again. For example, if the
759 // user navigates back to this point in the history, we should
760 // always restart the exact same activity.
761 intent.setComponent(new ComponentName(
762 aInfo.applicationInfo.packageName, aInfo.name));
763
764 // Don't debug things in the system process
765 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
766 if (!aInfo.processName.equals("system")) {
767 mService.setDebugApp(aInfo.processName, true, false);
768 }
769 }
770
771 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
772 if (!aInfo.processName.equals("system")) {
773 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
774 }
775 }
776
777 if (profileFile != null) {
778 if (!aInfo.processName.equals("system")) {
779 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
780 profileFile, profileFd,
781 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
782 }
783 }
784 }
785 return aInfo;
786 }
787
Craig Mautner2219a1b2013-03-25 09:44:30 -0700788 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700789 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700790 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null, 0,
Craig Mautnere0a38842013-12-16 16:14:02 -0800791 null, false, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700792 }
793
Craig Mautner23ac33b2013-04-01 16:26:35 -0700794 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700795 String callingPackage, Intent intent, String resolvedType,
796 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
797 IBinder resultTo, String resultWho, int requestCode, int startFlags, String profileFile,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700798 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
Craig Mautnere0a38842013-12-16 16:14:02 -0800799 Bundle options, int userId, IActivityContainer iContainer) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700800 // Refuse possible leaked file descriptors
801 if (intent != null && intent.hasFileDescriptors()) {
802 throw new IllegalArgumentException("File descriptors passed in Intent");
803 }
804 boolean componentSpecified = intent.getComponent() != null;
805
806 // Don't modify the client's object!
807 intent = new Intent(intent);
808
809 // Collect information about the target of the Intent.
810 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
811 profileFile, profileFd, userId);
812
Craig Mautnere0a38842013-12-16 16:14:02 -0800813 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700814 synchronized (mService) {
815 int callingPid;
816 if (callingUid >= 0) {
817 callingPid = -1;
818 } else if (caller == null) {
819 callingPid = Binder.getCallingPid();
820 callingUid = Binder.getCallingUid();
821 } else {
822 callingPid = callingUid = -1;
823 }
824
Craig Mautnere0a38842013-12-16 16:14:02 -0800825 final ActivityStack stack;
826 if (container == null || container.mStack.isOnHomeDisplay()) {
827 stack = getFocusedStack();
828 } else {
829 stack = container.mStack;
830 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700831 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700832 && mService.mConfiguration.diff(config) != 0;
833 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700834 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700835
836 final long origId = Binder.clearCallingIdentity();
837
838 if (aInfo != null &&
839 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
840 // This may be a heavy-weight process! Check to see if we already
841 // have another, different heavy-weight process running.
842 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
843 if (mService.mHeavyWeightProcess != null &&
844 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
845 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700846 int realCallingUid = callingUid;
847 if (caller != null) {
848 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
849 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700850 realCallingUid = callerApp.info.uid;
851 } else {
852 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700853 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700854 + intent.toString());
855 ActivityOptions.abort(options);
856 return ActivityManager.START_PERMISSION_DENIED;
857 }
858 }
859
860 IIntentSender target = mService.getIntentSenderLocked(
861 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
862 realCallingUid, userId, null, null, 0, new Intent[] { intent },
863 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
864 | PendingIntent.FLAG_ONE_SHOT, null);
865
866 Intent newIntent = new Intent();
867 if (requestCode >= 0) {
868 // Caller is requesting a result.
869 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
870 }
871 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
872 new IntentSender(target));
873 if (mService.mHeavyWeightProcess.activities.size() > 0) {
874 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
875 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
876 hist.packageName);
877 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
878 hist.task.taskId);
879 }
880 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
881 aInfo.packageName);
882 newIntent.setFlags(intent.getFlags());
883 newIntent.setClassName("android",
884 HeavyWeightSwitcherActivity.class.getName());
885 intent = newIntent;
886 resolvedType = null;
887 caller = null;
888 callingUid = Binder.getCallingUid();
889 callingPid = Binder.getCallingPid();
890 componentSpecified = true;
891 try {
892 ResolveInfo rInfo =
893 AppGlobals.getPackageManager().resolveIntent(
894 intent, null,
895 PackageManager.MATCH_DEFAULT_ONLY
896 | ActivityManagerService.STOCK_PM_FLAGS, userId);
897 aInfo = rInfo != null ? rInfo.activityInfo : null;
898 aInfo = mService.getActivityInfoForUser(aInfo, userId);
899 } catch (RemoteException e) {
900 aInfo = null;
901 }
902 }
903 }
904 }
905
Dianne Hackborn91097de2014-04-04 18:02:06 -0700906 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
907 voiceSession, voiceInteractor, resultTo, resultWho,
Craig Mautnere0a38842013-12-16 16:14:02 -0800908 requestCode, callingPid, callingUid, callingPackage, startFlags, options,
909 componentSpecified, null, container);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700910
Craig Mautner85c11a82014-07-17 12:38:18 -0700911 Binder.restoreCallingIdentity(origId);
912
Craig Mautnerde4ef022013-04-07 19:01:33 -0700913 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700914 // If the caller also wants to switch to a new configuration,
915 // do so now. This allows a clean switch, as we are waiting
916 // for the current activity to pause (so we will not destroy
917 // it), and have not yet started the next activity.
918 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
919 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700920 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700921 if (DEBUG_CONFIGURATION) Slog.v(TAG,
922 "Updating to new configuration after starting activity.");
923 mService.updateConfigurationLocked(config, null, false, false);
924 }
925
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926 if (outResult != null) {
927 outResult.result = res;
928 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700929 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700930 do {
931 try {
932 mService.wait();
933 } catch (InterruptedException e) {
934 }
935 } while (!outResult.timeout && outResult.who == null);
936 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700937 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700938 if (r.nowVisible) {
939 outResult.timeout = false;
940 outResult.who = new ComponentName(r.info.packageName, r.info.name);
941 outResult.totalTime = 0;
942 outResult.thisTime = 0;
943 } else {
944 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700945 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700946 do {
947 try {
948 mService.wait();
949 } catch (InterruptedException e) {
950 }
951 } while (!outResult.timeout && outResult.who == null);
952 }
953 }
954 }
955
956 return res;
957 }
958 }
959
960 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
961 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
962 Bundle options, int userId) {
963 if (intents == null) {
964 throw new NullPointerException("intents is null");
965 }
966 if (resolvedTypes == null) {
967 throw new NullPointerException("resolvedTypes is null");
968 }
969 if (intents.length != resolvedTypes.length) {
970 throw new IllegalArgumentException("intents are length different than resolvedTypes");
971 }
972
Craig Mautner23ac33b2013-04-01 16:26:35 -0700973
974 int callingPid;
975 if (callingUid >= 0) {
976 callingPid = -1;
977 } else if (caller == null) {
978 callingPid = Binder.getCallingPid();
979 callingUid = Binder.getCallingUid();
980 } else {
981 callingPid = callingUid = -1;
982 }
983 final long origId = Binder.clearCallingIdentity();
984 try {
985 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700986 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700987 for (int i=0; i<intents.length; i++) {
988 Intent intent = intents[i];
989 if (intent == null) {
990 continue;
991 }
992
993 // Refuse possible leaked file descriptors
994 if (intent != null && intent.hasFileDescriptors()) {
995 throw new IllegalArgumentException("File descriptors passed in Intent");
996 }
997
998 boolean componentSpecified = intent.getComponent() != null;
999
1000 // Don't modify the client's object!
1001 intent = new Intent(intent);
1002
1003 // Collect information about the target of the Intent.
1004 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
1005 0, null, null, userId);
1006 // TODO: New, check if this is correct
1007 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1008
1009 if (aInfo != null &&
1010 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
1011 != 0) {
1012 throw new IllegalArgumentException(
1013 "FLAG_CANT_SAVE_STATE not supported here");
1014 }
1015
1016 Bundle theseOptions;
1017 if (options != null && i == intents.length-1) {
1018 theseOptions = options;
1019 } else {
1020 theseOptions = null;
1021 }
Craig Mautner6170f732013-04-02 13:05:23 -07001022 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn91097de2014-04-04 18:02:06 -07001023 aInfo, null, null, resultTo, null, -1, callingPid, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001024 0, theseOptions, componentSpecified, outActivity, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001025 if (res < 0) {
1026 return res;
1027 }
1028
1029 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1030 }
1031 }
1032 } finally {
1033 Binder.restoreCallingIdentity(origId);
1034 }
1035
1036 return ActivityManager.START_SUCCESS;
1037 }
1038
Craig Mautner2420ead2013-04-01 17:13:20 -07001039 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001040 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001041 throws RemoteException {
1042
1043 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001044 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001045 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001046
1047 // schedule launch ticks to collect information about slow apps.
1048 r.startLaunchTickingLocked();
1049
1050 // Have the window manager re-evaluate the orientation of
1051 // the screen based on the new activity order. Note that
1052 // as a result of this, it can call back into the activity
1053 // manager with a new orientation. We don't care about that,
1054 // because the activity is not currently running so we are
1055 // just restarting it anyway.
1056 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001057 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001058 mService.mConfiguration,
1059 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1060 mService.updateConfigurationLocked(config, r, false, false);
1061 }
1062
1063 r.app = app;
1064 app.waitingToKill = null;
1065 r.launchCount++;
1066 r.lastLaunchTime = SystemClock.uptimeMillis();
1067
1068 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1069
1070 int idx = app.activities.indexOf(r);
1071 if (idx < 0) {
1072 app.activities.add(r);
1073 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001074 mService.updateLruProcessLocked(app, true, null);
1075 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001076
1077 final ActivityStack stack = r.task.stack;
1078 try {
1079 if (app.thread == null) {
1080 throw new RemoteException();
1081 }
1082 List<ResultInfo> results = null;
1083 List<Intent> newIntents = null;
1084 if (andResume) {
1085 results = r.results;
1086 newIntents = r.newIntents;
1087 }
1088 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1089 + " icicle=" + r.icicle
1090 + " with results=" + results + " newIntents=" + newIntents
1091 + " andResume=" + andResume);
1092 if (andResume) {
1093 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1094 r.userId, System.identityHashCode(r),
1095 r.task.taskId, r.shortComponentName);
1096 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001097 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001098 // Home process is the root process of the task.
1099 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001100 }
1101 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1102 r.sleeping = false;
1103 r.forceNewConfig = false;
1104 mService.showAskCompatModeDialogLocked(r);
1105 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1106 String profileFile = null;
1107 ParcelFileDescriptor profileFd = null;
1108 boolean profileAutoStop = false;
1109 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1110 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1111 mService.mProfileProc = app;
1112 profileFile = mService.mProfileFile;
1113 profileFd = mService.mProfileFd;
1114 profileAutoStop = mService.mAutoStopProfiler;
1115 }
1116 }
1117 app.hasShownUi = true;
1118 app.pendingUiClean = true;
1119 if (profileFd != null) {
1120 try {
1121 profileFd = profileFd.dup();
1122 } catch (IOException e) {
1123 if (profileFd != null) {
1124 try {
1125 profileFd.close();
1126 } catch (IOException o) {
1127 }
1128 profileFd = null;
1129 }
1130 }
1131 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001132
Dianne Hackborna413dc02013-07-12 12:02:55 -07001133 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001134 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1135 System.identityHashCode(r), r.info,
Craig Mautnera0026042014-04-23 11:45:37 -07001136 new Configuration(mService.mConfiguration), r.compat, r.task.voiceInteractor,
1137 app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume,
Craig Mautner233ceee2014-05-09 17:05:11 -07001138 mService.isNextTransitionForward(), profileFile, profileFd, profileAutoStop
1139 );
Craig Mautner2420ead2013-04-01 17:13:20 -07001140
1141 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1142 // This may be a heavy-weight process! Note that the package
1143 // manager will ensure that only activity can run in the main
1144 // process of the .apk, which is the only thing that will be
1145 // considered heavy-weight.
1146 if (app.processName.equals(app.info.packageName)) {
1147 if (mService.mHeavyWeightProcess != null
1148 && mService.mHeavyWeightProcess != app) {
1149 Slog.w(TAG, "Starting new heavy weight process " + app
1150 + " when already running "
1151 + mService.mHeavyWeightProcess);
1152 }
1153 mService.mHeavyWeightProcess = app;
1154 Message msg = mService.mHandler.obtainMessage(
1155 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1156 msg.obj = r;
1157 mService.mHandler.sendMessage(msg);
1158 }
1159 }
1160
1161 } catch (RemoteException e) {
1162 if (r.launchFailed) {
1163 // This is the second time we failed -- finish activity
1164 // and give up.
1165 Slog.e(TAG, "Second failure launching "
1166 + r.intent.getComponent().flattenToShortString()
1167 + ", giving up", e);
1168 mService.appDiedLocked(app, app.pid, app.thread);
1169 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1170 "2nd-crash", false);
1171 return false;
1172 }
1173
1174 // This is the first time we failed -- restart process and
1175 // retry.
1176 app.activities.remove(r);
1177 throw e;
1178 }
1179
1180 r.launchFailed = false;
1181 if (stack.updateLRUListLocked(r)) {
1182 Slog.w(TAG, "Activity " + r
1183 + " being launched, but already in LRU list");
1184 }
1185
1186 if (andResume) {
1187 // As part of the process of launching, ActivityThread also performs
1188 // a resume.
1189 stack.minimalResumeActivityLocked(r);
1190 } else {
1191 // This activity is not starting in the resumed state... which
1192 // should look like we asked it to pause+stop (but remain visible),
1193 // and it has done so and reported back the current icicle and
1194 // other state.
1195 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1196 + " (starting in stopped state)");
1197 r.state = ActivityState.STOPPED;
1198 r.stopped = true;
1199 }
1200
1201 // Launch the new version setup screen if needed. We do this -after-
1202 // launching the initial activity (that is, home), so that it can have
1203 // a chance to initialize itself while in the background, making the
1204 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001205 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001206 mService.startSetupActivityLocked();
1207 }
1208
1209 return true;
1210 }
1211
Craig Mautnere79d42682013-04-01 19:01:53 -07001212 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001213 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001214 // Is this activity's application already running?
1215 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001216 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001217
1218 r.task.stack.setLaunchTime(r);
1219
1220 if (app != null && app.thread != null) {
1221 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001222 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1223 || !"android".equals(r.info.packageName)) {
1224 // Don't add this if it is a platform component that is marked
1225 // to run in multiple processes, because this is actually
1226 // part of the framework so doesn't make sense to track as a
1227 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001228 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1229 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001230 }
George Mount2c92c972014-03-20 09:38:23 -07001231 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001232 return;
1233 } catch (RemoteException e) {
1234 Slog.w(TAG, "Exception when starting activity "
1235 + r.intent.getComponent().flattenToShortString(), e);
1236 }
1237
1238 // If a dead object exception was thrown -- fall through to
1239 // restart the application.
1240 }
1241
1242 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001243 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001244 }
1245
Craig Mautner6170f732013-04-02 13:05:23 -07001246 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001247 Intent intent, String resolvedType, ActivityInfo aInfo,
1248 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1249 IBinder resultTo, String resultWho, int requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001250 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Craig Mautnere0a38842013-12-16 16:14:02 -08001251 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container) {
Craig Mautner6170f732013-04-02 13:05:23 -07001252 int err = ActivityManager.START_SUCCESS;
1253
1254 ProcessRecord callerApp = null;
1255 if (caller != null) {
1256 callerApp = mService.getRecordForAppLocked(caller);
1257 if (callerApp != null) {
1258 callingPid = callerApp.pid;
1259 callingUid = callerApp.info.uid;
1260 } else {
1261 Slog.w(TAG, "Unable to find app for caller " + caller
1262 + " (pid=" + callingPid + ") when starting: "
1263 + intent.toString());
1264 err = ActivityManager.START_PERMISSION_DENIED;
1265 }
1266 }
1267
1268 if (err == ActivityManager.START_SUCCESS) {
1269 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1270 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner9ef471f2014-02-07 13:11:47 -08001271 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)
1272 + " on display " + (container == null ? (mFocusedStack == null ?
1273 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1274 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1275 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001276 }
1277
1278 ActivityRecord sourceRecord = null;
1279 ActivityRecord resultRecord = null;
1280 if (resultTo != null) {
1281 sourceRecord = isInAnyStackLocked(resultTo);
1282 if (DEBUG_RESULTS) Slog.v(
1283 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1284 if (sourceRecord != null) {
1285 if (requestCode >= 0 && !sourceRecord.finishing) {
1286 resultRecord = sourceRecord;
1287 }
1288 }
1289 }
1290 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1291
Dianne Hackborn91097de2014-04-04 18:02:06 -07001292 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001293
1294 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1295 && sourceRecord != null) {
1296 // Transfer the result target from the source activity to the new
1297 // one being started, including any failures.
1298 if (requestCode >= 0) {
1299 ActivityOptions.abort(options);
1300 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1301 }
1302 resultRecord = sourceRecord.resultTo;
1303 resultWho = sourceRecord.resultWho;
1304 requestCode = sourceRecord.requestCode;
1305 sourceRecord.resultTo = null;
1306 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001307 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001308 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001309 if (sourceRecord.launchedFromUid == callingUid) {
1310 // The new activity is being launched from the same uid as the previous
1311 // activity in the flow, and asking to forward its result back to the
1312 // previous. In this case the activity is serving as a trampoline between
1313 // the two, so we also want to update its launchedFromPackage to be the
1314 // same as the previous activity. Note that this is safe, since we know
1315 // these two packages come from the same uid; the caller could just as
1316 // well have supplied that same package name itself. This specifially
1317 // deals with the case of an intent picker/chooser being launched in the app
1318 // flow to redirect to an activity picked by the user, where we want the final
1319 // activity to consider it to have been launched by the previous app activity.
1320 callingPackage = sourceRecord.launchedFromPackage;
1321 }
Craig Mautner6170f732013-04-02 13:05:23 -07001322 }
1323
1324 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1325 // We couldn't find a class that can handle the given Intent.
1326 // That's the end of that!
1327 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1328 }
1329
1330 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1331 // We couldn't find the specific class specified in the Intent.
1332 // Also the end of the line.
1333 err = ActivityManager.START_CLASS_NOT_FOUND;
1334 }
1335
Dianne Hackborn91097de2014-04-04 18:02:06 -07001336 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1337 && sourceRecord.task.voiceSession != null) {
1338 // If this activity is being launched as part of a voice session, we need
1339 // to ensure that it is safe to do so. If the upcoming activity will also
1340 // be part of the voice session, we can only launch it if it has explicitly
1341 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001342 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001343 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1344 try {
1345 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1346 intent, resolvedType)) {
1347 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1348 }
1349 } catch (RemoteException e) {
1350 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1351 }
1352 }
1353 }
1354
1355 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1356 // If the caller is starting a new voice session, just make sure the target
1357 // is actually allowing it to run this way.
1358 try {
1359 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1360 intent, resolvedType)) {
1361 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1362 }
1363 } catch (RemoteException e) {
1364 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1365 }
1366 }
1367
Craig Mautner6170f732013-04-02 13:05:23 -07001368 if (err != ActivityManager.START_SUCCESS) {
1369 if (resultRecord != null) {
1370 resultStack.sendActivityResultLocked(-1,
1371 resultRecord, resultWho, requestCode,
1372 Activity.RESULT_CANCELED, null);
1373 }
1374 setDismissKeyguard(false);
1375 ActivityOptions.abort(options);
1376 return err;
1377 }
1378
1379 final int startAnyPerm = mService.checkPermission(
1380 START_ANY_ACTIVITY, callingPid, callingUid);
1381 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1382 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1383 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1384 if (resultRecord != null) {
1385 resultStack.sendActivityResultLocked(-1,
1386 resultRecord, resultWho, requestCode,
1387 Activity.RESULT_CANCELED, null);
1388 }
1389 setDismissKeyguard(false);
1390 String msg;
1391 if (!aInfo.exported) {
1392 msg = "Permission Denial: starting " + intent.toString()
1393 + " from " + callerApp + " (pid=" + callingPid
1394 + ", uid=" + callingUid + ")"
1395 + " not exported from uid " + aInfo.applicationInfo.uid;
1396 } else {
1397 msg = "Permission Denial: starting " + intent.toString()
1398 + " from " + callerApp + " (pid=" + callingPid
1399 + ", uid=" + callingUid + ")"
1400 + " requires " + aInfo.permission;
1401 }
1402 Slog.w(TAG, msg);
1403 throw new SecurityException(msg);
1404 }
1405
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001406 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001407 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001408
Craig Mautner6170f732013-04-02 13:05:23 -07001409 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001410 try {
1411 // The Intent we give to the watcher has the extra data
1412 // stripped off, since it can contain private information.
1413 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001414 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001415 aInfo.applicationInfo.packageName);
1416 } catch (RemoteException e) {
1417 mService.mController = null;
1418 }
Ben Gruver5e207332013-04-03 17:41:37 -07001419 }
Craig Mautner6170f732013-04-02 13:05:23 -07001420
Ben Gruver5e207332013-04-03 17:41:37 -07001421 if (abort) {
1422 if (resultRecord != null) {
1423 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001424 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001425 }
Ben Gruver5e207332013-04-03 17:41:37 -07001426 // We pretend to the caller that it was really started, but
1427 // they will just get a cancel result.
1428 setDismissKeyguard(false);
1429 ActivityOptions.abort(options);
1430 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001431 }
1432
1433 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001434 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001435 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001436 if (outActivity != null) {
1437 outActivity[0] = r;
1438 }
1439
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001440 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001441 if (voiceSession == null && (stack.mResumedActivity == null
1442 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Craig Mautner6170f732013-04-02 13:05:23 -07001443 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1444 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001445 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001446 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001447 setDismissKeyguard(false);
1448 ActivityOptions.abort(options);
1449 return ActivityManager.START_SWITCHES_CANCELED;
1450 }
1451 }
1452
1453 if (mService.mDidAppSwitch) {
1454 // This is the second allowed switch since we stopped switches,
1455 // so now just generally allow switches. Use case: user presses
1456 // home (switches disabled, switch to home, mDidAppSwitch now true);
1457 // user taps a home icon (coming from home so allowed, we hit here
1458 // and now allow anyone to switch again).
1459 mService.mAppSwitchesAllowedTime = 0;
1460 } else {
1461 mService.mDidAppSwitch = true;
1462 }
1463
Craig Mautneree36c772014-07-16 14:56:05 -07001464 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001465
Dianne Hackborn91097de2014-04-04 18:02:06 -07001466 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
1467 startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001468
1469 if (allPausedActivitiesComplete()) {
1470 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001471 // activity start, but we are not actually doing an activity
1472 // switch... just dismiss the keyguard now, because we
1473 // probably want to see whatever is behind it.
1474 dismissKeyguard();
1475 }
1476 return err;
1477 }
1478
Craig Mautner1b4bf852014-05-26 15:06:32 -07001479 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001480 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001481
1482 // On leanback only devices we should keep all activities in the same stack.
1483 if (!mLeanbackOnlyDevice &&
1484 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001485 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001486 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001487 if (taskStack.isOnHomeDisplay()) {
1488 if (mFocusedStack != taskStack) {
1489 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1490 "focused stack to r=" + r + " task=" + task);
1491 mFocusedStack = taskStack;
1492 } else {
1493 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1494 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1495 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001496 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001497 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001498 }
1499
Craig Mautnere0a38842013-12-16 16:14:02 -08001500 final ActivityContainer container = r.mInitialActivityContainer;
1501 if (container != null) {
1502 // The first time put it on the desired stack, after this put on task stack.
1503 r.mInitialActivityContainer = null;
1504 return container.mStack;
1505 }
1506
Craig Mautner1b4bf852014-05-26 15:06:32 -07001507 if (mFocusedStack != mHomeStack && (!newTask ||
1508 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001509 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1510 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1511 return mFocusedStack;
1512 }
1513
Craig Mautnere0a38842013-12-16 16:14:02 -08001514 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1515 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1516 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1517 if (!stack.isHomeStack()) {
1518 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1519 "adjustStackFocus: Setting focused stack=" + stack);
1520 mFocusedStack = stack;
1521 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001522 }
1523 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001524
Craig Mautner4a1cb222013-12-04 16:14:06 -08001525 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001526 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001527 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1528 " stackId=" + stackId);
1529 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001530 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001531 }
1532 return mHomeStack;
1533 }
1534
Craig Mautner29219d92013-04-16 20:19:12 -07001535 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001536 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001537 final TaskRecord task = r.task;
1538 boolean isHomeActivity = !r.isApplicationActivity();
1539 if (!isHomeActivity && task != null) {
1540 isHomeActivity = !task.isApplicationTask();
1541 }
1542 if (!isHomeActivity && task != null) {
1543 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1544 isHomeActivity = parent != null && parent.isHomeActivity();
1545 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001546 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001547 }
1548 }
1549
Craig Mautner8849a5e2013-04-02 16:41:03 -07001550 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001551 ActivityRecord sourceRecord,
1552 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
1553 boolean doResume, Bundle options) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001554 final Intent intent = r.intent;
1555 final int callingUid = r.launchedFromUid;
1556
Craig Mautnera228ae92014-07-09 05:44:55 -07001557 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1558 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001559
Craig Mautnera228ae92014-07-09 05:44:55 -07001560 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001561 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001562 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001563 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1564 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1565 "\"singleInstance\" or \"singleTask\"");
1566 launchFlags &=
1567 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1568 } else {
1569 switch (r.info.documentLaunchMode) {
1570 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1571 break;
1572 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1573 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1574 break;
1575 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1576 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1577 break;
1578 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1579 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1580 break;
1581 }
1582 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001583
1584 final boolean launchTaskBehind = r.mLaunchTaskBehind &&
1585 (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001586
1587 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1588 // For whatever reason this activity is being launched into a new
1589 // task... yet the caller has requested a result back. Well, that
1590 // is pretty messed up, so instead immediately send back a cancel
1591 // and let the new task continue launched as normal without a
1592 // dependency on its originator.
1593 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1594 r.resultTo.task.stack.sendActivityResultLocked(-1,
1595 r.resultTo, r.resultWho, r.requestCode,
1596 Activity.RESULT_CANCELED, null);
1597 r.resultTo = null;
1598 }
1599
1600 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1601 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1602 }
1603
Craig Mautner8849a5e2013-04-02 16:41:03 -07001604 // We'll invoke onUserLeaving before onPause only if the launching
1605 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001606 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001607 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001608
1609 // If the caller has asked not to resume at this point, we make note
1610 // of this in the record so that we can skip it when trying to find
1611 // the top running activity.
1612 if (!doResume) {
1613 r.delayedResume = true;
1614 }
1615
Craig Mautnera254cd72014-05-25 16:47:39 -07001616 ActivityRecord notTop =
1617 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001618
1619 // If the onlyIfNeeded flag is set, then we can do this if the activity
1620 // being launched is the same as the one making the call... or, as
1621 // a special case, if we do not know the caller then we count the
1622 // current top activity as the caller.
1623 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1624 ActivityRecord checkedCaller = sourceRecord;
1625 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001626 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001627 }
1628 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1629 // Caller is not the same as launcher, so always needed.
1630 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1631 }
1632 }
1633
1634 if (sourceRecord == null) {
1635 // This activity is not being started from another... in this
1636 // case we -always- start a new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001637 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001638 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1639 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001640 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1641 }
1642 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1643 // The original activity who is starting us is running as a single
1644 // instance... this new activity it is starting must go on its
1645 // own task.
1646 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautnera228ae92014-07-09 05:44:55 -07001647 } else if (launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001648 // The activity being started is a single instance... it always
1649 // gets launched into its own task.
1650 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1651 }
1652
Craig Mautner88629292013-11-10 20:39:05 -08001653 ActivityInfo newTaskInfo = null;
1654 Intent newTaskIntent = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001655 final ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001656 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001657 if (sourceRecord.finishing) {
1658 // If the source is finishing, we can't further count it as our source. This
1659 // is because the task it is associated with may now be empty and on its way out,
1660 // so we don't want to blindly throw it in to that task. Instead we will take
Craig Mautner88629292013-11-10 20:39:05 -08001661 // the NEW_TASK flow and try to find a task for it. But save the task information
1662 // so it can be used when creating the new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001663 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001664 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1665 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1666 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautner88629292013-11-10 20:39:05 -08001667 newTaskInfo = sourceRecord.info;
1668 newTaskIntent = sourceRecord.task.intent;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001669 }
1670 sourceRecord = null;
1671 sourceStack = null;
1672 } else {
1673 sourceStack = sourceRecord.task.stack;
1674 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001675 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001676 sourceStack = null;
1677 }
1678
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001679 intent.setFlags(launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001680
1681 boolean addingToTask = false;
1682 boolean movedHome = false;
1683 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001684 ActivityStack targetStack;
Craig Mautnerd00f4742014-03-12 14:17:26 -07001685 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1686 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001687 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001688 // If bring to front is requested, and no result is requested, and
1689 // we can find a task that was started with this same
1690 // component, then instead of launching bring that one to the front.
1691 if (r.resultTo == null) {
1692 // See if there is a task to bring to the front. If this is
1693 // a SINGLE_INSTANCE activity, there can be one and only one
1694 // instance of it in the history, and it is always in its own
1695 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001696 ActivityRecord intentActivity = !launchSingleInstance ?
1697 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001698 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001699 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001700 showLockTaskToast();
Craig Mautneraea74a52014-03-08 14:23:10 -08001701 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
1702 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1703 }
Craig Mautner29219d92013-04-16 20:19:12 -07001704 if (r.task == null) {
1705 r.task = intentActivity.task;
1706 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001707 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001708 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001709 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1710 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001711 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001712 if (intentActivity.task.intent == null) {
1713 // This task was started because of movement of
1714 // the activity based on affinity... now that we
1715 // are actually launching it, we can assign the
1716 // base intent.
1717 intentActivity.task.setIntent(intent, r.info);
1718 }
1719 // If the target task is not in the front, then we need
1720 // to bring it to the front... except... well, with
1721 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1722 // to have the same behavior as if a new instance was
1723 // being started, which means not bringing it to the front
1724 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001725 final ActivityStack lastStack = getLastStack();
1726 ActivityRecord curTop = lastStack == null?
1727 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001728 if (curTop != null && (curTop.task != intentActivity.task ||
1729 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001730 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001731 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1732 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001733 // We really do want to push this one into the
1734 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001735 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001736 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1737 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001738 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001739 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001740 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001741 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1742 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001743 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001744 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001745 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001746 options = null;
1747 }
1748 }
1749 // If the caller has requested that the target task be
1750 // reset, then do so.
1751 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1752 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1753 }
1754 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1755 // We don't need to start a new activity, and
1756 // the client said not to do anything if that
1757 // is the case, so this is it! And for paranoia, make
1758 // sure we have correctly resumed the top activity.
1759 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001760 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001761 } else {
1762 ActivityOptions.abort(options);
1763 }
1764 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1765 }
1766 if ((launchFlags &
1767 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1768 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1769 // The caller has requested to completely replace any
1770 // existing task with its new activity. Well that should
1771 // not be too hard...
1772 reuseTask = intentActivity.task;
1773 reuseTask.performClearTaskLocked();
1774 reuseTask.setIntent(r.intent, r.info);
1775 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001776 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001777 // In this situation we want to remove all activities
1778 // from the task up to the one being started. In most
1779 // cases this means we are resetting the task to its
1780 // initial state.
1781 ActivityRecord top =
1782 intentActivity.task.performClearTaskLocked(r, launchFlags);
1783 if (top != null) {
1784 if (top.frontOfTask) {
1785 // Activity aliases may mean we use different
1786 // intents for the top activity, so make sure
1787 // the task now has the identity of the new
1788 // intent.
1789 top.task.setIntent(r.intent, r.info);
1790 }
1791 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1792 r, top.task);
1793 top.deliverNewIntentLocked(callingUid, r.intent);
1794 } else {
1795 // A special case: we need to
1796 // start the activity because it is not currently
1797 // running, and the caller has asked to clear the
1798 // current task to have this activity at the top.
1799 addingToTask = true;
1800 // Now pretend like this activity is being started
1801 // by the top of its task, so it is put in the
1802 // right place.
1803 sourceRecord = intentActivity;
1804 }
1805 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1806 // In this case the top activity on the task is the
1807 // same as the one being launched, so we take that
1808 // as a request to bring the task to the foreground.
1809 // If the top activity in the task is the root
1810 // activity, deliver this new intent to it if it
1811 // desires.
1812 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1813 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1814 && intentActivity.realActivity.equals(r.realActivity)) {
1815 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1816 intentActivity.task);
1817 if (intentActivity.frontOfTask) {
1818 intentActivity.task.setIntent(r.intent, r.info);
1819 }
1820 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1821 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1822 // In this case we are launching the root activity
1823 // of the task, but with a different intent. We
1824 // should start a new instance on top.
1825 addingToTask = true;
1826 sourceRecord = intentActivity;
1827 }
1828 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1829 // In this case an activity is being launched in to an
1830 // existing task, without resetting that task. This
1831 // is typically the situation of launching an activity
1832 // from a notification or shortcut. We want to place
1833 // the new activity on top of the current task.
1834 addingToTask = true;
1835 sourceRecord = intentActivity;
1836 } else if (!intentActivity.task.rootWasReset) {
1837 // In this case we are launching in to an existing task
1838 // that has not yet been started from its front door.
1839 // The current task has been brought to the front.
1840 // Ideally, we'd probably like to place this new task
1841 // at the bottom of its stack, but that's a little hard
1842 // to do with the current organization of the code so
1843 // for now we'll just drop it.
1844 intentActivity.task.setIntent(r.intent, r.info);
1845 }
1846 if (!addingToTask && reuseTask == null) {
1847 // We didn't do anything... but it was needed (a.k.a., client
1848 // don't use that intent!) And for paranoia, make
1849 // sure we have correctly resumed the top activity.
1850 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001851 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001852 } else {
1853 ActivityOptions.abort(options);
1854 }
1855 return ActivityManager.START_TASK_TO_FRONT;
1856 }
1857 }
1858 }
1859 }
1860
1861 //String uri = r.intent.toURI();
1862 //Intent intent2 = new Intent(uri);
1863 //Slog.i(TAG, "Given intent: " + r.intent);
1864 //Slog.i(TAG, "URI is: " + uri);
1865 //Slog.i(TAG, "To intent: " + intent2);
1866
1867 if (r.packageName != null) {
1868 // If the activity being launched is the same as the one currently
1869 // at the top, then we need to check if it should only be launched
1870 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001871 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001872 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001873 if (top != null && r.resultTo == null) {
1874 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1875 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001876 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001877 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001878 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1879 top.task);
1880 // For paranoia, make sure we have correctly
1881 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001882 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001883 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001884 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001885 }
1886 ActivityOptions.abort(options);
1887 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1888 // We don't need to start a new activity, and
1889 // the client said not to do anything if that
1890 // is the case, so this is it!
1891 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1892 }
1893 top.deliverNewIntentLocked(callingUid, r.intent);
1894 return ActivityManager.START_DELIVERED_TO_TOP;
1895 }
1896 }
1897 }
1898 }
1899
1900 } else {
1901 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001902 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1903 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001904 }
1905 ActivityOptions.abort(options);
1906 return ActivityManager.START_CLASS_NOT_FOUND;
1907 }
1908
1909 boolean newTask = false;
1910 boolean keepCurTransition = false;
1911
Craig Mautnerbb742462014-07-07 15:28:55 -07001912 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07001913 sourceRecord.task : null;
1914
Craig Mautner8849a5e2013-04-02 16:41:03 -07001915 // Should this be considered a new task?
1916 if (r.resultTo == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001917 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001918 if (isLockTaskModeViolation(reuseTask)) {
1919 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1920 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1921 }
Craig Mautner1b4bf852014-05-26 15:06:32 -07001922 newTask = true;
1923 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerbb742462014-07-07 15:28:55 -07001924 if (!launchTaskBehind) {
1925 targetStack.moveToFront();
1926 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001927 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08001928 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
1929 newTaskInfo != null ? newTaskInfo : r.info,
1930 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07001931 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
1932 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001933 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1934 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001935 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07001936 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001937 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001938 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001939 if ((launchFlags &
1940 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1941 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1942 // Caller wants to appear on home activity, so before starting
1943 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07001944 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001945 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001946 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001947 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001948 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08001949 if (isLockTaskModeViolation(sourceTask)) {
1950 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1951 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1952 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001953 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07001954 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07001955 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautnera228ae92014-07-09 05:44:55 -07001956 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001957 // In this case, we are adding the activity to an existing
1958 // task, but the caller has asked to clear that task if the
1959 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001960 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001961 keepCurTransition = true;
1962 if (top != null) {
1963 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1964 top.deliverNewIntentLocked(callingUid, r.intent);
1965 // For paranoia, make sure we have correctly
1966 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001967 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001968 if (doResume) {
1969 targetStack.resumeTopActivityLocked(null);
1970 }
1971 ActivityOptions.abort(options);
1972 return ActivityManager.START_DELIVERED_TO_TOP;
1973 }
1974 } else if (!addingToTask &&
1975 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1976 // In this case, we are launching an activity in our own task
1977 // that may already be running somewhere in the history, and
1978 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001979 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001980 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001981 final TaskRecord task = top.task;
1982 task.moveActivityToFrontLocked(top);
1983 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001984 top.updateOptionsLocked(options);
1985 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001986 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001987 if (doResume) {
1988 targetStack.resumeTopActivityLocked(null);
1989 }
1990 return ActivityManager.START_DELIVERED_TO_TOP;
1991 }
1992 }
1993 // An existing activity is starting this new activity, so we want
1994 // to keep the new one in the same task as the one that is starting
1995 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07001996 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001997 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001998 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001999
2000 } else {
2001 // This not being started from an existing activity, and not part
2002 // of a new task... just put it in the top task, though these days
2003 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002004 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07002005 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07002006 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002007 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002008 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002009 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002010 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2011 + " in new guessed " + r.task);
2012 }
2013
2014 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002015 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002016
Craig Mautner76e2a762014-06-24 09:05:43 -07002017 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2018 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2019 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002020 if (newTask) {
2021 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2022 }
2023 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002024 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002025 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002026 if (!launchTaskBehind) {
2027 // Don't set focus on an activity that's going to the back.
2028 mService.setFocusedActivityLocked(r);
2029 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002030 return ActivityManager.START_SUCCESS;
2031 }
2032
Craig Mautneree36c772014-07-16 14:56:05 -07002033 final void doPendingActivityLaunchesLocked(boolean doResume) {
2034 while (!mPendingActivityLaunches.isEmpty()) {
2035 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002036 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Craig Mautneree36c772014-07-16 14:56:05 -07002037 doResume && mPendingActivityLaunches.isEmpty(), null);
2038 }
2039 }
2040
Craig Mautner7f13ed32014-07-28 14:00:35 -07002041 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002042 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2043 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002044 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002045 mPendingActivityLaunches.remove(palNdx);
2046 }
2047 }
2048 }
2049
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002050 void acquireLaunchWakelock() {
2051 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2052 throw new IllegalStateException("Calling must be system uid");
2053 }
2054 mLaunchingActivity.acquire();
2055 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2056 // To be safe, don't allow the wake lock to be held for too long.
2057 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2058 }
2059 }
2060
Craig Mautnerf3333272013-04-22 10:55:53 -07002061 // Checked.
2062 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2063 Configuration config) {
2064 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2065
Craig Mautnerf3333272013-04-22 10:55:53 -07002066 ArrayList<ActivityRecord> stops = null;
2067 ArrayList<ActivityRecord> finishes = null;
2068 ArrayList<UserStartedState> startingUsers = null;
2069 int NS = 0;
2070 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002071 boolean booting = false;
2072 boolean enableScreen = false;
2073 boolean activityRemoved = false;
2074
2075 ActivityRecord r = ActivityRecord.forToken(token);
2076 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002077 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2078 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002079 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2080 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002081 if (fromTimeout) {
2082 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002083 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002084
2085 // This is a hack to semi-deal with a race condition
2086 // in the client where it can be constructed with a
2087 // newer configuration from when we asked it to launch.
2088 // We'll update with whatever configuration it now says
2089 // it used to launch.
2090 if (config != null) {
2091 r.configuration = config;
2092 }
2093
2094 // We are now idle. If someone is waiting for a thumbnail from
2095 // us, we can now deliver.
2096 r.idle = true;
2097
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002098 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
2099 if (!mService.mBooted && isFrontStack(r.task.stack)) {
2100 mService.mBooted = true;
2101 enableScreen = true;
2102 }
2103 }
2104
2105 if (allResumedActivitiesIdle()) {
2106 if (r != null) {
2107 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002108 }
2109
2110 if (mLaunchingActivity.isHeld()) {
2111 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2112 if (VALIDATE_WAKE_LOCK_CALLER &&
2113 Binder.getCallingUid() != Process.myUid()) {
2114 throw new IllegalStateException("Calling must be system uid");
2115 }
2116 mLaunchingActivity.release();
2117 }
2118 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002119 }
2120
2121 // Atomically retrieve all of the other things to do.
2122 stops = processStoppingActivitiesLocked(true);
2123 NS = stops != null ? stops.size() : 0;
2124 if ((NF=mFinishingActivities.size()) > 0) {
2125 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2126 mFinishingActivities.clear();
2127 }
2128
Prashant Malani84b08ad2014-07-15 16:28:24 -07002129 booting = mService.mBooting;
2130 mService.mBooting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002131
2132 if (mStartingUsers.size() > 0) {
2133 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2134 mStartingUsers.clear();
2135 }
2136
Craig Mautnerf3333272013-04-22 10:55:53 -07002137 // Stop any activities that are scheduled to do so but have been
2138 // waiting for the next one to start.
2139 for (int i = 0; i < NS; i++) {
2140 r = stops.get(i);
2141 final ActivityStack stack = r.task.stack;
2142 if (r.finishing) {
2143 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2144 } else {
2145 stack.stopActivityLocked(r);
2146 }
2147 }
2148
2149 // Finish any activities that are scheduled to do so but have been
2150 // waiting for the next one to start.
2151 for (int i = 0; i < NF; i++) {
2152 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002153 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002154 }
2155
2156 if (booting) {
2157 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002158 } else {
2159 // Complete user switch
2160 if (startingUsers != null) {
2161 for (int i = 0; i < startingUsers.size(); i++) {
2162 mService.finishUserSwitch(startingUsers.get(i));
2163 }
2164 }
2165 // Complete starting up of background users
2166 if (mStartingBackgroundUsers.size() > 0) {
2167 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2168 mStartingBackgroundUsers.clear();
2169 for (int i = 0; i < startingUsers.size(); i++) {
2170 mService.finishUserBoot(startingUsers.get(i));
2171 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002172 }
2173 }
2174
2175 mService.trimApplications();
2176 //dump();
2177 //mWindowManager.dump();
2178
2179 if (enableScreen) {
Jeff Brown4b4971b2014-07-25 19:30:05 -07002180 mService.postEnableScreenAfterBootLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002181 }
2182
2183 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002184 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002185 }
2186
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002187 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002188 }
2189
Craig Mautner8e569572013-10-11 17:36:59 -07002190 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002191 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002192 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2193 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002194 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2195 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2196 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002197 }
Craig Mautner19091252013-10-05 00:03:53 -07002198 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002199 }
2200
2201 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002202 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2203 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002204 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2205 stacks.get(stackNdx).closeSystemDialogsLocked();
2206 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002207 }
2208 }
2209
Craig Mautner93529a42013-10-04 15:03:13 -07002210 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002211 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002212 }
2213
Craig Mautner8d341ef2013-03-26 09:03:27 -07002214 /**
2215 * @return true if some activity was finished (or would have finished if doit were true).
2216 */
2217 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2218 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002219 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2220 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002221 final int numStacks = stacks.size();
2222 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2223 final ActivityStack stack = stacks.get(stackNdx);
2224 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2225 didSomething = true;
2226 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002227 }
2228 }
2229 return didSomething;
2230 }
2231
Dianne Hackborna413dc02013-07-12 12:02:55 -07002232 void updatePreviousProcessLocked(ActivityRecord r) {
2233 // Now that this process has stopped, we may want to consider
2234 // it to be the previous app to try to keep around in case
2235 // the user wants to return to it.
2236
2237 // First, found out what is currently the foreground app, so that
2238 // we don't blow away the previous app if this activity is being
2239 // hosted by the process that is actually still the foreground.
2240 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002241 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2242 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002243 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2244 final ActivityStack stack = stacks.get(stackNdx);
2245 if (isFrontStack(stack)) {
2246 if (stack.mResumedActivity != null) {
2247 fgApp = stack.mResumedActivity.app;
2248 } else if (stack.mPausingActivity != null) {
2249 fgApp = stack.mPausingActivity.app;
2250 }
2251 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002252 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002253 }
2254 }
2255
2256 // Now set this one as the previous process, only if that really
2257 // makes sense to.
2258 if (r.app != null && fgApp != null && r.app != fgApp
2259 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002260 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002261 mService.mPreviousProcess = r.app;
2262 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2263 }
2264 }
2265
Craig Mautner05d29032013-05-03 13:40:13 -07002266 boolean resumeTopActivitiesLocked() {
2267 return resumeTopActivitiesLocked(null, null, null);
2268 }
2269
2270 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2271 Bundle targetOptions) {
2272 if (targetStack == null) {
2273 targetStack = getFocusedStack();
2274 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002275 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002276 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002277 if (isFrontStack(targetStack)) {
2278 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2279 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002280 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2281 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002282 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2283 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002284 if (stack == targetStack) {
2285 // Already started above.
2286 continue;
2287 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002288 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002289 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002290 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002291 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002292 }
Craig Mautner05d29032013-05-03 13:40:13 -07002293 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002294 }
2295
2296 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002297 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2298 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002299 final int numStacks = stacks.size();
2300 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2301 final ActivityStack stack = stacks.get(stackNdx);
2302 stack.finishTopRunningActivityLocked(app);
2303 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002304 }
2305 }
2306
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002307 void finishVoiceTask(IVoiceInteractionSession session) {
2308 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2309 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2310 final int numStacks = stacks.size();
2311 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2312 final ActivityStack stack = stacks.get(stackNdx);
2313 stack.finishVoiceTask(session);
2314 }
2315 }
2316 }
2317
Craig Mautneraea74a52014-03-08 14:23:10 -08002318 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2319 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2320 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002321 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002322 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2323 // Caller wants the home activity moved with it. To accomplish this,
2324 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002325 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002326 }
2327 task.stack.moveTaskToFrontLocked(task, null, options);
2328 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2329 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002330 }
2331
Craig Mautner967212c2013-04-13 21:10:58 -07002332 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002333 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2334 if (activityContainer != null) {
2335 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002336 }
2337 return null;
2338 }
2339
Craig Mautner967212c2013-04-13 21:10:58 -07002340 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002341 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002342 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2343 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002344 }
2345 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002346 }
2347
Craig Mautner4a1cb222013-12-04 16:14:06 -08002348 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002349 ActivityRecord homeActivity = getHomeActivity();
2350 if (homeActivity != null) {
2351 return homeActivity.appToken;
2352 }
2353 return null;
2354 }
2355
2356 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002357 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2358 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2359 final TaskRecord task = tasks.get(taskNdx);
2360 if (task.isHomeTask()) {
2361 final ArrayList<ActivityRecord> activities = task.mActivities;
2362 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2363 final ActivityRecord r = activities.get(activityNdx);
2364 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002365 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002366 }
2367 }
2368 }
2369 }
2370 return null;
2371 }
2372
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002373 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002374 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002375 ActivityContainer activityContainer =
2376 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002377 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002378 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002379 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002380 return activityContainer;
2381 }
2382
Craig Mautner34b73df2014-01-12 21:11:08 -08002383 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002384 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2385 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2386 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002387 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2388 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002389 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002390 }
2391 }
2392
Craig Mautner95da1082014-02-24 17:54:35 -08002393 void deleteActivityContainer(IActivityContainer container) {
2394 ActivityContainer activityContainer = (ActivityContainer)container;
2395 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002396 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2397 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002398 final int stackId = activityContainer.mStackId;
2399 mActivityContainers.remove(stackId);
2400 mWindowManager.removeStack(stackId);
2401 }
2402 }
2403
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002404 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002405 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2406 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002407 return -1;
2408 }
2409
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002410 ActivityContainer activityContainer = new ActivityContainer(stackId);
2411 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002412 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002413 return stackId;
2414 }
2415
2416 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002417 while (true) {
2418 if (++mLastStackId <= HOME_STACK_ID) {
2419 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002420 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002421 if (getStack(mLastStackId) == null) {
2422 break;
2423 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002424 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002425 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002426 }
2427
Craig Mautneref73ee12014-04-23 11:45:37 -07002428 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2429 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2430 final ActivityStack stack = getStack(stackId);
2431 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2432 final TaskRecord task = tasks.get(taskNdx);
2433 stack.addTask(task, false, false);
2434 final int taskId = task.taskId;
2435 final ArrayList<ActivityRecord> activities = task.mActivities;
2436 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2437 final ActivityRecord r = activities.get(activityNdx);
2438 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2439 r.info.screenOrientation, r.fullscreen,
2440 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002441 r.userId, r.info.configChanges, task.voiceSession != null,
2442 r.mLaunchTaskBehind);
Craig Mautneref73ee12014-04-23 11:45:37 -07002443 }
2444 mWindowManager.addTask(taskId, stackId, false);
2445 }
Craig Mautner84984fa2014-06-19 11:19:20 -07002446 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002447 }
2448
Craig Mautner8d341ef2013-03-26 09:03:27 -07002449 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002450 final TaskRecord task = anyTaskForIdLocked(taskId);
2451 if (task == null) {
2452 return;
2453 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002454 final ActivityStack stack = getStack(stackId);
2455 if (stack == null) {
2456 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2457 return;
2458 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002459 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002460 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002461 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002462 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002463 }
2464
Craig Mautnerac6f8432013-07-17 13:24:59 -07002465 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002466 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002467 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2468 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002469 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2470 final ActivityStack stack = stacks.get(stackNdx);
2471 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002472 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2473 continue;
2474 }
2475 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2476 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2477 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002478 continue;
2479 }
2480 final ActivityRecord ar = stack.findTaskLocked(r);
2481 if (ar != null) {
2482 return ar;
2483 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002484 }
2485 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002486 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002487 return null;
2488 }
2489
2490 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002491 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2492 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002493 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2494 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2495 if (ar != null) {
2496 return ar;
2497 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002498 }
2499 }
2500 return null;
2501 }
2502
Craig Mautner8d341ef2013-03-26 09:03:27 -07002503 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002504 scheduleSleepTimeout();
2505 if (!mGoingToSleep.isHeld()) {
2506 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002507 if (mLaunchingActivity.isHeld()) {
2508 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2509 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002510 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002511 mLaunchingActivity.release();
2512 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002513 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002514 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002515 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002516 }
2517
2518 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002519 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002520
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002521 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002522 final long endTime = System.currentTimeMillis() + timeout;
2523 while (true) {
2524 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002525 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2526 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002527 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2528 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2529 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002530 }
2531 if (cantShutdown) {
2532 long timeRemaining = endTime - System.currentTimeMillis();
2533 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002534 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002535 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002536 } catch (InterruptedException e) {
2537 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002538 } else {
2539 Slog.w(TAG, "Activity manager shutdown timed out");
2540 timedout = true;
2541 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002542 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002543 } else {
2544 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002545 }
2546 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002547
2548 // Force checkReadyForSleep to complete.
2549 mSleepTimeout = true;
2550 checkReadyForSleepLocked();
2551
Craig Mautner8d341ef2013-03-26 09:03:27 -07002552 return timedout;
2553 }
2554
2555 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002556 removeSleepTimeouts();
2557 if (mGoingToSleep.isHeld()) {
2558 mGoingToSleep.release();
2559 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002560 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2561 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002562 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2563 final ActivityStack stack = stacks.get(stackNdx);
2564 stack.awakeFromSleepingLocked();
2565 if (isFrontStack(stack)) {
2566 resumeTopActivitiesLocked();
2567 }
Craig Mautner5314a402013-09-26 12:40:16 -07002568 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002569 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002570 mGoingToSleepActivities.clear();
2571 }
2572
2573 void activitySleptLocked(ActivityRecord r) {
2574 mGoingToSleepActivities.remove(r);
2575 checkReadyForSleepLocked();
2576 }
2577
2578 void checkReadyForSleepLocked() {
2579 if (!mService.isSleepingOrShuttingDown()) {
2580 // Do not care.
2581 return;
2582 }
2583
2584 if (!mSleepTimeout) {
2585 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002586 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2587 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002588 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2589 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2590 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002591 }
2592
2593 if (mStoppingActivities.size() > 0) {
2594 // Still need to tell some activities to stop; can't sleep yet.
2595 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2596 + mStoppingActivities.size() + " activities");
2597 scheduleIdleLocked();
2598 dontSleep = true;
2599 }
2600
2601 if (mGoingToSleepActivities.size() > 0) {
2602 // Still need to tell some activities to sleep; can't sleep yet.
2603 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2604 + mGoingToSleepActivities.size() + " activities");
2605 dontSleep = true;
2606 }
2607
2608 if (dontSleep) {
2609 return;
2610 }
2611 }
2612
Craig Mautnere0a38842013-12-16 16:14:02 -08002613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2614 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002615 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2616 stacks.get(stackNdx).goToSleep();
2617 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002618 }
2619
2620 removeSleepTimeouts();
2621
2622 if (mGoingToSleep.isHeld()) {
2623 mGoingToSleep.release();
2624 }
2625 if (mService.mShuttingDown) {
2626 mService.notifyAll();
2627 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002628 }
2629
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002630 boolean reportResumedActivityLocked(ActivityRecord r) {
2631 final ActivityStack stack = r.task.stack;
2632 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002633 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002634 }
2635 if (allResumedActivitiesComplete()) {
2636 ensureActivitiesVisibleLocked(null, 0);
2637 mWindowManager.executeAppTransition();
2638 return true;
2639 }
2640 return false;
2641 }
2642
Craig Mautner8d341ef2013-03-26 09:03:27 -07002643 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002644 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2645 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002646 final int numStacks = stacks.size();
2647 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2648 final ActivityStack stack = stacks.get(stackNdx);
2649 stack.handleAppCrashLocked(app);
2650 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002651 }
2652 }
2653
Craig Mautneree2e45a2014-06-27 12:10:03 -07002654 boolean setMediaPlayingLocked(ActivityRecord r, boolean playing) {
2655 final ActivityStack stack = r.task.stack;
2656 if (stack == null) {
2657 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlaying: r=" + r + " playing=" +
2658 playing + " stack is null");
2659 return false;
2660 }
2661 final boolean isPlaying = stack.isMediaPlaying();
2662 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlayer: r=" + r + " playing=" +
2663 playing + " isPlaying=" + isPlaying);
2664
2665 final ActivityRecord top = topRunningActivityLocked();
2666 if (top == null || top == r || (playing == isPlaying)) {
2667 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlaying: quick return");
2668 stack.setMediaPlayer(playing ? r : null);
2669 return true;
2670 }
2671
2672 // A non-top activity is reporting a visibility change.
2673 if (top.fullscreen || top.state != ActivityState.RESUMED || top.app == null ||
2674 top.app.thread == null) {
2675 // Can't carry out this request.
2676 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlaying: returning top.fullscreen=" +
2677 top.fullscreen+ " top.state=" + top.state + " top.app=" + top.app +
2678 " top.app.thread=" + top.app.thread);
2679 return false;
2680 }
2681
2682 stack.setMediaPlayer(playing ? r : null);
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002683 if (!playing) {
2684 // Make the activity immediately above r opaque.
2685 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2686 if (next != null) {
2687 mService.convertFromTranslucent(next.appToken);
2688 }
2689 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002690 try {
2691 top.app.thread.scheduleBackgroundMediaPlayingChanged(top.appToken, playing);
2692 } catch (RemoteException e) {
2693 }
2694 return true;
2695 }
2696
Craig Mautnerbb742462014-07-07 15:28:55 -07002697 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2698 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2699 r.mLaunchTaskBehind = false;
2700 final TaskRecord task = r.task;
2701 task.setLastThumbnail(task.stack.screenshotActivities(r));
2702 mService.addRecentTaskLocked(task);
2703 mWindowManager.setAppVisibility(r.appToken, false);
2704 }
2705
2706 void scheduleLaunchTaskBehindComplete(IBinder token) {
2707 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2708 }
2709
Craig Mautnerde4ef022013-04-07 19:01:33 -07002710 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002711 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002712 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2713 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002714 final int topStackNdx = stacks.size() - 1;
2715 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2716 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002717 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002718 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002719 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002720 }
2721
2722 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002723 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2724 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002725 final int numStacks = stacks.size();
2726 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2727 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002728 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002729 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002730 }
2731 }
2732
2733 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002734 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2735 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002736 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002737
Craig Mautner858d8a62013-04-23 17:08:34 -07002738 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002739 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2740 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002741 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002742 final ActivityStack stack = stacks.get(stackNdx);
2743 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002744 TaskRecord task = stack.topTask();
2745 if (task != null) {
2746 mWindowManager.moveTaskToTop(task.taskId);
2747 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002748 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002749 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002750
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002751 ActivityStack stack = getStack(restoreStackId);
2752 if (stack == null) {
2753 stack = mHomeStack;
2754 }
2755 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002756 if (stack.isOnHomeDisplay()) {
2757 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002758 TaskRecord task = stack.topTask();
2759 if (task != null) {
2760 mWindowManager.moveTaskToTop(task.taskId);
2761 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002762 } else {
2763 // Stack was moved to another display while user was swapped out.
Craig Mautner84984fa2014-06-19 11:19:20 -07002764 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002765 }
Craig Mautner93529a42013-10-04 15:03:13 -07002766 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002767 }
2768
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002769 /**
2770 * Add background users to send boot completed events to.
2771 * @param userId The user being started in the background
2772 * @param uss The state object for the user.
2773 */
2774 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2775 mStartingBackgroundUsers.add(uss);
2776 }
2777
Craig Mautnerde4ef022013-04-07 19:01:33 -07002778 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2779 int N = mStoppingActivities.size();
2780 if (N <= 0) return null;
2781
2782 ArrayList<ActivityRecord> stops = null;
2783
2784 final boolean nowVisible = allResumedActivitiesVisible();
2785 for (int i=0; i<N; i++) {
2786 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002787 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002788 + nowVisible + " waitingVisible=" + s.waitingVisible
2789 + " finishing=" + s.finishing);
2790 if (s.waitingVisible && nowVisible) {
2791 mWaitingVisibleActivities.remove(s);
2792 s.waitingVisible = false;
2793 if (s.finishing) {
2794 // If this activity is finishing, it is sitting on top of
2795 // everyone else but we now know it is no longer needed...
2796 // so get rid of it. Otherwise, we need to go through the
2797 // normal flow and hide it once we determine that it is
2798 // hidden by the activities in front of it.
2799 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002800 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002801 }
2802 }
2803 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2804 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2805 if (stops == null) {
2806 stops = new ArrayList<ActivityRecord>();
2807 }
2808 stops.add(s);
2809 mStoppingActivities.remove(i);
2810 N--;
2811 i--;
2812 }
2813 }
2814
2815 return stops;
2816 }
2817
Craig Mautnercf910b02013-04-23 11:23:27 -07002818 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002819 // FIXME
2820/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2821 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07002822 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002823 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002824 if (isFrontStack(stack)) {
2825 if (r == null) {
2826 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2827 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002828 final ActivityRecord pausing = stack.mPausingActivity;
2829 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002830 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002831 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002832 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002833 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002834 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002835 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002836 }
2837 }
2838 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002839 final ActivityRecord resumed = stack.mResumedActivity;
2840 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002841 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002842 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002843 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002844 if (r != null && (state == ActivityState.INITIALIZING
2845 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002846 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002847 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002848 }
2849 }
2850 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002851*/
Craig Mautner76ea2242013-05-15 11:40:05 -07002852 }
2853
Craig Mautner27084302013-03-25 08:05:25 -07002854 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002855 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity=");
Craig Mautner27084302013-03-25 08:05:25 -07002856 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002857 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002858 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002859 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2860 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2861 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002862 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07002863 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002864
Craig Mautner20e72272013-04-01 13:45:53 -07002865 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002866 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002867 }
2868
Dianne Hackborn390517b2013-05-30 15:03:32 -07002869 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2870 boolean needSep, String prefix) {
2871 if (activity != null) {
2872 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2873 if (needSep) {
2874 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002875 }
2876 pw.print(prefix);
2877 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002878 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002879 }
2880 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002881 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002882 }
2883
Craig Mautner8d341ef2013-03-26 09:03:27 -07002884 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2885 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002886 boolean printed = false;
2887 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002888 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2889 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002890 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
2891 pw.println(" (activities from bottom to top):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002892 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002893 final int numStacks = stacks.size();
2894 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2895 final ActivityStack stack = stacks.get(stackNdx);
2896 StringBuilder stackHeader = new StringBuilder(128);
2897 stackHeader.append(" Stack #");
2898 stackHeader.append(stack.mStackId);
2899 stackHeader.append(":");
2900 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2901 needSep, stackHeader.toString());
2902 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2903 !dumpAll, false, dumpPackage, true,
2904 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002905
Craig Mautner4a1cb222013-12-04 16:14:06 -08002906 needSep = printed;
2907 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2908 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002909 if (pr) {
2910 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002911 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002912 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002913 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2914 " mResumedActivity: ");
2915 if (pr) {
2916 printed = true;
2917 needSep = false;
2918 }
2919 if (dumpAll) {
2920 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2921 " mLastPausedActivity: ");
2922 if (pr) {
2923 printed = true;
2924 needSep = true;
2925 }
2926 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2927 needSep, " mLastNoHistoryActivity: ");
2928 }
2929 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002930 }
2931 }
2932
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002933 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2934 false, dumpPackage, true, " Activities waiting to finish:", null);
2935 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2936 false, dumpPackage, true, " Activities waiting to stop:", null);
2937 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2938 false, dumpPackage, true, " Activities waiting for another to become visible:",
2939 null);
2940 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2941 false, dumpPackage, true, " Activities waiting to sleep:", null);
2942 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2943 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002944
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002945 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002946 }
2947
Dianne Hackborn390517b2013-05-30 15:03:32 -07002948 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002949 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002950 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002951 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002952 String innerPrefix = null;
2953 String[] args = null;
2954 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002955 for (int i=list.size()-1; i>=0; i--) {
2956 final ActivityRecord r = list.get(i);
2957 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2958 continue;
2959 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002960 if (innerPrefix == null) {
2961 innerPrefix = prefix + " ";
2962 args = new String[0];
2963 }
2964 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002965 final boolean full = !brief && (complete || !r.isInHistory());
2966 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002967 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002968 needNL = false;
2969 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002970 if (header1 != null) {
2971 pw.println(header1);
2972 header1 = null;
2973 }
2974 if (header2 != null) {
2975 pw.println(header2);
2976 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002977 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002978 if (lastTask != r.task) {
2979 lastTask = r.task;
2980 pw.print(prefix);
2981 pw.print(full ? "* " : " ");
2982 pw.println(lastTask);
2983 if (full) {
2984 lastTask.dump(pw, prefix + " ");
2985 } else if (complete) {
2986 // Complete + brief == give a summary. Isn't that obvious?!?
2987 if (lastTask.intent != null) {
2988 pw.print(prefix); pw.print(" ");
2989 pw.println(lastTask.intent.toInsecureStringWithClip());
2990 }
2991 }
2992 }
2993 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2994 pw.print(" #"); pw.print(i); pw.print(": ");
2995 pw.println(r);
2996 if (full) {
2997 r.dump(pw, innerPrefix);
2998 } else if (complete) {
2999 // Complete + brief == give a summary. Isn't that obvious?!?
3000 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3001 if (r.app != null) {
3002 pw.print(innerPrefix); pw.println(r.app);
3003 }
3004 }
3005 if (client && r.app != null && r.app.thread != null) {
3006 // flush anything that is already in the PrintWriter since the thread is going
3007 // to write to the file descriptor directly
3008 pw.flush();
3009 try {
3010 TransferPipe tp = new TransferPipe();
3011 try {
3012 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3013 r.appToken, innerPrefix, args);
3014 // Short timeout, since blocking here can
3015 // deadlock with the application.
3016 tp.go(fd, 2000);
3017 } finally {
3018 tp.kill();
3019 }
3020 } catch (IOException e) {
3021 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3022 } catch (RemoteException e) {
3023 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3024 }
3025 needNL = true;
3026 }
3027 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003028 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003029 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003030
Craig Mautnerf3333272013-04-22 10:55:53 -07003031 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003032 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003033 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3034 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003035 }
3036
3037 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003038 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003039 }
3040
3041 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003042 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003043 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3044 }
3045
Craig Mautner05d29032013-05-03 13:40:13 -07003046 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003047 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3048 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3049 }
Craig Mautner05d29032013-05-03 13:40:13 -07003050 }
3051
Craig Mautner0eea92c2013-05-16 13:35:39 -07003052 void removeSleepTimeouts() {
3053 mSleepTimeout = false;
3054 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3055 }
3056
3057 final void scheduleSleepTimeout() {
3058 removeSleepTimeouts();
3059 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3060 }
3061
Craig Mautner4a1cb222013-12-04 16:14:06 -08003062 @Override
3063 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003064 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003065 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3066 }
3067
3068 @Override
3069 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003070 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003071 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3072 }
3073
3074 @Override
3075 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003076 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003077 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3078 }
3079
3080 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003081 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003082 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003083 newDisplay = mActivityDisplays.get(displayId) == null;
3084 if (newDisplay) {
3085 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
3086 mActivityDisplays.put(displayId, activityDisplay);
3087 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003088 }
Craig Mautner4504de52013-12-20 09:06:56 -08003089 if (newDisplay) {
3090 mWindowManager.onDisplayAdded(displayId);
3091 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003092 }
3093
3094 public void handleDisplayRemovedLocked(int displayId) {
3095 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003096 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3097 if (activityDisplay != null) {
3098 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003099 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003100 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003101 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003102 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003103 }
3104 }
3105 mWindowManager.onDisplayRemoved(displayId);
3106 }
3107
3108 public void handleDisplayChangedLocked(int displayId) {
3109 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003110 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3111 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003112 // TODO: Update the bounds.
3113 }
3114 }
3115 mWindowManager.onDisplayChanged(displayId);
3116 }
3117
3118 StackInfo getStackInfo(ActivityStack stack) {
3119 StackInfo info = new StackInfo();
3120 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3121 info.displayId = Display.DEFAULT_DISPLAY;
3122 info.stackId = stack.mStackId;
3123
3124 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3125 final int numTasks = tasks.size();
3126 int[] taskIds = new int[numTasks];
3127 String[] taskNames = new String[numTasks];
3128 for (int i = 0; i < numTasks; ++i) {
3129 final TaskRecord task = tasks.get(i);
3130 taskIds[i] = task.taskId;
3131 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3132 : task.realActivity != null ? task.realActivity.flattenToString()
3133 : task.getTopActivity() != null ? task.getTopActivity().packageName
3134 : "unknown";
3135 }
3136 info.taskIds = taskIds;
3137 info.taskNames = taskNames;
3138 return info;
3139 }
3140
3141 StackInfo getStackInfoLocked(int stackId) {
3142 ActivityStack stack = getStack(stackId);
3143 if (stack != null) {
3144 return getStackInfo(stack);
3145 }
3146 return null;
3147 }
3148
3149 ArrayList<StackInfo> getAllStackInfosLocked() {
3150 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003151 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3152 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003153 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3154 list.add(getStackInfo(stacks.get(ndx)));
3155 }
3156 }
3157 return list;
3158 }
3159
Jason Monka8f569c2014-07-07 12:15:21 -04003160 void showLockTaskToast() {
3161 mLockTaskNotify.showToast(mLockTaskIsLocked);
3162 }
3163
3164 void setLockTaskModeLocked(TaskRecord task, boolean isLocked) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003165 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003166 // Take out of lock task mode if necessary
3167 if (mLockTaskModeTask != null) {
3168 final Message lockTaskMsg = Message.obtain();
3169 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3170 lockTaskMsg.what = LOCK_TASK_END_MSG;
3171 mLockTaskModeTask = null;
3172 mHandler.sendMessage(lockTaskMsg);
3173 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003174 return;
3175 }
3176 if (isLockTaskModeViolation(task)) {
3177 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3178 return;
3179 }
3180 mLockTaskModeTask = task;
3181 findTaskToMoveToFrontLocked(task, 0, null);
3182 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003183
3184 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003185 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3186 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003187 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003188 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003189 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003190 }
3191
3192 boolean isLockTaskModeViolation(TaskRecord task) {
3193 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3194 }
3195
3196 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3197 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
3198 mLockTaskModeTask = null;
3199 }
3200 }
3201
3202 boolean isInLockTaskMode() {
3203 return mLockTaskModeTask != null;
3204 }
3205
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003206 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003207
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003208 public ActivityStackSupervisorHandler(Looper looper) {
3209 super(looper);
3210 }
3211
Craig Mautnerf3333272013-04-22 10:55:53 -07003212 void activityIdleInternal(ActivityRecord r) {
3213 synchronized (mService) {
3214 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3215 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003216 }
3217
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003218 @Override
3219 public void handleMessage(Message msg) {
3220 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003221 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003222 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003223 if (mService.mDidDexOpt) {
3224 mService.mDidDexOpt = false;
3225 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3226 nmsg.obj = msg.obj;
3227 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3228 return;
3229 }
3230 // We don't at this point know if the activity is fullscreen,
3231 // so we need to be conservative and assume it isn't.
3232 activityIdleInternal((ActivityRecord)msg.obj);
3233 } break;
3234 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003235 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003236 activityIdleInternal((ActivityRecord)msg.obj);
3237 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003238 case RESUME_TOP_ACTIVITY_MSG: {
3239 synchronized (mService) {
3240 resumeTopActivitiesLocked();
3241 }
3242 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003243 case SLEEP_TIMEOUT_MSG: {
3244 synchronized (mService) {
3245 if (mService.isSleepingOrShuttingDown()) {
3246 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3247 mSleepTimeout = true;
3248 checkReadyForSleepLocked();
3249 }
3250 }
3251 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003252 case LAUNCH_TIMEOUT_MSG: {
3253 if (mService.mDidDexOpt) {
3254 mService.mDidDexOpt = false;
3255 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3256 return;
3257 }
3258 synchronized (mService) {
3259 if (mLaunchingActivity.isHeld()) {
3260 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3261 if (VALIDATE_WAKE_LOCK_CALLER
3262 && Binder.getCallingUid() != Process.myUid()) {
3263 throw new IllegalStateException("Calling must be system uid");
3264 }
3265 mLaunchingActivity.release();
3266 }
3267 }
3268 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003269 case HANDLE_DISPLAY_ADDED: {
3270 handleDisplayAddedLocked(msg.arg1);
3271 } break;
3272 case HANDLE_DISPLAY_CHANGED: {
3273 handleDisplayChangedLocked(msg.arg1);
3274 } break;
3275 case HANDLE_DISPLAY_REMOVED: {
3276 handleDisplayRemovedLocked(msg.arg1);
3277 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003278 case CONTAINER_CALLBACK_VISIBILITY: {
3279 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003280 final IActivityContainerCallback callback = container.mCallback;
3281 if (callback != null) {
3282 try {
3283 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3284 } catch (RemoteException e) {
3285 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003286 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003287 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003288 case LOCK_TASK_START_MSG: {
3289 // When lock task starts, we disable the status bars.
3290 try {
Jason Monk62515be2014-05-21 16:06:19 -04003291 if (mLockTaskNotify == null) {
3292 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003293 }
Jason Monk62515be2014-05-21 16:06:19 -04003294 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003295 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003296 if (getStatusBarService() != null) {
3297 int flags =
3298 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003299 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003300 flags ^= StatusBarManager.DISABLE_HOME
3301 | StatusBarManager.DISABLE_RECENT;
3302 }
3303 getStatusBarService().disable(flags, mToken,
3304 mService.mContext.getPackageName());
3305 }
3306 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003307 if (getDevicePolicyManager() != null) {
3308 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003309 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003310 }
justinzhang5286d3f2014-05-12 17:06:01 -04003311 } catch (RemoteException ex) {
3312 throw new RuntimeException(ex);
3313 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003314 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003315 case LOCK_TASK_END_MSG: {
3316 // When lock task ends, we enable the status bars.
3317 try {
Jason Monk62515be2014-05-21 16:06:19 -04003318 if (getStatusBarService() != null) {
3319 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3320 mService.mContext.getPackageName());
3321 }
3322 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003323 if (getDevicePolicyManager() != null) {
3324 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3325 msg.arg1);
3326 }
Jason Monk62515be2014-05-21 16:06:19 -04003327 if (mLockTaskNotify == null) {
3328 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3329 }
3330 mLockTaskNotify.show(false);
3331 try {
3332 boolean shouldLockKeyguard = Settings.System.getInt(
3333 mService.mContext.getContentResolver(),
3334 Settings.System.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003335 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003336 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003337 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003338 new LockPatternUtils(mService.mContext)
3339 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003340 }
3341 } catch (SettingNotFoundException e) {
3342 // No setting, don't lock.
3343 }
justinzhang5286d3f2014-05-12 17:06:01 -04003344 } catch (RemoteException ex) {
3345 throw new RuntimeException(ex);
3346 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003347 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003348 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3349 final ActivityContainer container = (ActivityContainer) msg.obj;
3350 final IActivityContainerCallback callback = container.mCallback;
3351 if (callback != null) {
3352 try {
3353 callback.onAllActivitiesComplete(container.asBinder());
3354 } catch (RemoteException e) {
3355 }
3356 }
3357 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003358 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3359 synchronized (mService) {
3360 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3361 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003362 final ActivityContainer container = (ActivityContainer) msg.obj;
3363 container.mStack.finishAllActivitiesLocked(true);
3364 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003365 }
3366 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003367 case LAUNCH_TASK_BEHIND_COMPLETE: {
3368 synchronized (mService) {
3369 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3370 if (r != null) {
3371 handleLaunchTaskBehindCompleteLocked(r);
3372 }
3373 }
3374 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003375 }
3376 }
3377 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003378
Ying Wangb081a592014-04-22 15:20:16 -07003379 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003380 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003381 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003382 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003383 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003384 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003385 ActivityRecord mParentActivity = null;
3386 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003387
Craig Mautnere3a00d72014-04-16 08:31:19 -07003388 boolean mVisible = true;
3389
Craig Mautner4a1cb222013-12-04 16:14:06 -08003390 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003391 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003392
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003393 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3394 final static int CONTAINER_STATE_NO_SURFACE = 1;
3395 final static int CONTAINER_STATE_FINISHING = 2;
3396 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3397
3398 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003399 synchronized (mService) {
3400 mStackId = stackId;
3401 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003402 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003403 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003404 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003405 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003406
Craig Mautnere0a38842013-12-16 16:14:02 -08003407 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003408 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3409 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003410 mActivityDisplay = activityDisplay;
3411 mStack.mDisplayId = activityDisplay.mDisplayId;
3412 mStack.mStacks = activityDisplay.mStacks;
3413
3414 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003415 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003416 }
3417
3418 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003419 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003420 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003421 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3422 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003423 return;
3424 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003425 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003426 }
3427 }
3428
3429 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003430 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003431 synchronized (mService) {
3432 if (mActivityDisplay != null) {
3433 return mActivityDisplay.mDisplayId;
3434 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003435 }
3436 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003437 }
3438
Jeff Brownca9bc702014-02-11 14:32:56 -08003439 @Override
3440 public boolean injectEvent(InputEvent event) {
3441 final long origId = Binder.clearCallingIdentity();
3442 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003443 synchronized (mService) {
3444 if (mActivityDisplay != null) {
3445 return mInputManagerInternal.injectInputEvent(event,
3446 mActivityDisplay.mDisplayId,
3447 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3448 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003449 }
3450 return false;
3451 } finally {
3452 Binder.restoreCallingIdentity(origId);
3453 }
3454 }
3455
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003456 @Override
3457 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003458 synchronized (mService) {
3459 if (mContainerState == CONTAINER_STATE_FINISHING) {
3460 return;
3461 }
3462 mContainerState = CONTAINER_STATE_FINISHING;
3463
3464 final Message msg =
3465 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003466 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003467
3468 long origId = Binder.clearCallingIdentity();
3469 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003470 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003471 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003472 } finally {
3473 Binder.restoreCallingIdentity(origId);
3474 }
3475 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003476 }
3477
Craig Mautner34b73df2014-01-12 21:11:08 -08003478 private void detachLocked() {
3479 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3480 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003481 if (mActivityDisplay != null) {
3482 mActivityDisplay.detachActivitiesLocked(mStack);
3483 mActivityDisplay = null;
3484 mStack.mDisplayId = -1;
3485 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003486 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003487 }
3488 }
3489
3490 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003491 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003492 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003493 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003494 Binder.getCallingUid(), mCurrentUser, false,
3495 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003496 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003497 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003498 String mimeType = intent.getType();
3499 if (mimeType == null && intent.getData() != null
3500 && "content".equals(intent.getData().getScheme())) {
3501 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3502 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003503 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0, 0, null,
Craig Mautnere0a38842013-12-16 16:14:02 -08003504 null, null, null, null, userId, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003505 }
3506
3507 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003508 public final int startActivityIntentSender(IIntentSender intentSender) {
3509 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3510
3511 if (!(intentSender instanceof PendingIntentRecord)) {
3512 throw new IllegalArgumentException("Bad PendingIntent object");
3513 }
3514
3515 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003516 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003517 }
3518
Craig Mautner247ab652014-04-25 10:09:00 -07003519 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3520 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003521 Binder.getCallingUid(), mCurrentUser, false,
3522 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003523 if (resolvedType == null) {
3524 resolvedType = intent.getType();
3525 if (resolvedType == null && intent.getData() != null
3526 && "content".equals(intent.getData().getScheme())) {
3527 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3528 }
3529 }
3530 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003531 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003532 throw new SecurityException(
3533 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3534 }
3535 }
3536
3537 /** Throw a SecurityException if allowEmbedded is not true */
3538 @Override
3539 public final void checkEmbeddedAllowed(Intent intent) {
3540 checkEmbeddedAllowedInner(intent, null);
3541 }
3542
3543 /** Throw a SecurityException if allowEmbedded is not true */
3544 @Override
3545 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3546 if (!(intentSender instanceof PendingIntentRecord)) {
3547 throw new IllegalArgumentException("Bad PendingIntent object");
3548 }
3549 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3550 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3551 pendingIntent.key.requestResolvedType);
3552 }
3553
Craig Mautnerdf88d732014-01-27 09:21:32 -08003554 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003555 public IBinder asBinder() {
3556 return this;
3557 }
3558
Craig Mautner4504de52013-12-20 09:06:56 -08003559 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003560 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003561 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003562 }
3563
Craig Mautner4a1cb222013-12-04 16:14:06 -08003564 ActivityStackSupervisor getOuter() {
3565 return ActivityStackSupervisor.this;
3566 }
3567
Craig Mautnerd163e752014-06-13 17:18:47 -07003568 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003569 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003570 }
3571
3572 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003573 synchronized (mService) {
3574 if (mActivityDisplay != null) {
3575 mActivityDisplay.getBounds(outBounds);
3576 } else {
3577 outBounds.set(0, 0);
3578 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003579 }
3580 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003581
Craig Mautner6985bad2014-04-21 15:22:06 -07003582 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003583 void setVisible(boolean visible) {
3584 if (mVisible != visible) {
3585 mVisible = visible;
3586 if (mCallback != null) {
3587 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3588 0 /* unused */, this).sendToTarget();
3589 }
3590 }
3591 }
3592
Craig Mautner6985bad2014-04-21 15:22:06 -07003593 void setDrawn() {
3594 }
3595
Craig Mautner1b4bf852014-05-26 15:06:32 -07003596 // You can always start a new task on a regular ActivityStack.
3597 boolean isEligibleForNewTasks() {
3598 return true;
3599 }
3600
Craig Mautnerd163e752014-06-13 17:18:47 -07003601 void onTaskListEmptyLocked() {
3602 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3603 if (!mStack.isHomeStack()) {
3604 detachLocked();
3605 deleteActivityContainer(this);
3606 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003607 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3608 }
3609
Craig Mautner34b73df2014-01-12 21:11:08 -08003610 @Override
3611 public String toString() {
3612 return mIdString + (mActivityDisplay == null ? "N" : "A");
3613 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003614 }
3615
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003616 private class VirtualActivityContainer extends ActivityContainer {
3617 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003618 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003619
3620 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3621 super(getNextStackId());
3622 mParentActivity = parent;
3623 mCallback = callback;
3624 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003625 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003626 }
3627
3628 @Override
3629 public void setSurface(Surface surface, int width, int height, int density) {
3630 super.setSurface(surface, width, height, density);
3631
3632 synchronized (mService) {
3633 final long origId = Binder.clearCallingIdentity();
3634 try {
3635 setSurfaceLocked(surface, width, height, density);
3636 } finally {
3637 Binder.restoreCallingIdentity(origId);
3638 }
3639 }
3640 }
3641
3642 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3643 if (mContainerState == CONTAINER_STATE_FINISHING) {
3644 return;
3645 }
3646 VirtualActivityDisplay virtualActivityDisplay =
3647 (VirtualActivityDisplay) mActivityDisplay;
3648 if (virtualActivityDisplay == null) {
3649 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003650 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003651 mActivityDisplay = virtualActivityDisplay;
3652 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3653 attachToDisplayLocked(virtualActivityDisplay);
3654 }
3655
3656 if (mSurface != null) {
3657 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003658 }
3659
Craig Mautner6985bad2014-04-21 15:22:06 -07003660 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003661 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003662 mStack.resumeTopActivityLocked(null);
3663 } else {
3664 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003665 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003666 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
3667 mStack.startPausingLocked(false, true);
3668 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003669 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003670
Craig Mautnerd163e752014-06-13 17:18:47 -07003671 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003672
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003673 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3674 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003675 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003676
Craig Mautner6985bad2014-04-21 15:22:06 -07003677 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003678 boolean isAttachedLocked() {
3679 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003680 }
3681
3682 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003683 void setDrawn() {
3684 synchronized (mService) {
3685 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003686 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003687 }
3688 }
3689
Craig Mautner1b4bf852014-05-26 15:06:32 -07003690 // Never start a new task on an ActivityView if it isn't explicitly specified.
3691 @Override
3692 boolean isEligibleForNewTasks() {
3693 return false;
3694 }
3695
Craig Mautnerd163e752014-06-13 17:18:47 -07003696 private void setSurfaceIfReadyLocked() {
3697 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003698 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3699 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3700 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3701 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3702 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003703 }
3704 }
3705
Craig Mautner4a1cb222013-12-04 16:14:06 -08003706 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3707 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003708 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003709 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003710 int mDisplayId;
3711 Display mDisplay;
3712 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003713
Craig Mautner4a1cb222013-12-04 16:14:06 -08003714 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3715 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003716 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003717
Craig Mautneree2e45a2014-06-27 12:10:03 -07003718 ActivityRecord mMediaPlayingActivity;
3719
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003720 ActivityDisplay() {
3721 }
Craig Mautner4504de52013-12-20 09:06:56 -08003722
Craig Mautnere0a38842013-12-16 16:14:02 -08003723 ActivityDisplay(int displayId) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003724 init(mDisplayManager.getDisplay(displayId));
Craig Mautner4504de52013-12-20 09:06:56 -08003725 }
3726
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003727 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003728 mDisplay = display;
3729 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003730 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003731 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003732
3733 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003734 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3735 + mDisplayId);
3736 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003737 }
3738
Craig Mautnere0a38842013-12-16 16:14:02 -08003739 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003740 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003741 + " from displayId=" + mDisplayId);
3742 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003743 }
3744
3745 void getBounds(Point bounds) {
3746 mDisplay.getDisplayInfo(mDisplayInfo);
3747 bounds.x = mDisplayInfo.appWidth;
3748 bounds.y = mDisplayInfo.appHeight;
3749 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003750
Craig Mautneree2e45a2014-06-27 12:10:03 -07003751 void setMediaPlaying(ActivityRecord r) {
3752 mMediaPlayingActivity = r;
3753 }
3754
3755 boolean isMediaPlaying() {
3756 return mMediaPlayingActivity != null;
3757 }
3758
Craig Mautner34b73df2014-01-12 21:11:08 -08003759 @Override
3760 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003761 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3762 }
3763 }
3764
3765 class VirtualActivityDisplay extends ActivityDisplay {
3766 VirtualDisplay mVirtualDisplay;
3767
Craig Mautner6985bad2014-04-21 15:22:06 -07003768 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003769 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003770 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3771 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3772 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3773 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003774
3775 init(mVirtualDisplay.getDisplay());
3776
3777 mWindowManager.handleDisplayAdded(mDisplayId);
3778 }
3779
3780 void setSurface(Surface surface) {
3781 if (mVirtualDisplay != null) {
3782 mVirtualDisplay.setSurface(surface);
3783 }
3784 }
3785
3786 @Override
3787 void detachActivitiesLocked(ActivityStack stack) {
3788 super.detachActivitiesLocked(stack);
3789 if (mVirtualDisplay != null) {
3790 mVirtualDisplay.release();
3791 mVirtualDisplay = null;
3792 }
3793 }
3794
3795 @Override
3796 public String toString() {
3797 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003798 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003799 }
Jose Lima58e66d62014-05-27 20:00:27 -07003800
3801 private boolean isLeanbackOnlyDevice() {
3802 boolean onLeanbackOnly = false;
3803 try {
3804 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
3805 PackageManager.FEATURE_LEANBACK_ONLY);
3806 } catch (RemoteException e) {
3807 // noop
3808 }
3809
3810 return onLeanbackOnly;
3811 }
Craig Mautner27084302013-03-25 08:05:25 -07003812}