blob: 383f297d5bb54a2112291ac0eab289e0eaa5a05d [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
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800169 private final RecentTasks mRecentTasks;
170
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700171 final ActivityStackSupervisorHandler mHandler;
172
173 /** Short cut */
174 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800175 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700176
Craig Mautner8d341ef2013-03-26 09:03:27 -0700177 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700178 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700179
180 /** Task identifier that activities are currently being started in. Incremented each time a
181 * new task is created. */
182 private int mCurTaskId = 0;
183
Craig Mautner2420ead2013-04-01 17:13:20 -0700184 /** The current user */
185 private int mCurrentUser;
186
Craig Mautnere0a38842013-12-16 16:14:02 -0800187 /** The stack containing the launcher app. Assumed to always be attached to
188 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700189 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700190
Craig Mautnere0a38842013-12-16 16:14:02 -0800191 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700192 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700193
Craig Mautner4a1cb222013-12-04 16:14:06 -0800194 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
195 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800196 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800197 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700198
199 /** List of activities that are waiting for a new activity to become visible before completing
200 * whatever operation they are supposed to do. */
201 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
202
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700203 /** List of processes waiting to find out about the next visible activity. */
204 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
205 new ArrayList<IActivityManager.WaitResult>();
206
207 /** List of processes waiting to find out about the next launched activity. */
208 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
209 new ArrayList<IActivityManager.WaitResult>();
210
Craig Mautnerde4ef022013-04-07 19:01:33 -0700211 /** List of activities that are ready to be stopped, but waiting for the next activity to
212 * settle down before doing so. */
213 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
214
Craig Mautnerf3333272013-04-22 10:55:53 -0700215 /** List of activities that are ready to be finished, but waiting for the previous activity to
216 * settle down before doing so. It contains ActivityRecord objects. */
217 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
218
Craig Mautner0eea92c2013-05-16 13:35:39 -0700219 /** List of activities that are in the process of going to sleep. */
220 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
221
Craig Mautnerf3333272013-04-22 10:55:53 -0700222 /** Used on user changes */
223 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
224
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700225 /** Used to queue up any background users being started */
226 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
227
Craig Mautnerde4ef022013-04-07 19:01:33 -0700228 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
229 * is being brought in front of us. */
230 boolean mUserLeaving = false;
231
Craig Mautner0eea92c2013-05-16 13:35:39 -0700232 /** Set when we have taken too long waiting to go to sleep. */
233 boolean mSleepTimeout = false;
234
Jose Lima58e66d62014-05-27 20:00:27 -0700235 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
236 * setWindowManager is called. **/
237 private boolean mLeanbackOnlyDevice;
238
Craig Mautner0eea92c2013-05-16 13:35:39 -0700239 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700240 * We don't want to allow the device to go to sleep while in the process
241 * of launching an activity. This is primarily to allow alarm intent
242 * receivers to launch an activity and get that to run before the device
243 * goes back to sleep.
244 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700245 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700246
247 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700248 * Set when the system is going to sleep, until we have
249 * successfully paused the current activity and released our wake lock.
250 * At that point the system is allowed to actually sleep.
251 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700252 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700253
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700254 /** Stack id of the front stack when user switched, indexed by userId. */
255 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700256
Craig Mautner4504de52013-12-20 09:06:56 -0800257 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800258 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700259 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800260
261 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700262 private final SparseArray<ActivityDisplay> mActivityDisplays =
263 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800264
Jeff Brownca9bc702014-02-11 14:32:56 -0800265 InputManagerInternal mInputManagerInternal;
266
Craig Mautneraea74a52014-03-08 14:23:10 -0800267 /** If non-null then the task specified remains in front and no other tasks may be started
268 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400269 TaskRecord mLockTaskModeTask;
Jason Monka8f569c2014-07-07 12:15:21 -0400270 /** Whether lock task has been entered by an authorized app and cannot
271 * be exited. */
272 private boolean mLockTaskIsLocked;
Jason Monk62515be2014-05-21 16:06:19 -0400273 /**
274 * Notifies the user when entering/exiting lock-task.
275 */
276 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800277
Craig Mautneree36c772014-07-16 14:56:05 -0700278 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
279 = new ArrayList<PendingActivityLaunch>();
280
Craig Mautner42d04db2014-11-06 12:13:23 -0800281 /** Used to keep resumeTopActivityLocked() from being entered recursively */
282 boolean inResumeTopActivity;
283
Craig Mautneree36c772014-07-16 14:56:05 -0700284 /**
285 * Description of a request to start a new activity, which has been held
286 * due to app switches being disabled.
287 */
288 static class PendingActivityLaunch {
289 final ActivityRecord r;
290 final ActivityRecord sourceRecord;
291 final int startFlags;
292 final ActivityStack stack;
293
294 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
295 int _startFlags, ActivityStack _stack) {
296 r = _r;
297 sourceRecord = _sourceRecord;
298 startFlags = _startFlags;
299 stack = _stack;
300 }
301 }
302
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800303 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700304 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800305 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700306 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
307 }
308
309 /**
310 * At the time when the constructor runs, the power manager has not yet been
311 * initialized. So we initialize our wakelocks afterwards.
312 */
313 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800314 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700315 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700316 mLaunchingActivity =
317 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
318 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700319 }
320
justinzhang5286d3f2014-05-12 17:06:01 -0400321 // This function returns a IStatusBarService. The value is from ServiceManager.
322 // getService and is cached.
323 private IStatusBarService getStatusBarService() {
324 synchronized (mService) {
325 if (mStatusBarService == null) {
326 mStatusBarService = IStatusBarService.Stub.asInterface(
327 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
328 if (mStatusBarService == null) {
329 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
330 }
331 }
332 return mStatusBarService;
333 }
334 }
335
Jason Monk35c62a42014-06-17 10:24:47 -0400336 private IDevicePolicyManager getDevicePolicyManager() {
337 synchronized (mService) {
338 if (mDevicePolicyManager == null) {
339 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
340 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
341 if (mDevicePolicyManager == null) {
342 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
343 }
344 }
345 return mDevicePolicyManager;
346 }
347 }
348
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700349 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800350 synchronized (mService) {
351 mWindowManager = wm;
352
353 mDisplayManager =
354 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
355 mDisplayManager.registerDisplayListener(this, null);
356
357 Display[] displays = mDisplayManager.getDisplays();
358 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
359 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800360 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700361 if (activityDisplay.mDisplay == null) {
362 throw new IllegalStateException("Default Display does not exist");
363 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800364 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800365 }
366
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700367 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800368 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800369
370 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700371
372 // Initialize this here, now that we can get a valid reference to PackageManager.
373 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800374 }
Craig Mautner27084302013-03-25 08:05:25 -0700375 }
376
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200377 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700378 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200379 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700380 }
381
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700382 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700384 }
385
Craig Mautnerde4ef022013-04-07 19:01:33 -0700386 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800387 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700388 }
389
Craig Mautner4a1cb222013-12-04 16:14:06 -0800390 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
391 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700392 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800393 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
394 if (parent != null) {
395 stack = parent.task.stack;
396 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800397 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800398 if (stacks != null && !stacks.isEmpty()) {
399 return stack == stacks.get(stacks.size() - 1);
400 }
401 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700402 }
403
Craig Mautnerde4ef022013-04-07 19:01:33 -0700404 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800405 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800406 int topNdx = stacks.size() - 1;
407 if (topNdx <= 0) {
408 return;
409 }
410 ActivityStack topStack = stacks.get(topNdx);
411 final boolean homeInFront = topStack == mHomeStack;
412 if (homeInFront != toFront) {
413 mLastFocusedStack = topStack;
414 stacks.remove(mHomeStack);
415 stacks.add(toFront ? topNdx : 0, mHomeStack);
416 mFocusedStack = stacks.get(topNdx);
417 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
418 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700419 }
420 }
421
Craig Mautner84984fa2014-06-19 11:19:20 -0700422 void moveHomeStackTaskToTop(int homeStackTaskType) {
423 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
424 mWindowManager.showRecentApps();
425 return;
426 }
Craig Mautner69ada552013-04-18 13:51:51 -0700427 moveHomeStack(true);
Craig Mautner84984fa2014-06-19 11:19:20 -0700428 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700429 }
430
Craig Mautner84984fa2014-06-19 11:19:20 -0700431 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700432 if (!mService.mBooting && !mService.mBooted) {
433 // Not ready yet!
434 return false;
435 }
436
Craig Mautner84984fa2014-06-19 11:19:20 -0700437 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
438 mWindowManager.showRecentApps();
439 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700440 }
Craig Mautner84984fa2014-06-19 11:19:20 -0700441 moveHomeStackTaskToTop(homeStackTaskType);
442 if (prev != null) {
443 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
444 }
445
Craig Mautnera8a90e02013-06-28 15:24:50 -0700446 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700447 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000448 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700449 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700450 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700451 }
452 return mService.startHomeActivityLocked(mCurrentUser);
453 }
454
Craig Mautner8d341ef2013-03-26 09:03:27 -0700455 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800456 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800457 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800458 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800459 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
460 ActivityStack stack = stacks.get(stackNdx);
461 TaskRecord task = stack.taskForIdLocked(id);
462 if (task != null) {
463 return task;
464 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700465 }
466 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800467
468 // Don't give up! Look in recents.
469 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800470 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800471 if (task == null) {
472 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
473 return null;
474 }
475
476 if (!restoreRecentTaskLocked(task)) {
477 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
478 return null;
479 }
480 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
481 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700482 }
483
Craig Mautner6170f732013-04-02 13:05:23 -0700484 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800485 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800486 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800487 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800488 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
489 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
490 if (r != null) {
491 return r;
492 }
Craig Mautner6170f732013-04-02 13:05:23 -0700493 }
494 }
495 return null;
496 }
497
Craig Mautneref73ee12014-04-23 11:45:37 -0700498 void setNextTaskId(int taskId) {
499 if (taskId > mCurTaskId) {
500 mCurTaskId = taskId;
501 }
502 }
503
Craig Mautner8d341ef2013-03-26 09:03:27 -0700504 int getNextTaskId() {
505 do {
506 mCurTaskId++;
507 if (mCurTaskId <= 0) {
508 mCurTaskId = 1;
509 }
510 } while (anyTaskForIdLocked(mCurTaskId) != null);
511 return mCurTaskId;
512 }
513
Craig Mautnerde4ef022013-04-07 19:01:33 -0700514 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700515 ActivityStack stack = getFocusedStack();
516 if (stack == null) {
517 return null;
518 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700519 ActivityRecord resumedActivity = stack.mResumedActivity;
520 if (resumedActivity == null || resumedActivity.app == null) {
521 resumedActivity = stack.mPausingActivity;
522 if (resumedActivity == null || resumedActivity.app == null) {
523 resumedActivity = stack.topRunningActivityLocked(null);
524 }
525 }
526 return resumedActivity;
527 }
528
Dianne Hackbornff072722014-09-24 10:56:28 -0700529 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700530 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800531 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800532 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
533 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800534 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
535 final ActivityStack stack = stacks.get(stackNdx);
536 if (!isFrontStack(stack)) {
537 continue;
538 }
539 ActivityRecord hr = stack.topRunningActivityLocked(null);
540 if (hr != null) {
541 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
542 && processName.equals(hr.processName)) {
543 try {
George Mount2c92c972014-03-20 09:38:23 -0700544 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 didSomething = true;
546 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700547 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800548 Slog.w(TAG, "Exception in new application when starting activity "
549 + hr.intent.getComponent().flattenToShortString(), e);
550 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700551 }
Craig Mautner20e72272013-04-01 13:45:53 -0700552 }
Craig Mautner20e72272013-04-01 13:45:53 -0700553 }
554 }
555 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700556 if (!didSomething) {
557 ensureActivitiesVisibleLocked(null, 0);
558 }
Craig Mautner20e72272013-04-01 13:45:53 -0700559 return didSomething;
560 }
561
562 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800563 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
564 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800565 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
566 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800567 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 continue;
569 }
570 final ActivityRecord resumedActivity = stack.mResumedActivity;
571 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800572 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
573 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800574 return false;
575 }
Craig Mautner20e72272013-04-01 13:45:53 -0700576 }
577 }
578 return true;
579 }
580
Craig Mautnerde4ef022013-04-07 19:01:33 -0700581 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800582 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
583 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800584 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
585 final ActivityStack stack = stacks.get(stackNdx);
586 if (isFrontStack(stack)) {
587 final ActivityRecord r = stack.mResumedActivity;
588 if (r != null && r.state != ActivityState.RESUMED) {
589 return false;
590 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700591 }
592 }
593 }
594 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800595 if (DEBUG_STACK) Slog.d(TAG,
596 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
597 mLastFocusedStack + " to=" + mFocusedStack);
598 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700599 return true;
600 }
601
602 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800603 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
604 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
606 final ActivityStack stack = stacks.get(stackNdx);
607 final ActivityRecord r = stack.mResumedActivity;
608 if (r != null && (!r.nowVisible || r.waitingVisible)) {
609 return false;
610 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700611 }
612 }
613 return true;
614 }
615
Craig Mautner2acc3892013-09-23 10:28:14 -0700616 /**
617 * Pause all activities in either all of the stacks or just the back stacks.
618 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700619 * @return true if any activity was paused as a result of this call.
620 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700621 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700622 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800623 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
624 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
626 final ActivityStack stack = stacks.get(stackNdx);
627 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
628 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
629 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700630 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
631 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800632 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700633 }
634 }
635 return someActivityPaused;
636 }
637
Craig Mautnerde4ef022013-04-07 19:01:33 -0700638 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700639 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800640 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
641 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800642 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
643 final ActivityStack stack = stacks.get(stackNdx);
644 final ActivityRecord r = stack.mPausingActivity;
645 if (r != null && r.state != ActivityState.PAUSED
646 && r.state != ActivityState.STOPPED
647 && r.state != ActivityState.STOPPING) {
648 if (DEBUG_STATES) {
649 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
650 pausing = false;
651 } else {
652 return false;
653 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700654 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700655 }
656 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700657 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700658 }
659
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700660 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
661 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500662 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800663 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
664 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
665 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
666 final ActivityStack stack = stacks.get(stackNdx);
667 if (stack.mResumedActivity != null &&
668 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700669 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800670 }
671 }
672 }
673 }
674
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700675 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700676 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700677 }
678
679 void sendWaitingVisibleReportLocked(ActivityRecord r) {
680 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700681 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700682 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700683 if (w.who == null) {
684 changed = true;
685 w.timeout = false;
686 if (r != null) {
687 w.who = new ComponentName(r.info.packageName, r.info.name);
688 }
689 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
690 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700691 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700692 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700693 if (changed) {
694 mService.notifyAll();
695 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700696 }
697
698 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
699 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700700 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700701 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700702 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700703 if (w.who == null) {
704 changed = true;
705 w.timeout = timeout;
706 if (r != null) {
707 w.who = new ComponentName(r.info.packageName, r.info.name);
708 }
709 w.thisTime = thisTime;
710 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700711 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700712 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700713 if (changed) {
714 mService.notifyAll();
715 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700716 }
717
Craig Mautner29219d92013-04-16 20:19:12 -0700718 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700719 final ActivityStack focusedStack = getFocusedStack();
720 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
721 if (r != null) {
722 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700723 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700724
Craig Mautner4a1cb222013-12-04 16:14:06 -0800725 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800726 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
728 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700729 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700730 r = stack.topRunningActivityLocked(null);
731 if (r != null) {
732 return r;
733 }
734 }
735 }
736 return null;
737 }
738
Dianne Hackborn09233282014-04-30 11:33:59 -0700739 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700740 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800741 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
742 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800743 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800744 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800745 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800746 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
747 final ActivityStack stack = stacks.get(stackNdx);
748 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
749 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700750 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700751 }
752 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700753
754 // The lists are already sorted from most recent to oldest. Just pull the most recent off
755 // each list and add it to list. Stop when all lists are empty or maxNum reached.
756 while (maxNum > 0) {
757 long mostRecentActiveTime = Long.MIN_VALUE;
758 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800759 final int numTaskLists = runningTaskLists.size();
760 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
761 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700762 if (!stackTaskList.isEmpty()) {
763 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
764 if (lastActiveTime > mostRecentActiveTime) {
765 mostRecentActiveTime = lastActiveTime;
766 selectedStackList = stackTaskList;
767 }
768 }
769 }
770 if (selectedStackList != null) {
771 list.add(selectedStackList.remove(0));
772 --maxNum;
773 } else {
774 break;
775 }
776 }
Craig Mautner20e72272013-04-01 13:45:53 -0700777 }
778
Craig Mautner23ac33b2013-04-01 16:26:35 -0700779 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700780 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700781 // Collect information about the target of the Intent.
782 ActivityInfo aInfo;
783 try {
784 ResolveInfo rInfo =
785 AppGlobals.getPackageManager().resolveIntent(
786 intent, resolvedType,
787 PackageManager.MATCH_DEFAULT_ONLY
788 | ActivityManagerService.STOCK_PM_FLAGS, userId);
789 aInfo = rInfo != null ? rInfo.activityInfo : null;
790 } catch (RemoteException e) {
791 aInfo = null;
792 }
793
794 if (aInfo != null) {
795 // Store the found target back into the intent, because now that
796 // we have it we never want to do this again. For example, if the
797 // user navigates back to this point in the history, we should
798 // always restart the exact same activity.
799 intent.setComponent(new ComponentName(
800 aInfo.applicationInfo.packageName, aInfo.name));
801
802 // Don't debug things in the system process
803 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
804 if (!aInfo.processName.equals("system")) {
805 mService.setDebugApp(aInfo.processName, true, false);
806 }
807 }
808
809 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
810 if (!aInfo.processName.equals("system")) {
811 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
812 }
813 }
814
Jeff Hao1b012d32014-08-20 10:35:34 -0700815 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700816 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700817 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700818 }
819 }
820 }
821 return aInfo;
822 }
823
Craig Mautner2219a1b2013-03-25 09:44:30 -0700824 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700825 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE);
Dianne Hackborn95465202014-09-15 16:21:55 -0700826 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
827 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700828 }
829
Craig Mautner23ac33b2013-04-01 16:26:35 -0700830 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700831 String callingPackage, Intent intent, String resolvedType,
832 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700833 IBinder resultTo, String resultWho, int requestCode, int startFlags,
834 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700835 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700836 // Refuse possible leaked file descriptors
837 if (intent != null && intent.hasFileDescriptors()) {
838 throw new IllegalArgumentException("File descriptors passed in Intent");
839 }
840 boolean componentSpecified = intent.getComponent() != null;
841
842 // Don't modify the client's object!
843 intent = new Intent(intent);
844
845 // Collect information about the target of the Intent.
846 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700847 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700848
Craig Mautnere0a38842013-12-16 16:14:02 -0800849 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700850 synchronized (mService) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700851 final int realCallingPid = Binder.getCallingPid();
852 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700853 int callingPid;
854 if (callingUid >= 0) {
855 callingPid = -1;
856 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700857 callingPid = realCallingPid;
858 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700859 } else {
860 callingPid = callingUid = -1;
861 }
862
Craig Mautnere0a38842013-12-16 16:14:02 -0800863 final ActivityStack stack;
864 if (container == null || container.mStack.isOnHomeDisplay()) {
865 stack = getFocusedStack();
866 } else {
867 stack = container.mStack;
868 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700869 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700870 && mService.mConfiguration.diff(config) != 0;
871 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700872 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700873
874 final long origId = Binder.clearCallingIdentity();
875
876 if (aInfo != null &&
877 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
878 // This may be a heavy-weight process! Check to see if we already
879 // have another, different heavy-weight process running.
880 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
881 if (mService.mHeavyWeightProcess != null &&
882 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
883 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700884 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700885 if (caller != null) {
886 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
887 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700888 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700889 } else {
890 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700891 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700892 + intent.toString());
893 ActivityOptions.abort(options);
894 return ActivityManager.START_PERMISSION_DENIED;
895 }
896 }
897
898 IIntentSender target = mService.getIntentSenderLocked(
899 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700900 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700901 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
902 | PendingIntent.FLAG_ONE_SHOT, null);
903
904 Intent newIntent = new Intent();
905 if (requestCode >= 0) {
906 // Caller is requesting a result.
907 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
908 }
909 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
910 new IntentSender(target));
911 if (mService.mHeavyWeightProcess.activities.size() > 0) {
912 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
913 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
914 hist.packageName);
915 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
916 hist.task.taskId);
917 }
918 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
919 aInfo.packageName);
920 newIntent.setFlags(intent.getFlags());
921 newIntent.setClassName("android",
922 HeavyWeightSwitcherActivity.class.getName());
923 intent = newIntent;
924 resolvedType = null;
925 caller = null;
926 callingUid = Binder.getCallingUid();
927 callingPid = Binder.getCallingPid();
928 componentSpecified = true;
929 try {
930 ResolveInfo rInfo =
931 AppGlobals.getPackageManager().resolveIntent(
932 intent, null,
933 PackageManager.MATCH_DEFAULT_ONLY
934 | ActivityManagerService.STOCK_PM_FLAGS, userId);
935 aInfo = rInfo != null ? rInfo.activityInfo : null;
936 aInfo = mService.getActivityInfoForUser(aInfo, userId);
937 } catch (RemoteException e) {
938 aInfo = null;
939 }
940 }
941 }
942 }
943
Dianne Hackborn91097de2014-04-04 18:02:06 -0700944 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
945 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700946 requestCode, callingPid, callingUid, callingPackage,
947 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700948 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700949
Craig Mautner85c11a82014-07-17 12:38:18 -0700950 Binder.restoreCallingIdentity(origId);
951
Craig Mautnerde4ef022013-04-07 19:01:33 -0700952 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700953 // If the caller also wants to switch to a new configuration,
954 // do so now. This allows a clean switch, as we are waiting
955 // for the current activity to pause (so we will not destroy
956 // it), and have not yet started the next activity.
957 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
958 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700959 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700960 if (DEBUG_CONFIGURATION) Slog.v(TAG,
961 "Updating to new configuration after starting activity.");
962 mService.updateConfigurationLocked(config, null, false, false);
963 }
964
Craig Mautner23ac33b2013-04-01 16:26:35 -0700965 if (outResult != null) {
966 outResult.result = res;
967 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700968 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700969 do {
970 try {
971 mService.wait();
972 } catch (InterruptedException e) {
973 }
974 } while (!outResult.timeout && outResult.who == null);
975 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700976 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700977 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700978 outResult.timeout = false;
979 outResult.who = new ComponentName(r.info.packageName, r.info.name);
980 outResult.totalTime = 0;
981 outResult.thisTime = 0;
982 } else {
983 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700984 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985 do {
986 try {
987 mService.wait();
988 } catch (InterruptedException e) {
989 }
990 } while (!outResult.timeout && outResult.who == null);
991 }
992 }
993 }
994
995 return res;
996 }
997 }
998
999 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1000 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1001 Bundle options, int userId) {
1002 if (intents == null) {
1003 throw new NullPointerException("intents is null");
1004 }
1005 if (resolvedTypes == null) {
1006 throw new NullPointerException("resolvedTypes is null");
1007 }
1008 if (intents.length != resolvedTypes.length) {
1009 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1010 }
1011
Craig Mautner23ac33b2013-04-01 16:26:35 -07001012
1013 int callingPid;
1014 if (callingUid >= 0) {
1015 callingPid = -1;
1016 } else if (caller == null) {
1017 callingPid = Binder.getCallingPid();
1018 callingUid = Binder.getCallingUid();
1019 } else {
1020 callingPid = callingUid = -1;
1021 }
1022 final long origId = Binder.clearCallingIdentity();
1023 try {
1024 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001025 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001026 for (int i=0; i<intents.length; i++) {
1027 Intent intent = intents[i];
1028 if (intent == null) {
1029 continue;
1030 }
1031
1032 // Refuse possible leaked file descriptors
1033 if (intent != null && intent.hasFileDescriptors()) {
1034 throw new IllegalArgumentException("File descriptors passed in Intent");
1035 }
1036
1037 boolean componentSpecified = intent.getComponent() != null;
1038
1039 // Don't modify the client's object!
1040 intent = new Intent(intent);
1041
1042 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001043 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001044 // TODO: New, check if this is correct
1045 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1046
1047 if (aInfo != null &&
1048 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
1049 != 0) {
1050 throw new IllegalArgumentException(
1051 "FLAG_CANT_SAVE_STATE not supported here");
1052 }
1053
1054 Bundle theseOptions;
1055 if (options != null && i == intents.length-1) {
1056 theseOptions = options;
1057 } else {
1058 theseOptions = null;
1059 }
Craig Mautner6170f732013-04-02 13:05:23 -07001060 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001061 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1062 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001063 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001064 if (res < 0) {
1065 return res;
1066 }
1067
1068 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1069 }
1070 }
1071 } finally {
1072 Binder.restoreCallingIdentity(origId);
1073 }
1074
1075 return ActivityManager.START_SUCCESS;
1076 }
1077
Craig Mautner2420ead2013-04-01 17:13:20 -07001078 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001079 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001080 throws RemoteException {
1081
1082 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001083 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001084 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001085
1086 // schedule launch ticks to collect information about slow apps.
1087 r.startLaunchTickingLocked();
1088
1089 // Have the window manager re-evaluate the orientation of
1090 // the screen based on the new activity order. Note that
1091 // as a result of this, it can call back into the activity
1092 // manager with a new orientation. We don't care about that,
1093 // because the activity is not currently running so we are
1094 // just restarting it anyway.
1095 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001096 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001097 mService.mConfiguration,
1098 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1099 mService.updateConfigurationLocked(config, r, false, false);
1100 }
1101
1102 r.app = app;
1103 app.waitingToKill = null;
1104 r.launchCount++;
1105 r.lastLaunchTime = SystemClock.uptimeMillis();
1106
1107 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1108
1109 int idx = app.activities.indexOf(r);
1110 if (idx < 0) {
1111 app.activities.add(r);
1112 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001113 mService.updateLruProcessLocked(app, true, null);
1114 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001115
1116 final ActivityStack stack = r.task.stack;
1117 try {
1118 if (app.thread == null) {
1119 throw new RemoteException();
1120 }
1121 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001122 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001123 if (andResume) {
1124 results = r.results;
1125 newIntents = r.newIntents;
1126 }
1127 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1128 + " icicle=" + r.icicle
1129 + " with results=" + results + " newIntents=" + newIntents
1130 + " andResume=" + andResume);
1131 if (andResume) {
1132 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1133 r.userId, System.identityHashCode(r),
1134 r.task.taskId, r.shortComponentName);
1135 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001136 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001137 // Home process is the root process of the task.
1138 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001139 }
1140 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1141 r.sleeping = false;
1142 r.forceNewConfig = false;
1143 mService.showAskCompatModeDialogLocked(r);
1144 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1145 String profileFile = null;
1146 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001147 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1148 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1149 mService.mProfileProc = app;
1150 profileFile = mService.mProfileFile;
1151 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001152 }
1153 }
1154 app.hasShownUi = true;
1155 app.pendingUiClean = true;
1156 if (profileFd != null) {
1157 try {
1158 profileFd = profileFd.dup();
1159 } catch (IOException e) {
1160 if (profileFd != null) {
1161 try {
1162 profileFd.close();
1163 } catch (IOException o) {
1164 }
1165 profileFd = null;
1166 }
1167 }
1168 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001169
Jeff Hao1b012d32014-08-20 10:35:34 -07001170 ProfilerInfo profilerInfo = profileFile != null
1171 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1172 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001173 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001174 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001175 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001176 r.compat, r.launchedFromPackage, r.task.voiceInteractor, app.repProcState,
1177 r.icicle, r.persistentState, results, newIntents, !andResume,
1178 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001179
1180 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1181 // This may be a heavy-weight process! Note that the package
1182 // manager will ensure that only activity can run in the main
1183 // process of the .apk, which is the only thing that will be
1184 // considered heavy-weight.
1185 if (app.processName.equals(app.info.packageName)) {
1186 if (mService.mHeavyWeightProcess != null
1187 && mService.mHeavyWeightProcess != app) {
1188 Slog.w(TAG, "Starting new heavy weight process " + app
1189 + " when already running "
1190 + mService.mHeavyWeightProcess);
1191 }
1192 mService.mHeavyWeightProcess = app;
1193 Message msg = mService.mHandler.obtainMessage(
1194 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1195 msg.obj = r;
1196 mService.mHandler.sendMessage(msg);
1197 }
1198 }
1199
1200 } catch (RemoteException e) {
1201 if (r.launchFailed) {
1202 // This is the second time we failed -- finish activity
1203 // and give up.
1204 Slog.e(TAG, "Second failure launching "
1205 + r.intent.getComponent().flattenToShortString()
1206 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001207 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1209 "2nd-crash", false);
1210 return false;
1211 }
1212
1213 // This is the first time we failed -- restart process and
1214 // retry.
1215 app.activities.remove(r);
1216 throw e;
1217 }
1218
1219 r.launchFailed = false;
1220 if (stack.updateLRUListLocked(r)) {
1221 Slog.w(TAG, "Activity " + r
1222 + " being launched, but already in LRU list");
1223 }
1224
1225 if (andResume) {
1226 // As part of the process of launching, ActivityThread also performs
1227 // a resume.
1228 stack.minimalResumeActivityLocked(r);
1229 } else {
1230 // This activity is not starting in the resumed state... which
1231 // should look like we asked it to pause+stop (but remain visible),
1232 // and it has done so and reported back the current icicle and
1233 // other state.
1234 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1235 + " (starting in stopped state)");
1236 r.state = ActivityState.STOPPED;
1237 r.stopped = true;
1238 }
1239
1240 // Launch the new version setup screen if needed. We do this -after-
1241 // launching the initial activity (that is, home), so that it can have
1242 // a chance to initialize itself while in the background, making the
1243 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001244 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001245 mService.startSetupActivityLocked();
1246 }
1247
Dianne Hackborn465fa392014-09-14 14:21:18 -07001248 // Update any services we are bound to that might care about whether
1249 // their client may have activities.
1250 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1251
Craig Mautner2420ead2013-04-01 17:13:20 -07001252 return true;
1253 }
1254
Craig Mautnere79d42682013-04-01 19:01:53 -07001255 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001256 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001257 // Is this activity's application already running?
1258 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001259 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001260
1261 r.task.stack.setLaunchTime(r);
1262
1263 if (app != null && app.thread != null) {
1264 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001265 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1266 || !"android".equals(r.info.packageName)) {
1267 // Don't add this if it is a platform component that is marked
1268 // to run in multiple processes, because this is actually
1269 // part of the framework so doesn't make sense to track as a
1270 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001271 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1272 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001273 }
George Mount2c92c972014-03-20 09:38:23 -07001274 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001275 return;
1276 } catch (RemoteException e) {
1277 Slog.w(TAG, "Exception when starting activity "
1278 + r.intent.getComponent().flattenToShortString(), e);
1279 }
1280
1281 // If a dead object exception was thrown -- fall through to
1282 // restart the application.
1283 }
1284
1285 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001286 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001287 }
1288
Craig Mautner6170f732013-04-02 13:05:23 -07001289 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001290 Intent intent, String resolvedType, ActivityInfo aInfo,
1291 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1292 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001293 int callingPid, int callingUid, String callingPackage,
1294 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001295 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1296 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001297 int err = ActivityManager.START_SUCCESS;
1298
1299 ProcessRecord callerApp = null;
1300 if (caller != null) {
1301 callerApp = mService.getRecordForAppLocked(caller);
1302 if (callerApp != null) {
1303 callingPid = callerApp.pid;
1304 callingUid = callerApp.info.uid;
1305 } else {
1306 Slog.w(TAG, "Unable to find app for caller " + caller
1307 + " (pid=" + callingPid + ") when starting: "
1308 + intent.toString());
1309 err = ActivityManager.START_PERMISSION_DENIED;
1310 }
1311 }
1312
1313 if (err == ActivityManager.START_SUCCESS) {
1314 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1315 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001316 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001317 + " on display " + (container == null ? (mFocusedStack == null ?
1318 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1319 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1320 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001321 }
1322
1323 ActivityRecord sourceRecord = null;
1324 ActivityRecord resultRecord = null;
1325 if (resultTo != null) {
1326 sourceRecord = isInAnyStackLocked(resultTo);
1327 if (DEBUG_RESULTS) Slog.v(
1328 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1329 if (sourceRecord != null) {
1330 if (requestCode >= 0 && !sourceRecord.finishing) {
1331 resultRecord = sourceRecord;
1332 }
1333 }
1334 }
Craig Mautner6170f732013-04-02 13:05:23 -07001335
Dianne Hackborn91097de2014-04-04 18:02:06 -07001336 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001337
Dianne Hackborn95465202014-09-15 16:21:55 -07001338 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001339 // Transfer the result target from the source activity to the new
1340 // one being started, including any failures.
1341 if (requestCode >= 0) {
1342 ActivityOptions.abort(options);
1343 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1344 }
1345 resultRecord = sourceRecord.resultTo;
1346 resultWho = sourceRecord.resultWho;
1347 requestCode = sourceRecord.requestCode;
1348 sourceRecord.resultTo = null;
1349 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001350 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001351 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001352 if (sourceRecord.launchedFromUid == callingUid) {
1353 // The new activity is being launched from the same uid as the previous
1354 // activity in the flow, and asking to forward its result back to the
1355 // previous. In this case the activity is serving as a trampoline between
1356 // the two, so we also want to update its launchedFromPackage to be the
1357 // same as the previous activity. Note that this is safe, since we know
1358 // these two packages come from the same uid; the caller could just as
1359 // well have supplied that same package name itself. This specifially
1360 // deals with the case of an intent picker/chooser being launched in the app
1361 // flow to redirect to an activity picked by the user, where we want the final
1362 // activity to consider it to have been launched by the previous app activity.
1363 callingPackage = sourceRecord.launchedFromPackage;
1364 }
Craig Mautner6170f732013-04-02 13:05:23 -07001365 }
1366
1367 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1368 // We couldn't find a class that can handle the given Intent.
1369 // That's the end of that!
1370 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1371 }
1372
1373 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1374 // We couldn't find the specific class specified in the Intent.
1375 // Also the end of the line.
1376 err = ActivityManager.START_CLASS_NOT_FOUND;
1377 }
1378
Dianne Hackborn91097de2014-04-04 18:02:06 -07001379 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1380 && sourceRecord.task.voiceSession != null) {
1381 // If this activity is being launched as part of a voice session, we need
1382 // to ensure that it is safe to do so. If the upcoming activity will also
1383 // be part of the voice session, we can only launch it if it has explicitly
1384 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001385 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001386 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1387 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001388 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1389 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001390 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1391 }
1392 } catch (RemoteException e) {
1393 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1394 }
1395 }
1396 }
1397
1398 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1399 // If the caller is starting a new voice session, just make sure the target
1400 // is actually allowing it to run this way.
1401 try {
1402 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1403 intent, resolvedType)) {
1404 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1405 }
1406 } catch (RemoteException e) {
1407 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1408 }
1409 }
1410
louis_changcd5d1982014-08-01 10:09:08 +08001411 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1412
Craig Mautner6170f732013-04-02 13:05:23 -07001413 if (err != ActivityManager.START_SUCCESS) {
1414 if (resultRecord != null) {
1415 resultStack.sendActivityResultLocked(-1,
1416 resultRecord, resultWho, requestCode,
1417 Activity.RESULT_CANCELED, null);
1418 }
Craig Mautner6170f732013-04-02 13:05:23 -07001419 ActivityOptions.abort(options);
1420 return err;
1421 }
1422
1423 final int startAnyPerm = mService.checkPermission(
1424 START_ANY_ACTIVITY, callingPid, callingUid);
1425 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1426 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1427 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1428 if (resultRecord != null) {
1429 resultStack.sendActivityResultLocked(-1,
1430 resultRecord, resultWho, requestCode,
1431 Activity.RESULT_CANCELED, null);
1432 }
Craig Mautner6170f732013-04-02 13:05:23 -07001433 String msg;
1434 if (!aInfo.exported) {
1435 msg = "Permission Denial: starting " + intent.toString()
1436 + " from " + callerApp + " (pid=" + callingPid
1437 + ", uid=" + callingUid + ")"
1438 + " not exported from uid " + aInfo.applicationInfo.uid;
1439 } else {
1440 msg = "Permission Denial: starting " + intent.toString()
1441 + " from " + callerApp + " (pid=" + callingPid
1442 + ", uid=" + callingUid + ")"
1443 + " requires " + aInfo.permission;
1444 }
1445 Slog.w(TAG, msg);
1446 throw new SecurityException(msg);
1447 }
1448
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001449 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001450 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001451
Craig Mautner6170f732013-04-02 13:05:23 -07001452 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001453 try {
1454 // The Intent we give to the watcher has the extra data
1455 // stripped off, since it can contain private information.
1456 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001457 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001458 aInfo.applicationInfo.packageName);
1459 } catch (RemoteException e) {
1460 mService.mController = null;
1461 }
Ben Gruver5e207332013-04-03 17:41:37 -07001462 }
Craig Mautner6170f732013-04-02 13:05:23 -07001463
Ben Gruver5e207332013-04-03 17:41:37 -07001464 if (abort) {
1465 if (resultRecord != null) {
1466 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001467 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001468 }
Ben Gruver5e207332013-04-03 17:41:37 -07001469 // We pretend to the caller that it was really started, but
1470 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001471 ActivityOptions.abort(options);
1472 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001473 }
1474
1475 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001476 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001477 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001478 if (outActivity != null) {
1479 outActivity[0] = r;
1480 }
1481
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001482 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001483 if (voiceSession == null && (stack.mResumedActivity == null
1484 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001485 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1486 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001487 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001488 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001489 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001490 ActivityOptions.abort(options);
1491 return ActivityManager.START_SWITCHES_CANCELED;
1492 }
1493 }
1494
1495 if (mService.mDidAppSwitch) {
1496 // This is the second allowed switch since we stopped switches,
1497 // so now just generally allow switches. Use case: user presses
1498 // home (switches disabled, switch to home, mDidAppSwitch now true);
1499 // user taps a home icon (coming from home so allowed, we hit here
1500 // and now allow anyone to switch again).
1501 mService.mAppSwitchesAllowedTime = 0;
1502 } else {
1503 mService.mDidAppSwitch = true;
1504 }
1505
Craig Mautneree36c772014-07-16 14:56:05 -07001506 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001507
Dianne Hackborn91097de2014-04-04 18:02:06 -07001508 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001509 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001510
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001511 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001512 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001513 // activity start, but we are not actually doing an activity
1514 // switch... just dismiss the keyguard now, because we
1515 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001516 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001517 }
1518 return err;
1519 }
1520
Craig Mautner1b4bf852014-05-26 15:06:32 -07001521 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001522 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001523
1524 // On leanback only devices we should keep all activities in the same stack.
1525 if (!mLeanbackOnlyDevice &&
1526 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001527 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001528 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001529 if (taskStack.isOnHomeDisplay()) {
1530 if (mFocusedStack != taskStack) {
1531 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1532 "focused stack to r=" + r + " task=" + task);
1533 mFocusedStack = taskStack;
1534 } else {
1535 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1536 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1537 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001538 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001539 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001540 }
1541
Craig Mautnere0a38842013-12-16 16:14:02 -08001542 final ActivityContainer container = r.mInitialActivityContainer;
1543 if (container != null) {
1544 // The first time put it on the desired stack, after this put on task stack.
1545 r.mInitialActivityContainer = null;
1546 return container.mStack;
1547 }
1548
Craig Mautner1b4bf852014-05-26 15:06:32 -07001549 if (mFocusedStack != mHomeStack && (!newTask ||
1550 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001551 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1552 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1553 return mFocusedStack;
1554 }
1555
Craig Mautnere0a38842013-12-16 16:14:02 -08001556 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1557 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1558 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1559 if (!stack.isHomeStack()) {
1560 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1561 "adjustStackFocus: Setting focused stack=" + stack);
1562 mFocusedStack = stack;
1563 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001564 }
1565 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001566
Craig Mautner4a1cb222013-12-04 16:14:06 -08001567 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001568 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001569 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1570 " stackId=" + stackId);
1571 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001572 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001573 }
1574 return mHomeStack;
1575 }
1576
Craig Mautner29219d92013-04-16 20:19:12 -07001577 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001578 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001579 final TaskRecord task = r.task;
1580 boolean isHomeActivity = !r.isApplicationActivity();
1581 if (!isHomeActivity && task != null) {
1582 isHomeActivity = !task.isApplicationTask();
1583 }
1584 if (!isHomeActivity && task != null) {
1585 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1586 isHomeActivity = parent != null && parent.isHomeActivity();
1587 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001588 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001589 }
1590 }
1591
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001592 final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001593 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001594 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001595 final Intent intent = r.intent;
1596 final int callingUid = r.launchedFromUid;
1597
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001598 // In some flows in to this function, we retrieve the task record and hold on to it
1599 // without a lock before calling back in to here... so the task at this point may
1600 // not actually be in recents. Check for that, and if it isn't in recents just
1601 // consider it invalid.
1602 if (inTask != null && !inTask.inRecents) {
1603 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1604 inTask = null;
1605 }
1606
Craig Mautnerad400af2014-08-13 16:41:36 -07001607 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001608 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1609 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001610
Craig Mautnera228ae92014-07-09 05:44:55 -07001611 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001612 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001613 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001614 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1615 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1616 "\"singleInstance\" or \"singleTask\"");
1617 launchFlags &=
1618 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1619 } else {
1620 switch (r.info.documentLaunchMode) {
1621 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1622 break;
1623 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1624 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1625 break;
1626 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1627 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1628 break;
1629 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1630 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1631 break;
1632 }
1633 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001634
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001635 final boolean launchTaskBehind = r.mLaunchTaskBehind
1636 && !launchSingleTask && !launchSingleInstance
1637 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001638
1639 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1640 // For whatever reason this activity is being launched into a new
1641 // task... yet the caller has requested a result back. Well, that
1642 // is pretty messed up, so instead immediately send back a cancel
1643 // and let the new task continue launched as normal without a
1644 // dependency on its originator.
1645 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1646 r.resultTo.task.stack.sendActivityResultLocked(-1,
1647 r.resultTo, r.resultWho, r.requestCode,
1648 Activity.RESULT_CANCELED, null);
1649 r.resultTo = null;
1650 }
1651
1652 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1653 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1654 }
1655
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001656 // If we are actually going to launch in to a new task, there are some cases where
1657 // we further want to do multiple task.
1658 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1659 if (launchTaskBehind
1660 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1661 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1662 }
1663 }
1664
Craig Mautner8849a5e2013-04-02 16:41:03 -07001665 // We'll invoke onUserLeaving before onPause only if the launching
1666 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001667 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001668 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001669
1670 // If the caller has asked not to resume at this point, we make note
1671 // of this in the record so that we can skip it when trying to find
1672 // the top running activity.
1673 if (!doResume) {
1674 r.delayedResume = true;
1675 }
1676
Craig Mautnera254cd72014-05-25 16:47:39 -07001677 ActivityRecord notTop =
1678 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001679
1680 // If the onlyIfNeeded flag is set, then we can do this if the activity
1681 // being launched is the same as the one making the call... or, as
1682 // a special case, if we do not know the caller then we count the
1683 // current top activity as the caller.
1684 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1685 ActivityRecord checkedCaller = sourceRecord;
1686 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001687 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001688 }
1689 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1690 // Caller is not the same as launcher, so always needed.
1691 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1692 }
1693 }
1694
Craig Mautner8849a5e2013-04-02 16:41:03 -07001695 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001696 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001697
1698 // If the caller is not coming from another activity, but has given us an
1699 // explicit task into which they would like us to launch the new activity,
1700 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001701 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1702 final Intent baseIntent = inTask.getBaseIntent();
1703 final ActivityRecord root = inTask.getRootActivity();
1704 if (baseIntent == null) {
1705 ActivityOptions.abort(options);
1706 throw new IllegalArgumentException("Launching into task without base intent: "
1707 + inTask);
1708 }
1709
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001710 // If this task is empty, then we are adding the first activity -- it
1711 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001712 if (launchSingleInstance || launchSingleTask) {
1713 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1714 ActivityOptions.abort(options);
1715 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1716 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001717 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001718 if (root != null) {
1719 ActivityOptions.abort(options);
1720 throw new IllegalArgumentException("Caller with inTask " + inTask
1721 + " has root " + root + " but target is singleInstance/Task");
1722 }
1723 }
1724
1725 // If task is empty, then adopt the interesting intent launch flags in to the
1726 // activity being started.
1727 if (root == null) {
1728 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1729 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1730 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1731 launchFlags = (launchFlags&~flagsOfInterest)
1732 | (baseIntent.getFlags()&flagsOfInterest);
1733 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001734 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001735 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001736
Dianne Hackborn962d5352014-08-29 16:27:40 -07001737 // If the task is not empty and the caller is asking to start it as the root
1738 // of a new task, then we don't actually want to start this on the task. We
1739 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001740 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001741 addingToTask = false;
1742
1743 } else {
1744 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001745 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001746
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001747 reuseTask = inTask;
1748 } else {
1749 inTask = null;
1750 }
1751
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001752 if (inTask == null) {
1753 if (sourceRecord == null) {
1754 // This activity is not being started from another... in this
1755 // case we -always- start a new task.
1756 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1757 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1758 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1759 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1760 }
1761 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1762 // The original activity who is starting us is running as a single
1763 // instance... this new activity it is starting must go on its
1764 // own task.
1765 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1766 } else if (launchSingleInstance || launchSingleTask) {
1767 // The activity being started is a single instance... it always
1768 // gets launched into its own task.
1769 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1770 }
1771 }
1772
1773 ActivityInfo newTaskInfo = null;
1774 Intent newTaskIntent = null;
1775 ActivityStack sourceStack;
1776 if (sourceRecord != null) {
1777 if (sourceRecord.finishing) {
1778 // If the source is finishing, we can't further count it as our source. This
1779 // is because the task it is associated with may now be empty and on its way out,
1780 // so we don't want to blindly throw it in to that task. Instead we will take
1781 // the NEW_TASK flow and try to find a task for it. But save the task information
1782 // so it can be used when creating the new task.
1783 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1784 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1785 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1786 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1787 newTaskInfo = sourceRecord.info;
1788 newTaskIntent = sourceRecord.task.intent;
1789 }
1790 sourceRecord = null;
1791 sourceStack = null;
1792 } else {
1793 sourceStack = sourceRecord.task.stack;
1794 }
1795 } else {
1796 sourceStack = null;
1797 }
1798
1799 boolean movedHome = false;
1800 ActivityStack targetStack;
1801
1802 intent.setFlags(launchFlags);
1803
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001804 // We may want to try to place the new activity in to an existing task. We always
1805 // do this if the target activity is singleTask or singleInstance; we will also do
1806 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1807 // us to still place it in a new task: multi task, always doc mode, or being asked to
1808 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001809 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1810 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001811 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001812 // If bring to front is requested, and no result is requested and we have not
1813 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001814 // we can find a task that was started with this same
1815 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001816 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001817 // See if there is a task to bring to the front. If this is
1818 // a SINGLE_INSTANCE activity, there can be one and only one
1819 // instance of it in the history, and it is always in its own
1820 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001821 ActivityRecord intentActivity = !launchSingleInstance ?
1822 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001823 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001824 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001825 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001826 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001827 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1828 }
Craig Mautner29219d92013-04-16 20:19:12 -07001829 if (r.task == null) {
1830 r.task = intentActivity.task;
1831 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001832 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001833 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001834 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1835 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001836 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001837 if (intentActivity.task.intent == null) {
1838 // This task was started because of movement of
1839 // the activity based on affinity... now that we
1840 // are actually launching it, we can assign the
1841 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001842 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001843 }
1844 // If the target task is not in the front, then we need
1845 // to bring it to the front... except... well, with
1846 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1847 // to have the same behavior as if a new instance was
1848 // being started, which means not bringing it to the front
1849 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001850 final ActivityStack lastStack = getLastStack();
1851 ActivityRecord curTop = lastStack == null?
1852 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001853 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001854 if (curTop != null && (curTop.task != intentActivity.task ||
1855 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001856 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001857 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1858 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001859 // We really do want to push this one into the
1860 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001861 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001862 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1863 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001864 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001865 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001866 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001867 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1868 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001869 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001870 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001871 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001872 options = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001873 movedToFront = true;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001874 }
1875 }
1876 // If the caller has requested that the target task be
1877 // reset, then do so.
1878 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1879 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1880 }
1881 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1882 // We don't need to start a new activity, and
1883 // the client said not to do anything if that
1884 // is the case, so this is it! And for paranoia, make
1885 // sure we have correctly resumed the top activity.
1886 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001887 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001888
1889 // Make sure to notify Keyguard as well if we are not running an app
1890 // transition later.
1891 if (!movedToFront) {
1892 notifyActivityDrawnForKeyguard();
1893 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001894 } else {
1895 ActivityOptions.abort(options);
1896 }
1897 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1898 }
1899 if ((launchFlags &
1900 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1901 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1902 // The caller has requested to completely replace any
1903 // existing task with its new activity. Well that should
1904 // not be too hard...
1905 reuseTask = intentActivity.task;
1906 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001907 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001908 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001909 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001910 // In this situation we want to remove all activities
1911 // from the task up to the one being started. In most
1912 // cases this means we are resetting the task to its
1913 // initial state.
1914 ActivityRecord top =
1915 intentActivity.task.performClearTaskLocked(r, launchFlags);
1916 if (top != null) {
1917 if (top.frontOfTask) {
1918 // Activity aliases may mean we use different
1919 // intents for the top activity, so make sure
1920 // the task now has the identity of the new
1921 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001922 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001923 }
1924 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1925 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001926 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001927 } else {
1928 // A special case: we need to
1929 // start the activity because it is not currently
1930 // running, and the caller has asked to clear the
1931 // current task to have this activity at the top.
1932 addingToTask = true;
1933 // Now pretend like this activity is being started
1934 // by the top of its task, so it is put in the
1935 // right place.
1936 sourceRecord = intentActivity;
1937 }
1938 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1939 // In this case the top activity on the task is the
1940 // same as the one being launched, so we take that
1941 // as a request to bring the task to the foreground.
1942 // If the top activity in the task is the root
1943 // activity, deliver this new intent to it if it
1944 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001945 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001946 && intentActivity.realActivity.equals(r.realActivity)) {
1947 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1948 intentActivity.task);
1949 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001950 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001951 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001952 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
1953 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001954 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1955 // In this case we are launching the root activity
1956 // of the task, but with a different intent. We
1957 // should start a new instance on top.
1958 addingToTask = true;
1959 sourceRecord = intentActivity;
1960 }
1961 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1962 // In this case an activity is being launched in to an
1963 // existing task, without resetting that task. This
1964 // is typically the situation of launching an activity
1965 // from a notification or shortcut. We want to place
1966 // the new activity on top of the current task.
1967 addingToTask = true;
1968 sourceRecord = intentActivity;
1969 } else if (!intentActivity.task.rootWasReset) {
1970 // In this case we are launching in to an existing task
1971 // that has not yet been started from its front door.
1972 // The current task has been brought to the front.
1973 // Ideally, we'd probably like to place this new task
1974 // at the bottom of its stack, but that's a little hard
1975 // to do with the current organization of the code so
1976 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07001977 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001978 }
1979 if (!addingToTask && reuseTask == null) {
1980 // We didn't do anything... but it was needed (a.k.a., client
1981 // don't use that intent!) And for paranoia, make
1982 // sure we have correctly resumed the top activity.
1983 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001984 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001985 if (!movedToFront) {
1986 // Make sure to notify Keyguard as well if we are not running an app
1987 // transition later.
1988 notifyActivityDrawnForKeyguard();
1989 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001990 } else {
1991 ActivityOptions.abort(options);
1992 }
1993 return ActivityManager.START_TASK_TO_FRONT;
1994 }
1995 }
1996 }
1997 }
1998
1999 //String uri = r.intent.toURI();
2000 //Intent intent2 = new Intent(uri);
2001 //Slog.i(TAG, "Given intent: " + r.intent);
2002 //Slog.i(TAG, "URI is: " + uri);
2003 //Slog.i(TAG, "To intent: " + intent2);
2004
2005 if (r.packageName != null) {
2006 // If the activity being launched is the same as the one currently
2007 // at the top, then we need to check if it should only be launched
2008 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002009 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002010 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002011 if (top != null && r.resultTo == null) {
2012 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2013 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002014 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002015 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002016 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2017 top.task);
2018 // For paranoia, make sure we have correctly
2019 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002020 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002021 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002022 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002023 }
2024 ActivityOptions.abort(options);
2025 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2026 // We don't need to start a new activity, and
2027 // the client said not to do anything if that
2028 // is the case, so this is it!
2029 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2030 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002031 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002032 return ActivityManager.START_DELIVERED_TO_TOP;
2033 }
2034 }
2035 }
2036 }
2037
2038 } else {
2039 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002040 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2041 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002042 }
2043 ActivityOptions.abort(options);
2044 return ActivityManager.START_CLASS_NOT_FOUND;
2045 }
2046
2047 boolean newTask = false;
2048 boolean keepCurTransition = false;
2049
Craig Mautnerbb742462014-07-07 15:28:55 -07002050 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002051 sourceRecord.task : null;
2052
Craig Mautner8849a5e2013-04-02 16:41:03 -07002053 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002054 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002055 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002056 if (isLockTaskModeViolation(reuseTask)) {
2057 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2058 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2059 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002060 newTask = true;
2061 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerbb742462014-07-07 15:28:55 -07002062 if (!launchTaskBehind) {
2063 targetStack.moveToFront();
2064 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002065 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002066 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2067 newTaskInfo != null ? newTaskInfo : r.info,
2068 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002069 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2070 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002071 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2072 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002073 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002074 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002075 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002076 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002077 if ((launchFlags &
2078 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2079 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2080 // Caller wants to appear on home activity, so before starting
2081 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002082 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002083 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002084 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002085 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002086 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002087 if (isLockTaskModeViolation(sourceTask)) {
2088 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2089 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2090 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002091 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07002092 targetStack.moveToFront();
Craig Mautner737fae22014-10-15 12:52:10 -07002093 final TaskRecord topTask = targetStack.topTask();
2094 if (topTask != sourceTask) {
2095 targetStack.moveTaskToFrontLocked(sourceTask, r, options);
Craig Mautner737fae22014-10-15 12:52:10 -07002096 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002097 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002098 // In this case, we are adding the activity to an existing
2099 // task, but the caller has asked to clear that task if the
2100 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002101 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002102 keepCurTransition = true;
2103 if (top != null) {
2104 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002105 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002106 // For paranoia, make sure we have correctly
2107 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002108 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002109 if (doResume) {
2110 targetStack.resumeTopActivityLocked(null);
2111 }
2112 ActivityOptions.abort(options);
2113 return ActivityManager.START_DELIVERED_TO_TOP;
2114 }
2115 } else if (!addingToTask &&
2116 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2117 // In this case, we are launching an activity in our own task
2118 // that may already be running somewhere in the history, and
2119 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002120 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002122 final TaskRecord task = top.task;
2123 task.moveActivityToFrontLocked(top);
2124 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002125 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002126 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002127 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002128 if (doResume) {
2129 targetStack.resumeTopActivityLocked(null);
2130 }
2131 return ActivityManager.START_DELIVERED_TO_TOP;
2132 }
2133 }
2134 // An existing activity is starting this new activity, so we want
2135 // to keep the new one in the same task as the one that is starting
2136 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002137 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002138 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002139 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002140
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002141 } else if (inTask != null) {
2142 // The calling is asking that the new activity be started in an explicit
2143 // task it has provided to us.
2144 if (isLockTaskModeViolation(inTask)) {
2145 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2146 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2147 }
2148 targetStack = inTask.stack;
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002149 targetStack.moveTaskToFrontLocked(inTask, r, options);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002150
2151 // Check whether we should actually launch the new activity in to the task,
2152 // or just reuse the current activity on top.
2153 ActivityRecord top = inTask.getTopActivity();
2154 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2155 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2156 || launchSingleTop || launchSingleTask) {
2157 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2158 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2159 // We don't need to start a new activity, and
2160 // the client said not to do anything if that
2161 // is the case, so this is it!
2162 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2163 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002164 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002165 return ActivityManager.START_DELIVERED_TO_TOP;
2166 }
2167 }
2168
Dianne Hackborn962d5352014-08-29 16:27:40 -07002169 if (!addingToTask) {
2170 // We don't actually want to have this activity added to the task, so just
2171 // stop here but still tell the caller that we consumed the intent.
2172 ActivityOptions.abort(options);
2173 return ActivityManager.START_TASK_TO_FRONT;
2174 }
2175
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002176 r.setTask(inTask, null);
2177 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2178 + " in explicit task " + r.task);
2179
Craig Mautner8849a5e2013-04-02 16:41:03 -07002180 } else {
2181 // This not being started from an existing activity, and not part
2182 // of a new task... just put it in the top task, though these days
2183 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002184 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07002185 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07002186 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002187 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002188 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002189 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002190 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2191 + " in new guessed " + r.task);
2192 }
2193
2194 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002195 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196
Craig Mautner76e2a762014-06-24 09:05:43 -07002197 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2198 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2199 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002200 if (newTask) {
2201 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2202 }
2203 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002204 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002205 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002206 if (!launchTaskBehind) {
2207 // Don't set focus on an activity that's going to the back.
2208 mService.setFocusedActivityLocked(r);
2209 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002210 return ActivityManager.START_SUCCESS;
2211 }
2212
Craig Mautneree36c772014-07-16 14:56:05 -07002213 final void doPendingActivityLaunchesLocked(boolean doResume) {
2214 while (!mPendingActivityLaunches.isEmpty()) {
2215 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002216 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002217 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002218 }
2219 }
2220
Craig Mautner7f13ed32014-07-28 14:00:35 -07002221 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002222 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2223 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002224 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002225 mPendingActivityLaunches.remove(palNdx);
2226 }
2227 }
2228 }
2229
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002230 void acquireLaunchWakelock() {
2231 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2232 throw new IllegalStateException("Calling must be system uid");
2233 }
2234 mLaunchingActivity.acquire();
2235 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2236 // To be safe, don't allow the wake lock to be held for too long.
2237 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2238 }
2239 }
2240
Craig Mautnerf3333272013-04-22 10:55:53 -07002241 // Checked.
2242 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2243 Configuration config) {
2244 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2245
Craig Mautnerf3333272013-04-22 10:55:53 -07002246 ArrayList<ActivityRecord> stops = null;
2247 ArrayList<ActivityRecord> finishes = null;
2248 ArrayList<UserStartedState> startingUsers = null;
2249 int NS = 0;
2250 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002251 boolean booting = false;
2252 boolean enableScreen = false;
2253 boolean activityRemoved = false;
2254
2255 ActivityRecord r = ActivityRecord.forToken(token);
2256 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002257 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2258 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002259 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2260 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002261 if (fromTimeout) {
2262 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002263 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002264
2265 // This is a hack to semi-deal with a race condition
2266 // in the client where it can be constructed with a
2267 // newer configuration from when we asked it to launch.
2268 // We'll update with whatever configuration it now says
2269 // it used to launch.
2270 if (config != null) {
2271 r.configuration = config;
2272 }
2273
2274 // We are now idle. If someone is waiting for a thumbnail from
2275 // us, we can now deliver.
2276 r.idle = true;
2277
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002278 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002279 if (isFrontStack(r.task.stack) || fromTimeout) {
2280 booting = mService.mBooting;
2281 mService.mBooting = false;
2282 if (!mService.mBooted) {
2283 mService.mBooted = true;
2284 enableScreen = true;
2285 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002286 }
2287 }
2288
2289 if (allResumedActivitiesIdle()) {
2290 if (r != null) {
2291 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002292 }
2293
2294 if (mLaunchingActivity.isHeld()) {
2295 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2296 if (VALIDATE_WAKE_LOCK_CALLER &&
2297 Binder.getCallingUid() != Process.myUid()) {
2298 throw new IllegalStateException("Calling must be system uid");
2299 }
2300 mLaunchingActivity.release();
2301 }
2302 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002303 }
2304
2305 // Atomically retrieve all of the other things to do.
2306 stops = processStoppingActivitiesLocked(true);
2307 NS = stops != null ? stops.size() : 0;
2308 if ((NF=mFinishingActivities.size()) > 0) {
2309 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2310 mFinishingActivities.clear();
2311 }
2312
Craig Mautnerf3333272013-04-22 10:55:53 -07002313 if (mStartingUsers.size() > 0) {
2314 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2315 mStartingUsers.clear();
2316 }
2317
Craig Mautnerf3333272013-04-22 10:55:53 -07002318 // Stop any activities that are scheduled to do so but have been
2319 // waiting for the next one to start.
2320 for (int i = 0; i < NS; i++) {
2321 r = stops.get(i);
2322 final ActivityStack stack = r.task.stack;
2323 if (r.finishing) {
2324 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2325 } else {
2326 stack.stopActivityLocked(r);
2327 }
2328 }
2329
2330 // Finish any activities that are scheduled to do so but have been
2331 // waiting for the next one to start.
2332 for (int i = 0; i < NF; i++) {
2333 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002334 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002335 }
2336
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002337 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002338 // Complete user switch
2339 if (startingUsers != null) {
2340 for (int i = 0; i < startingUsers.size(); i++) {
2341 mService.finishUserSwitch(startingUsers.get(i));
2342 }
2343 }
2344 // Complete starting up of background users
2345 if (mStartingBackgroundUsers.size() > 0) {
2346 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2347 mStartingBackgroundUsers.clear();
2348 for (int i = 0; i < startingUsers.size(); i++) {
2349 mService.finishUserBoot(startingUsers.get(i));
2350 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002351 }
2352 }
2353
2354 mService.trimApplications();
2355 //dump();
2356 //mWindowManager.dump();
2357
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002358 if (booting || enableScreen) {
2359 mService.postFinishBooting(booting, enableScreen);
Craig Mautnerf3333272013-04-22 10:55:53 -07002360 }
2361
2362 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002363 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002364 }
2365
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002366 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002367 }
2368
Craig Mautner8e569572013-10-11 17:36:59 -07002369 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002370 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002371 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2372 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002373 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2374 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2375 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002376 }
Craig Mautner19091252013-10-05 00:03:53 -07002377 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002378 }
2379
2380 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002381 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2382 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002383 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2384 stacks.get(stackNdx).closeSystemDialogsLocked();
2385 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002386 }
2387 }
2388
Craig Mautner93529a42013-10-04 15:03:13 -07002389 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002390 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002391 }
2392
Craig Mautner8d341ef2013-03-26 09:03:27 -07002393 /**
2394 * @return true if some activity was finished (or would have finished if doit were true).
2395 */
2396 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2397 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002398 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2399 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002400 final int numStacks = stacks.size();
2401 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2402 final ActivityStack stack = stacks.get(stackNdx);
2403 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2404 didSomething = true;
2405 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002406 }
2407 }
2408 return didSomething;
2409 }
2410
Dianne Hackborna413dc02013-07-12 12:02:55 -07002411 void updatePreviousProcessLocked(ActivityRecord r) {
2412 // Now that this process has stopped, we may want to consider
2413 // it to be the previous app to try to keep around in case
2414 // the user wants to return to it.
2415
2416 // First, found out what is currently the foreground app, so that
2417 // we don't blow away the previous app if this activity is being
2418 // hosted by the process that is actually still the foreground.
2419 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002420 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2421 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002422 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2423 final ActivityStack stack = stacks.get(stackNdx);
2424 if (isFrontStack(stack)) {
2425 if (stack.mResumedActivity != null) {
2426 fgApp = stack.mResumedActivity.app;
2427 } else if (stack.mPausingActivity != null) {
2428 fgApp = stack.mPausingActivity.app;
2429 }
2430 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002431 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002432 }
2433 }
2434
2435 // Now set this one as the previous process, only if that really
2436 // makes sense to.
2437 if (r.app != null && fgApp != null && r.app != fgApp
2438 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002439 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002440 mService.mPreviousProcess = r.app;
2441 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2442 }
2443 }
2444
Craig Mautner05d29032013-05-03 13:40:13 -07002445 boolean resumeTopActivitiesLocked() {
2446 return resumeTopActivitiesLocked(null, null, null);
2447 }
2448
2449 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2450 Bundle targetOptions) {
2451 if (targetStack == null) {
2452 targetStack = getFocusedStack();
2453 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002454 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002455 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002456 if (isFrontStack(targetStack)) {
2457 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2458 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002459 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2460 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002461 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2462 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002463 if (stack == targetStack) {
2464 // Already started above.
2465 continue;
2466 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002467 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002468 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002469 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002470 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002471 }
Craig Mautner05d29032013-05-03 13:40:13 -07002472 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002473 }
2474
Todd Kennedy539db512014-12-15 09:57:55 -08002475 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002476 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2477 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002478 final int numStacks = stacks.size();
2479 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2480 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002481 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002482 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002483 }
2484 }
2485
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002486 void finishVoiceTask(IVoiceInteractionSession session) {
2487 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2488 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2489 final int numStacks = stacks.size();
2490 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2491 final ActivityStack stack = stacks.get(stackNdx);
2492 stack.finishVoiceTask(session);
2493 }
2494 }
2495 }
2496
Craig Mautneraea74a52014-03-08 14:23:10 -08002497 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2498 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2499 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002500 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002501 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2502 // Caller wants the home activity moved with it. To accomplish this,
2503 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002504 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002505 }
2506 task.stack.moveTaskToFrontLocked(task, null, options);
2507 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2508 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002509 }
2510
Craig Mautner967212c2013-04-13 21:10:58 -07002511 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002512 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2513 if (activityContainer != null) {
2514 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002515 }
2516 return null;
2517 }
2518
Craig Mautner967212c2013-04-13 21:10:58 -07002519 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002520 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002521 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2522 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002523 }
2524 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002525 }
2526
Craig Mautner4a1cb222013-12-04 16:14:06 -08002527 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002528 ActivityRecord homeActivity = getHomeActivity();
2529 if (homeActivity != null) {
2530 return homeActivity.appToken;
2531 }
2532 return null;
2533 }
2534
2535 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002536 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2537 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2538 final TaskRecord task = tasks.get(taskNdx);
2539 if (task.isHomeTask()) {
2540 final ArrayList<ActivityRecord> activities = task.mActivities;
2541 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2542 final ActivityRecord r = activities.get(activityNdx);
2543 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002544 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002545 }
2546 }
2547 }
2548 }
2549 return null;
2550 }
2551
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002552 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002553 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002554 ActivityContainer activityContainer =
2555 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002556 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002557 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002558 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002559 return activityContainer;
2560 }
2561
Craig Mautner34b73df2014-01-12 21:11:08 -08002562 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002563 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2564 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2565 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002566 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2567 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002568 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002569 }
2570 }
2571
Craig Mautner95da1082014-02-24 17:54:35 -08002572 void deleteActivityContainer(IActivityContainer container) {
2573 ActivityContainer activityContainer = (ActivityContainer)container;
2574 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002575 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2576 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002577 final int stackId = activityContainer.mStackId;
2578 mActivityContainers.remove(stackId);
2579 mWindowManager.removeStack(stackId);
2580 }
2581 }
2582
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002583 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002584 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2585 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002586 return -1;
2587 }
2588
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002589 ActivityContainer activityContainer = new ActivityContainer(stackId);
2590 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002591 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002592 return stackId;
2593 }
2594
2595 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002596 while (true) {
2597 if (++mLastStackId <= HOME_STACK_ID) {
2598 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002599 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002600 if (getStack(mLastStackId) == null) {
2601 break;
2602 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002603 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002604 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002605 }
2606
Wale Ogunwale7de05352014-12-12 15:21:33 -08002607 private boolean restoreRecentTaskLocked(TaskRecord task) {
2608 ActivityStack stack = null;
2609 // Determine stack to restore task to.
2610 if (mLeanbackOnlyDevice) {
2611 // There is only one stack for lean back devices.
2612 stack = mHomeStack;
2613 } else {
2614 // Look for the top stack on the home display that isn't the home stack.
2615 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2616 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2617 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
2618 if (!tmpStack.isHomeStack()) {
2619 stack = tmpStack;
2620 break;
2621 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002622 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002623 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002624
2625 if (stack == null) {
2626 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2627 // before an application stack is created...Go ahead and create one on the default
2628 // display.
2629 stack = getStack(createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY));
2630 if (DEBUG_RECENTS)
2631 Slog.v(TAG, "Created stack=" + stack + " for recents restoration.");
2632 }
2633
2634 if (stack == null) {
2635 // What does this mean??? Not sure how we would get here...
2636 if (DEBUG_RECENTS)
2637 Slog.v(TAG, "Unable to find/create stack to restore recent task=" + task);
2638 return false;
2639 }
2640
2641 stack.addTask(task, false, false);
2642 if (DEBUG_RECENTS)
2643 Slog.v(TAG, "Added restored task=" + task + " to stack=" + stack);
2644 final ArrayList<ActivityRecord> activities = task.mActivities;
2645 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2646 final ActivityRecord r = activities.get(activityNdx);
2647 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2648 r.info.screenOrientation, r.fullscreen,
2649 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2650 r.userId, r.info.configChanges, task.voiceSession != null,
2651 r.mLaunchTaskBehind);
2652 }
2653 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002654 }
2655
Craig Mautner8d341ef2013-03-26 09:03:27 -07002656 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002657 final TaskRecord task = anyTaskForIdLocked(taskId);
2658 if (task == null) {
2659 return;
2660 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002661 final ActivityStack stack = getStack(stackId);
2662 if (stack == null) {
2663 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2664 return;
2665 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002666 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002667 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002668 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002669 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002670 }
2671
Craig Mautnerac6f8432013-07-17 13:24:59 -07002672 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002673 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002674 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2675 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002676 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2677 final ActivityStack stack = stacks.get(stackNdx);
2678 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002679 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2680 continue;
2681 }
2682 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2683 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2684 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002685 continue;
2686 }
2687 final ActivityRecord ar = stack.findTaskLocked(r);
2688 if (ar != null) {
2689 return ar;
2690 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002691 }
2692 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002693 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002694 return null;
2695 }
2696
2697 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002698 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2699 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002700 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2701 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2702 if (ar != null) {
2703 return ar;
2704 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002705 }
2706 }
2707 return null;
2708 }
2709
Craig Mautner8d341ef2013-03-26 09:03:27 -07002710 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002711 scheduleSleepTimeout();
2712 if (!mGoingToSleep.isHeld()) {
2713 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002714 if (mLaunchingActivity.isHeld()) {
2715 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2716 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002717 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002718 mLaunchingActivity.release();
2719 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002720 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002721 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002722 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002723 }
2724
2725 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002726 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002727
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002728 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002729 final long endTime = System.currentTimeMillis() + timeout;
2730 while (true) {
2731 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002732 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2733 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002734 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2735 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2736 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002737 }
2738 if (cantShutdown) {
2739 long timeRemaining = endTime - System.currentTimeMillis();
2740 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002741 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002742 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002743 } catch (InterruptedException e) {
2744 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002745 } else {
2746 Slog.w(TAG, "Activity manager shutdown timed out");
2747 timedout = true;
2748 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002749 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002750 } else {
2751 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002752 }
2753 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002754
2755 // Force checkReadyForSleep to complete.
2756 mSleepTimeout = true;
2757 checkReadyForSleepLocked();
2758
Craig Mautner8d341ef2013-03-26 09:03:27 -07002759 return timedout;
2760 }
2761
2762 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002763 removeSleepTimeouts();
2764 if (mGoingToSleep.isHeld()) {
2765 mGoingToSleep.release();
2766 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2768 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2770 final ActivityStack stack = stacks.get(stackNdx);
2771 stack.awakeFromSleepingLocked();
2772 if (isFrontStack(stack)) {
2773 resumeTopActivitiesLocked();
2774 }
Craig Mautner5314a402013-09-26 12:40:16 -07002775 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002776 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002777 mGoingToSleepActivities.clear();
2778 }
2779
2780 void activitySleptLocked(ActivityRecord r) {
2781 mGoingToSleepActivities.remove(r);
2782 checkReadyForSleepLocked();
2783 }
2784
2785 void checkReadyForSleepLocked() {
2786 if (!mService.isSleepingOrShuttingDown()) {
2787 // Do not care.
2788 return;
2789 }
2790
2791 if (!mSleepTimeout) {
2792 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002793 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2794 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002795 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2796 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2797 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002798 }
2799
2800 if (mStoppingActivities.size() > 0) {
2801 // Still need to tell some activities to stop; can't sleep yet.
2802 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2803 + mStoppingActivities.size() + " activities");
2804 scheduleIdleLocked();
2805 dontSleep = true;
2806 }
2807
2808 if (mGoingToSleepActivities.size() > 0) {
2809 // Still need to tell some activities to sleep; can't sleep yet.
2810 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2811 + mGoingToSleepActivities.size() + " activities");
2812 dontSleep = true;
2813 }
2814
2815 if (dontSleep) {
2816 return;
2817 }
2818 }
2819
Craig Mautnere0a38842013-12-16 16:14:02 -08002820 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2821 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002822 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2823 stacks.get(stackNdx).goToSleep();
2824 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002825 }
2826
2827 removeSleepTimeouts();
2828
2829 if (mGoingToSleep.isHeld()) {
2830 mGoingToSleep.release();
2831 }
2832 if (mService.mShuttingDown) {
2833 mService.notifyAll();
2834 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002835 }
2836
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002837 boolean reportResumedActivityLocked(ActivityRecord r) {
2838 final ActivityStack stack = r.task.stack;
2839 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002840 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002841 }
2842 if (allResumedActivitiesComplete()) {
2843 ensureActivitiesVisibleLocked(null, 0);
2844 mWindowManager.executeAppTransition();
2845 return true;
2846 }
2847 return false;
2848 }
2849
Craig Mautner8d341ef2013-03-26 09:03:27 -07002850 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002851 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2852 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002853 final int numStacks = stacks.size();
2854 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2855 final ActivityStack stack = stacks.get(stackNdx);
2856 stack.handleAppCrashLocked(app);
2857 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002858 }
2859 }
2860
Jose Lima4b6c6692014-08-12 17:41:12 -07002861 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002862 final ActivityStack stack = r.task.stack;
2863 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002864 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2865 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002866 return false;
2867 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002868 final boolean isVisible = stack.hasVisibleBehindActivity();
2869 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2870 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002871
2872 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002873 if (top == null || top == r || (visible == isVisible)) {
2874 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2875 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002876 return true;
2877 }
2878
2879 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002880 if (visible && top.fullscreen) {
2881 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07002882 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07002883 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07002884 " top.app.thread=" + top.app.thread);
2885 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002886 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2887 // Only the activity set as currently visible behind should actively reset its
2888 // visible behind state.
2889 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
2890 + visible + " stack.getVisibleBehindActivity()=" +
2891 stack.getVisibleBehindActivity() + " r=" + r);
2892 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002893 }
2894
Jose Lima4b6c6692014-08-12 17:41:12 -07002895 stack.setVisibleBehindActivity(visible ? r : null);
2896 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002897 // Make the activity immediately above r opaque.
2898 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2899 if (next != null) {
2900 mService.convertFromTranslucent(next.appToken);
2901 }
2902 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002903 if (top.app != null && top.app.thread != null) {
2904 // Notify the top app of the change.
2905 try {
2906 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2907 } catch (RemoteException e) {
2908 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002909 }
2910 return true;
2911 }
2912
Craig Mautnerbb742462014-07-07 15:28:55 -07002913 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2914 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2915 r.mLaunchTaskBehind = false;
2916 final TaskRecord task = r.task;
2917 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002918 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002919 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002920 mWindowManager.setAppVisibility(r.appToken, false);
2921 }
2922
2923 void scheduleLaunchTaskBehindComplete(IBinder token) {
2924 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2925 }
2926
Craig Mautnerde4ef022013-04-07 19:01:33 -07002927 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002928 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002929 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2930 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002931 final int topStackNdx = stacks.size() - 1;
2932 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2933 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002934 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002935 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002936 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002937 }
2938
2939 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002940 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2941 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002942 final int numStacks = stacks.size();
2943 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2944 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002945 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002947 }
2948 }
2949
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002950 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2951 // Examine all activities currently running in the process.
2952 TaskRecord firstTask = null;
2953 // Tasks is non-null only if two or more tasks are found.
2954 ArraySet<TaskRecord> tasks = null;
2955 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
2956 for (int i=0; i<app.activities.size(); i++) {
2957 ActivityRecord r = app.activities.get(i);
2958 // First, if we find an activity that is in the process of being destroyed,
2959 // then we just aren't going to do anything for now; we want things to settle
2960 // down before we try to prune more activities.
2961 if (r.finishing || r.state == ActivityState.DESTROYING
2962 || r.state == ActivityState.DESTROYED) {
2963 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
2964 return;
2965 }
2966 // Don't consider any activies that are currently not in a state where they
2967 // can be destroyed.
2968 if (r.visible || !r.stopped || !r.haveState
2969 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
2970 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
2971 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
2972 continue;
2973 }
2974 if (r.task != null) {
2975 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
2976 + " from " + r);
2977 if (firstTask == null) {
2978 firstTask = r.task;
2979 } else if (firstTask != r.task) {
2980 if (tasks == null) {
2981 tasks = new ArraySet<>();
2982 tasks.add(firstTask);
2983 }
2984 tasks.add(r.task);
2985 }
2986 }
2987 }
2988 if (tasks == null) {
2989 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
2990 return;
2991 }
2992 // If we have activities in multiple tasks that are in a position to be destroyed,
2993 // let's iterate through the tasks and release the oldest one.
2994 final int numDisplays = mActivityDisplays.size();
2995 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2996 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2997 // Step through all stacks starting from behind, to hit the oldest things first.
2998 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2999 final ActivityStack stack = stacks.get(stackNdx);
3000 // Try to release activities in this stack; if we manage to, we are done.
3001 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3002 return;
3003 }
3004 }
3005 }
3006 }
3007
Craig Mautner8d341ef2013-03-26 09:03:27 -07003008 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003009 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
3010 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003011 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003012
Craig Mautner858d8a62013-04-23 17:08:34 -07003013 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003014 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3015 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003016 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003017 final ActivityStack stack = stacks.get(stackNdx);
3018 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003019 TaskRecord task = stack.topTask();
3020 if (task != null) {
3021 mWindowManager.moveTaskToTop(task.taskId);
3022 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003023 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003024 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003025
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003026 ActivityStack stack = getStack(restoreStackId);
3027 if (stack == null) {
3028 stack = mHomeStack;
3029 }
3030 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003031 if (stack.isOnHomeDisplay()) {
3032 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003033 TaskRecord task = stack.topTask();
3034 if (task != null) {
3035 mWindowManager.moveTaskToTop(task.taskId);
3036 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003037 } else {
3038 // Stack was moved to another display while user was swapped out.
Craig Mautner84984fa2014-06-19 11:19:20 -07003039 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003040 }
Craig Mautner93529a42013-10-04 15:03:13 -07003041 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003042 }
3043
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003044 /**
3045 * Add background users to send boot completed events to.
3046 * @param userId The user being started in the background
3047 * @param uss The state object for the user.
3048 */
3049 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3050 mStartingBackgroundUsers.add(uss);
3051 }
3052
Craig Mautnerde4ef022013-04-07 19:01:33 -07003053 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
3054 int N = mStoppingActivities.size();
3055 if (N <= 0) return null;
3056
3057 ArrayList<ActivityRecord> stops = null;
3058
3059 final boolean nowVisible = allResumedActivitiesVisible();
3060 for (int i=0; i<N; i++) {
3061 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07003062 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07003063 + nowVisible + " waitingVisible=" + s.waitingVisible
3064 + " finishing=" + s.finishing);
3065 if (s.waitingVisible && nowVisible) {
3066 mWaitingVisibleActivities.remove(s);
3067 s.waitingVisible = false;
3068 if (s.finishing) {
3069 // If this activity is finishing, it is sitting on top of
3070 // everyone else but we now know it is no longer needed...
3071 // so get rid of it. Otherwise, we need to go through the
3072 // normal flow and hide it once we determine that it is
3073 // hidden by the activities in front of it.
3074 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003075 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003076 }
3077 }
3078 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
3079 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3080 if (stops == null) {
3081 stops = new ArrayList<ActivityRecord>();
3082 }
3083 stops.add(s);
3084 mStoppingActivities.remove(i);
3085 N--;
3086 i--;
3087 }
3088 }
3089
3090 return stops;
3091 }
3092
Craig Mautnercf910b02013-04-23 11:23:27 -07003093 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003094 // FIXME
3095/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3096 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003097 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003098 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003099 if (isFrontStack(stack)) {
3100 if (r == null) {
3101 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3102 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003103 final ActivityRecord pausing = stack.mPausingActivity;
3104 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003105 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003106 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003107 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003108 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003109 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003110 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003111 }
3112 }
3113 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003114 final ActivityRecord resumed = stack.mResumedActivity;
3115 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003116 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003117 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003118 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003119 if (r != null && (state == ActivityState.INITIALIZING
3120 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003121 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003122 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003123 }
3124 }
3125 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003126*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003127 }
3128
Craig Mautner27084302013-03-25 08:05:25 -07003129 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003130 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003131 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003132 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3133 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3134 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003135 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003136 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003137
Craig Mautner20e72272013-04-01 13:45:53 -07003138 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003139 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003140 }
3141
Dianne Hackborn390517b2013-05-30 15:03:32 -07003142 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3143 boolean needSep, String prefix) {
3144 if (activity != null) {
3145 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3146 if (needSep) {
3147 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003148 }
3149 pw.print(prefix);
3150 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003151 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003152 }
3153 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003154 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003155 }
3156
Craig Mautner8d341ef2013-03-26 09:03:27 -07003157 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3158 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003159 boolean printed = false;
3160 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003161 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3162 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003163 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003164 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003165 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003166 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003167 final ActivityStack stack = stacks.get(stackNdx);
3168 StringBuilder stackHeader = new StringBuilder(128);
3169 stackHeader.append(" Stack #");
3170 stackHeader.append(stack.mStackId);
3171 stackHeader.append(":");
3172 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3173 needSep, stackHeader.toString());
3174 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3175 !dumpAll, false, dumpPackage, true,
3176 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003177
Craig Mautner4a1cb222013-12-04 16:14:06 -08003178 needSep = printed;
3179 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3180 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003181 if (pr) {
3182 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003183 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003184 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003185 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3186 " mResumedActivity: ");
3187 if (pr) {
3188 printed = true;
3189 needSep = false;
3190 }
3191 if (dumpAll) {
3192 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3193 " mLastPausedActivity: ");
3194 if (pr) {
3195 printed = true;
3196 needSep = true;
3197 }
3198 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3199 needSep, " mLastNoHistoryActivity: ");
3200 }
3201 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003202 }
3203 }
3204
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003205 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3206 false, dumpPackage, true, " Activities waiting to finish:", null);
3207 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3208 false, dumpPackage, true, " Activities waiting to stop:", null);
3209 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3210 false, dumpPackage, true, " Activities waiting for another to become visible:",
3211 null);
3212 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3213 false, dumpPackage, true, " Activities waiting to sleep:", null);
3214 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3215 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003216
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003217 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003218 }
3219
Dianne Hackborn390517b2013-05-30 15:03:32 -07003220 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003221 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003222 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003223 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003224 String innerPrefix = null;
3225 String[] args = null;
3226 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003227 for (int i=list.size()-1; i>=0; i--) {
3228 final ActivityRecord r = list.get(i);
3229 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3230 continue;
3231 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003232 if (innerPrefix == null) {
3233 innerPrefix = prefix + " ";
3234 args = new String[0];
3235 }
3236 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003237 final boolean full = !brief && (complete || !r.isInHistory());
3238 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003239 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003240 needNL = false;
3241 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003242 if (header1 != null) {
3243 pw.println(header1);
3244 header1 = null;
3245 }
3246 if (header2 != null) {
3247 pw.println(header2);
3248 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003249 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003250 if (lastTask != r.task) {
3251 lastTask = r.task;
3252 pw.print(prefix);
3253 pw.print(full ? "* " : " ");
3254 pw.println(lastTask);
3255 if (full) {
3256 lastTask.dump(pw, prefix + " ");
3257 } else if (complete) {
3258 // Complete + brief == give a summary. Isn't that obvious?!?
3259 if (lastTask.intent != null) {
3260 pw.print(prefix); pw.print(" ");
3261 pw.println(lastTask.intent.toInsecureStringWithClip());
3262 }
3263 }
3264 }
3265 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3266 pw.print(" #"); pw.print(i); pw.print(": ");
3267 pw.println(r);
3268 if (full) {
3269 r.dump(pw, innerPrefix);
3270 } else if (complete) {
3271 // Complete + brief == give a summary. Isn't that obvious?!?
3272 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3273 if (r.app != null) {
3274 pw.print(innerPrefix); pw.println(r.app);
3275 }
3276 }
3277 if (client && r.app != null && r.app.thread != null) {
3278 // flush anything that is already in the PrintWriter since the thread is going
3279 // to write to the file descriptor directly
3280 pw.flush();
3281 try {
3282 TransferPipe tp = new TransferPipe();
3283 try {
3284 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3285 r.appToken, innerPrefix, args);
3286 // Short timeout, since blocking here can
3287 // deadlock with the application.
3288 tp.go(fd, 2000);
3289 } finally {
3290 tp.kill();
3291 }
3292 } catch (IOException e) {
3293 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3294 } catch (RemoteException e) {
3295 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3296 }
3297 needNL = true;
3298 }
3299 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003300 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003301 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003302
Craig Mautnerf3333272013-04-22 10:55:53 -07003303 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003304 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003305 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3306 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003307 }
3308
3309 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003310 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003311 }
3312
3313 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003314 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003315 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3316 }
3317
Craig Mautner05d29032013-05-03 13:40:13 -07003318 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003319 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3320 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3321 }
Craig Mautner05d29032013-05-03 13:40:13 -07003322 }
3323
Craig Mautner0eea92c2013-05-16 13:35:39 -07003324 void removeSleepTimeouts() {
3325 mSleepTimeout = false;
3326 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3327 }
3328
3329 final void scheduleSleepTimeout() {
3330 removeSleepTimeouts();
3331 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3332 }
3333
Craig Mautner4a1cb222013-12-04 16:14:06 -08003334 @Override
3335 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003336 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003337 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3338 }
3339
3340 @Override
3341 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003342 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003343 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3344 }
3345
3346 @Override
3347 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003348 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003349 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3350 }
3351
3352 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003353 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003354 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003355 newDisplay = mActivityDisplays.get(displayId) == null;
3356 if (newDisplay) {
3357 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003358 if (activityDisplay.mDisplay == null) {
3359 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3360 return;
3361 }
Craig Mautner4504de52013-12-20 09:06:56 -08003362 mActivityDisplays.put(displayId, activityDisplay);
3363 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003364 }
Craig Mautner4504de52013-12-20 09:06:56 -08003365 if (newDisplay) {
3366 mWindowManager.onDisplayAdded(displayId);
3367 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003368 }
3369
3370 public void handleDisplayRemovedLocked(int displayId) {
3371 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003372 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3373 if (activityDisplay != null) {
3374 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003375 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003376 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003377 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003378 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003379 }
3380 }
3381 mWindowManager.onDisplayRemoved(displayId);
3382 }
3383
3384 public void handleDisplayChangedLocked(int displayId) {
3385 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003386 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3387 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003388 // TODO: Update the bounds.
3389 }
3390 }
3391 mWindowManager.onDisplayChanged(displayId);
3392 }
3393
3394 StackInfo getStackInfo(ActivityStack stack) {
3395 StackInfo info = new StackInfo();
3396 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3397 info.displayId = Display.DEFAULT_DISPLAY;
3398 info.stackId = stack.mStackId;
3399
3400 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3401 final int numTasks = tasks.size();
3402 int[] taskIds = new int[numTasks];
3403 String[] taskNames = new String[numTasks];
3404 for (int i = 0; i < numTasks; ++i) {
3405 final TaskRecord task = tasks.get(i);
3406 taskIds[i] = task.taskId;
3407 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3408 : task.realActivity != null ? task.realActivity.flattenToString()
3409 : task.getTopActivity() != null ? task.getTopActivity().packageName
3410 : "unknown";
3411 }
3412 info.taskIds = taskIds;
3413 info.taskNames = taskNames;
3414 return info;
3415 }
3416
3417 StackInfo getStackInfoLocked(int stackId) {
3418 ActivityStack stack = getStack(stackId);
3419 if (stack != null) {
3420 return getStackInfo(stack);
3421 }
3422 return null;
3423 }
3424
3425 ArrayList<StackInfo> getAllStackInfosLocked() {
3426 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003427 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3428 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003429 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3430 list.add(getStackInfo(stacks.get(ndx)));
3431 }
3432 }
3433 return list;
3434 }
3435
Jason Monka8f569c2014-07-07 12:15:21 -04003436 void showLockTaskToast() {
3437 mLockTaskNotify.showToast(mLockTaskIsLocked);
3438 }
3439
3440 void setLockTaskModeLocked(TaskRecord task, boolean isLocked) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003441 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003442 // Take out of lock task mode if necessary
3443 if (mLockTaskModeTask != null) {
3444 final Message lockTaskMsg = Message.obtain();
3445 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3446 lockTaskMsg.what = LOCK_TASK_END_MSG;
3447 mLockTaskModeTask = null;
3448 mHandler.sendMessage(lockTaskMsg);
3449 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003450 return;
3451 }
3452 if (isLockTaskModeViolation(task)) {
3453 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3454 return;
3455 }
3456 mLockTaskModeTask = task;
3457 findTaskToMoveToFrontLocked(task, 0, null);
3458 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003459
3460 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003461 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3462 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003463 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003464 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003465 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003466 }
3467
3468 boolean isLockTaskModeViolation(TaskRecord task) {
3469 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3470 }
3471
3472 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3473 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003474 final Message lockTaskMsg = Message.obtain();
3475 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3476 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003477 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003478 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003479 }
3480 }
3481
3482 boolean isInLockTaskMode() {
3483 return mLockTaskModeTask != null;
3484 }
3485
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003486 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003487
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003488 public ActivityStackSupervisorHandler(Looper looper) {
3489 super(looper);
3490 }
3491
Craig Mautnerf3333272013-04-22 10:55:53 -07003492 void activityIdleInternal(ActivityRecord r) {
3493 synchronized (mService) {
3494 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3495 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003496 }
3497
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003498 @Override
3499 public void handleMessage(Message msg) {
3500 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003501 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003502 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003503 if (mService.mDidDexOpt) {
3504 mService.mDidDexOpt = false;
3505 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3506 nmsg.obj = msg.obj;
3507 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3508 return;
3509 }
3510 // We don't at this point know if the activity is fullscreen,
3511 // so we need to be conservative and assume it isn't.
3512 activityIdleInternal((ActivityRecord)msg.obj);
3513 } break;
3514 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003515 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003516 activityIdleInternal((ActivityRecord)msg.obj);
3517 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003518 case RESUME_TOP_ACTIVITY_MSG: {
3519 synchronized (mService) {
3520 resumeTopActivitiesLocked();
3521 }
3522 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003523 case SLEEP_TIMEOUT_MSG: {
3524 synchronized (mService) {
3525 if (mService.isSleepingOrShuttingDown()) {
3526 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3527 mSleepTimeout = true;
3528 checkReadyForSleepLocked();
3529 }
3530 }
3531 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003532 case LAUNCH_TIMEOUT_MSG: {
3533 if (mService.mDidDexOpt) {
3534 mService.mDidDexOpt = false;
3535 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3536 return;
3537 }
3538 synchronized (mService) {
3539 if (mLaunchingActivity.isHeld()) {
3540 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3541 if (VALIDATE_WAKE_LOCK_CALLER
3542 && Binder.getCallingUid() != Process.myUid()) {
3543 throw new IllegalStateException("Calling must be system uid");
3544 }
3545 mLaunchingActivity.release();
3546 }
3547 }
3548 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003549 case HANDLE_DISPLAY_ADDED: {
3550 handleDisplayAddedLocked(msg.arg1);
3551 } break;
3552 case HANDLE_DISPLAY_CHANGED: {
3553 handleDisplayChangedLocked(msg.arg1);
3554 } break;
3555 case HANDLE_DISPLAY_REMOVED: {
3556 handleDisplayRemovedLocked(msg.arg1);
3557 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003558 case CONTAINER_CALLBACK_VISIBILITY: {
3559 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003560 final IActivityContainerCallback callback = container.mCallback;
3561 if (callback != null) {
3562 try {
3563 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3564 } catch (RemoteException e) {
3565 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003566 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003567 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003568 case LOCK_TASK_START_MSG: {
3569 // When lock task starts, we disable the status bars.
3570 try {
Jason Monk62515be2014-05-21 16:06:19 -04003571 if (mLockTaskNotify == null) {
3572 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003573 }
Jason Monk62515be2014-05-21 16:06:19 -04003574 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003575 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003576 if (getStatusBarService() != null) {
3577 int flags =
3578 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003579 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003580 flags ^= StatusBarManager.DISABLE_HOME
3581 | StatusBarManager.DISABLE_RECENT;
3582 }
3583 getStatusBarService().disable(flags, mToken,
3584 mService.mContext.getPackageName());
3585 }
3586 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003587 if (getDevicePolicyManager() != null) {
3588 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003589 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003590 }
justinzhang5286d3f2014-05-12 17:06:01 -04003591 } catch (RemoteException ex) {
3592 throw new RuntimeException(ex);
3593 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003594 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003595 case LOCK_TASK_END_MSG: {
3596 // When lock task ends, we enable the status bars.
3597 try {
Jason Monk62515be2014-05-21 16:06:19 -04003598 if (getStatusBarService() != null) {
3599 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3600 mService.mContext.getPackageName());
3601 }
3602 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003603 if (getDevicePolicyManager() != null) {
3604 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3605 msg.arg1);
3606 }
Jason Monk62515be2014-05-21 16:06:19 -04003607 if (mLockTaskNotify == null) {
3608 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3609 }
3610 mLockTaskNotify.show(false);
3611 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003612 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003613 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003614 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003615 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003616 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003617 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003618 new LockPatternUtils(mService.mContext)
3619 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003620 }
3621 } catch (SettingNotFoundException e) {
3622 // No setting, don't lock.
3623 }
justinzhang5286d3f2014-05-12 17:06:01 -04003624 } catch (RemoteException ex) {
3625 throw new RuntimeException(ex);
3626 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003627 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003628 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3629 final ActivityContainer container = (ActivityContainer) msg.obj;
3630 final IActivityContainerCallback callback = container.mCallback;
3631 if (callback != null) {
3632 try {
3633 callback.onAllActivitiesComplete(container.asBinder());
3634 } catch (RemoteException e) {
3635 }
3636 }
3637 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003638 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3639 synchronized (mService) {
3640 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3641 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003642 final ActivityContainer container = (ActivityContainer) msg.obj;
3643 container.mStack.finishAllActivitiesLocked(true);
3644 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003645 }
3646 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003647 case LAUNCH_TASK_BEHIND_COMPLETE: {
3648 synchronized (mService) {
3649 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3650 if (r != null) {
3651 handleLaunchTaskBehindCompleteLocked(r);
3652 }
3653 }
3654 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003655 }
3656 }
3657 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003658
Ying Wangb081a592014-04-22 15:20:16 -07003659 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003660 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003661 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003662 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003663 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003664 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003665 ActivityRecord mParentActivity = null;
3666 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003667
Craig Mautnere3a00d72014-04-16 08:31:19 -07003668 boolean mVisible = true;
3669
Craig Mautner4a1cb222013-12-04 16:14:06 -08003670 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003671 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003672
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003673 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3674 final static int CONTAINER_STATE_NO_SURFACE = 1;
3675 final static int CONTAINER_STATE_FINISHING = 2;
3676 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3677
3678 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003679 synchronized (mService) {
3680 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003681 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003682 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003683 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003684 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003685 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003686
Craig Mautnere0a38842013-12-16 16:14:02 -08003687 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003688 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3689 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003690 mActivityDisplay = activityDisplay;
3691 mStack.mDisplayId = activityDisplay.mDisplayId;
3692 mStack.mStacks = activityDisplay.mStacks;
3693
3694 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003695 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003696 }
3697
3698 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003699 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003700 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003701 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3702 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003703 return;
3704 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003705 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003706 }
3707 }
3708
3709 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003710 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003711 synchronized (mService) {
3712 if (mActivityDisplay != null) {
3713 return mActivityDisplay.mDisplayId;
3714 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003715 }
3716 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003717 }
3718
Jeff Brownca9bc702014-02-11 14:32:56 -08003719 @Override
3720 public boolean injectEvent(InputEvent event) {
3721 final long origId = Binder.clearCallingIdentity();
3722 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003723 synchronized (mService) {
3724 if (mActivityDisplay != null) {
3725 return mInputManagerInternal.injectInputEvent(event,
3726 mActivityDisplay.mDisplayId,
3727 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3728 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003729 }
3730 return false;
3731 } finally {
3732 Binder.restoreCallingIdentity(origId);
3733 }
3734 }
3735
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003736 @Override
3737 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003738 synchronized (mService) {
3739 if (mContainerState == CONTAINER_STATE_FINISHING) {
3740 return;
3741 }
3742 mContainerState = CONTAINER_STATE_FINISHING;
3743
3744 final Message msg =
3745 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003746 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003747
3748 long origId = Binder.clearCallingIdentity();
3749 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003750 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003751 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003752 } finally {
3753 Binder.restoreCallingIdentity(origId);
3754 }
3755 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003756 }
3757
Craig Mautner60257702014-09-17 15:02:33 -07003758 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003759 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3760 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003761 if (mActivityDisplay != null) {
3762 mActivityDisplay.detachActivitiesLocked(mStack);
3763 mActivityDisplay = null;
3764 mStack.mDisplayId = -1;
3765 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003766 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003767 }
3768 }
3769
3770 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003771 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003772 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003773 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003774 Binder.getCallingUid(), mCurrentUser, false,
3775 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003776 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003777 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003778 String mimeType = intent.getType();
3779 if (mimeType == null && intent.getData() != null
3780 && "content".equals(intent.getData().getScheme())) {
3781 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3782 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003783 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0,
3784 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003785 }
3786
3787 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003788 public final int startActivityIntentSender(IIntentSender intentSender) {
3789 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3790
3791 if (!(intentSender instanceof PendingIntentRecord)) {
3792 throw new IllegalArgumentException("Bad PendingIntent object");
3793 }
3794
3795 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003796 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003797 }
3798
Craig Mautner247ab652014-04-25 10:09:00 -07003799 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3800 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003801 Binder.getCallingUid(), mCurrentUser, false,
3802 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003803 if (resolvedType == null) {
3804 resolvedType = intent.getType();
3805 if (resolvedType == null && intent.getData() != null
3806 && "content".equals(intent.getData().getScheme())) {
3807 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3808 }
3809 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003810 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003811 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003812 throw new SecurityException(
3813 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3814 }
3815 }
3816
3817 /** Throw a SecurityException if allowEmbedded is not true */
3818 @Override
3819 public final void checkEmbeddedAllowed(Intent intent) {
3820 checkEmbeddedAllowedInner(intent, null);
3821 }
3822
3823 /** Throw a SecurityException if allowEmbedded is not true */
3824 @Override
3825 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3826 if (!(intentSender instanceof PendingIntentRecord)) {
3827 throw new IllegalArgumentException("Bad PendingIntent object");
3828 }
3829 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3830 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3831 pendingIntent.key.requestResolvedType);
3832 }
3833
Craig Mautnerdf88d732014-01-27 09:21:32 -08003834 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003835 public IBinder asBinder() {
3836 return this;
3837 }
3838
Craig Mautner4504de52013-12-20 09:06:56 -08003839 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003840 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003841 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003842 }
3843
Craig Mautner4a1cb222013-12-04 16:14:06 -08003844 ActivityStackSupervisor getOuter() {
3845 return ActivityStackSupervisor.this;
3846 }
3847
Craig Mautnerd163e752014-06-13 17:18:47 -07003848 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003849 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003850 }
3851
3852 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003853 synchronized (mService) {
3854 if (mActivityDisplay != null) {
3855 mActivityDisplay.getBounds(outBounds);
3856 } else {
3857 outBounds.set(0, 0);
3858 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003859 }
3860 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003861
Craig Mautner6985bad2014-04-21 15:22:06 -07003862 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003863 void setVisible(boolean visible) {
3864 if (mVisible != visible) {
3865 mVisible = visible;
3866 if (mCallback != null) {
3867 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3868 0 /* unused */, this).sendToTarget();
3869 }
3870 }
3871 }
3872
Craig Mautner6985bad2014-04-21 15:22:06 -07003873 void setDrawn() {
3874 }
3875
Craig Mautner1b4bf852014-05-26 15:06:32 -07003876 // You can always start a new task on a regular ActivityStack.
3877 boolean isEligibleForNewTasks() {
3878 return true;
3879 }
3880
Craig Mautnerd163e752014-06-13 17:18:47 -07003881 void onTaskListEmptyLocked() {
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003882 }
3883
Craig Mautner34b73df2014-01-12 21:11:08 -08003884 @Override
3885 public String toString() {
3886 return mIdString + (mActivityDisplay == null ? "N" : "A");
3887 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003888 }
3889
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003890 private class VirtualActivityContainer extends ActivityContainer {
3891 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003892 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003893
3894 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3895 super(getNextStackId());
3896 mParentActivity = parent;
3897 mCallback = callback;
3898 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003899 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003900 }
3901
3902 @Override
3903 public void setSurface(Surface surface, int width, int height, int density) {
3904 super.setSurface(surface, width, height, density);
3905
3906 synchronized (mService) {
3907 final long origId = Binder.clearCallingIdentity();
3908 try {
3909 setSurfaceLocked(surface, width, height, density);
3910 } finally {
3911 Binder.restoreCallingIdentity(origId);
3912 }
3913 }
3914 }
3915
3916 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3917 if (mContainerState == CONTAINER_STATE_FINISHING) {
3918 return;
3919 }
3920 VirtualActivityDisplay virtualActivityDisplay =
3921 (VirtualActivityDisplay) mActivityDisplay;
3922 if (virtualActivityDisplay == null) {
3923 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003924 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003925 mActivityDisplay = virtualActivityDisplay;
3926 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3927 attachToDisplayLocked(virtualActivityDisplay);
3928 }
3929
3930 if (mSurface != null) {
3931 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003932 }
3933
Craig Mautner6985bad2014-04-21 15:22:06 -07003934 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003935 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003936 mStack.resumeTopActivityLocked(null);
3937 } else {
3938 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003939 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003940 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003941 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003942 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003943 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003944
Craig Mautnerd163e752014-06-13 17:18:47 -07003945 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003946
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003947 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3948 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003949 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003950
Craig Mautner6985bad2014-04-21 15:22:06 -07003951 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003952 boolean isAttachedLocked() {
3953 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003954 }
3955
3956 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003957 void setDrawn() {
3958 synchronized (mService) {
3959 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003960 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003961 }
3962 }
3963
Craig Mautner1b4bf852014-05-26 15:06:32 -07003964 // Never start a new task on an ActivityView if it isn't explicitly specified.
3965 @Override
3966 boolean isEligibleForNewTasks() {
3967 return false;
3968 }
3969
Craig Mautner60257702014-09-17 15:02:33 -07003970 void onTaskListEmptyLocked() {
3971 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3972 detachLocked();
3973 deleteActivityContainer(this);
3974 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3975 }
3976
Craig Mautnerd163e752014-06-13 17:18:47 -07003977 private void setSurfaceIfReadyLocked() {
3978 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003979 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3980 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3981 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3982 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3983 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003984 }
3985 }
3986
Craig Mautner4a1cb222013-12-04 16:14:06 -08003987 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3988 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003989 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003990 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003991 int mDisplayId;
3992 Display mDisplay;
3993 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003994
Craig Mautner4a1cb222013-12-04 16:14:06 -08003995 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3996 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003997 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003998
Jose Lima4b6c6692014-08-12 17:41:12 -07003999 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004000
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004001 ActivityDisplay() {
4002 }
Craig Mautner4504de52013-12-20 09:06:56 -08004003
Craig Mautner1a70a162014-09-13 12:09:31 -07004004 // After instantiation, check that mDisplay is not null before using this. The alternative
4005 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004006 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004007 final Display display = mDisplayManager.getDisplay(displayId);
4008 if (display == null) {
4009 return;
4010 }
4011 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004012 }
4013
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004014 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004015 mDisplay = display;
4016 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004017 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004018 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004019
4020 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004021 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4022 + mDisplayId);
4023 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004024 }
4025
Craig Mautnere0a38842013-12-16 16:14:02 -08004026 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004027 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004028 + " from displayId=" + mDisplayId);
4029 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004030 }
4031
4032 void getBounds(Point bounds) {
4033 mDisplay.getDisplayInfo(mDisplayInfo);
4034 bounds.x = mDisplayInfo.appWidth;
4035 bounds.y = mDisplayInfo.appHeight;
4036 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004037
Jose Lima4b6c6692014-08-12 17:41:12 -07004038 void setVisibleBehindActivity(ActivityRecord r) {
4039 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004040 }
4041
Jose Lima4b6c6692014-08-12 17:41:12 -07004042 boolean hasVisibleBehindActivity() {
4043 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004044 }
4045
Craig Mautner34b73df2014-01-12 21:11:08 -08004046 @Override
4047 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004048 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4049 }
4050 }
4051
4052 class VirtualActivityDisplay extends ActivityDisplay {
4053 VirtualDisplay mVirtualDisplay;
4054
Craig Mautner6985bad2014-04-21 15:22:06 -07004055 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004056 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004057 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4058 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4059 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4060 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004061
4062 init(mVirtualDisplay.getDisplay());
4063
4064 mWindowManager.handleDisplayAdded(mDisplayId);
4065 }
4066
4067 void setSurface(Surface surface) {
4068 if (mVirtualDisplay != null) {
4069 mVirtualDisplay.setSurface(surface);
4070 }
4071 }
4072
4073 @Override
4074 void detachActivitiesLocked(ActivityStack stack) {
4075 super.detachActivitiesLocked(stack);
4076 if (mVirtualDisplay != null) {
4077 mVirtualDisplay.release();
4078 mVirtualDisplay = null;
4079 }
4080 }
4081
4082 @Override
4083 public String toString() {
4084 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004085 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004086 }
Jose Lima58e66d62014-05-27 20:00:27 -07004087
4088 private boolean isLeanbackOnlyDevice() {
4089 boolean onLeanbackOnly = false;
4090 try {
4091 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4092 PackageManager.FEATURE_LEANBACK_ONLY);
4093 } catch (RemoteException e) {
4094 // noop
4095 }
4096
4097 return onLeanbackOnly;
4098 }
Craig Mautner27084302013-03-25 08:05:25 -07004099}