blob: 3227efab6bd075db83f6bdf89108ff9f08048e42 [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;
Wale Ogunwale7de05352014-12-12 15:21:33 -080027import static com.android.server.am.ActivityManagerService.DEBUG_RECENTS;
Craig Mautner6170f732013-04-02 13:05:23 -070028import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070029import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070030import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070031import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
32import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070033import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070034import static com.android.server.am.ActivityManagerService.TAG;
Craig Mautner84984fa2014-06-19 11:19:20 -070035import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
37import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070038
Craig Mautner2420ead2013-04-01 17:13:20 -070039import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070040import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080041import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.app.ActivityOptions;
43import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080044import android.app.IActivityContainer;
45import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070048import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070049import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070050import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070052import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040053import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040054import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070056import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.pm.ApplicationInfo;
62import android.content.pm.PackageManager;
63import android.content.pm.ResolveInfo;
64import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080065import android.graphics.Point;
66import android.hardware.display.DisplayManager;
67import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080068import android.hardware.display.DisplayManagerGlobal;
69import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080070import android.hardware.input.InputManager;
71import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070072import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070073import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070074import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070075import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070077import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070078import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070079import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070080import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070081import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070082import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040083import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070085import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040086import android.provider.Settings;
87import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070088import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070089import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070090import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070091import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080092import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070093
Craig Mautner4a1cb222013-12-04 16:14:06 -080094import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080095import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080096import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080097import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080098import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700100import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800101import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700102import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400103import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400104import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800105import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700106import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700107import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108
justinzhang5286d3f2014-05-12 17:06:01 -0400109
Craig Mautner8d341ef2013-03-26 09:03:27 -0700110import java.io.FileDescriptor;
111import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700112import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700113import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700114import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700115
Craig Mautner4a1cb222013-12-04 16:14:06 -0800116public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700117 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
118 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
119 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700120 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700121 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700122 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700123 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
124 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner30bd3bb2014-11-13 00:55:53 +0000125 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700126 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700127
Craig Mautner2219a1b2013-03-25 09:44:30 -0700128 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700129
Craig Mautnerf3333272013-04-22 10:55:53 -0700130 /** How long we wait until giving up on the last activity telling us it is idle. */
131 static final int IDLE_TIMEOUT = 10*1000;
132
Craig Mautner0eea92c2013-05-16 13:35:39 -0700133 /** How long we can hold the sleep wake lock before giving up. */
134 static final int SLEEP_TIMEOUT = 5*1000;
135
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700136 // How long we can hold the launch wake lock before giving up.
137 static final int LAUNCH_TIMEOUT = 10*1000;
138
Craig Mautner05d29032013-05-03 13:40:13 -0700139 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
140 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
141 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700142 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700143 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800144 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
145 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
146 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700147 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400148 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
149 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700150 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700151 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700152 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800153
Craig Mautner4504de52013-12-20 09:06:56 -0800154 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700155
Jason Monk62515be2014-05-21 16:06:19 -0400156 private static final String LOCK_TASK_TAG = "Lock-to-App";
157
justinzhang5286d3f2014-05-12 17:06:01 -0400158 /** Status Bar Service **/
159 private IBinder mToken = new Binder();
160 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400161 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400162
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700163 // For debugging to make sure the caller when acquiring/releasing our
164 // wake lock is the system process.
165 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700166
Craig Mautner27084302013-03-25 08:05:25 -0700167 final ActivityManagerService mService;
168
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700169 final ActivityStackSupervisorHandler mHandler;
170
171 /** Short cut */
172 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800173 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700174
Craig Mautner8d341ef2013-03-26 09:03:27 -0700175 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700176 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700177
178 /** Task identifier that activities are currently being started in. Incremented each time a
179 * new task is created. */
180 private int mCurTaskId = 0;
181
Craig Mautner2420ead2013-04-01 17:13:20 -0700182 /** The current user */
183 private int mCurrentUser;
184
Craig Mautnere0a38842013-12-16 16:14:02 -0800185 /** The stack containing the launcher app. Assumed to always be attached to
186 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700187 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700188
Craig Mautnere0a38842013-12-16 16:14:02 -0800189 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700190 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700191
Craig Mautner4a1cb222013-12-04 16:14:06 -0800192 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
193 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800194 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800195 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700196
197 /** List of activities that are waiting for a new activity to become visible before completing
198 * whatever operation they are supposed to do. */
199 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
200
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700201 /** List of processes waiting to find out about the next visible activity. */
202 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
203 new ArrayList<IActivityManager.WaitResult>();
204
205 /** List of processes waiting to find out about the next launched activity. */
206 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
207 new ArrayList<IActivityManager.WaitResult>();
208
Craig Mautnerde4ef022013-04-07 19:01:33 -0700209 /** List of activities that are ready to be stopped, but waiting for the next activity to
210 * settle down before doing so. */
211 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
212
Craig Mautnerf3333272013-04-22 10:55:53 -0700213 /** List of activities that are ready to be finished, but waiting for the previous activity to
214 * settle down before doing so. It contains ActivityRecord objects. */
215 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
216
Craig Mautner0eea92c2013-05-16 13:35:39 -0700217 /** List of activities that are in the process of going to sleep. */
218 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
219
Craig Mautnerf3333272013-04-22 10:55:53 -0700220 /** Used on user changes */
221 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
222
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700223 /** Used to queue up any background users being started */
224 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
225
Craig Mautnerde4ef022013-04-07 19:01:33 -0700226 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
227 * is being brought in front of us. */
228 boolean mUserLeaving = false;
229
Craig Mautner0eea92c2013-05-16 13:35:39 -0700230 /** Set when we have taken too long waiting to go to sleep. */
231 boolean mSleepTimeout = false;
232
Jose Lima58e66d62014-05-27 20:00:27 -0700233 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
234 * setWindowManager is called. **/
235 private boolean mLeanbackOnlyDevice;
236
Craig Mautner0eea92c2013-05-16 13:35:39 -0700237 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700238 * We don't want to allow the device to go to sleep while in the process
239 * of launching an activity. This is primarily to allow alarm intent
240 * receivers to launch an activity and get that to run before the device
241 * goes back to sleep.
242 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700243 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700244
245 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700246 * Set when the system is going to sleep, until we have
247 * successfully paused the current activity and released our wake lock.
248 * At that point the system is allowed to actually sleep.
249 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700250 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700251
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700252 /** Stack id of the front stack when user switched, indexed by userId. */
253 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700254
Craig Mautner4504de52013-12-20 09:06:56 -0800255 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800256 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700257 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800258
259 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700260 private final SparseArray<ActivityDisplay> mActivityDisplays =
261 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800262
Jeff Brownca9bc702014-02-11 14:32:56 -0800263 InputManagerInternal mInputManagerInternal;
264
Craig Mautneraea74a52014-03-08 14:23:10 -0800265 /** If non-null then the task specified remains in front and no other tasks may be started
266 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400267 TaskRecord mLockTaskModeTask;
Jason Monka8f569c2014-07-07 12:15:21 -0400268 /** Whether lock task has been entered by an authorized app and cannot
269 * be exited. */
270 private boolean mLockTaskIsLocked;
Jason Monk62515be2014-05-21 16:06:19 -0400271 /**
272 * Notifies the user when entering/exiting lock-task.
273 */
274 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800275
Craig Mautneree36c772014-07-16 14:56:05 -0700276 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
277 = new ArrayList<PendingActivityLaunch>();
278
Craig Mautner42d04db2014-11-06 12:13:23 -0800279 /** Used to keep resumeTopActivityLocked() from being entered recursively */
280 boolean inResumeTopActivity;
281
Craig Mautneree36c772014-07-16 14:56:05 -0700282 /**
283 * Description of a request to start a new activity, which has been held
284 * due to app switches being disabled.
285 */
286 static class PendingActivityLaunch {
287 final ActivityRecord r;
288 final ActivityRecord sourceRecord;
289 final int startFlags;
290 final ActivityStack stack;
291
292 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
293 int _startFlags, ActivityStack _stack) {
294 r = _r;
295 sourceRecord = _sourceRecord;
296 startFlags = _startFlags;
297 stack = _stack;
298 }
299 }
300
Craig Mautner4a1cb222013-12-04 16:14:06 -0800301 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700302 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700303 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
304 }
305
306 /**
307 * At the time when the constructor runs, the power manager has not yet been
308 * initialized. So we initialize our wakelocks afterwards.
309 */
310 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800311 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700312 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700313 mLaunchingActivity =
314 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
315 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700316 }
317
justinzhang5286d3f2014-05-12 17:06:01 -0400318 // This function returns a IStatusBarService. The value is from ServiceManager.
319 // getService and is cached.
320 private IStatusBarService getStatusBarService() {
321 synchronized (mService) {
322 if (mStatusBarService == null) {
323 mStatusBarService = IStatusBarService.Stub.asInterface(
324 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
325 if (mStatusBarService == null) {
326 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
327 }
328 }
329 return mStatusBarService;
330 }
331 }
332
Jason Monk35c62a42014-06-17 10:24:47 -0400333 private IDevicePolicyManager getDevicePolicyManager() {
334 synchronized (mService) {
335 if (mDevicePolicyManager == null) {
336 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
337 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
338 if (mDevicePolicyManager == null) {
339 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
340 }
341 }
342 return mDevicePolicyManager;
343 }
344 }
345
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700346 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800347 synchronized (mService) {
348 mWindowManager = wm;
349
350 mDisplayManager =
351 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
352 mDisplayManager.registerDisplayListener(this, null);
353
354 Display[] displays = mDisplayManager.getDisplays();
355 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
356 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800357 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700358 if (activityDisplay.mDisplay == null) {
359 throw new IllegalStateException("Default Display does not exist");
360 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800361 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800362 }
363
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700364 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800365 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800366
367 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700368
369 // Initialize this here, now that we can get a valid reference to PackageManager.
370 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800371 }
Craig Mautner27084302013-03-25 08:05:25 -0700372 }
373
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200374 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700375 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200376 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700377 }
378
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700379 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700381 }
382
Craig Mautnerde4ef022013-04-07 19:01:33 -0700383 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800384 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700385 }
386
Craig Mautner4a1cb222013-12-04 16:14:06 -0800387 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
388 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700389 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800390 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
391 if (parent != null) {
392 stack = parent.task.stack;
393 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800394 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800395 if (stacks != null && !stacks.isEmpty()) {
396 return stack == stacks.get(stacks.size() - 1);
397 }
398 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700399 }
400
Craig Mautnerde4ef022013-04-07 19:01:33 -0700401 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800402 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800403 int topNdx = stacks.size() - 1;
404 if (topNdx <= 0) {
405 return;
406 }
407 ActivityStack topStack = stacks.get(topNdx);
408 final boolean homeInFront = topStack == mHomeStack;
409 if (homeInFront != toFront) {
410 mLastFocusedStack = topStack;
411 stacks.remove(mHomeStack);
412 stacks.add(toFront ? topNdx : 0, mHomeStack);
413 mFocusedStack = stacks.get(topNdx);
414 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
415 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700416 }
417 }
418
Craig Mautner84984fa2014-06-19 11:19:20 -0700419 void moveHomeStackTaskToTop(int homeStackTaskType) {
420 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
421 mWindowManager.showRecentApps();
422 return;
423 }
Craig Mautner69ada552013-04-18 13:51:51 -0700424 moveHomeStack(true);
Craig Mautner84984fa2014-06-19 11:19:20 -0700425 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700426 }
427
Craig Mautner84984fa2014-06-19 11:19:20 -0700428 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700429 if (!mService.mBooting && !mService.mBooted) {
430 // Not ready yet!
431 return false;
432 }
433
Craig Mautner84984fa2014-06-19 11:19:20 -0700434 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
435 mWindowManager.showRecentApps();
436 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700437 }
Craig Mautner84984fa2014-06-19 11:19:20 -0700438 moveHomeStackTaskToTop(homeStackTaskType);
439 if (prev != null) {
440 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
441 }
442
Craig Mautnera8a90e02013-06-28 15:24:50 -0700443 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700444 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000445 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700446 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700447 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700448 }
449 return mService.startHomeActivityLocked(mCurrentUser);
450 }
451
Craig Mautner8d341ef2013-03-26 09:03:27 -0700452 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800453 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800454 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800455 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800456 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
457 ActivityStack stack = stacks.get(stackNdx);
458 TaskRecord task = stack.taskForIdLocked(id);
459 if (task != null) {
460 return task;
461 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700462 }
463 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800464
465 // Don't give up! Look in recents.
466 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
467 TaskRecord task = mService.recentTaskForIdLocked(id);
468 if (task == null) {
469 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
470 return null;
471 }
472
473 if (!restoreRecentTaskLocked(task)) {
474 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
475 return null;
476 }
477 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
478 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700479 }
480
Craig Mautner6170f732013-04-02 13:05:23 -0700481 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800482 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800483 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800484 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800485 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
486 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
487 if (r != null) {
488 return r;
489 }
Craig Mautner6170f732013-04-02 13:05:23 -0700490 }
491 }
492 return null;
493 }
494
Craig Mautneref73ee12014-04-23 11:45:37 -0700495 void setNextTaskId(int taskId) {
496 if (taskId > mCurTaskId) {
497 mCurTaskId = taskId;
498 }
499 }
500
Craig Mautner8d341ef2013-03-26 09:03:27 -0700501 int getNextTaskId() {
502 do {
503 mCurTaskId++;
504 if (mCurTaskId <= 0) {
505 mCurTaskId = 1;
506 }
507 } while (anyTaskForIdLocked(mCurTaskId) != null);
508 return mCurTaskId;
509 }
510
Craig Mautnerde4ef022013-04-07 19:01:33 -0700511 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700512 ActivityStack stack = getFocusedStack();
513 if (stack == null) {
514 return null;
515 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700516 ActivityRecord resumedActivity = stack.mResumedActivity;
517 if (resumedActivity == null || resumedActivity.app == null) {
518 resumedActivity = stack.mPausingActivity;
519 if (resumedActivity == null || resumedActivity.app == null) {
520 resumedActivity = stack.topRunningActivityLocked(null);
521 }
522 }
523 return resumedActivity;
524 }
525
Dianne Hackbornff072722014-09-24 10:56:28 -0700526 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700527 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800528 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800529 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
530 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800531 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
532 final ActivityStack stack = stacks.get(stackNdx);
533 if (!isFrontStack(stack)) {
534 continue;
535 }
536 ActivityRecord hr = stack.topRunningActivityLocked(null);
537 if (hr != null) {
538 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
539 && processName.equals(hr.processName)) {
540 try {
George Mount2c92c972014-03-20 09:38:23 -0700541 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 didSomething = true;
543 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700544 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 Slog.w(TAG, "Exception in new application when starting activity "
546 + hr.intent.getComponent().flattenToShortString(), e);
547 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700548 }
Craig Mautner20e72272013-04-01 13:45:53 -0700549 }
Craig Mautner20e72272013-04-01 13:45:53 -0700550 }
551 }
552 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700553 if (!didSomething) {
554 ensureActivitiesVisibleLocked(null, 0);
555 }
Craig Mautner20e72272013-04-01 13:45:53 -0700556 return didSomething;
557 }
558
559 boolean allResumedActivitiesIdle() {
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);
Craig Mautner34b73df2014-01-12 21:11:08 -0800564 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800565 continue;
566 }
567 final ActivityRecord resumedActivity = stack.mResumedActivity;
568 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800569 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
570 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800571 return false;
572 }
Craig Mautner20e72272013-04-01 13:45:53 -0700573 }
574 }
575 return true;
576 }
577
Craig Mautnerde4ef022013-04-07 19:01:33 -0700578 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800579 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
580 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800581 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
582 final ActivityStack stack = stacks.get(stackNdx);
583 if (isFrontStack(stack)) {
584 final ActivityRecord r = stack.mResumedActivity;
585 if (r != null && r.state != ActivityState.RESUMED) {
586 return false;
587 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700588 }
589 }
590 }
591 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 if (DEBUG_STACK) Slog.d(TAG,
593 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
594 mLastFocusedStack + " to=" + mFocusedStack);
595 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700596 return true;
597 }
598
599 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800600 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
601 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
603 final ActivityStack stack = stacks.get(stackNdx);
604 final ActivityRecord r = stack.mResumedActivity;
605 if (r != null && (!r.nowVisible || r.waitingVisible)) {
606 return false;
607 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700608 }
609 }
610 return true;
611 }
612
Craig Mautner2acc3892013-09-23 10:28:14 -0700613 /**
614 * Pause all activities in either all of the stacks or just the back stacks.
615 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700616 * @return true if any activity was paused as a result of this call.
617 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700618 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700619 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800620 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
621 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800622 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
623 final ActivityStack stack = stacks.get(stackNdx);
624 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
625 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
626 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700627 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
628 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700630 }
631 }
632 return someActivityPaused;
633 }
634
Craig Mautnerde4ef022013-04-07 19:01:33 -0700635 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700636 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800637 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
638 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800639 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
640 final ActivityStack stack = stacks.get(stackNdx);
641 final ActivityRecord r = stack.mPausingActivity;
642 if (r != null && r.state != ActivityState.PAUSED
643 && r.state != ActivityState.STOPPED
644 && r.state != ActivityState.STOPPING) {
645 if (DEBUG_STATES) {
646 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
647 pausing = false;
648 } else {
649 return false;
650 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700651 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700652 }
653 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700654 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700655 }
656
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700657 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
658 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500659 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800660 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
661 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
662 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
663 final ActivityStack stack = stacks.get(stackNdx);
664 if (stack.mResumedActivity != null &&
665 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700666 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800667 }
668 }
669 }
670 }
671
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700672 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700673 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700674 }
675
676 void sendWaitingVisibleReportLocked(ActivityRecord r) {
677 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700678 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700679 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700680 if (w.who == null) {
681 changed = true;
682 w.timeout = false;
683 if (r != null) {
684 w.who = new ComponentName(r.info.packageName, r.info.name);
685 }
686 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
687 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700688 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700689 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700690 if (changed) {
691 mService.notifyAll();
692 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700693 }
694
695 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
696 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700697 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700698 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700699 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700700 if (w.who == null) {
701 changed = true;
702 w.timeout = timeout;
703 if (r != null) {
704 w.who = new ComponentName(r.info.packageName, r.info.name);
705 }
706 w.thisTime = thisTime;
707 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700708 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700709 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700710 if (changed) {
711 mService.notifyAll();
712 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700713 }
714
Craig Mautner29219d92013-04-16 20:19:12 -0700715 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700716 final ActivityStack focusedStack = getFocusedStack();
717 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
718 if (r != null) {
719 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700720 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700721
Craig Mautner4a1cb222013-12-04 16:14:06 -0800722 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800723 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700726 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700727 r = stack.topRunningActivityLocked(null);
728 if (r != null) {
729 return r;
730 }
731 }
732 }
733 return null;
734 }
735
Dianne Hackborn09233282014-04-30 11:33:59 -0700736 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700737 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800738 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
739 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800740 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800741 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800742 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800743 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
744 final ActivityStack stack = stacks.get(stackNdx);
745 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
746 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700747 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700748 }
749 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700750
751 // The lists are already sorted from most recent to oldest. Just pull the most recent off
752 // each list and add it to list. Stop when all lists are empty or maxNum reached.
753 while (maxNum > 0) {
754 long mostRecentActiveTime = Long.MIN_VALUE;
755 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 final int numTaskLists = runningTaskLists.size();
757 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
758 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700759 if (!stackTaskList.isEmpty()) {
760 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
761 if (lastActiveTime > mostRecentActiveTime) {
762 mostRecentActiveTime = lastActiveTime;
763 selectedStackList = stackTaskList;
764 }
765 }
766 }
767 if (selectedStackList != null) {
768 list.add(selectedStackList.remove(0));
769 --maxNum;
770 } else {
771 break;
772 }
773 }
Craig Mautner20e72272013-04-01 13:45:53 -0700774 }
775
Craig Mautner23ac33b2013-04-01 16:26:35 -0700776 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700777 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700778 // Collect information about the target of the Intent.
779 ActivityInfo aInfo;
780 try {
781 ResolveInfo rInfo =
782 AppGlobals.getPackageManager().resolveIntent(
783 intent, resolvedType,
784 PackageManager.MATCH_DEFAULT_ONLY
785 | ActivityManagerService.STOCK_PM_FLAGS, userId);
786 aInfo = rInfo != null ? rInfo.activityInfo : null;
787 } catch (RemoteException e) {
788 aInfo = null;
789 }
790
791 if (aInfo != null) {
792 // Store the found target back into the intent, because now that
793 // we have it we never want to do this again. For example, if the
794 // user navigates back to this point in the history, we should
795 // always restart the exact same activity.
796 intent.setComponent(new ComponentName(
797 aInfo.applicationInfo.packageName, aInfo.name));
798
799 // Don't debug things in the system process
800 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
801 if (!aInfo.processName.equals("system")) {
802 mService.setDebugApp(aInfo.processName, true, false);
803 }
804 }
805
806 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
807 if (!aInfo.processName.equals("system")) {
808 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
809 }
810 }
811
Jeff Hao1b012d32014-08-20 10:35:34 -0700812 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700813 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700814 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700815 }
816 }
817 }
818 return aInfo;
819 }
820
Craig Mautner2219a1b2013-03-25 09:44:30 -0700821 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700822 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE);
Dianne Hackborn95465202014-09-15 16:21:55 -0700823 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
824 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700825 }
826
Craig Mautner23ac33b2013-04-01 16:26:35 -0700827 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700828 String callingPackage, Intent intent, String resolvedType,
829 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700830 IBinder resultTo, String resultWho, int requestCode, int startFlags,
831 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700832 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700833 // Refuse possible leaked file descriptors
834 if (intent != null && intent.hasFileDescriptors()) {
835 throw new IllegalArgumentException("File descriptors passed in Intent");
836 }
837 boolean componentSpecified = intent.getComponent() != null;
838
839 // Don't modify the client's object!
840 intent = new Intent(intent);
841
842 // Collect information about the target of the Intent.
843 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700844 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700845
Craig Mautnere0a38842013-12-16 16:14:02 -0800846 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700847 synchronized (mService) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700848 final int realCallingPid = Binder.getCallingPid();
849 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700850 int callingPid;
851 if (callingUid >= 0) {
852 callingPid = -1;
853 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700854 callingPid = realCallingPid;
855 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700856 } else {
857 callingPid = callingUid = -1;
858 }
859
Craig Mautnere0a38842013-12-16 16:14:02 -0800860 final ActivityStack stack;
861 if (container == null || container.mStack.isOnHomeDisplay()) {
862 stack = getFocusedStack();
863 } else {
864 stack = container.mStack;
865 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700866 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700867 && mService.mConfiguration.diff(config) != 0;
868 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700869 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700870
871 final long origId = Binder.clearCallingIdentity();
872
873 if (aInfo != null &&
874 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
875 // This may be a heavy-weight process! Check to see if we already
876 // have another, different heavy-weight process running.
877 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
878 if (mService.mHeavyWeightProcess != null &&
879 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
880 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700881 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700882 if (caller != null) {
883 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
884 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700885 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700886 } else {
887 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700888 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700889 + intent.toString());
890 ActivityOptions.abort(options);
891 return ActivityManager.START_PERMISSION_DENIED;
892 }
893 }
894
895 IIntentSender target = mService.getIntentSenderLocked(
896 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700897 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700898 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
899 | PendingIntent.FLAG_ONE_SHOT, null);
900
901 Intent newIntent = new Intent();
902 if (requestCode >= 0) {
903 // Caller is requesting a result.
904 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
905 }
906 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
907 new IntentSender(target));
908 if (mService.mHeavyWeightProcess.activities.size() > 0) {
909 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
910 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
911 hist.packageName);
912 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
913 hist.task.taskId);
914 }
915 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
916 aInfo.packageName);
917 newIntent.setFlags(intent.getFlags());
918 newIntent.setClassName("android",
919 HeavyWeightSwitcherActivity.class.getName());
920 intent = newIntent;
921 resolvedType = null;
922 caller = null;
923 callingUid = Binder.getCallingUid();
924 callingPid = Binder.getCallingPid();
925 componentSpecified = true;
926 try {
927 ResolveInfo rInfo =
928 AppGlobals.getPackageManager().resolveIntent(
929 intent, null,
930 PackageManager.MATCH_DEFAULT_ONLY
931 | ActivityManagerService.STOCK_PM_FLAGS, userId);
932 aInfo = rInfo != null ? rInfo.activityInfo : null;
933 aInfo = mService.getActivityInfoForUser(aInfo, userId);
934 } catch (RemoteException e) {
935 aInfo = null;
936 }
937 }
938 }
939 }
940
Dianne Hackborn91097de2014-04-04 18:02:06 -0700941 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
942 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700943 requestCode, callingPid, callingUid, callingPackage,
944 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700945 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700946
Craig Mautner85c11a82014-07-17 12:38:18 -0700947 Binder.restoreCallingIdentity(origId);
948
Craig Mautnerde4ef022013-04-07 19:01:33 -0700949 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700950 // If the caller also wants to switch to a new configuration,
951 // do so now. This allows a clean switch, as we are waiting
952 // for the current activity to pause (so we will not destroy
953 // it), and have not yet started the next activity.
954 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
955 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700956 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700957 if (DEBUG_CONFIGURATION) Slog.v(TAG,
958 "Updating to new configuration after starting activity.");
959 mService.updateConfigurationLocked(config, null, false, false);
960 }
961
Craig Mautner23ac33b2013-04-01 16:26:35 -0700962 if (outResult != null) {
963 outResult.result = res;
964 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700965 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700966 do {
967 try {
968 mService.wait();
969 } catch (InterruptedException e) {
970 }
971 } while (!outResult.timeout && outResult.who == null);
972 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700973 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700974 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700975 outResult.timeout = false;
976 outResult.who = new ComponentName(r.info.packageName, r.info.name);
977 outResult.totalTime = 0;
978 outResult.thisTime = 0;
979 } else {
980 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700981 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700982 do {
983 try {
984 mService.wait();
985 } catch (InterruptedException e) {
986 }
987 } while (!outResult.timeout && outResult.who == null);
988 }
989 }
990 }
991
992 return res;
993 }
994 }
995
996 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
997 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
998 Bundle options, int userId) {
999 if (intents == null) {
1000 throw new NullPointerException("intents is null");
1001 }
1002 if (resolvedTypes == null) {
1003 throw new NullPointerException("resolvedTypes is null");
1004 }
1005 if (intents.length != resolvedTypes.length) {
1006 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1007 }
1008
Craig Mautner23ac33b2013-04-01 16:26:35 -07001009
1010 int callingPid;
1011 if (callingUid >= 0) {
1012 callingPid = -1;
1013 } else if (caller == null) {
1014 callingPid = Binder.getCallingPid();
1015 callingUid = Binder.getCallingUid();
1016 } else {
1017 callingPid = callingUid = -1;
1018 }
1019 final long origId = Binder.clearCallingIdentity();
1020 try {
1021 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001022 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001023 for (int i=0; i<intents.length; i++) {
1024 Intent intent = intents[i];
1025 if (intent == null) {
1026 continue;
1027 }
1028
1029 // Refuse possible leaked file descriptors
1030 if (intent != null && intent.hasFileDescriptors()) {
1031 throw new IllegalArgumentException("File descriptors passed in Intent");
1032 }
1033
1034 boolean componentSpecified = intent.getComponent() != null;
1035
1036 // Don't modify the client's object!
1037 intent = new Intent(intent);
1038
1039 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001040 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001041 // TODO: New, check if this is correct
1042 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1043
1044 if (aInfo != null &&
1045 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
1046 != 0) {
1047 throw new IllegalArgumentException(
1048 "FLAG_CANT_SAVE_STATE not supported here");
1049 }
1050
1051 Bundle theseOptions;
1052 if (options != null && i == intents.length-1) {
1053 theseOptions = options;
1054 } else {
1055 theseOptions = null;
1056 }
Craig Mautner6170f732013-04-02 13:05:23 -07001057 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001058 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1059 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001060 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001061 if (res < 0) {
1062 return res;
1063 }
1064
1065 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1066 }
1067 }
1068 } finally {
1069 Binder.restoreCallingIdentity(origId);
1070 }
1071
1072 return ActivityManager.START_SUCCESS;
1073 }
1074
Craig Mautner2420ead2013-04-01 17:13:20 -07001075 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001076 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001077 throws RemoteException {
1078
1079 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001080 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001081 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001082
1083 // schedule launch ticks to collect information about slow apps.
1084 r.startLaunchTickingLocked();
1085
1086 // Have the window manager re-evaluate the orientation of
1087 // the screen based on the new activity order. Note that
1088 // as a result of this, it can call back into the activity
1089 // manager with a new orientation. We don't care about that,
1090 // because the activity is not currently running so we are
1091 // just restarting it anyway.
1092 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001093 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001094 mService.mConfiguration,
1095 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1096 mService.updateConfigurationLocked(config, r, false, false);
1097 }
1098
1099 r.app = app;
1100 app.waitingToKill = null;
1101 r.launchCount++;
1102 r.lastLaunchTime = SystemClock.uptimeMillis();
1103
1104 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1105
1106 int idx = app.activities.indexOf(r);
1107 if (idx < 0) {
1108 app.activities.add(r);
1109 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001110 mService.updateLruProcessLocked(app, true, null);
1111 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001112
1113 final ActivityStack stack = r.task.stack;
1114 try {
1115 if (app.thread == null) {
1116 throw new RemoteException();
1117 }
1118 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001119 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001120 if (andResume) {
1121 results = r.results;
1122 newIntents = r.newIntents;
1123 }
1124 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1125 + " icicle=" + r.icicle
1126 + " with results=" + results + " newIntents=" + newIntents
1127 + " andResume=" + andResume);
1128 if (andResume) {
1129 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1130 r.userId, System.identityHashCode(r),
1131 r.task.taskId, r.shortComponentName);
1132 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001133 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001134 // Home process is the root process of the task.
1135 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001136 }
1137 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1138 r.sleeping = false;
1139 r.forceNewConfig = false;
1140 mService.showAskCompatModeDialogLocked(r);
1141 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1142 String profileFile = null;
1143 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001144 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1145 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1146 mService.mProfileProc = app;
1147 profileFile = mService.mProfileFile;
1148 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001149 }
1150 }
1151 app.hasShownUi = true;
1152 app.pendingUiClean = true;
1153 if (profileFd != null) {
1154 try {
1155 profileFd = profileFd.dup();
1156 } catch (IOException e) {
1157 if (profileFd != null) {
1158 try {
1159 profileFd.close();
1160 } catch (IOException o) {
1161 }
1162 profileFd = null;
1163 }
1164 }
1165 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001166
Jeff Hao1b012d32014-08-20 10:35:34 -07001167 ProfilerInfo profilerInfo = profileFile != null
1168 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1169 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001170 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001171 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001172 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001173 r.compat, r.launchedFromPackage, r.task.voiceInteractor, app.repProcState,
1174 r.icicle, r.persistentState, results, newIntents, !andResume,
1175 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001176
1177 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1178 // This may be a heavy-weight process! Note that the package
1179 // manager will ensure that only activity can run in the main
1180 // process of the .apk, which is the only thing that will be
1181 // considered heavy-weight.
1182 if (app.processName.equals(app.info.packageName)) {
1183 if (mService.mHeavyWeightProcess != null
1184 && mService.mHeavyWeightProcess != app) {
1185 Slog.w(TAG, "Starting new heavy weight process " + app
1186 + " when already running "
1187 + mService.mHeavyWeightProcess);
1188 }
1189 mService.mHeavyWeightProcess = app;
1190 Message msg = mService.mHandler.obtainMessage(
1191 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1192 msg.obj = r;
1193 mService.mHandler.sendMessage(msg);
1194 }
1195 }
1196
1197 } catch (RemoteException e) {
1198 if (r.launchFailed) {
1199 // This is the second time we failed -- finish activity
1200 // and give up.
1201 Slog.e(TAG, "Second failure launching "
1202 + r.intent.getComponent().flattenToShortString()
1203 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001204 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001205 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1206 "2nd-crash", false);
1207 return false;
1208 }
1209
1210 // This is the first time we failed -- restart process and
1211 // retry.
1212 app.activities.remove(r);
1213 throw e;
1214 }
1215
1216 r.launchFailed = false;
1217 if (stack.updateLRUListLocked(r)) {
1218 Slog.w(TAG, "Activity " + r
1219 + " being launched, but already in LRU list");
1220 }
1221
1222 if (andResume) {
1223 // As part of the process of launching, ActivityThread also performs
1224 // a resume.
1225 stack.minimalResumeActivityLocked(r);
1226 } else {
1227 // This activity is not starting in the resumed state... which
1228 // should look like we asked it to pause+stop (but remain visible),
1229 // and it has done so and reported back the current icicle and
1230 // other state.
1231 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1232 + " (starting in stopped state)");
1233 r.state = ActivityState.STOPPED;
1234 r.stopped = true;
1235 }
1236
1237 // Launch the new version setup screen if needed. We do this -after-
1238 // launching the initial activity (that is, home), so that it can have
1239 // a chance to initialize itself while in the background, making the
1240 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001241 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001242 mService.startSetupActivityLocked();
1243 }
1244
Dianne Hackborn465fa392014-09-14 14:21:18 -07001245 // Update any services we are bound to that might care about whether
1246 // their client may have activities.
1247 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1248
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 return true;
1250 }
1251
Craig Mautnere79d42682013-04-01 19:01:53 -07001252 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001253 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001254 // Is this activity's application already running?
1255 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001256 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001257
1258 r.task.stack.setLaunchTime(r);
1259
1260 if (app != null && app.thread != null) {
1261 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001262 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1263 || !"android".equals(r.info.packageName)) {
1264 // Don't add this if it is a platform component that is marked
1265 // to run in multiple processes, because this is actually
1266 // part of the framework so doesn't make sense to track as a
1267 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001268 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1269 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001270 }
George Mount2c92c972014-03-20 09:38:23 -07001271 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001272 return;
1273 } catch (RemoteException e) {
1274 Slog.w(TAG, "Exception when starting activity "
1275 + r.intent.getComponent().flattenToShortString(), e);
1276 }
1277
1278 // If a dead object exception was thrown -- fall through to
1279 // restart the application.
1280 }
1281
1282 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001283 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001284 }
1285
Craig Mautner6170f732013-04-02 13:05:23 -07001286 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001287 Intent intent, String resolvedType, ActivityInfo aInfo,
1288 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1289 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001290 int callingPid, int callingUid, String callingPackage,
1291 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001292 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1293 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001294 int err = ActivityManager.START_SUCCESS;
1295
1296 ProcessRecord callerApp = null;
1297 if (caller != null) {
1298 callerApp = mService.getRecordForAppLocked(caller);
1299 if (callerApp != null) {
1300 callingPid = callerApp.pid;
1301 callingUid = callerApp.info.uid;
1302 } else {
1303 Slog.w(TAG, "Unable to find app for caller " + caller
1304 + " (pid=" + callingPid + ") when starting: "
1305 + intent.toString());
1306 err = ActivityManager.START_PERMISSION_DENIED;
1307 }
1308 }
1309
1310 if (err == ActivityManager.START_SUCCESS) {
1311 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1312 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001313 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001314 + " on display " + (container == null ? (mFocusedStack == null ?
1315 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1316 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1317 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001318 }
1319
1320 ActivityRecord sourceRecord = null;
1321 ActivityRecord resultRecord = null;
1322 if (resultTo != null) {
1323 sourceRecord = isInAnyStackLocked(resultTo);
1324 if (DEBUG_RESULTS) Slog.v(
1325 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1326 if (sourceRecord != null) {
1327 if (requestCode >= 0 && !sourceRecord.finishing) {
1328 resultRecord = sourceRecord;
1329 }
1330 }
1331 }
Craig Mautner6170f732013-04-02 13:05:23 -07001332
Dianne Hackborn91097de2014-04-04 18:02:06 -07001333 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001334
Dianne Hackborn95465202014-09-15 16:21:55 -07001335 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001336 // Transfer the result target from the source activity to the new
1337 // one being started, including any failures.
1338 if (requestCode >= 0) {
1339 ActivityOptions.abort(options);
1340 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1341 }
1342 resultRecord = sourceRecord.resultTo;
1343 resultWho = sourceRecord.resultWho;
1344 requestCode = sourceRecord.requestCode;
1345 sourceRecord.resultTo = null;
1346 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001347 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001348 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001349 if (sourceRecord.launchedFromUid == callingUid) {
1350 // The new activity is being launched from the same uid as the previous
1351 // activity in the flow, and asking to forward its result back to the
1352 // previous. In this case the activity is serving as a trampoline between
1353 // the two, so we also want to update its launchedFromPackage to be the
1354 // same as the previous activity. Note that this is safe, since we know
1355 // these two packages come from the same uid; the caller could just as
1356 // well have supplied that same package name itself. This specifially
1357 // deals with the case of an intent picker/chooser being launched in the app
1358 // flow to redirect to an activity picked by the user, where we want the final
1359 // activity to consider it to have been launched by the previous app activity.
1360 callingPackage = sourceRecord.launchedFromPackage;
1361 }
Craig Mautner6170f732013-04-02 13:05:23 -07001362 }
1363
1364 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1365 // We couldn't find a class that can handle the given Intent.
1366 // That's the end of that!
1367 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1368 }
1369
1370 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1371 // We couldn't find the specific class specified in the Intent.
1372 // Also the end of the line.
1373 err = ActivityManager.START_CLASS_NOT_FOUND;
1374 }
1375
Dianne Hackborn91097de2014-04-04 18:02:06 -07001376 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1377 && sourceRecord.task.voiceSession != null) {
1378 // If this activity is being launched as part of a voice session, we need
1379 // to ensure that it is safe to do so. If the upcoming activity will also
1380 // be part of the voice session, we can only launch it if it has explicitly
1381 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001382 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001383 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1384 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001385 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1386 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001387 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1388 }
1389 } catch (RemoteException e) {
1390 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1391 }
1392 }
1393 }
1394
1395 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1396 // If the caller is starting a new voice session, just make sure the target
1397 // is actually allowing it to run this way.
1398 try {
1399 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1400 intent, resolvedType)) {
1401 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1402 }
1403 } catch (RemoteException e) {
1404 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1405 }
1406 }
1407
louis_changcd5d1982014-08-01 10:09:08 +08001408 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1409
Craig Mautner6170f732013-04-02 13:05:23 -07001410 if (err != ActivityManager.START_SUCCESS) {
1411 if (resultRecord != null) {
1412 resultStack.sendActivityResultLocked(-1,
1413 resultRecord, resultWho, requestCode,
1414 Activity.RESULT_CANCELED, null);
1415 }
Craig Mautner6170f732013-04-02 13:05:23 -07001416 ActivityOptions.abort(options);
1417 return err;
1418 }
1419
1420 final int startAnyPerm = mService.checkPermission(
1421 START_ANY_ACTIVITY, callingPid, callingUid);
1422 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1423 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1424 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1425 if (resultRecord != null) {
1426 resultStack.sendActivityResultLocked(-1,
1427 resultRecord, resultWho, requestCode,
1428 Activity.RESULT_CANCELED, null);
1429 }
Craig Mautner6170f732013-04-02 13:05:23 -07001430 String msg;
1431 if (!aInfo.exported) {
1432 msg = "Permission Denial: starting " + intent.toString()
1433 + " from " + callerApp + " (pid=" + callingPid
1434 + ", uid=" + callingUid + ")"
1435 + " not exported from uid " + aInfo.applicationInfo.uid;
1436 } else {
1437 msg = "Permission Denial: starting " + intent.toString()
1438 + " from " + callerApp + " (pid=" + callingPid
1439 + ", uid=" + callingUid + ")"
1440 + " requires " + aInfo.permission;
1441 }
1442 Slog.w(TAG, msg);
1443 throw new SecurityException(msg);
1444 }
1445
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001446 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001447 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001448
Craig Mautner6170f732013-04-02 13:05:23 -07001449 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001450 try {
1451 // The Intent we give to the watcher has the extra data
1452 // stripped off, since it can contain private information.
1453 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001454 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001455 aInfo.applicationInfo.packageName);
1456 } catch (RemoteException e) {
1457 mService.mController = null;
1458 }
Ben Gruver5e207332013-04-03 17:41:37 -07001459 }
Craig Mautner6170f732013-04-02 13:05:23 -07001460
Ben Gruver5e207332013-04-03 17:41:37 -07001461 if (abort) {
1462 if (resultRecord != null) {
1463 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001464 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001465 }
Ben Gruver5e207332013-04-03 17:41:37 -07001466 // We pretend to the caller that it was really started, but
1467 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001468 ActivityOptions.abort(options);
1469 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001470 }
1471
1472 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001473 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001474 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001475 if (outActivity != null) {
1476 outActivity[0] = r;
1477 }
1478
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001479 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001480 if (voiceSession == null && (stack.mResumedActivity == null
1481 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001482 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1483 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001484 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001485 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001486 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001487 ActivityOptions.abort(options);
1488 return ActivityManager.START_SWITCHES_CANCELED;
1489 }
1490 }
1491
1492 if (mService.mDidAppSwitch) {
1493 // This is the second allowed switch since we stopped switches,
1494 // so now just generally allow switches. Use case: user presses
1495 // home (switches disabled, switch to home, mDidAppSwitch now true);
1496 // user taps a home icon (coming from home so allowed, we hit here
1497 // and now allow anyone to switch again).
1498 mService.mAppSwitchesAllowedTime = 0;
1499 } else {
1500 mService.mDidAppSwitch = true;
1501 }
1502
Craig Mautneree36c772014-07-16 14:56:05 -07001503 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001504
Dianne Hackborn91097de2014-04-04 18:02:06 -07001505 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001506 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001507
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001508 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001509 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001510 // activity start, but we are not actually doing an activity
1511 // switch... just dismiss the keyguard now, because we
1512 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001513 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001514 }
1515 return err;
1516 }
1517
Craig Mautner1b4bf852014-05-26 15:06:32 -07001518 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001519 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001520
1521 // On leanback only devices we should keep all activities in the same stack.
1522 if (!mLeanbackOnlyDevice &&
1523 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001524 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001525 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001526 if (taskStack.isOnHomeDisplay()) {
1527 if (mFocusedStack != taskStack) {
1528 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1529 "focused stack to r=" + r + " task=" + task);
1530 mFocusedStack = taskStack;
1531 } else {
1532 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1533 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1534 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001535 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001536 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001537 }
1538
Craig Mautnere0a38842013-12-16 16:14:02 -08001539 final ActivityContainer container = r.mInitialActivityContainer;
1540 if (container != null) {
1541 // The first time put it on the desired stack, after this put on task stack.
1542 r.mInitialActivityContainer = null;
1543 return container.mStack;
1544 }
1545
Craig Mautner1b4bf852014-05-26 15:06:32 -07001546 if (mFocusedStack != mHomeStack && (!newTask ||
1547 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001548 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1549 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1550 return mFocusedStack;
1551 }
1552
Craig Mautnere0a38842013-12-16 16:14:02 -08001553 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1554 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1555 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1556 if (!stack.isHomeStack()) {
1557 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1558 "adjustStackFocus: Setting focused stack=" + stack);
1559 mFocusedStack = stack;
1560 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001561 }
1562 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001563
Craig Mautner4a1cb222013-12-04 16:14:06 -08001564 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001565 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001566 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1567 " stackId=" + stackId);
1568 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001569 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001570 }
1571 return mHomeStack;
1572 }
1573
Craig Mautner29219d92013-04-16 20:19:12 -07001574 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001575 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001576 final TaskRecord task = r.task;
1577 boolean isHomeActivity = !r.isApplicationActivity();
1578 if (!isHomeActivity && task != null) {
1579 isHomeActivity = !task.isApplicationTask();
1580 }
1581 if (!isHomeActivity && task != null) {
1582 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1583 isHomeActivity = parent != null && parent.isHomeActivity();
1584 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001585 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001586 }
1587 }
1588
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001589 final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001590 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001591 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001592 final Intent intent = r.intent;
1593 final int callingUid = r.launchedFromUid;
1594
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001595 // In some flows in to this function, we retrieve the task record and hold on to it
1596 // without a lock before calling back in to here... so the task at this point may
1597 // not actually be in recents. Check for that, and if it isn't in recents just
1598 // consider it invalid.
1599 if (inTask != null && !inTask.inRecents) {
1600 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1601 inTask = null;
1602 }
1603
Craig Mautnerad400af2014-08-13 16:41:36 -07001604 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001605 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1606 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001607
Craig Mautnera228ae92014-07-09 05:44:55 -07001608 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001609 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001610 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001611 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1612 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1613 "\"singleInstance\" or \"singleTask\"");
1614 launchFlags &=
1615 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1616 } else {
1617 switch (r.info.documentLaunchMode) {
1618 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1619 break;
1620 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1621 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1622 break;
1623 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1624 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1625 break;
1626 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1627 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1628 break;
1629 }
1630 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001631
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001632 final boolean launchTaskBehind = r.mLaunchTaskBehind
1633 && !launchSingleTask && !launchSingleInstance
1634 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001635
1636 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1637 // For whatever reason this activity is being launched into a new
1638 // task... yet the caller has requested a result back. Well, that
1639 // is pretty messed up, so instead immediately send back a cancel
1640 // and let the new task continue launched as normal without a
1641 // dependency on its originator.
1642 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1643 r.resultTo.task.stack.sendActivityResultLocked(-1,
1644 r.resultTo, r.resultWho, r.requestCode,
1645 Activity.RESULT_CANCELED, null);
1646 r.resultTo = null;
1647 }
1648
1649 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1650 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1651 }
1652
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001653 // If we are actually going to launch in to a new task, there are some cases where
1654 // we further want to do multiple task.
1655 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1656 if (launchTaskBehind
1657 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1658 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1659 }
1660 }
1661
Craig Mautner8849a5e2013-04-02 16:41:03 -07001662 // We'll invoke onUserLeaving before onPause only if the launching
1663 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001664 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001665 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001666
1667 // If the caller has asked not to resume at this point, we make note
1668 // of this in the record so that we can skip it when trying to find
1669 // the top running activity.
1670 if (!doResume) {
1671 r.delayedResume = true;
1672 }
1673
Craig Mautnera254cd72014-05-25 16:47:39 -07001674 ActivityRecord notTop =
1675 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001676
1677 // If the onlyIfNeeded flag is set, then we can do this if the activity
1678 // being launched is the same as the one making the call... or, as
1679 // a special case, if we do not know the caller then we count the
1680 // current top activity as the caller.
1681 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1682 ActivityRecord checkedCaller = sourceRecord;
1683 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001684 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001685 }
1686 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1687 // Caller is not the same as launcher, so always needed.
1688 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1689 }
1690 }
1691
Craig Mautner8849a5e2013-04-02 16:41:03 -07001692 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001693 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001694
1695 // If the caller is not coming from another activity, but has given us an
1696 // explicit task into which they would like us to launch the new activity,
1697 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001698 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1699 final Intent baseIntent = inTask.getBaseIntent();
1700 final ActivityRecord root = inTask.getRootActivity();
1701 if (baseIntent == null) {
1702 ActivityOptions.abort(options);
1703 throw new IllegalArgumentException("Launching into task without base intent: "
1704 + inTask);
1705 }
1706
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001707 // If this task is empty, then we are adding the first activity -- it
1708 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001709 if (launchSingleInstance || launchSingleTask) {
1710 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1711 ActivityOptions.abort(options);
1712 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1713 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001714 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001715 if (root != null) {
1716 ActivityOptions.abort(options);
1717 throw new IllegalArgumentException("Caller with inTask " + inTask
1718 + " has root " + root + " but target is singleInstance/Task");
1719 }
1720 }
1721
1722 // If task is empty, then adopt the interesting intent launch flags in to the
1723 // activity being started.
1724 if (root == null) {
1725 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1726 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1727 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1728 launchFlags = (launchFlags&~flagsOfInterest)
1729 | (baseIntent.getFlags()&flagsOfInterest);
1730 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001731 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001732 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001733
Dianne Hackborn962d5352014-08-29 16:27:40 -07001734 // If the task is not empty and the caller is asking to start it as the root
1735 // of a new task, then we don't actually want to start this on the task. We
1736 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001737 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001738 addingToTask = false;
1739
1740 } else {
1741 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001742 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001743
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001744 reuseTask = inTask;
1745 } else {
1746 inTask = null;
1747 }
1748
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001749 if (inTask == null) {
1750 if (sourceRecord == null) {
1751 // This activity is not being started from another... in this
1752 // case we -always- start a new task.
1753 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1754 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1755 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1756 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1757 }
1758 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1759 // The original activity who is starting us is running as a single
1760 // instance... this new activity it is starting must go on its
1761 // own task.
1762 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1763 } else if (launchSingleInstance || launchSingleTask) {
1764 // The activity being started is a single instance... it always
1765 // gets launched into its own task.
1766 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1767 }
1768 }
1769
1770 ActivityInfo newTaskInfo = null;
1771 Intent newTaskIntent = null;
1772 ActivityStack sourceStack;
1773 if (sourceRecord != null) {
1774 if (sourceRecord.finishing) {
1775 // If the source is finishing, we can't further count it as our source. This
1776 // is because the task it is associated with may now be empty and on its way out,
1777 // so we don't want to blindly throw it in to that task. Instead we will take
1778 // the NEW_TASK flow and try to find a task for it. But save the task information
1779 // so it can be used when creating the new task.
1780 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1781 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1782 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1783 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1784 newTaskInfo = sourceRecord.info;
1785 newTaskIntent = sourceRecord.task.intent;
1786 }
1787 sourceRecord = null;
1788 sourceStack = null;
1789 } else {
1790 sourceStack = sourceRecord.task.stack;
1791 }
1792 } else {
1793 sourceStack = null;
1794 }
1795
1796 boolean movedHome = false;
1797 ActivityStack targetStack;
1798
1799 intent.setFlags(launchFlags);
1800
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001801 // We may want to try to place the new activity in to an existing task. We always
1802 // do this if the target activity is singleTask or singleInstance; we will also do
1803 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1804 // us to still place it in a new task: multi task, always doc mode, or being asked to
1805 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001806 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1807 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001808 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001809 // If bring to front is requested, and no result is requested and we have not
1810 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001811 // we can find a task that was started with this same
1812 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001813 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001814 // See if there is a task to bring to the front. If this is
1815 // a SINGLE_INSTANCE activity, there can be one and only one
1816 // instance of it in the history, and it is always in its own
1817 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001818 ActivityRecord intentActivity = !launchSingleInstance ?
1819 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001820 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001821 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001822 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001823 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001824 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1825 }
Craig Mautner29219d92013-04-16 20:19:12 -07001826 if (r.task == null) {
1827 r.task = intentActivity.task;
1828 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001829 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001830 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001831 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1832 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001833 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001834 if (intentActivity.task.intent == null) {
1835 // This task was started because of movement of
1836 // the activity based on affinity... now that we
1837 // are actually launching it, we can assign the
1838 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001839 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001840 }
1841 // If the target task is not in the front, then we need
1842 // to bring it to the front... except... well, with
1843 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1844 // to have the same behavior as if a new instance was
1845 // being started, which means not bringing it to the front
1846 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001847 final ActivityStack lastStack = getLastStack();
1848 ActivityRecord curTop = lastStack == null?
1849 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001850 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001851 if (curTop != null && (curTop.task != intentActivity.task ||
1852 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001853 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001854 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1855 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001856 // We really do want to push this one into the
1857 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001858 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001859 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1860 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001861 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001862 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001863 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001864 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1865 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001866 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001867 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001868 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001869 options = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001870 movedToFront = true;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001871 }
1872 }
1873 // If the caller has requested that the target task be
1874 // reset, then do so.
1875 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1876 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1877 }
1878 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1879 // We don't need to start a new activity, and
1880 // the client said not to do anything if that
1881 // is the case, so this is it! And for paranoia, make
1882 // sure we have correctly resumed the top activity.
1883 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001884 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001885
1886 // Make sure to notify Keyguard as well if we are not running an app
1887 // transition later.
1888 if (!movedToFront) {
1889 notifyActivityDrawnForKeyguard();
1890 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001891 } else {
1892 ActivityOptions.abort(options);
1893 }
1894 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1895 }
1896 if ((launchFlags &
1897 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1898 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1899 // The caller has requested to completely replace any
1900 // existing task with its new activity. Well that should
1901 // not be too hard...
1902 reuseTask = intentActivity.task;
1903 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001904 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001905 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001906 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001907 // In this situation we want to remove all activities
1908 // from the task up to the one being started. In most
1909 // cases this means we are resetting the task to its
1910 // initial state.
1911 ActivityRecord top =
1912 intentActivity.task.performClearTaskLocked(r, launchFlags);
1913 if (top != null) {
1914 if (top.frontOfTask) {
1915 // Activity aliases may mean we use different
1916 // intents for the top activity, so make sure
1917 // the task now has the identity of the new
1918 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001919 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920 }
1921 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1922 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001923 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001924 } else {
1925 // A special case: we need to
1926 // start the activity because it is not currently
1927 // running, and the caller has asked to clear the
1928 // current task to have this activity at the top.
1929 addingToTask = true;
1930 // Now pretend like this activity is being started
1931 // by the top of its task, so it is put in the
1932 // right place.
1933 sourceRecord = intentActivity;
1934 }
1935 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1936 // In this case the top activity on the task is the
1937 // same as the one being launched, so we take that
1938 // as a request to bring the task to the foreground.
1939 // If the top activity in the task is the root
1940 // activity, deliver this new intent to it if it
1941 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001942 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001943 && intentActivity.realActivity.equals(r.realActivity)) {
1944 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1945 intentActivity.task);
1946 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001947 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001948 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001949 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
1950 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001951 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1952 // In this case we are launching the root activity
1953 // of the task, but with a different intent. We
1954 // should start a new instance on top.
1955 addingToTask = true;
1956 sourceRecord = intentActivity;
1957 }
1958 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1959 // In this case an activity is being launched in to an
1960 // existing task, without resetting that task. This
1961 // is typically the situation of launching an activity
1962 // from a notification or shortcut. We want to place
1963 // the new activity on top of the current task.
1964 addingToTask = true;
1965 sourceRecord = intentActivity;
1966 } else if (!intentActivity.task.rootWasReset) {
1967 // In this case we are launching in to an existing task
1968 // that has not yet been started from its front door.
1969 // The current task has been brought to the front.
1970 // Ideally, we'd probably like to place this new task
1971 // at the bottom of its stack, but that's a little hard
1972 // to do with the current organization of the code so
1973 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07001974 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001975 }
1976 if (!addingToTask && reuseTask == null) {
1977 // We didn't do anything... but it was needed (a.k.a., client
1978 // don't use that intent!) And for paranoia, make
1979 // sure we have correctly resumed the top activity.
1980 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001981 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001982 if (!movedToFront) {
1983 // Make sure to notify Keyguard as well if we are not running an app
1984 // transition later.
1985 notifyActivityDrawnForKeyguard();
1986 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001987 } else {
1988 ActivityOptions.abort(options);
1989 }
1990 return ActivityManager.START_TASK_TO_FRONT;
1991 }
1992 }
1993 }
1994 }
1995
1996 //String uri = r.intent.toURI();
1997 //Intent intent2 = new Intent(uri);
1998 //Slog.i(TAG, "Given intent: " + r.intent);
1999 //Slog.i(TAG, "URI is: " + uri);
2000 //Slog.i(TAG, "To intent: " + intent2);
2001
2002 if (r.packageName != null) {
2003 // If the activity being launched is the same as the one currently
2004 // at the top, then we need to check if it should only be launched
2005 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002006 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002007 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002008 if (top != null && r.resultTo == null) {
2009 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2010 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002011 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002012 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002013 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2014 top.task);
2015 // For paranoia, make sure we have correctly
2016 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002017 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002018 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002019 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002020 }
2021 ActivityOptions.abort(options);
2022 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2023 // We don't need to start a new activity, and
2024 // the client said not to do anything if that
2025 // is the case, so this is it!
2026 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2027 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002028 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002029 return ActivityManager.START_DELIVERED_TO_TOP;
2030 }
2031 }
2032 }
2033 }
2034
2035 } else {
2036 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002037 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2038 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002039 }
2040 ActivityOptions.abort(options);
2041 return ActivityManager.START_CLASS_NOT_FOUND;
2042 }
2043
2044 boolean newTask = false;
2045 boolean keepCurTransition = false;
2046
Craig Mautnerbb742462014-07-07 15:28:55 -07002047 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002048 sourceRecord.task : null;
2049
Craig Mautner8849a5e2013-04-02 16:41:03 -07002050 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002051 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002052 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002053 if (isLockTaskModeViolation(reuseTask)) {
2054 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2055 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2056 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002057 newTask = true;
2058 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerbb742462014-07-07 15:28:55 -07002059 if (!launchTaskBehind) {
2060 targetStack.moveToFront();
2061 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002062 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002063 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2064 newTaskInfo != null ? newTaskInfo : r.info,
2065 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002066 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2067 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002068 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2069 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002070 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002071 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002072 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002073 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002074 if ((launchFlags &
2075 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2076 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2077 // Caller wants to appear on home activity, so before starting
2078 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002079 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002080 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002081 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002082 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002083 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002084 if (isLockTaskModeViolation(sourceTask)) {
2085 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2086 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2087 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002088 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07002089 targetStack.moveToFront();
Craig Mautner737fae22014-10-15 12:52:10 -07002090 final TaskRecord topTask = targetStack.topTask();
2091 if (topTask != sourceTask) {
2092 targetStack.moveTaskToFrontLocked(sourceTask, r, options);
Craig Mautner737fae22014-10-15 12:52:10 -07002093 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002094 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002095 // In this case, we are adding the activity to an existing
2096 // task, but the caller has asked to clear that task if the
2097 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002098 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002099 keepCurTransition = true;
2100 if (top != null) {
2101 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002102 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002103 // For paranoia, make sure we have correctly
2104 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002105 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002106 if (doResume) {
2107 targetStack.resumeTopActivityLocked(null);
2108 }
2109 ActivityOptions.abort(options);
2110 return ActivityManager.START_DELIVERED_TO_TOP;
2111 }
2112 } else if (!addingToTask &&
2113 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2114 // In this case, we are launching an activity in our own task
2115 // that may already be running somewhere in the history, and
2116 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002117 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002118 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002119 final TaskRecord task = top.task;
2120 task.moveActivityToFrontLocked(top);
2121 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002122 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002123 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002124 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002125 if (doResume) {
2126 targetStack.resumeTopActivityLocked(null);
2127 }
2128 return ActivityManager.START_DELIVERED_TO_TOP;
2129 }
2130 }
2131 // An existing activity is starting this new activity, so we want
2132 // to keep the new one in the same task as the one that is starting
2133 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002134 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002135 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002136 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002137
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002138 } else if (inTask != null) {
2139 // The calling is asking that the new activity be started in an explicit
2140 // task it has provided to us.
2141 if (isLockTaskModeViolation(inTask)) {
2142 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2143 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2144 }
2145 targetStack = inTask.stack;
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002146 targetStack.moveTaskToFrontLocked(inTask, r, options);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002147
2148 // Check whether we should actually launch the new activity in to the task,
2149 // or just reuse the current activity on top.
2150 ActivityRecord top = inTask.getTopActivity();
2151 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2152 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2153 || launchSingleTop || launchSingleTask) {
2154 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2155 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2156 // We don't need to start a new activity, and
2157 // the client said not to do anything if that
2158 // is the case, so this is it!
2159 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2160 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002161 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002162 return ActivityManager.START_DELIVERED_TO_TOP;
2163 }
2164 }
2165
Dianne Hackborn962d5352014-08-29 16:27:40 -07002166 if (!addingToTask) {
2167 // We don't actually want to have this activity added to the task, so just
2168 // stop here but still tell the caller that we consumed the intent.
2169 ActivityOptions.abort(options);
2170 return ActivityManager.START_TASK_TO_FRONT;
2171 }
2172
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002173 r.setTask(inTask, null);
2174 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2175 + " in explicit task " + r.task);
2176
Craig Mautner8849a5e2013-04-02 16:41:03 -07002177 } else {
2178 // This not being started from an existing activity, and not part
2179 // of a new task... just put it in the top task, though these days
2180 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002181 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07002182 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07002183 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002184 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002185 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002186 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002187 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2188 + " in new guessed " + r.task);
2189 }
2190
2191 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002192 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002193
Craig Mautner76e2a762014-06-24 09:05:43 -07002194 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2195 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2196 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002197 if (newTask) {
2198 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2199 }
2200 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002201 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002202 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002203 if (!launchTaskBehind) {
2204 // Don't set focus on an activity that's going to the back.
2205 mService.setFocusedActivityLocked(r);
2206 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002207 return ActivityManager.START_SUCCESS;
2208 }
2209
Craig Mautneree36c772014-07-16 14:56:05 -07002210 final void doPendingActivityLaunchesLocked(boolean doResume) {
2211 while (!mPendingActivityLaunches.isEmpty()) {
2212 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002213 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002214 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002215 }
2216 }
2217
Craig Mautner7f13ed32014-07-28 14:00:35 -07002218 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002219 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2220 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002221 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002222 mPendingActivityLaunches.remove(palNdx);
2223 }
2224 }
2225 }
2226
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002227 void acquireLaunchWakelock() {
2228 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2229 throw new IllegalStateException("Calling must be system uid");
2230 }
2231 mLaunchingActivity.acquire();
2232 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2233 // To be safe, don't allow the wake lock to be held for too long.
2234 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2235 }
2236 }
2237
Craig Mautnerf3333272013-04-22 10:55:53 -07002238 // Checked.
2239 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2240 Configuration config) {
2241 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2242
Craig Mautnerf3333272013-04-22 10:55:53 -07002243 ArrayList<ActivityRecord> stops = null;
2244 ArrayList<ActivityRecord> finishes = null;
2245 ArrayList<UserStartedState> startingUsers = null;
2246 int NS = 0;
2247 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002248 boolean booting = false;
2249 boolean enableScreen = false;
2250 boolean activityRemoved = false;
2251
2252 ActivityRecord r = ActivityRecord.forToken(token);
2253 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002254 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2255 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002256 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2257 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002258 if (fromTimeout) {
2259 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002260 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002261
2262 // This is a hack to semi-deal with a race condition
2263 // in the client where it can be constructed with a
2264 // newer configuration from when we asked it to launch.
2265 // We'll update with whatever configuration it now says
2266 // it used to launch.
2267 if (config != null) {
2268 r.configuration = config;
2269 }
2270
2271 // We are now idle. If someone is waiting for a thumbnail from
2272 // us, we can now deliver.
2273 r.idle = true;
2274
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002275 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002276 if (isFrontStack(r.task.stack) || fromTimeout) {
2277 booting = mService.mBooting;
2278 mService.mBooting = false;
2279 if (!mService.mBooted) {
2280 mService.mBooted = true;
2281 enableScreen = true;
2282 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002283 }
2284 }
2285
2286 if (allResumedActivitiesIdle()) {
2287 if (r != null) {
2288 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002289 }
2290
2291 if (mLaunchingActivity.isHeld()) {
2292 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2293 if (VALIDATE_WAKE_LOCK_CALLER &&
2294 Binder.getCallingUid() != Process.myUid()) {
2295 throw new IllegalStateException("Calling must be system uid");
2296 }
2297 mLaunchingActivity.release();
2298 }
2299 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002300 }
2301
2302 // Atomically retrieve all of the other things to do.
2303 stops = processStoppingActivitiesLocked(true);
2304 NS = stops != null ? stops.size() : 0;
2305 if ((NF=mFinishingActivities.size()) > 0) {
2306 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2307 mFinishingActivities.clear();
2308 }
2309
Craig Mautnerf3333272013-04-22 10:55:53 -07002310 if (mStartingUsers.size() > 0) {
2311 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2312 mStartingUsers.clear();
2313 }
2314
Craig Mautnerf3333272013-04-22 10:55:53 -07002315 // Stop any activities that are scheduled to do so but have been
2316 // waiting for the next one to start.
2317 for (int i = 0; i < NS; i++) {
2318 r = stops.get(i);
2319 final ActivityStack stack = r.task.stack;
2320 if (r.finishing) {
2321 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2322 } else {
2323 stack.stopActivityLocked(r);
2324 }
2325 }
2326
2327 // Finish any activities that are scheduled to do so but have been
2328 // waiting for the next one to start.
2329 for (int i = 0; i < NF; i++) {
2330 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002331 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002332 }
2333
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002334 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002335 // Complete user switch
2336 if (startingUsers != null) {
2337 for (int i = 0; i < startingUsers.size(); i++) {
2338 mService.finishUserSwitch(startingUsers.get(i));
2339 }
2340 }
2341 // Complete starting up of background users
2342 if (mStartingBackgroundUsers.size() > 0) {
2343 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2344 mStartingBackgroundUsers.clear();
2345 for (int i = 0; i < startingUsers.size(); i++) {
2346 mService.finishUserBoot(startingUsers.get(i));
2347 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002348 }
2349 }
2350
2351 mService.trimApplications();
2352 //dump();
2353 //mWindowManager.dump();
2354
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002355 if (booting || enableScreen) {
2356 mService.postFinishBooting(booting, enableScreen);
Craig Mautnerf3333272013-04-22 10:55:53 -07002357 }
2358
2359 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002360 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002361 }
2362
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002363 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002364 }
2365
Craig Mautner8e569572013-10-11 17:36:59 -07002366 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002367 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002368 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2369 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002370 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2371 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2372 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002373 }
Craig Mautner19091252013-10-05 00:03:53 -07002374 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002375 }
2376
2377 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002378 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2379 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002380 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2381 stacks.get(stackNdx).closeSystemDialogsLocked();
2382 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002383 }
2384 }
2385
Craig Mautner93529a42013-10-04 15:03:13 -07002386 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002387 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002388 }
2389
Craig Mautner8d341ef2013-03-26 09:03:27 -07002390 /**
2391 * @return true if some activity was finished (or would have finished if doit were true).
2392 */
2393 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2394 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002395 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2396 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002397 final int numStacks = stacks.size();
2398 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2399 final ActivityStack stack = stacks.get(stackNdx);
2400 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2401 didSomething = true;
2402 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002403 }
2404 }
2405 return didSomething;
2406 }
2407
Dianne Hackborna413dc02013-07-12 12:02:55 -07002408 void updatePreviousProcessLocked(ActivityRecord r) {
2409 // Now that this process has stopped, we may want to consider
2410 // it to be the previous app to try to keep around in case
2411 // the user wants to return to it.
2412
2413 // First, found out what is currently the foreground app, so that
2414 // we don't blow away the previous app if this activity is being
2415 // hosted by the process that is actually still the foreground.
2416 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002417 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2418 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002419 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2420 final ActivityStack stack = stacks.get(stackNdx);
2421 if (isFrontStack(stack)) {
2422 if (stack.mResumedActivity != null) {
2423 fgApp = stack.mResumedActivity.app;
2424 } else if (stack.mPausingActivity != null) {
2425 fgApp = stack.mPausingActivity.app;
2426 }
2427 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002428 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002429 }
2430 }
2431
2432 // Now set this one as the previous process, only if that really
2433 // makes sense to.
2434 if (r.app != null && fgApp != null && r.app != fgApp
2435 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002436 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002437 mService.mPreviousProcess = r.app;
2438 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2439 }
2440 }
2441
Craig Mautner05d29032013-05-03 13:40:13 -07002442 boolean resumeTopActivitiesLocked() {
2443 return resumeTopActivitiesLocked(null, null, null);
2444 }
2445
2446 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2447 Bundle targetOptions) {
2448 if (targetStack == null) {
2449 targetStack = getFocusedStack();
2450 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002451 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002452 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002453 if (isFrontStack(targetStack)) {
2454 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2455 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002456 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2457 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002458 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2459 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002460 if (stack == targetStack) {
2461 // Already started above.
2462 continue;
2463 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002464 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002465 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002466 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002467 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002468 }
Craig Mautner05d29032013-05-03 13:40:13 -07002469 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002470 }
2471
Todd Kennedy539db512014-12-15 09:57:55 -08002472 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002473 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2474 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002475 final int numStacks = stacks.size();
2476 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2477 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002478 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002479 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002480 }
2481 }
2482
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002483 void finishVoiceTask(IVoiceInteractionSession session) {
2484 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2485 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2486 final int numStacks = stacks.size();
2487 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2488 final ActivityStack stack = stacks.get(stackNdx);
2489 stack.finishVoiceTask(session);
2490 }
2491 }
2492 }
2493
Craig Mautneraea74a52014-03-08 14:23:10 -08002494 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2495 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2496 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002497 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002498 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2499 // Caller wants the home activity moved with it. To accomplish this,
2500 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002501 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002502 }
2503 task.stack.moveTaskToFrontLocked(task, null, options);
2504 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2505 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002506 }
2507
Craig Mautner967212c2013-04-13 21:10:58 -07002508 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002509 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2510 if (activityContainer != null) {
2511 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002512 }
2513 return null;
2514 }
2515
Craig Mautner967212c2013-04-13 21:10:58 -07002516 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002517 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002518 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2519 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002520 }
2521 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002522 }
2523
Craig Mautner4a1cb222013-12-04 16:14:06 -08002524 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002525 ActivityRecord homeActivity = getHomeActivity();
2526 if (homeActivity != null) {
2527 return homeActivity.appToken;
2528 }
2529 return null;
2530 }
2531
2532 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002533 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2534 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2535 final TaskRecord task = tasks.get(taskNdx);
2536 if (task.isHomeTask()) {
2537 final ArrayList<ActivityRecord> activities = task.mActivities;
2538 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2539 final ActivityRecord r = activities.get(activityNdx);
2540 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002541 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002542 }
2543 }
2544 }
2545 }
2546 return null;
2547 }
2548
Todd Kennedyca4d8422015-01-15 15:19:22 -08002549 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002550 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002551 ActivityContainer activityContainer =
2552 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002553 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002554 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002555 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002556 return activityContainer;
2557 }
2558
Craig Mautner34b73df2014-01-12 21:11:08 -08002559 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002560 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2561 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2562 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002563 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2564 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002565 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002566 }
2567 }
2568
Craig Mautner95da1082014-02-24 17:54:35 -08002569 void deleteActivityContainer(IActivityContainer container) {
2570 ActivityContainer activityContainer = (ActivityContainer)container;
2571 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002572 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2573 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002574 final int stackId = activityContainer.mStackId;
2575 mActivityContainers.remove(stackId);
2576 mWindowManager.removeStack(stackId);
2577 }
2578 }
2579
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002580 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002581 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2582 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002583 return -1;
2584 }
2585
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002586 ActivityContainer activityContainer = new ActivityContainer(stackId);
2587 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002588 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002589 return stackId;
2590 }
2591
2592 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002593 while (true) {
2594 if (++mLastStackId <= HOME_STACK_ID) {
2595 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002596 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002597 if (getStack(mLastStackId) == null) {
2598 break;
2599 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002600 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002601 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002602 }
2603
Wale Ogunwale7de05352014-12-12 15:21:33 -08002604 private boolean restoreRecentTaskLocked(TaskRecord task) {
2605 ActivityStack stack = null;
2606 // Determine stack to restore task to.
2607 if (mLeanbackOnlyDevice) {
2608 // There is only one stack for lean back devices.
2609 stack = mHomeStack;
2610 } else {
2611 // Look for the top stack on the home display that isn't the home stack.
2612 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2613 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2614 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
2615 if (!tmpStack.isHomeStack()) {
2616 stack = tmpStack;
2617 break;
2618 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002619 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002620 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002621
2622 if (stack == null) {
2623 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2624 // before an application stack is created...Go ahead and create one on the default
2625 // display.
2626 stack = getStack(createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY));
2627 if (DEBUG_RECENTS)
2628 Slog.v(TAG, "Created stack=" + stack + " for recents restoration.");
2629 }
2630
2631 if (stack == null) {
2632 // What does this mean??? Not sure how we would get here...
2633 if (DEBUG_RECENTS)
2634 Slog.v(TAG, "Unable to find/create stack to restore recent task=" + task);
2635 return false;
2636 }
2637
2638 stack.addTask(task, false, false);
2639 if (DEBUG_RECENTS)
2640 Slog.v(TAG, "Added restored task=" + task + " to stack=" + stack);
2641 final ArrayList<ActivityRecord> activities = task.mActivities;
2642 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2643 final ActivityRecord r = activities.get(activityNdx);
2644 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2645 r.info.screenOrientation, r.fullscreen,
2646 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2647 r.userId, r.info.configChanges, task.voiceSession != null,
2648 r.mLaunchTaskBehind);
2649 }
2650 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002651 }
2652
Craig Mautner8d341ef2013-03-26 09:03:27 -07002653 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002654 final TaskRecord task = anyTaskForIdLocked(taskId);
2655 if (task == null) {
2656 return;
2657 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002658 final ActivityStack stack = getStack(stackId);
2659 if (stack == null) {
2660 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2661 return;
2662 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002663 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002664 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002665 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002666 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002667 }
2668
Craig Mautnerac6f8432013-07-17 13:24:59 -07002669 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002670 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002671 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2672 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002673 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2674 final ActivityStack stack = stacks.get(stackNdx);
2675 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002676 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2677 continue;
2678 }
2679 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2680 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2681 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002682 continue;
2683 }
2684 final ActivityRecord ar = stack.findTaskLocked(r);
2685 if (ar != null) {
2686 return ar;
2687 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002688 }
2689 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002690 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002691 return null;
2692 }
2693
2694 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002695 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2696 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002697 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2698 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2699 if (ar != null) {
2700 return ar;
2701 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002702 }
2703 }
2704 return null;
2705 }
2706
Craig Mautner8d341ef2013-03-26 09:03:27 -07002707 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002708 scheduleSleepTimeout();
2709 if (!mGoingToSleep.isHeld()) {
2710 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002711 if (mLaunchingActivity.isHeld()) {
2712 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2713 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002714 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002715 mLaunchingActivity.release();
2716 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002717 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002718 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002719 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002720 }
2721
2722 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002723 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002724
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002725 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002726 final long endTime = System.currentTimeMillis() + timeout;
2727 while (true) {
2728 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002729 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2730 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002731 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2732 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2733 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002734 }
2735 if (cantShutdown) {
2736 long timeRemaining = endTime - System.currentTimeMillis();
2737 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002738 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002739 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002740 } catch (InterruptedException e) {
2741 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002742 } else {
2743 Slog.w(TAG, "Activity manager shutdown timed out");
2744 timedout = true;
2745 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002746 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002747 } else {
2748 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002749 }
2750 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002751
2752 // Force checkReadyForSleep to complete.
2753 mSleepTimeout = true;
2754 checkReadyForSleepLocked();
2755
Craig Mautner8d341ef2013-03-26 09:03:27 -07002756 return timedout;
2757 }
2758
2759 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002760 removeSleepTimeouts();
2761 if (mGoingToSleep.isHeld()) {
2762 mGoingToSleep.release();
2763 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002764 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2765 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002766 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2767 final ActivityStack stack = stacks.get(stackNdx);
2768 stack.awakeFromSleepingLocked();
2769 if (isFrontStack(stack)) {
2770 resumeTopActivitiesLocked();
2771 }
Craig Mautner5314a402013-09-26 12:40:16 -07002772 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002773 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002774 mGoingToSleepActivities.clear();
2775 }
2776
2777 void activitySleptLocked(ActivityRecord r) {
2778 mGoingToSleepActivities.remove(r);
2779 checkReadyForSleepLocked();
2780 }
2781
2782 void checkReadyForSleepLocked() {
2783 if (!mService.isSleepingOrShuttingDown()) {
2784 // Do not care.
2785 return;
2786 }
2787
2788 if (!mSleepTimeout) {
2789 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002790 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2791 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002792 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2793 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2794 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002795 }
2796
2797 if (mStoppingActivities.size() > 0) {
2798 // Still need to tell some activities to stop; can't sleep yet.
2799 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2800 + mStoppingActivities.size() + " activities");
2801 scheduleIdleLocked();
2802 dontSleep = true;
2803 }
2804
2805 if (mGoingToSleepActivities.size() > 0) {
2806 // Still need to tell some activities to sleep; can't sleep yet.
2807 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2808 + mGoingToSleepActivities.size() + " activities");
2809 dontSleep = true;
2810 }
2811
2812 if (dontSleep) {
2813 return;
2814 }
2815 }
2816
Craig Mautnere0a38842013-12-16 16:14:02 -08002817 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2818 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002819 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2820 stacks.get(stackNdx).goToSleep();
2821 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002822 }
2823
2824 removeSleepTimeouts();
2825
2826 if (mGoingToSleep.isHeld()) {
2827 mGoingToSleep.release();
2828 }
2829 if (mService.mShuttingDown) {
2830 mService.notifyAll();
2831 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002832 }
2833
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002834 boolean reportResumedActivityLocked(ActivityRecord r) {
2835 final ActivityStack stack = r.task.stack;
2836 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002837 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002838 }
2839 if (allResumedActivitiesComplete()) {
2840 ensureActivitiesVisibleLocked(null, 0);
2841 mWindowManager.executeAppTransition();
2842 return true;
2843 }
2844 return false;
2845 }
2846
Craig Mautner8d341ef2013-03-26 09:03:27 -07002847 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002848 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2849 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002850 final int numStacks = stacks.size();
2851 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2852 final ActivityStack stack = stacks.get(stackNdx);
2853 stack.handleAppCrashLocked(app);
2854 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002855 }
2856 }
2857
Jose Lima4b6c6692014-08-12 17:41:12 -07002858 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002859 final ActivityStack stack = r.task.stack;
2860 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002861 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2862 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002863 return false;
2864 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002865 final boolean isVisible = stack.hasVisibleBehindActivity();
2866 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2867 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002868
2869 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002870 if (top == null || top == r || (visible == isVisible)) {
2871 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2872 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002873 return true;
2874 }
2875
2876 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002877 if (visible && top.fullscreen) {
2878 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07002879 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07002880 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07002881 " top.app.thread=" + top.app.thread);
2882 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002883 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2884 // Only the activity set as currently visible behind should actively reset its
2885 // visible behind state.
2886 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
2887 + visible + " stack.getVisibleBehindActivity()=" +
2888 stack.getVisibleBehindActivity() + " r=" + r);
2889 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002890 }
2891
Jose Lima4b6c6692014-08-12 17:41:12 -07002892 stack.setVisibleBehindActivity(visible ? r : null);
2893 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002894 // Make the activity immediately above r opaque.
2895 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2896 if (next != null) {
2897 mService.convertFromTranslucent(next.appToken);
2898 }
2899 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002900 if (top.app != null && top.app.thread != null) {
2901 // Notify the top app of the change.
2902 try {
2903 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2904 } catch (RemoteException e) {
2905 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002906 }
2907 return true;
2908 }
2909
Craig Mautnerbb742462014-07-07 15:28:55 -07002910 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2911 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2912 r.mLaunchTaskBehind = false;
2913 final TaskRecord task = r.task;
2914 task.setLastThumbnail(task.stack.screenshotActivities(r));
2915 mService.addRecentTaskLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002916 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002917 mWindowManager.setAppVisibility(r.appToken, false);
2918 }
2919
2920 void scheduleLaunchTaskBehindComplete(IBinder token) {
2921 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2922 }
2923
Craig Mautnerde4ef022013-04-07 19:01:33 -07002924 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002925 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002926 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2927 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002928 final int topStackNdx = stacks.size() - 1;
2929 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2930 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002931 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002932 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002933 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002934 }
2935
2936 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002937 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2938 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002939 final int numStacks = stacks.size();
2940 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2941 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002942 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002943 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002944 }
2945 }
2946
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002947 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2948 // Examine all activities currently running in the process.
2949 TaskRecord firstTask = null;
2950 // Tasks is non-null only if two or more tasks are found.
2951 ArraySet<TaskRecord> tasks = null;
2952 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
2953 for (int i=0; i<app.activities.size(); i++) {
2954 ActivityRecord r = app.activities.get(i);
2955 // First, if we find an activity that is in the process of being destroyed,
2956 // then we just aren't going to do anything for now; we want things to settle
2957 // down before we try to prune more activities.
2958 if (r.finishing || r.state == ActivityState.DESTROYING
2959 || r.state == ActivityState.DESTROYED) {
2960 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
2961 return;
2962 }
2963 // Don't consider any activies that are currently not in a state where they
2964 // can be destroyed.
2965 if (r.visible || !r.stopped || !r.haveState
2966 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
2967 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
2968 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
2969 continue;
2970 }
2971 if (r.task != null) {
2972 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
2973 + " from " + r);
2974 if (firstTask == null) {
2975 firstTask = r.task;
2976 } else if (firstTask != r.task) {
2977 if (tasks == null) {
2978 tasks = new ArraySet<>();
2979 tasks.add(firstTask);
2980 }
2981 tasks.add(r.task);
2982 }
2983 }
2984 }
2985 if (tasks == null) {
2986 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
2987 return;
2988 }
2989 // If we have activities in multiple tasks that are in a position to be destroyed,
2990 // let's iterate through the tasks and release the oldest one.
2991 final int numDisplays = mActivityDisplays.size();
2992 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2993 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2994 // Step through all stacks starting from behind, to hit the oldest things first.
2995 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2996 final ActivityStack stack = stacks.get(stackNdx);
2997 // Try to release activities in this stack; if we manage to, we are done.
2998 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2999 return;
3000 }
3001 }
3002 }
3003 }
3004
Craig Mautner8d341ef2013-03-26 09:03:27 -07003005 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003006 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
3007 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003008 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003009
Craig Mautner858d8a62013-04-23 17:08:34 -07003010 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003011 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3012 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003013 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003014 final ActivityStack stack = stacks.get(stackNdx);
3015 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003016 TaskRecord task = stack.topTask();
3017 if (task != null) {
3018 mWindowManager.moveTaskToTop(task.taskId);
3019 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003020 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003021 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003022
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003023 ActivityStack stack = getStack(restoreStackId);
3024 if (stack == null) {
3025 stack = mHomeStack;
3026 }
3027 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003028 if (stack.isOnHomeDisplay()) {
3029 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003030 TaskRecord task = stack.topTask();
3031 if (task != null) {
3032 mWindowManager.moveTaskToTop(task.taskId);
3033 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003034 } else {
3035 // Stack was moved to another display while user was swapped out.
Craig Mautner84984fa2014-06-19 11:19:20 -07003036 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003037 }
Craig Mautner93529a42013-10-04 15:03:13 -07003038 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003039 }
3040
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003041 /**
3042 * Add background users to send boot completed events to.
3043 * @param userId The user being started in the background
3044 * @param uss The state object for the user.
3045 */
3046 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3047 mStartingBackgroundUsers.add(uss);
3048 }
3049
Craig Mautnerde4ef022013-04-07 19:01:33 -07003050 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
3051 int N = mStoppingActivities.size();
3052 if (N <= 0) return null;
3053
3054 ArrayList<ActivityRecord> stops = null;
3055
3056 final boolean nowVisible = allResumedActivitiesVisible();
3057 for (int i=0; i<N; i++) {
3058 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07003059 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07003060 + nowVisible + " waitingVisible=" + s.waitingVisible
3061 + " finishing=" + s.finishing);
3062 if (s.waitingVisible && nowVisible) {
3063 mWaitingVisibleActivities.remove(s);
3064 s.waitingVisible = false;
3065 if (s.finishing) {
3066 // If this activity is finishing, it is sitting on top of
3067 // everyone else but we now know it is no longer needed...
3068 // so get rid of it. Otherwise, we need to go through the
3069 // normal flow and hide it once we determine that it is
3070 // hidden by the activities in front of it.
3071 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003072 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003073 }
3074 }
3075 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
3076 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3077 if (stops == null) {
3078 stops = new ArrayList<ActivityRecord>();
3079 }
3080 stops.add(s);
3081 mStoppingActivities.remove(i);
3082 N--;
3083 i--;
3084 }
3085 }
3086
3087 return stops;
3088 }
3089
Craig Mautnercf910b02013-04-23 11:23:27 -07003090 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003091 // FIXME
3092/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3093 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003094 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003095 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003096 if (isFrontStack(stack)) {
3097 if (r == null) {
3098 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3099 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003100 final ActivityRecord pausing = stack.mPausingActivity;
3101 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003102 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003103 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003104 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003105 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003106 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003107 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003108 }
3109 }
3110 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003111 final ActivityRecord resumed = stack.mResumedActivity;
3112 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003113 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003114 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003115 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003116 if (r != null && (state == ActivityState.INITIALIZING
3117 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003118 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003119 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003120 }
3121 }
3122 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003123*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003124 }
3125
Craig Mautner27084302013-03-25 08:05:25 -07003126 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003127 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003128 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003129 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3130 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3131 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003132 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003133 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003134
Craig Mautner20e72272013-04-01 13:45:53 -07003135 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003136 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003137 }
3138
Dianne Hackborn390517b2013-05-30 15:03:32 -07003139 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3140 boolean needSep, String prefix) {
3141 if (activity != null) {
3142 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3143 if (needSep) {
3144 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003145 }
3146 pw.print(prefix);
3147 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003148 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003149 }
3150 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003151 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003152 }
3153
Craig Mautner8d341ef2013-03-26 09:03:27 -07003154 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3155 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003156 boolean printed = false;
3157 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003158 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3159 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003160 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003161 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003162 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003163 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003164 final ActivityStack stack = stacks.get(stackNdx);
3165 StringBuilder stackHeader = new StringBuilder(128);
3166 stackHeader.append(" Stack #");
3167 stackHeader.append(stack.mStackId);
3168 stackHeader.append(":");
3169 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3170 needSep, stackHeader.toString());
3171 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3172 !dumpAll, false, dumpPackage, true,
3173 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003174
Craig Mautner4a1cb222013-12-04 16:14:06 -08003175 needSep = printed;
3176 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3177 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003178 if (pr) {
3179 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003180 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003181 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003182 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3183 " mResumedActivity: ");
3184 if (pr) {
3185 printed = true;
3186 needSep = false;
3187 }
3188 if (dumpAll) {
3189 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3190 " mLastPausedActivity: ");
3191 if (pr) {
3192 printed = true;
3193 needSep = true;
3194 }
3195 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3196 needSep, " mLastNoHistoryActivity: ");
3197 }
3198 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003199 }
3200 }
3201
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003202 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3203 false, dumpPackage, true, " Activities waiting to finish:", null);
3204 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3205 false, dumpPackage, true, " Activities waiting to stop:", null);
3206 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3207 false, dumpPackage, true, " Activities waiting for another to become visible:",
3208 null);
3209 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3210 false, dumpPackage, true, " Activities waiting to sleep:", null);
3211 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3212 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003213
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003214 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003215 }
3216
Dianne Hackborn390517b2013-05-30 15:03:32 -07003217 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003218 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003219 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003220 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003221 String innerPrefix = null;
3222 String[] args = null;
3223 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003224 for (int i=list.size()-1; i>=0; i--) {
3225 final ActivityRecord r = list.get(i);
3226 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3227 continue;
3228 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003229 if (innerPrefix == null) {
3230 innerPrefix = prefix + " ";
3231 args = new String[0];
3232 }
3233 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003234 final boolean full = !brief && (complete || !r.isInHistory());
3235 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003236 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003237 needNL = false;
3238 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003239 if (header1 != null) {
3240 pw.println(header1);
3241 header1 = null;
3242 }
3243 if (header2 != null) {
3244 pw.println(header2);
3245 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003246 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003247 if (lastTask != r.task) {
3248 lastTask = r.task;
3249 pw.print(prefix);
3250 pw.print(full ? "* " : " ");
3251 pw.println(lastTask);
3252 if (full) {
3253 lastTask.dump(pw, prefix + " ");
3254 } else if (complete) {
3255 // Complete + brief == give a summary. Isn't that obvious?!?
3256 if (lastTask.intent != null) {
3257 pw.print(prefix); pw.print(" ");
3258 pw.println(lastTask.intent.toInsecureStringWithClip());
3259 }
3260 }
3261 }
3262 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3263 pw.print(" #"); pw.print(i); pw.print(": ");
3264 pw.println(r);
3265 if (full) {
3266 r.dump(pw, innerPrefix);
3267 } else if (complete) {
3268 // Complete + brief == give a summary. Isn't that obvious?!?
3269 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3270 if (r.app != null) {
3271 pw.print(innerPrefix); pw.println(r.app);
3272 }
3273 }
3274 if (client && r.app != null && r.app.thread != null) {
3275 // flush anything that is already in the PrintWriter since the thread is going
3276 // to write to the file descriptor directly
3277 pw.flush();
3278 try {
3279 TransferPipe tp = new TransferPipe();
3280 try {
3281 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3282 r.appToken, innerPrefix, args);
3283 // Short timeout, since blocking here can
3284 // deadlock with the application.
3285 tp.go(fd, 2000);
3286 } finally {
3287 tp.kill();
3288 }
3289 } catch (IOException e) {
3290 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3291 } catch (RemoteException e) {
3292 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3293 }
3294 needNL = true;
3295 }
3296 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003297 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003298 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003299
Craig Mautnerf3333272013-04-22 10:55:53 -07003300 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003301 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003302 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3303 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003304 }
3305
3306 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003307 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003308 }
3309
3310 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003311 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003312 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3313 }
3314
Craig Mautner05d29032013-05-03 13:40:13 -07003315 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003316 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3317 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3318 }
Craig Mautner05d29032013-05-03 13:40:13 -07003319 }
3320
Craig Mautner0eea92c2013-05-16 13:35:39 -07003321 void removeSleepTimeouts() {
3322 mSleepTimeout = false;
3323 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3324 }
3325
3326 final void scheduleSleepTimeout() {
3327 removeSleepTimeouts();
3328 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3329 }
3330
Craig Mautner4a1cb222013-12-04 16:14:06 -08003331 @Override
3332 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003333 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003334 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3335 }
3336
3337 @Override
3338 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003339 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003340 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3341 }
3342
3343 @Override
3344 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003345 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003346 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3347 }
3348
3349 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003350 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003351 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003352 newDisplay = mActivityDisplays.get(displayId) == null;
3353 if (newDisplay) {
3354 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003355 if (activityDisplay.mDisplay == null) {
3356 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3357 return;
3358 }
Craig Mautner4504de52013-12-20 09:06:56 -08003359 mActivityDisplays.put(displayId, activityDisplay);
3360 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003361 }
Craig Mautner4504de52013-12-20 09:06:56 -08003362 if (newDisplay) {
3363 mWindowManager.onDisplayAdded(displayId);
3364 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003365 }
3366
3367 public void handleDisplayRemovedLocked(int displayId) {
3368 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003369 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3370 if (activityDisplay != null) {
3371 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003372 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003373 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003374 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003375 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003376 }
3377 }
3378 mWindowManager.onDisplayRemoved(displayId);
3379 }
3380
3381 public void handleDisplayChangedLocked(int displayId) {
3382 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003383 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3384 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003385 // TODO: Update the bounds.
3386 }
3387 }
3388 mWindowManager.onDisplayChanged(displayId);
3389 }
3390
3391 StackInfo getStackInfo(ActivityStack stack) {
3392 StackInfo info = new StackInfo();
3393 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3394 info.displayId = Display.DEFAULT_DISPLAY;
3395 info.stackId = stack.mStackId;
3396
3397 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3398 final int numTasks = tasks.size();
3399 int[] taskIds = new int[numTasks];
3400 String[] taskNames = new String[numTasks];
3401 for (int i = 0; i < numTasks; ++i) {
3402 final TaskRecord task = tasks.get(i);
3403 taskIds[i] = task.taskId;
3404 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3405 : task.realActivity != null ? task.realActivity.flattenToString()
3406 : task.getTopActivity() != null ? task.getTopActivity().packageName
3407 : "unknown";
3408 }
3409 info.taskIds = taskIds;
3410 info.taskNames = taskNames;
3411 return info;
3412 }
3413
3414 StackInfo getStackInfoLocked(int stackId) {
3415 ActivityStack stack = getStack(stackId);
3416 if (stack != null) {
3417 return getStackInfo(stack);
3418 }
3419 return null;
3420 }
3421
3422 ArrayList<StackInfo> getAllStackInfosLocked() {
3423 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003424 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3425 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003426 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3427 list.add(getStackInfo(stacks.get(ndx)));
3428 }
3429 }
3430 return list;
3431 }
3432
Jason Monka8f569c2014-07-07 12:15:21 -04003433 void showLockTaskToast() {
3434 mLockTaskNotify.showToast(mLockTaskIsLocked);
3435 }
3436
3437 void setLockTaskModeLocked(TaskRecord task, boolean isLocked) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003438 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003439 // Take out of lock task mode if necessary
3440 if (mLockTaskModeTask != null) {
3441 final Message lockTaskMsg = Message.obtain();
3442 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3443 lockTaskMsg.what = LOCK_TASK_END_MSG;
3444 mLockTaskModeTask = null;
3445 mHandler.sendMessage(lockTaskMsg);
3446 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003447 return;
3448 }
3449 if (isLockTaskModeViolation(task)) {
3450 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3451 return;
3452 }
3453 mLockTaskModeTask = task;
3454 findTaskToMoveToFrontLocked(task, 0, null);
3455 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003456
3457 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003458 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3459 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003460 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003461 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003462 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003463 }
3464
3465 boolean isLockTaskModeViolation(TaskRecord task) {
3466 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3467 }
3468
3469 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3470 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003471 final Message lockTaskMsg = Message.obtain();
3472 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3473 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003474 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003475 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003476 }
3477 }
3478
3479 boolean isInLockTaskMode() {
3480 return mLockTaskModeTask != null;
3481 }
3482
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003483 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003484
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003485 public ActivityStackSupervisorHandler(Looper looper) {
3486 super(looper);
3487 }
3488
Craig Mautnerf3333272013-04-22 10:55:53 -07003489 void activityIdleInternal(ActivityRecord r) {
3490 synchronized (mService) {
3491 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3492 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003493 }
3494
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003495 @Override
3496 public void handleMessage(Message msg) {
3497 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003498 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003499 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003500 if (mService.mDidDexOpt) {
3501 mService.mDidDexOpt = false;
3502 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3503 nmsg.obj = msg.obj;
3504 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3505 return;
3506 }
3507 // We don't at this point know if the activity is fullscreen,
3508 // so we need to be conservative and assume it isn't.
3509 activityIdleInternal((ActivityRecord)msg.obj);
3510 } break;
3511 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003512 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003513 activityIdleInternal((ActivityRecord)msg.obj);
3514 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003515 case RESUME_TOP_ACTIVITY_MSG: {
3516 synchronized (mService) {
3517 resumeTopActivitiesLocked();
3518 }
3519 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003520 case SLEEP_TIMEOUT_MSG: {
3521 synchronized (mService) {
3522 if (mService.isSleepingOrShuttingDown()) {
3523 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3524 mSleepTimeout = true;
3525 checkReadyForSleepLocked();
3526 }
3527 }
3528 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003529 case LAUNCH_TIMEOUT_MSG: {
3530 if (mService.mDidDexOpt) {
3531 mService.mDidDexOpt = false;
3532 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3533 return;
3534 }
3535 synchronized (mService) {
3536 if (mLaunchingActivity.isHeld()) {
3537 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3538 if (VALIDATE_WAKE_LOCK_CALLER
3539 && Binder.getCallingUid() != Process.myUid()) {
3540 throw new IllegalStateException("Calling must be system uid");
3541 }
3542 mLaunchingActivity.release();
3543 }
3544 }
3545 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003546 case HANDLE_DISPLAY_ADDED: {
3547 handleDisplayAddedLocked(msg.arg1);
3548 } break;
3549 case HANDLE_DISPLAY_CHANGED: {
3550 handleDisplayChangedLocked(msg.arg1);
3551 } break;
3552 case HANDLE_DISPLAY_REMOVED: {
3553 handleDisplayRemovedLocked(msg.arg1);
3554 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003555 case CONTAINER_CALLBACK_VISIBILITY: {
3556 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003557 final IActivityContainerCallback callback = container.mCallback;
3558 if (callback != null) {
3559 try {
3560 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3561 } catch (RemoteException e) {
3562 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003563 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003564 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003565 case LOCK_TASK_START_MSG: {
3566 // When lock task starts, we disable the status bars.
3567 try {
Jason Monk62515be2014-05-21 16:06:19 -04003568 if (mLockTaskNotify == null) {
3569 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003570 }
Jason Monk62515be2014-05-21 16:06:19 -04003571 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003572 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003573 if (getStatusBarService() != null) {
3574 int flags =
3575 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003576 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003577 flags ^= StatusBarManager.DISABLE_HOME
3578 | StatusBarManager.DISABLE_RECENT;
3579 }
3580 getStatusBarService().disable(flags, mToken,
3581 mService.mContext.getPackageName());
3582 }
3583 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003584 if (getDevicePolicyManager() != null) {
3585 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003586 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003587 }
justinzhang5286d3f2014-05-12 17:06:01 -04003588 } catch (RemoteException ex) {
3589 throw new RuntimeException(ex);
3590 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003591 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003592 case LOCK_TASK_END_MSG: {
3593 // When lock task ends, we enable the status bars.
3594 try {
Jason Monk62515be2014-05-21 16:06:19 -04003595 if (getStatusBarService() != null) {
3596 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3597 mService.mContext.getPackageName());
3598 }
3599 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003600 if (getDevicePolicyManager() != null) {
3601 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3602 msg.arg1);
3603 }
Jason Monk62515be2014-05-21 16:06:19 -04003604 if (mLockTaskNotify == null) {
3605 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3606 }
3607 mLockTaskNotify.show(false);
3608 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003609 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003610 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003611 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003612 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003613 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003614 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003615 new LockPatternUtils(mService.mContext)
3616 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003617 }
3618 } catch (SettingNotFoundException e) {
3619 // No setting, don't lock.
3620 }
justinzhang5286d3f2014-05-12 17:06:01 -04003621 } catch (RemoteException ex) {
3622 throw new RuntimeException(ex);
3623 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003624 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003625 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3626 final ActivityContainer container = (ActivityContainer) msg.obj;
3627 final IActivityContainerCallback callback = container.mCallback;
3628 if (callback != null) {
3629 try {
3630 callback.onAllActivitiesComplete(container.asBinder());
3631 } catch (RemoteException e) {
3632 }
3633 }
3634 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003635 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3636 synchronized (mService) {
3637 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3638 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003639 final ActivityContainer container = (ActivityContainer) msg.obj;
3640 container.mStack.finishAllActivitiesLocked(true);
3641 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003642 }
3643 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003644 case LAUNCH_TASK_BEHIND_COMPLETE: {
3645 synchronized (mService) {
3646 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3647 if (r != null) {
3648 handleLaunchTaskBehindCompleteLocked(r);
3649 }
3650 }
3651 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003652 }
3653 }
3654 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003655
Ying Wangb081a592014-04-22 15:20:16 -07003656 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003657 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003658 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003659 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003660 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003661 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003662 ActivityRecord mParentActivity = null;
3663 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003664
Craig Mautnere3a00d72014-04-16 08:31:19 -07003665 boolean mVisible = true;
3666
Craig Mautner4a1cb222013-12-04 16:14:06 -08003667 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003668 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003669
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003670 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3671 final static int CONTAINER_STATE_NO_SURFACE = 1;
3672 final static int CONTAINER_STATE_FINISHING = 2;
3673 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3674
3675 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003676 synchronized (mService) {
3677 mStackId = stackId;
3678 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003679 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003680 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003681 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003682 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003683
Craig Mautnere0a38842013-12-16 16:14:02 -08003684 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003685 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3686 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003687 mActivityDisplay = activityDisplay;
3688 mStack.mDisplayId = activityDisplay.mDisplayId;
3689 mStack.mStacks = activityDisplay.mStacks;
3690
3691 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003692 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003693 }
3694
3695 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003696 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003697 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003698 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3699 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003700 return;
3701 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003702 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003703 }
3704 }
3705
3706 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003707 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003708 synchronized (mService) {
3709 if (mActivityDisplay != null) {
3710 return mActivityDisplay.mDisplayId;
3711 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003712 }
3713 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003714 }
3715
Jeff Brownca9bc702014-02-11 14:32:56 -08003716 @Override
3717 public boolean injectEvent(InputEvent event) {
3718 final long origId = Binder.clearCallingIdentity();
3719 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003720 synchronized (mService) {
3721 if (mActivityDisplay != null) {
3722 return mInputManagerInternal.injectInputEvent(event,
3723 mActivityDisplay.mDisplayId,
3724 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3725 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003726 }
3727 return false;
3728 } finally {
3729 Binder.restoreCallingIdentity(origId);
3730 }
3731 }
3732
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003733 @Override
3734 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003735 synchronized (mService) {
3736 if (mContainerState == CONTAINER_STATE_FINISHING) {
3737 return;
3738 }
3739 mContainerState = CONTAINER_STATE_FINISHING;
3740
3741 final Message msg =
3742 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003743 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003744
3745 long origId = Binder.clearCallingIdentity();
3746 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003747 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003748 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003749 } finally {
3750 Binder.restoreCallingIdentity(origId);
3751 }
3752 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003753 }
3754
Craig Mautner60257702014-09-17 15:02:33 -07003755 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003756 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3757 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003758 if (mActivityDisplay != null) {
3759 mActivityDisplay.detachActivitiesLocked(mStack);
3760 mActivityDisplay = null;
3761 mStack.mDisplayId = -1;
3762 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003763 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003764 }
3765 }
3766
3767 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003768 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003769 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003770 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003771 Binder.getCallingUid(), mCurrentUser, false,
3772 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003773 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003774 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003775 String mimeType = intent.getType();
3776 if (mimeType == null && intent.getData() != null
3777 && "content".equals(intent.getData().getScheme())) {
3778 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3779 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003780 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0,
3781 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003782 }
3783
3784 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003785 public final int startActivityIntentSender(IIntentSender intentSender) {
3786 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3787
3788 if (!(intentSender instanceof PendingIntentRecord)) {
3789 throw new IllegalArgumentException("Bad PendingIntent object");
3790 }
3791
3792 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003793 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003794 }
3795
Craig Mautner247ab652014-04-25 10:09:00 -07003796 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3797 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003798 Binder.getCallingUid(), mCurrentUser, false,
3799 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003800 if (resolvedType == null) {
3801 resolvedType = intent.getType();
3802 if (resolvedType == null && intent.getData() != null
3803 && "content".equals(intent.getData().getScheme())) {
3804 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3805 }
3806 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003807 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003808 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003809 throw new SecurityException(
3810 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3811 }
3812 }
3813
3814 /** Throw a SecurityException if allowEmbedded is not true */
3815 @Override
3816 public final void checkEmbeddedAllowed(Intent intent) {
3817 checkEmbeddedAllowedInner(intent, null);
3818 }
3819
3820 /** Throw a SecurityException if allowEmbedded is not true */
3821 @Override
3822 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3823 if (!(intentSender instanceof PendingIntentRecord)) {
3824 throw new IllegalArgumentException("Bad PendingIntent object");
3825 }
3826 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3827 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3828 pendingIntent.key.requestResolvedType);
3829 }
3830
Craig Mautnerdf88d732014-01-27 09:21:32 -08003831 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003832 public IBinder asBinder() {
3833 return this;
3834 }
3835
Craig Mautner4504de52013-12-20 09:06:56 -08003836 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003837 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003838 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003839 }
3840
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841 ActivityStackSupervisor getOuter() {
3842 return ActivityStackSupervisor.this;
3843 }
3844
Craig Mautnerd163e752014-06-13 17:18:47 -07003845 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003846 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003847 }
3848
3849 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003850 synchronized (mService) {
3851 if (mActivityDisplay != null) {
3852 mActivityDisplay.getBounds(outBounds);
3853 } else {
3854 outBounds.set(0, 0);
3855 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003856 }
3857 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003858
Craig Mautner6985bad2014-04-21 15:22:06 -07003859 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003860 void setVisible(boolean visible) {
3861 if (mVisible != visible) {
3862 mVisible = visible;
3863 if (mCallback != null) {
3864 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3865 0 /* unused */, this).sendToTarget();
3866 }
3867 }
3868 }
3869
Craig Mautner6985bad2014-04-21 15:22:06 -07003870 void setDrawn() {
3871 }
3872
Craig Mautner1b4bf852014-05-26 15:06:32 -07003873 // You can always start a new task on a regular ActivityStack.
3874 boolean isEligibleForNewTasks() {
3875 return true;
3876 }
3877
Craig Mautnerd163e752014-06-13 17:18:47 -07003878 void onTaskListEmptyLocked() {
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003879 }
3880
Craig Mautner34b73df2014-01-12 21:11:08 -08003881 @Override
3882 public String toString() {
3883 return mIdString + (mActivityDisplay == null ? "N" : "A");
3884 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003885 }
3886
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003887 private class VirtualActivityContainer extends ActivityContainer {
3888 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003889 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003890
3891 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3892 super(getNextStackId());
3893 mParentActivity = parent;
3894 mCallback = callback;
3895 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003896 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003897 }
3898
3899 @Override
3900 public void setSurface(Surface surface, int width, int height, int density) {
3901 super.setSurface(surface, width, height, density);
3902
3903 synchronized (mService) {
3904 final long origId = Binder.clearCallingIdentity();
3905 try {
3906 setSurfaceLocked(surface, width, height, density);
3907 } finally {
3908 Binder.restoreCallingIdentity(origId);
3909 }
3910 }
3911 }
3912
3913 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3914 if (mContainerState == CONTAINER_STATE_FINISHING) {
3915 return;
3916 }
3917 VirtualActivityDisplay virtualActivityDisplay =
3918 (VirtualActivityDisplay) mActivityDisplay;
3919 if (virtualActivityDisplay == null) {
3920 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003921 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003922 mActivityDisplay = virtualActivityDisplay;
3923 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3924 attachToDisplayLocked(virtualActivityDisplay);
3925 }
3926
3927 if (mSurface != null) {
3928 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003929 }
3930
Craig Mautner6985bad2014-04-21 15:22:06 -07003931 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003932 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003933 mStack.resumeTopActivityLocked(null);
3934 } else {
3935 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003936 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003937 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003938 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003939 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003940 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003941
Craig Mautnerd163e752014-06-13 17:18:47 -07003942 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003943
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003944 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3945 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003946 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003947
Craig Mautner6985bad2014-04-21 15:22:06 -07003948 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003949 boolean isAttachedLocked() {
3950 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003951 }
3952
3953 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003954 void setDrawn() {
3955 synchronized (mService) {
3956 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003957 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003958 }
3959 }
3960
Craig Mautner1b4bf852014-05-26 15:06:32 -07003961 // Never start a new task on an ActivityView if it isn't explicitly specified.
3962 @Override
3963 boolean isEligibleForNewTasks() {
3964 return false;
3965 }
3966
Craig Mautner60257702014-09-17 15:02:33 -07003967 void onTaskListEmptyLocked() {
3968 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3969 detachLocked();
3970 deleteActivityContainer(this);
3971 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3972 }
3973
Craig Mautnerd163e752014-06-13 17:18:47 -07003974 private void setSurfaceIfReadyLocked() {
3975 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003976 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3977 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3978 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3979 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3980 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003981 }
3982 }
3983
Craig Mautner4a1cb222013-12-04 16:14:06 -08003984 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3985 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003986 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003987 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003988 int mDisplayId;
3989 Display mDisplay;
3990 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003991
Craig Mautner4a1cb222013-12-04 16:14:06 -08003992 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3993 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003994 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003995
Jose Lima4b6c6692014-08-12 17:41:12 -07003996 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003997
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003998 ActivityDisplay() {
3999 }
Craig Mautner4504de52013-12-20 09:06:56 -08004000
Craig Mautner1a70a162014-09-13 12:09:31 -07004001 // After instantiation, check that mDisplay is not null before using this. The alternative
4002 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004003 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004004 final Display display = mDisplayManager.getDisplay(displayId);
4005 if (display == null) {
4006 return;
4007 }
4008 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004009 }
4010
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004011 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004012 mDisplay = display;
4013 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004014 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004015 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004016
4017 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004018 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4019 + mDisplayId);
4020 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004021 }
4022
Craig Mautnere0a38842013-12-16 16:14:02 -08004023 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004024 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004025 + " from displayId=" + mDisplayId);
4026 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004027 }
4028
4029 void getBounds(Point bounds) {
4030 mDisplay.getDisplayInfo(mDisplayInfo);
4031 bounds.x = mDisplayInfo.appWidth;
4032 bounds.y = mDisplayInfo.appHeight;
4033 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004034
Jose Lima4b6c6692014-08-12 17:41:12 -07004035 void setVisibleBehindActivity(ActivityRecord r) {
4036 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004037 }
4038
Jose Lima4b6c6692014-08-12 17:41:12 -07004039 boolean hasVisibleBehindActivity() {
4040 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004041 }
4042
Craig Mautner34b73df2014-01-12 21:11:08 -08004043 @Override
4044 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004045 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4046 }
4047 }
4048
4049 class VirtualActivityDisplay extends ActivityDisplay {
4050 VirtualDisplay mVirtualDisplay;
4051
Craig Mautner6985bad2014-04-21 15:22:06 -07004052 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004053 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004054 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4055 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4056 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4057 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004058
4059 init(mVirtualDisplay.getDisplay());
4060
4061 mWindowManager.handleDisplayAdded(mDisplayId);
4062 }
4063
4064 void setSurface(Surface surface) {
4065 if (mVirtualDisplay != null) {
4066 mVirtualDisplay.setSurface(surface);
4067 }
4068 }
4069
4070 @Override
4071 void detachActivitiesLocked(ActivityStack stack) {
4072 super.detachActivitiesLocked(stack);
4073 if (mVirtualDisplay != null) {
4074 mVirtualDisplay.release();
4075 mVirtualDisplay = null;
4076 }
4077 }
4078
4079 @Override
4080 public String toString() {
4081 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004082 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004083 }
Jose Lima58e66d62014-05-27 20:00:27 -07004084
4085 private boolean isLeanbackOnlyDevice() {
4086 boolean onLeanbackOnly = false;
4087 try {
4088 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4089 PackageManager.FEATURE_LEANBACK_ONLY);
4090 } catch (RemoteException e) {
4091 // noop
4092 }
4093
4094 return onLeanbackOnly;
4095 }
Craig Mautner27084302013-03-25 08:05:25 -07004096}