blob: 35ac9c95c8442f44c27c8fc4152448ef2b3f9788 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner29219d92013-04-16 20:19:12 -070020import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070022import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Craig Mautner2420ead2013-04-01 17:13:20 -070023import static com.android.server.am.ActivityManagerService.localLOGV;
Craig Mautner23ac33b2013-04-01 16:26:35 -070024import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070025import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Craig Mautner6170f732013-04-02 13:05:23 -070027import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070029import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070030import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
31import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070032import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070033import static com.android.server.am.ActivityManagerService.TAG;
34
Craig Mautner2420ead2013-04-01 17:13:20 -070035import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070036import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080037import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070038import android.app.ActivityOptions;
39import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080040import android.app.IActivityContainer;
41import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070042import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.PendingIntent;
Craig Mautner20e72272013-04-01 13:45:53 -070045import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070047import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040048import android.app.StatusBarManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070050import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070052import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070054import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.content.pm.ApplicationInfo;
56import android.content.pm.PackageManager;
57import android.content.pm.ResolveInfo;
58import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080059import android.graphics.Point;
60import android.hardware.display.DisplayManager;
61import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080062import android.hardware.display.DisplayManagerGlobal;
63import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080064import android.hardware.input.InputManager;
65import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070066import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070067import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070068import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070069import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070070import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070071import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070072import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070073import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070074import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070075import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070076import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040077import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070078import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070079import android.os.UserHandle;
Dianne Hackborn91097de2014-04-04 18:02:06 -070080import android.service.voice.IVoiceInteractionSession;
Craig Mautner2420ead2013-04-01 17:13:20 -070081import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070082import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080083import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070084
Craig Mautner4a1cb222013-12-04 16:14:06 -080085import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080086import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080087import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080088import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080089import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070090import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070091import com.android.internal.app.IVoiceInteractor;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070092import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040093import com.android.internal.statusbar.IStatusBarService;
Jeff Brownca9bc702014-02-11 14:32:56 -080094import com.android.server.LocalServices;
Craig Mautner6170f732013-04-02 13:05:23 -070095import com.android.server.am.ActivityManagerService.PendingActivityLaunch;
Craig Mautner2420ead2013-04-01 17:13:20 -070096import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070097import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070098
justinzhang5286d3f2014-05-12 17:06:01 -040099
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import java.io.FileDescriptor;
101import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700102import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700103import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700104import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700105
Craig Mautner4a1cb222013-12-04 16:14:06 -0800106public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700107 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
108 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
109 static final boolean DEBUG_APP = DEBUG || false;
110 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
Craig Mautnera7f2bd42013-10-15 16:13:50 -0700111 static final boolean DEBUG_STATES = DEBUG || false;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700112 static final boolean DEBUG_IDLE = DEBUG || false;
Winson Chungcc12f282014-05-21 17:43:28 -0700113 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700114
Craig Mautner2219a1b2013-03-25 09:44:30 -0700115 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700116
Craig Mautnerf3333272013-04-22 10:55:53 -0700117 /** How long we wait until giving up on the last activity telling us it is idle. */
118 static final int IDLE_TIMEOUT = 10*1000;
119
Craig Mautner0eea92c2013-05-16 13:35:39 -0700120 /** How long we can hold the sleep wake lock before giving up. */
121 static final int SLEEP_TIMEOUT = 5*1000;
122
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700123 // How long we can hold the launch wake lock before giving up.
124 static final int LAUNCH_TIMEOUT = 10*1000;
125
Craig Mautner05d29032013-05-03 13:40:13 -0700126 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
127 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
128 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700129 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700130 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800131 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
132 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
133 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700134 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400135 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
136 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700137 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800138
Craig Mautner4504de52013-12-20 09:06:56 -0800139 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700140
justinzhang5286d3f2014-05-12 17:06:01 -0400141 /** Status Bar Service **/
142 private IBinder mToken = new Binder();
143 private IStatusBarService mStatusBarService;
144
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700145 // For debugging to make sure the caller when acquiring/releasing our
146 // wake lock is the system process.
147 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700148
Craig Mautner27084302013-03-25 08:05:25 -0700149 final ActivityManagerService mService;
150
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700151 final ActivityStackSupervisorHandler mHandler;
152
153 /** Short cut */
154 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800155 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700156
Craig Mautner27084302013-03-25 08:05:25 -0700157 /** Dismiss the keyguard after the next activity is displayed? */
Craig Mautner5314a402013-09-26 12:40:16 -0700158 boolean mDismissKeyguardOnNextActivity = false;
Craig Mautner27084302013-03-25 08:05:25 -0700159
Craig Mautner8d341ef2013-03-26 09:03:27 -0700160 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700161 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700162
163 /** Task identifier that activities are currently being started in. Incremented each time a
164 * new task is created. */
165 private int mCurTaskId = 0;
166
Craig Mautner2420ead2013-04-01 17:13:20 -0700167 /** The current user */
168 private int mCurrentUser;
169
Craig Mautnere0a38842013-12-16 16:14:02 -0800170 /** The stack containing the launcher app. Assumed to always be attached to
171 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700172 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700173
Craig Mautnere0a38842013-12-16 16:14:02 -0800174 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700175 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700176
Craig Mautner4a1cb222013-12-04 16:14:06 -0800177 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
178 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800179 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800180 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700181
182 /** List of activities that are waiting for a new activity to become visible before completing
183 * whatever operation they are supposed to do. */
184 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
185
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700186 /** List of processes waiting to find out about the next visible activity. */
187 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
188 new ArrayList<IActivityManager.WaitResult>();
189
190 /** List of processes waiting to find out about the next launched activity. */
191 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
192 new ArrayList<IActivityManager.WaitResult>();
193
Craig Mautnerde4ef022013-04-07 19:01:33 -0700194 /** List of activities that are ready to be stopped, but waiting for the next activity to
195 * settle down before doing so. */
196 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
197
Craig Mautnerf3333272013-04-22 10:55:53 -0700198 /** List of activities that are ready to be finished, but waiting for the previous activity to
199 * settle down before doing so. It contains ActivityRecord objects. */
200 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
201
Craig Mautner0eea92c2013-05-16 13:35:39 -0700202 /** List of activities that are in the process of going to sleep. */
203 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
204
Craig Mautnerf3333272013-04-22 10:55:53 -0700205 /** Used on user changes */
206 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
207
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700208 /** Used to queue up any background users being started */
209 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
210
Craig Mautnerde4ef022013-04-07 19:01:33 -0700211 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
212 * is being brought in front of us. */
213 boolean mUserLeaving = false;
214
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215 /** Set when we have taken too long waiting to go to sleep. */
216 boolean mSleepTimeout = false;
217
Jose Lima58e66d62014-05-27 20:00:27 -0700218 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
219 * setWindowManager is called. **/
220 private boolean mLeanbackOnlyDevice;
221
Craig Mautner0eea92c2013-05-16 13:35:39 -0700222 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700223 * We don't want to allow the device to go to sleep while in the process
224 * of launching an activity. This is primarily to allow alarm intent
225 * receivers to launch an activity and get that to run before the device
226 * goes back to sleep.
227 */
228 final PowerManager.WakeLock mLaunchingActivity;
229
230 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700231 * Set when the system is going to sleep, until we have
232 * successfully paused the current activity and released our wake lock.
233 * At that point the system is allowed to actually sleep.
234 */
235 final PowerManager.WakeLock mGoingToSleep;
236
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700237 /** Stack id of the front stack when user switched, indexed by userId. */
238 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700239
Craig Mautner4504de52013-12-20 09:06:56 -0800240 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800241 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700242 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800243
244 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700245 private final SparseArray<ActivityDisplay> mActivityDisplays =
246 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800247
Jeff Brownca9bc702014-02-11 14:32:56 -0800248 InputManagerInternal mInputManagerInternal;
249
Craig Mautneraea74a52014-03-08 14:23:10 -0800250 /** If non-null then the task specified remains in front and no other tasks may be started
251 * until the task exits or #stopLockTaskMode() is called. */
252 private TaskRecord mLockTaskModeTask;
253
Craig Mautner4a1cb222013-12-04 16:14:06 -0800254 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700255 mService = service;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800256 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700257 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800258 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700259 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
260 throw new IllegalStateException("Calling must be system uid");
261 }
262 mLaunchingActivity =
263 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
264 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700265 }
266
justinzhang5286d3f2014-05-12 17:06:01 -0400267 // This function returns a IStatusBarService. The value is from ServiceManager.
268 // getService and is cached.
269 private IStatusBarService getStatusBarService() {
270 synchronized (mService) {
271 if (mStatusBarService == null) {
272 mStatusBarService = IStatusBarService.Stub.asInterface(
273 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
274 if (mStatusBarService == null) {
275 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
276 }
277 }
278 return mStatusBarService;
279 }
280 }
281
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700282 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800283 synchronized (mService) {
284 mWindowManager = wm;
285
286 mDisplayManager =
287 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
288 mDisplayManager.registerDisplayListener(this, null);
289
290 Display[] displays = mDisplayManager.getDisplays();
291 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
292 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800293 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
294 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800295 }
296
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700297 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800298 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800299
300 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700301
302 // Initialize this here, now that we can get a valid reference to PackageManager.
303 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800304 }
Craig Mautner27084302013-03-25 08:05:25 -0700305 }
306
307 void dismissKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700308 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner27084302013-03-25 08:05:25 -0700309 if (mDismissKeyguardOnNextActivity) {
310 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700311 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700312 }
313 }
314
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700315 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800316 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700317 }
318
Craig Mautnerde4ef022013-04-07 19:01:33 -0700319 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700321 }
322
Craig Mautner4a1cb222013-12-04 16:14:06 -0800323 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
324 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700325 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800326 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
327 if (parent != null) {
328 stack = parent.task.stack;
329 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800330 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800331 if (stacks != null && !stacks.isEmpty()) {
332 return stack == stacks.get(stacks.size() - 1);
333 }
334 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700335 }
336
Craig Mautnerde4ef022013-04-07 19:01:33 -0700337 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800338 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800339 int topNdx = stacks.size() - 1;
340 if (topNdx <= 0) {
341 return;
342 }
343 ActivityStack topStack = stacks.get(topNdx);
344 final boolean homeInFront = topStack == mHomeStack;
345 if (homeInFront != toFront) {
346 mLastFocusedStack = topStack;
347 stacks.remove(mHomeStack);
348 stacks.add(toFront ? topNdx : 0, mHomeStack);
349 mFocusedStack = stacks.get(topNdx);
350 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
351 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700352 }
353 }
354
Craig Mautner719e6212014-05-29 16:49:41 +0000355 void moveHomeToTop() {
Craig Mautner69ada552013-04-18 13:51:51 -0700356 moveHomeStack(true);
Craig Mautner719e6212014-05-29 16:49:41 +0000357 mHomeStack.moveHomeTaskToTop();
Craig Mautner8e569572013-10-11 17:36:59 -0700358 }
359
Craig Mautner719e6212014-05-29 16:49:41 +0000360 boolean resumeHomeActivity(ActivityRecord prev) {
361 moveHomeToTop();
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700362 if (prev != null) {
Craig Mautner719e6212014-05-29 16:49:41 +0000363 prev.task.mOnTopOfHome = false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700364 }
Craig Mautnera8a90e02013-06-28 15:24:50 -0700365 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner719e6212014-05-29 16:49:41 +0000366 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700367 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700368 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700369 }
370 return mService.startHomeActivityLocked(mCurrentUser);
371 }
372
Craig Mautner27084302013-03-25 08:05:25 -0700373 void setDismissKeyguard(boolean dismiss) {
Craig Mautner5314a402013-09-26 12:40:16 -0700374 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen(" dismiss=" + dismiss);
Craig Mautner27084302013-03-25 08:05:25 -0700375 mDismissKeyguardOnNextActivity = dismiss;
376 }
377
Craig Mautner8d341ef2013-03-26 09:03:27 -0700378 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800379 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800381 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800382 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
383 ActivityStack stack = stacks.get(stackNdx);
384 TaskRecord task = stack.taskForIdLocked(id);
385 if (task != null) {
386 return task;
387 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700388 }
389 }
390 return null;
391 }
392
Craig Mautner6170f732013-04-02 13:05:23 -0700393 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800394 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800395 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800396 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800397 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
398 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
399 if (r != null) {
400 return r;
401 }
Craig Mautner6170f732013-04-02 13:05:23 -0700402 }
403 }
404 return null;
405 }
406
Craig Mautneref73ee12014-04-23 11:45:37 -0700407 void setNextTaskId(int taskId) {
408 if (taskId > mCurTaskId) {
409 mCurTaskId = taskId;
410 }
411 }
412
Craig Mautner8d341ef2013-03-26 09:03:27 -0700413 int getNextTaskId() {
414 do {
415 mCurTaskId++;
416 if (mCurTaskId <= 0) {
417 mCurTaskId = 1;
418 }
419 } while (anyTaskForIdLocked(mCurTaskId) != null);
420 return mCurTaskId;
421 }
422
Craig Mautnerde4ef022013-04-07 19:01:33 -0700423 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700424 ActivityStack stack = getFocusedStack();
425 if (stack == null) {
426 return null;
427 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700428 ActivityRecord resumedActivity = stack.mResumedActivity;
429 if (resumedActivity == null || resumedActivity.app == null) {
430 resumedActivity = stack.mPausingActivity;
431 if (resumedActivity == null || resumedActivity.app == null) {
432 resumedActivity = stack.topRunningActivityLocked(null);
433 }
434 }
435 return resumedActivity;
436 }
437
Mike Lockwooded8902d2013-11-15 11:01:47 -0800438 boolean attachApplicationLocked(ProcessRecord app) throws Exception {
Craig Mautner20e72272013-04-01 13:45:53 -0700439 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800440 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800441 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
442 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800443 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
444 final ActivityStack stack = stacks.get(stackNdx);
445 if (!isFrontStack(stack)) {
446 continue;
447 }
448 ActivityRecord hr = stack.topRunningActivityLocked(null);
449 if (hr != null) {
450 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
451 && processName.equals(hr.processName)) {
452 try {
George Mount2c92c972014-03-20 09:38:23 -0700453 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800454 didSomething = true;
455 }
456 } catch (Exception e) {
457 Slog.w(TAG, "Exception in new application when starting activity "
458 + hr.intent.getComponent().flattenToShortString(), e);
459 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700460 }
Craig Mautner20e72272013-04-01 13:45:53 -0700461 }
Craig Mautner20e72272013-04-01 13:45:53 -0700462 }
463 }
464 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700465 if (!didSomething) {
466 ensureActivitiesVisibleLocked(null, 0);
467 }
Craig Mautner20e72272013-04-01 13:45:53 -0700468 return didSomething;
469 }
470
471 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800472 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
473 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800474 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
475 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800476 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800477 continue;
478 }
479 final ActivityRecord resumedActivity = stack.mResumedActivity;
480 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800481 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
482 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800483 return false;
484 }
Craig Mautner20e72272013-04-01 13:45:53 -0700485 }
486 }
487 return true;
488 }
489
Craig Mautnerde4ef022013-04-07 19:01:33 -0700490 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800491 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
492 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800493 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
494 final ActivityStack stack = stacks.get(stackNdx);
495 if (isFrontStack(stack)) {
496 final ActivityRecord r = stack.mResumedActivity;
497 if (r != null && r.state != ActivityState.RESUMED) {
498 return false;
499 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700500 }
501 }
502 }
503 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800504 if (DEBUG_STACK) Slog.d(TAG,
505 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
506 mLastFocusedStack + " to=" + mFocusedStack);
507 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700508 return true;
509 }
510
511 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800512 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
513 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800514 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
515 final ActivityStack stack = stacks.get(stackNdx);
516 final ActivityRecord r = stack.mResumedActivity;
517 if (r != null && (!r.nowVisible || r.waitingVisible)) {
518 return false;
519 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700520 }
521 }
522 return true;
523 }
524
Craig Mautner2acc3892013-09-23 10:28:14 -0700525 /**
526 * Pause all activities in either all of the stacks or just the back stacks.
527 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700528 * @return true if any activity was paused as a result of this call.
529 */
Craig Mautner5314a402013-09-26 12:40:16 -0700530 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700531 boolean someActivityPaused = 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) && stack.mResumedActivity != null) {
537 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
538 " mResumedActivity=" + stack.mResumedActivity);
539 stack.startPausingLocked(userLeaving, false);
540 someActivityPaused = true;
541 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700542 }
543 }
544 return someActivityPaused;
545 }
546
Craig Mautnerde4ef022013-04-07 19:01:33 -0700547 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700548 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800549 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
550 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800551 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
552 final ActivityStack stack = stacks.get(stackNdx);
553 final ActivityRecord r = stack.mPausingActivity;
554 if (r != null && r.state != ActivityState.PAUSED
555 && r.state != ActivityState.STOPPED
556 && r.state != ActivityState.STOPPING) {
557 if (DEBUG_STATES) {
558 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
559 pausing = false;
560 } else {
561 return false;
562 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700563 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700564 }
565 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700566 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700567 }
568
Craig Mautnerdf88d732014-01-27 09:21:32 -0800569 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping) {
John Spurlock8a985d22014-02-25 09:40:05 -0500570 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800571 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
572 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
573 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
574 final ActivityStack stack = stacks.get(stackNdx);
575 if (stack.mResumedActivity != null &&
576 stack.mActivityContainer.mParentActivity == parent) {
577 stack.startPausingLocked(userLeaving, uiSleeping);
578 }
579 }
580 }
581 }
582
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700583 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700584 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700585 WaitResult w = mWaitingActivityVisible.get(i);
586 w.timeout = false;
587 if (r != null) {
588 w.who = new ComponentName(r.info.packageName, r.info.name);
589 }
590 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
591 w.thisTime = w.totalTime;
592 }
593 mService.notifyAll();
594 dismissKeyguard();
595 }
596
597 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
598 long thisTime, long totalTime) {
599 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700600 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700601 w.timeout = timeout;
602 if (r != null) {
603 w.who = new ComponentName(r.info.packageName, r.info.name);
604 }
605 w.thisTime = thisTime;
606 w.totalTime = totalTime;
607 }
608 mService.notifyAll();
609 }
610
Craig Mautner29219d92013-04-16 20:19:12 -0700611 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700612 final ActivityStack focusedStack = getFocusedStack();
613 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
614 if (r != null) {
615 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700616 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700617
Craig Mautner4a1cb222013-12-04 16:14:06 -0800618 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800619 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800620 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
621 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700622 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700623 r = stack.topRunningActivityLocked(null);
624 if (r != null) {
625 return r;
626 }
627 }
628 }
629 return null;
630 }
631
Dianne Hackborn09233282014-04-30 11:33:59 -0700632 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700633 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
635 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800636 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800637 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800638 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800639 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
640 final ActivityStack stack = stacks.get(stackNdx);
641 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
642 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700643 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700644 }
645 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700646
647 // The lists are already sorted from most recent to oldest. Just pull the most recent off
648 // each list and add it to list. Stop when all lists are empty or maxNum reached.
649 while (maxNum > 0) {
650 long mostRecentActiveTime = Long.MIN_VALUE;
651 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800652 final int numTaskLists = runningTaskLists.size();
653 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
654 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700655 if (!stackTaskList.isEmpty()) {
656 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
657 if (lastActiveTime > mostRecentActiveTime) {
658 mostRecentActiveTime = lastActiveTime;
659 selectedStackList = stackTaskList;
660 }
661 }
662 }
663 if (selectedStackList != null) {
664 list.add(selectedStackList.remove(0));
665 --maxNum;
666 } else {
667 break;
668 }
669 }
Craig Mautner20e72272013-04-01 13:45:53 -0700670 }
671
Craig Mautner23ac33b2013-04-01 16:26:35 -0700672 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
673 String profileFile, ParcelFileDescriptor profileFd, int userId) {
674 // Collect information about the target of the Intent.
675 ActivityInfo aInfo;
676 try {
677 ResolveInfo rInfo =
678 AppGlobals.getPackageManager().resolveIntent(
679 intent, resolvedType,
680 PackageManager.MATCH_DEFAULT_ONLY
681 | ActivityManagerService.STOCK_PM_FLAGS, userId);
682 aInfo = rInfo != null ? rInfo.activityInfo : null;
683 } catch (RemoteException e) {
684 aInfo = null;
685 }
686
687 if (aInfo != null) {
688 // Store the found target back into the intent, because now that
689 // we have it we never want to do this again. For example, if the
690 // user navigates back to this point in the history, we should
691 // always restart the exact same activity.
692 intent.setComponent(new ComponentName(
693 aInfo.applicationInfo.packageName, aInfo.name));
694
695 // Don't debug things in the system process
696 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
697 if (!aInfo.processName.equals("system")) {
698 mService.setDebugApp(aInfo.processName, true, false);
699 }
700 }
701
702 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
703 if (!aInfo.processName.equals("system")) {
704 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
705 }
706 }
707
708 if (profileFile != null) {
709 if (!aInfo.processName.equals("system")) {
710 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
711 profileFile, profileFd,
712 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
713 }
714 }
715 }
716 return aInfo;
717 }
718
Craig Mautner2219a1b2013-03-25 09:44:30 -0700719 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner719e6212014-05-29 16:49:41 +0000720 moveHomeToTop();
Dianne Hackborn91097de2014-04-04 18:02:06 -0700721 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null, 0,
Craig Mautnere0a38842013-12-16 16:14:02 -0800722 null, false, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700723 }
724
Craig Mautner23ac33b2013-04-01 16:26:35 -0700725 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700726 String callingPackage, Intent intent, String resolvedType,
727 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
728 IBinder resultTo, String resultWho, int requestCode, int startFlags, String profileFile,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700729 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
Craig Mautnere0a38842013-12-16 16:14:02 -0800730 Bundle options, int userId, IActivityContainer iContainer) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700731 // Refuse possible leaked file descriptors
732 if (intent != null && intent.hasFileDescriptors()) {
733 throw new IllegalArgumentException("File descriptors passed in Intent");
734 }
735 boolean componentSpecified = intent.getComponent() != null;
736
737 // Don't modify the client's object!
738 intent = new Intent(intent);
739
740 // Collect information about the target of the Intent.
741 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
742 profileFile, profileFd, userId);
743
Craig Mautnere0a38842013-12-16 16:14:02 -0800744 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700745 synchronized (mService) {
746 int callingPid;
747 if (callingUid >= 0) {
748 callingPid = -1;
749 } else if (caller == null) {
750 callingPid = Binder.getCallingPid();
751 callingUid = Binder.getCallingUid();
752 } else {
753 callingPid = callingUid = -1;
754 }
755
Craig Mautnere0a38842013-12-16 16:14:02 -0800756 final ActivityStack stack;
757 if (container == null || container.mStack.isOnHomeDisplay()) {
758 stack = getFocusedStack();
759 } else {
760 stack = container.mStack;
761 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700762 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700763 && mService.mConfiguration.diff(config) != 0;
764 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700765 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700766
767 final long origId = Binder.clearCallingIdentity();
768
769 if (aInfo != null &&
770 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
771 // This may be a heavy-weight process! Check to see if we already
772 // have another, different heavy-weight process running.
773 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
774 if (mService.mHeavyWeightProcess != null &&
775 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
776 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700777 int realCallingUid = callingUid;
778 if (caller != null) {
779 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
780 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700781 realCallingUid = callerApp.info.uid;
782 } else {
783 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700784 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700785 + intent.toString());
786 ActivityOptions.abort(options);
787 return ActivityManager.START_PERMISSION_DENIED;
788 }
789 }
790
791 IIntentSender target = mService.getIntentSenderLocked(
792 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
793 realCallingUid, userId, null, null, 0, new Intent[] { intent },
794 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
795 | PendingIntent.FLAG_ONE_SHOT, null);
796
797 Intent newIntent = new Intent();
798 if (requestCode >= 0) {
799 // Caller is requesting a result.
800 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
801 }
802 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
803 new IntentSender(target));
804 if (mService.mHeavyWeightProcess.activities.size() > 0) {
805 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
806 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
807 hist.packageName);
808 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
809 hist.task.taskId);
810 }
811 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
812 aInfo.packageName);
813 newIntent.setFlags(intent.getFlags());
814 newIntent.setClassName("android",
815 HeavyWeightSwitcherActivity.class.getName());
816 intent = newIntent;
817 resolvedType = null;
818 caller = null;
819 callingUid = Binder.getCallingUid();
820 callingPid = Binder.getCallingPid();
821 componentSpecified = true;
822 try {
823 ResolveInfo rInfo =
824 AppGlobals.getPackageManager().resolveIntent(
825 intent, null,
826 PackageManager.MATCH_DEFAULT_ONLY
827 | ActivityManagerService.STOCK_PM_FLAGS, userId);
828 aInfo = rInfo != null ? rInfo.activityInfo : null;
829 aInfo = mService.getActivityInfoForUser(aInfo, userId);
830 } catch (RemoteException e) {
831 aInfo = null;
832 }
833 }
834 }
835 }
836
Dianne Hackborn91097de2014-04-04 18:02:06 -0700837 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
838 voiceSession, voiceInteractor, resultTo, resultWho,
Craig Mautnere0a38842013-12-16 16:14:02 -0800839 requestCode, callingPid, callingUid, callingPackage, startFlags, options,
840 componentSpecified, null, container);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700841
Craig Mautnerde4ef022013-04-07 19:01:33 -0700842 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700843 // If the caller also wants to switch to a new configuration,
844 // do so now. This allows a clean switch, as we are waiting
845 // for the current activity to pause (so we will not destroy
846 // it), and have not yet started the next activity.
847 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
848 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700849 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700850 if (DEBUG_CONFIGURATION) Slog.v(TAG,
851 "Updating to new configuration after starting activity.");
852 mService.updateConfigurationLocked(config, null, false, false);
853 }
854
855 Binder.restoreCallingIdentity(origId);
856
857 if (outResult != null) {
858 outResult.result = res;
859 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700860 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700861 do {
862 try {
863 mService.wait();
864 } catch (InterruptedException e) {
865 }
866 } while (!outResult.timeout && outResult.who == null);
867 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700868 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700869 if (r.nowVisible) {
870 outResult.timeout = false;
871 outResult.who = new ComponentName(r.info.packageName, r.info.name);
872 outResult.totalTime = 0;
873 outResult.thisTime = 0;
874 } else {
875 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700876 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700877 do {
878 try {
879 mService.wait();
880 } catch (InterruptedException e) {
881 }
882 } while (!outResult.timeout && outResult.who == null);
883 }
884 }
885 }
886
887 return res;
888 }
889 }
890
891 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
892 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
893 Bundle options, int userId) {
894 if (intents == null) {
895 throw new NullPointerException("intents is null");
896 }
897 if (resolvedTypes == null) {
898 throw new NullPointerException("resolvedTypes is null");
899 }
900 if (intents.length != resolvedTypes.length) {
901 throw new IllegalArgumentException("intents are length different than resolvedTypes");
902 }
903
Craig Mautner23ac33b2013-04-01 16:26:35 -0700904
905 int callingPid;
906 if (callingUid >= 0) {
907 callingPid = -1;
908 } else if (caller == null) {
909 callingPid = Binder.getCallingPid();
910 callingUid = Binder.getCallingUid();
911 } else {
912 callingPid = callingUid = -1;
913 }
914 final long origId = Binder.clearCallingIdentity();
915 try {
916 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700917 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700918 for (int i=0; i<intents.length; i++) {
919 Intent intent = intents[i];
920 if (intent == null) {
921 continue;
922 }
923
924 // Refuse possible leaked file descriptors
925 if (intent != null && intent.hasFileDescriptors()) {
926 throw new IllegalArgumentException("File descriptors passed in Intent");
927 }
928
929 boolean componentSpecified = intent.getComponent() != null;
930
931 // Don't modify the client's object!
932 intent = new Intent(intent);
933
934 // Collect information about the target of the Intent.
935 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
936 0, null, null, userId);
937 // TODO: New, check if this is correct
938 aInfo = mService.getActivityInfoForUser(aInfo, userId);
939
940 if (aInfo != null &&
941 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
942 != 0) {
943 throw new IllegalArgumentException(
944 "FLAG_CANT_SAVE_STATE not supported here");
945 }
946
947 Bundle theseOptions;
948 if (options != null && i == intents.length-1) {
949 theseOptions = options;
950 } else {
951 theseOptions = null;
952 }
Craig Mautner6170f732013-04-02 13:05:23 -0700953 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn91097de2014-04-04 18:02:06 -0700954 aInfo, null, null, resultTo, null, -1, callingPid, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -0800955 0, theseOptions, componentSpecified, outActivity, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700956 if (res < 0) {
957 return res;
958 }
959
960 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
961 }
962 }
963 } finally {
964 Binder.restoreCallingIdentity(origId);
965 }
966
967 return ActivityManager.START_SUCCESS;
968 }
969
Craig Mautner2420ead2013-04-01 17:13:20 -0700970 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -0700971 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -0700972 throws RemoteException {
973
974 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -0700975 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700976 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700977
978 // schedule launch ticks to collect information about slow apps.
979 r.startLaunchTickingLocked();
980
981 // Have the window manager re-evaluate the orientation of
982 // the screen based on the new activity order. Note that
983 // as a result of this, it can call back into the activity
984 // manager with a new orientation. We don't care about that,
985 // because the activity is not currently running so we are
986 // just restarting it anyway.
987 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700988 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -0700989 mService.mConfiguration,
990 r.mayFreezeScreenLocked(app) ? r.appToken : null);
991 mService.updateConfigurationLocked(config, r, false, false);
992 }
993
994 r.app = app;
995 app.waitingToKill = null;
996 r.launchCount++;
997 r.lastLaunchTime = SystemClock.uptimeMillis();
998
999 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1000
1001 int idx = app.activities.indexOf(r);
1002 if (idx < 0) {
1003 app.activities.add(r);
1004 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001005 mService.updateLruProcessLocked(app, true, null);
1006 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001007
1008 final ActivityStack stack = r.task.stack;
1009 try {
1010 if (app.thread == null) {
1011 throw new RemoteException();
1012 }
1013 List<ResultInfo> results = null;
1014 List<Intent> newIntents = null;
1015 if (andResume) {
1016 results = r.results;
1017 newIntents = r.newIntents;
1018 }
1019 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1020 + " icicle=" + r.icicle
1021 + " with results=" + results + " newIntents=" + newIntents
1022 + " andResume=" + andResume);
1023 if (andResume) {
1024 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1025 r.userId, System.identityHashCode(r),
1026 r.task.taskId, r.shortComponentName);
1027 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001028 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001029 // Home process is the root process of the task.
1030 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001031 }
1032 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1033 r.sleeping = false;
1034 r.forceNewConfig = false;
1035 mService.showAskCompatModeDialogLocked(r);
1036 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1037 String profileFile = null;
1038 ParcelFileDescriptor profileFd = null;
1039 boolean profileAutoStop = false;
1040 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1041 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1042 mService.mProfileProc = app;
1043 profileFile = mService.mProfileFile;
1044 profileFd = mService.mProfileFd;
1045 profileAutoStop = mService.mAutoStopProfiler;
1046 }
1047 }
1048 app.hasShownUi = true;
1049 app.pendingUiClean = true;
1050 if (profileFd != null) {
1051 try {
1052 profileFd = profileFd.dup();
1053 } catch (IOException e) {
1054 if (profileFd != null) {
1055 try {
1056 profileFd.close();
1057 } catch (IOException o) {
1058 }
1059 profileFd = null;
1060 }
1061 }
1062 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001063
Dianne Hackborna413dc02013-07-12 12:02:55 -07001064 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001065 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1066 System.identityHashCode(r), r.info,
Craig Mautnera0026042014-04-23 11:45:37 -07001067 new Configuration(mService.mConfiguration), r.compat, r.task.voiceInteractor,
1068 app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume,
Craig Mautner233ceee2014-05-09 17:05:11 -07001069 mService.isNextTransitionForward(), profileFile, profileFd, profileAutoStop
1070 );
Craig Mautner2420ead2013-04-01 17:13:20 -07001071
1072 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1073 // This may be a heavy-weight process! Note that the package
1074 // manager will ensure that only activity can run in the main
1075 // process of the .apk, which is the only thing that will be
1076 // considered heavy-weight.
1077 if (app.processName.equals(app.info.packageName)) {
1078 if (mService.mHeavyWeightProcess != null
1079 && mService.mHeavyWeightProcess != app) {
1080 Slog.w(TAG, "Starting new heavy weight process " + app
1081 + " when already running "
1082 + mService.mHeavyWeightProcess);
1083 }
1084 mService.mHeavyWeightProcess = app;
1085 Message msg = mService.mHandler.obtainMessage(
1086 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1087 msg.obj = r;
1088 mService.mHandler.sendMessage(msg);
1089 }
1090 }
1091
1092 } catch (RemoteException e) {
1093 if (r.launchFailed) {
1094 // This is the second time we failed -- finish activity
1095 // and give up.
1096 Slog.e(TAG, "Second failure launching "
1097 + r.intent.getComponent().flattenToShortString()
1098 + ", giving up", e);
1099 mService.appDiedLocked(app, app.pid, app.thread);
1100 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1101 "2nd-crash", false);
1102 return false;
1103 }
1104
1105 // This is the first time we failed -- restart process and
1106 // retry.
1107 app.activities.remove(r);
1108 throw e;
1109 }
1110
1111 r.launchFailed = false;
1112 if (stack.updateLRUListLocked(r)) {
1113 Slog.w(TAG, "Activity " + r
1114 + " being launched, but already in LRU list");
1115 }
1116
1117 if (andResume) {
1118 // As part of the process of launching, ActivityThread also performs
1119 // a resume.
1120 stack.minimalResumeActivityLocked(r);
1121 } else {
1122 // This activity is not starting in the resumed state... which
1123 // should look like we asked it to pause+stop (but remain visible),
1124 // and it has done so and reported back the current icicle and
1125 // other state.
1126 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1127 + " (starting in stopped state)");
1128 r.state = ActivityState.STOPPED;
1129 r.stopped = true;
1130 }
1131
1132 // Launch the new version setup screen if needed. We do this -after-
1133 // launching the initial activity (that is, home), so that it can have
1134 // a chance to initialize itself while in the background, making the
1135 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001136 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001137 mService.startSetupActivityLocked();
1138 }
1139
1140 return true;
1141 }
1142
Craig Mautnere79d42682013-04-01 19:01:53 -07001143 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001144 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001145 // Is this activity's application already running?
1146 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001147 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001148
1149 r.task.stack.setLaunchTime(r);
1150
1151 if (app != null && app.thread != null) {
1152 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001153 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1154 || !"android".equals(r.info.packageName)) {
1155 // Don't add this if it is a platform component that is marked
1156 // to run in multiple processes, because this is actually
1157 // part of the framework so doesn't make sense to track as a
1158 // separate apk in the process.
1159 app.addPackage(r.info.packageName, mService.mProcessStats);
1160 }
George Mount2c92c972014-03-20 09:38:23 -07001161 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001162 return;
1163 } catch (RemoteException e) {
1164 Slog.w(TAG, "Exception when starting activity "
1165 + r.intent.getComponent().flattenToShortString(), e);
1166 }
1167
1168 // If a dead object exception was thrown -- fall through to
1169 // restart the application.
1170 }
1171
1172 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001173 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001174 }
1175
Craig Mautner6170f732013-04-02 13:05:23 -07001176 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001177 Intent intent, String resolvedType, ActivityInfo aInfo,
1178 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1179 IBinder resultTo, String resultWho, int requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001180 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Craig Mautnere0a38842013-12-16 16:14:02 -08001181 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container) {
Craig Mautner6170f732013-04-02 13:05:23 -07001182 int err = ActivityManager.START_SUCCESS;
1183
1184 ProcessRecord callerApp = null;
1185 if (caller != null) {
1186 callerApp = mService.getRecordForAppLocked(caller);
1187 if (callerApp != null) {
1188 callingPid = callerApp.pid;
1189 callingUid = callerApp.info.uid;
1190 } else {
1191 Slog.w(TAG, "Unable to find app for caller " + caller
1192 + " (pid=" + callingPid + ") when starting: "
1193 + intent.toString());
1194 err = ActivityManager.START_PERMISSION_DENIED;
1195 }
1196 }
1197
1198 if (err == ActivityManager.START_SUCCESS) {
1199 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1200 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner9ef471f2014-02-07 13:11:47 -08001201 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)
1202 + " on display " + (container == null ? (mFocusedStack == null ?
1203 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1204 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1205 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001206 }
1207
1208 ActivityRecord sourceRecord = null;
1209 ActivityRecord resultRecord = null;
1210 if (resultTo != null) {
1211 sourceRecord = isInAnyStackLocked(resultTo);
1212 if (DEBUG_RESULTS) Slog.v(
1213 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1214 if (sourceRecord != null) {
1215 if (requestCode >= 0 && !sourceRecord.finishing) {
1216 resultRecord = sourceRecord;
1217 }
1218 }
1219 }
1220 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1221
Dianne Hackborn91097de2014-04-04 18:02:06 -07001222 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001223
1224 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1225 && sourceRecord != null) {
1226 // Transfer the result target from the source activity to the new
1227 // one being started, including any failures.
1228 if (requestCode >= 0) {
1229 ActivityOptions.abort(options);
1230 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1231 }
1232 resultRecord = sourceRecord.resultTo;
1233 resultWho = sourceRecord.resultWho;
1234 requestCode = sourceRecord.requestCode;
1235 sourceRecord.resultTo = null;
1236 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001237 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001238 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001239 if (sourceRecord.launchedFromUid == callingUid) {
1240 // The new activity is being launched from the same uid as the previous
1241 // activity in the flow, and asking to forward its result back to the
1242 // previous. In this case the activity is serving as a trampoline between
1243 // the two, so we also want to update its launchedFromPackage to be the
1244 // same as the previous activity. Note that this is safe, since we know
1245 // these two packages come from the same uid; the caller could just as
1246 // well have supplied that same package name itself. This specifially
1247 // deals with the case of an intent picker/chooser being launched in the app
1248 // flow to redirect to an activity picked by the user, where we want the final
1249 // activity to consider it to have been launched by the previous app activity.
1250 callingPackage = sourceRecord.launchedFromPackage;
1251 }
Craig Mautner6170f732013-04-02 13:05:23 -07001252 }
1253
1254 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1255 // We couldn't find a class that can handle the given Intent.
1256 // That's the end of that!
1257 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1258 }
1259
1260 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1261 // We couldn't find the specific class specified in the Intent.
1262 // Also the end of the line.
1263 err = ActivityManager.START_CLASS_NOT_FOUND;
1264 }
1265
Dianne Hackborn91097de2014-04-04 18:02:06 -07001266 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1267 && sourceRecord.task.voiceSession != null) {
1268 // If this activity is being launched as part of a voice session, we need
1269 // to ensure that it is safe to do so. If the upcoming activity will also
1270 // be part of the voice session, we can only launch it if it has explicitly
1271 // said it supports the VOICE category, or it is a part of the calling app.
1272 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
1273 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1274 try {
1275 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1276 intent, resolvedType)) {
1277 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1278 }
1279 } catch (RemoteException e) {
1280 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1281 }
1282 }
1283 }
1284
1285 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1286 // If the caller is starting a new voice session, just make sure the target
1287 // is actually allowing it to run this way.
1288 try {
1289 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1290 intent, resolvedType)) {
1291 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1292 }
1293 } catch (RemoteException e) {
1294 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1295 }
1296 }
1297
Craig Mautner6170f732013-04-02 13:05:23 -07001298 if (err != ActivityManager.START_SUCCESS) {
1299 if (resultRecord != null) {
1300 resultStack.sendActivityResultLocked(-1,
1301 resultRecord, resultWho, requestCode,
1302 Activity.RESULT_CANCELED, null);
1303 }
1304 setDismissKeyguard(false);
1305 ActivityOptions.abort(options);
1306 return err;
1307 }
1308
1309 final int startAnyPerm = mService.checkPermission(
1310 START_ANY_ACTIVITY, callingPid, callingUid);
1311 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1312 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1313 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1314 if (resultRecord != null) {
1315 resultStack.sendActivityResultLocked(-1,
1316 resultRecord, resultWho, requestCode,
1317 Activity.RESULT_CANCELED, null);
1318 }
1319 setDismissKeyguard(false);
1320 String msg;
1321 if (!aInfo.exported) {
1322 msg = "Permission Denial: starting " + intent.toString()
1323 + " from " + callerApp + " (pid=" + callingPid
1324 + ", uid=" + callingUid + ")"
1325 + " not exported from uid " + aInfo.applicationInfo.uid;
1326 } else {
1327 msg = "Permission Denial: starting " + intent.toString()
1328 + " from " + callerApp + " (pid=" + callingPid
1329 + ", uid=" + callingUid + ")"
1330 + " requires " + aInfo.permission;
1331 }
1332 Slog.w(TAG, msg);
1333 throw new SecurityException(msg);
1334 }
1335
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001336 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001337 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001338
Craig Mautner6170f732013-04-02 13:05:23 -07001339 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001340 try {
1341 // The Intent we give to the watcher has the extra data
1342 // stripped off, since it can contain private information.
1343 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001344 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001345 aInfo.applicationInfo.packageName);
1346 } catch (RemoteException e) {
1347 mService.mController = null;
1348 }
Ben Gruver5e207332013-04-03 17:41:37 -07001349 }
Craig Mautner6170f732013-04-02 13:05:23 -07001350
Ben Gruver5e207332013-04-03 17:41:37 -07001351 if (abort) {
1352 if (resultRecord != null) {
1353 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001354 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001355 }
Ben Gruver5e207332013-04-03 17:41:37 -07001356 // We pretend to the caller that it was really started, but
1357 // they will just get a cancel result.
1358 setDismissKeyguard(false);
1359 ActivityOptions.abort(options);
1360 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001361 }
1362
1363 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001364 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001365 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001366 if (outActivity != null) {
1367 outActivity[0] = r;
1368 }
1369
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001370 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001371 if (voiceSession == null && (stack.mResumedActivity == null
1372 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Craig Mautner6170f732013-04-02 13:05:23 -07001373 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1374 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001375 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautner6170f732013-04-02 13:05:23 -07001376 mService.mPendingActivityLaunches.add(pal);
1377 setDismissKeyguard(false);
1378 ActivityOptions.abort(options);
1379 return ActivityManager.START_SWITCHES_CANCELED;
1380 }
1381 }
1382
1383 if (mService.mDidAppSwitch) {
1384 // This is the second allowed switch since we stopped switches,
1385 // so now just generally allow switches. Use case: user presses
1386 // home (switches disabled, switch to home, mDidAppSwitch now true);
1387 // user taps a home icon (coming from home so allowed, we hit here
1388 // and now allow anyone to switch again).
1389 mService.mAppSwitchesAllowedTime = 0;
1390 } else {
1391 mService.mDidAppSwitch = true;
1392 }
1393
1394 mService.doPendingActivityLaunchesLocked(false);
1395
Dianne Hackborn91097de2014-04-04 18:02:06 -07001396 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
1397 startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001398
1399 if (allPausedActivitiesComplete()) {
1400 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001401 // activity start, but we are not actually doing an activity
1402 // switch... just dismiss the keyguard now, because we
1403 // probably want to see whatever is behind it.
1404 dismissKeyguard();
1405 }
1406 return err;
1407 }
1408
Craig Mautner1b4bf852014-05-26 15:06:32 -07001409 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001410 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001411
1412 // On leanback only devices we should keep all activities in the same stack.
1413 if (!mLeanbackOnlyDevice &&
1414 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001415 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001416 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001417 if (taskStack.isOnHomeDisplay()) {
1418 if (mFocusedStack != taskStack) {
1419 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1420 "focused stack to r=" + r + " task=" + task);
1421 mFocusedStack = taskStack;
1422 } else {
1423 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1424 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1425 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001426 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001427 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001428 }
1429
Craig Mautnere0a38842013-12-16 16:14:02 -08001430 final ActivityContainer container = r.mInitialActivityContainer;
1431 if (container != null) {
1432 // The first time put it on the desired stack, after this put on task stack.
1433 r.mInitialActivityContainer = null;
1434 return container.mStack;
1435 }
1436
Craig Mautner1b4bf852014-05-26 15:06:32 -07001437 if (mFocusedStack != mHomeStack && (!newTask ||
1438 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001439 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1440 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1441 return mFocusedStack;
1442 }
1443
Craig Mautnere0a38842013-12-16 16:14:02 -08001444 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1445 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1446 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1447 if (!stack.isHomeStack()) {
1448 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1449 "adjustStackFocus: Setting focused stack=" + stack);
1450 mFocusedStack = stack;
1451 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001452 }
1453 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001454
Craig Mautner4a1cb222013-12-04 16:14:06 -08001455 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001456 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001457 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1458 " stackId=" + stackId);
1459 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001460 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001461 }
1462 return mHomeStack;
1463 }
1464
Craig Mautner29219d92013-04-16 20:19:12 -07001465 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001466 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001467 final TaskRecord task = r.task;
1468 boolean isHomeActivity = !r.isApplicationActivity();
1469 if (!isHomeActivity && task != null) {
1470 isHomeActivity = !task.isApplicationTask();
1471 }
1472 if (!isHomeActivity && task != null) {
1473 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1474 isHomeActivity = parent != null && parent.isHomeActivity();
1475 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001476 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001477 }
1478 }
1479
Craig Mautner8849a5e2013-04-02 16:41:03 -07001480 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001481 ActivityRecord sourceRecord,
1482 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
1483 boolean doResume, Bundle options) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001484 final Intent intent = r.intent;
1485 final int callingUid = r.launchedFromUid;
1486
1487 int launchFlags = intent.getFlags();
1488
Craig Mautner8849a5e2013-04-02 16:41:03 -07001489 // We'll invoke onUserLeaving before onPause only if the launching
1490 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001491 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001492 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001493
1494 // If the caller has asked not to resume at this point, we make note
1495 // of this in the record so that we can skip it when trying to find
1496 // the top running activity.
1497 if (!doResume) {
1498 r.delayedResume = true;
1499 }
1500
Craig Mautnera254cd72014-05-25 16:47:39 -07001501 ActivityRecord notTop =
1502 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001503
1504 // If the onlyIfNeeded flag is set, then we can do this if the activity
1505 // being launched is the same as the one making the call... or, as
1506 // a special case, if we do not know the caller then we count the
1507 // current top activity as the caller.
1508 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1509 ActivityRecord checkedCaller = sourceRecord;
1510 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001511 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001512 }
1513 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1514 // Caller is not the same as launcher, so always needed.
1515 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1516 }
1517 }
1518
Craig Mautner41db4a72014-05-07 17:20:56 -07001519 switch (r.info.documentLaunchMode) {
1520 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1521 break;
1522 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1523 intent.addFlags(
1524 Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
Craig Mautnera254cd72014-05-25 16:47:39 -07001525 launchFlags = intent.getFlags();
Craig Mautner41db4a72014-05-07 17:20:56 -07001526 break;
1527 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1528 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Craig Mautnera254cd72014-05-25 16:47:39 -07001529 launchFlags = intent.getFlags();
Craig Mautner41db4a72014-05-07 17:20:56 -07001530 break;
1531 }
Craig Mautnerd00f4742014-03-12 14:17:26 -07001532 final boolean newDocument = intent.isDocument();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001533 if (sourceRecord == null) {
1534 // This activity is not being started from another... in this
1535 // case we -always- start a new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001536 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001537 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1538 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001539 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1540 }
Craig Mautnerd00f4742014-03-12 14:17:26 -07001541 } else if (newDocument) {
1542 if (r.launchMode != ActivityInfo.LAUNCH_MULTIPLE) {
1543 Slog.w(TAG, "FLAG_ACTIVITY_NEW_DOCUMENT and launchMode != \"standard\"");
1544 r.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
1545 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001546 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1547 // The original activity who is starting us is running as a single
1548 // instance... this new activity it is starting must go on its
1549 // own task.
1550 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1551 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
1552 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1553 // The activity being started is a single instance... it always
1554 // gets launched into its own task.
1555 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1556 }
1557
Craig Mautner88629292013-11-10 20:39:05 -08001558 ActivityInfo newTaskInfo = null;
1559 Intent newTaskIntent = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001560 final ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001561 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001562 if (sourceRecord.finishing) {
1563 // If the source is finishing, we can't further count it as our source. This
1564 // is because the task it is associated with may now be empty and on its way out,
1565 // so we don't want to blindly throw it in to that task. Instead we will take
Craig Mautner88629292013-11-10 20:39:05 -08001566 // the NEW_TASK flow and try to find a task for it. But save the task information
1567 // so it can be used when creating the new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001568 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001569 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1570 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1571 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautner88629292013-11-10 20:39:05 -08001572 newTaskInfo = sourceRecord.info;
1573 newTaskIntent = sourceRecord.task.intent;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001574 }
1575 sourceRecord = null;
1576 sourceStack = null;
1577 } else {
1578 sourceStack = sourceRecord.task.stack;
1579 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001580 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001581 sourceStack = null;
1582 }
1583
Craig Mautnerd00f4742014-03-12 14:17:26 -07001584 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001585 // For whatever reason this activity is being launched into a new
1586 // task... yet the caller has requested a result back. Well, that
1587 // is pretty messed up, so instead immediately send back a cancel
1588 // and let the new task continue launched as normal without a
1589 // dependency on its originator.
1590 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1591 r.resultTo.task.stack.sendActivityResultLocked(-1,
1592 r.resultTo, r.resultWho, r.requestCode,
1593 Activity.RESULT_CANCELED, null);
1594 r.resultTo = null;
1595 }
1596
1597 boolean addingToTask = false;
1598 boolean movedHome = false;
1599 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001600 ActivityStack targetStack;
Craig Mautnerd00f4742014-03-12 14:17:26 -07001601 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1602 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001603 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1604 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1605 // If bring to front is requested, and no result is requested, and
1606 // we can find a task that was started with this same
1607 // component, then instead of launching bring that one to the front.
1608 if (r.resultTo == null) {
1609 // See if there is a task to bring to the front. If this is
1610 // a SINGLE_INSTANCE activity, there can be one and only one
1611 // instance of it in the history, and it is always in its own
1612 // unique task, so we do a special search.
1613 ActivityRecord intentActivity = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
Craig Mautnerac6f8432013-07-17 13:24:59 -07001614 ? findTaskLocked(r)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001615 : findActivityLocked(intent, r.info);
1616 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001617 if (isLockTaskModeViolation(intentActivity.task)) {
1618 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
1619 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1620 }
Craig Mautner29219d92013-04-16 20:19:12 -07001621 if (r.task == null) {
1622 r.task = intentActivity.task;
1623 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001624 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001625 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001626 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1627 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001628 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001629 if (intentActivity.task.intent == null) {
1630 // This task was started because of movement of
1631 // the activity based on affinity... now that we
1632 // are actually launching it, we can assign the
1633 // base intent.
1634 intentActivity.task.setIntent(intent, r.info);
1635 }
1636 // If the target task is not in the front, then we need
1637 // to bring it to the front... except... well, with
1638 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1639 // to have the same behavior as if a new instance was
1640 // being started, which means not bringing it to the front
1641 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001642 final ActivityStack lastStack = getLastStack();
1643 ActivityRecord curTop = lastStack == null?
1644 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001645 if (curTop != null && (curTop.task != intentActivity.task ||
1646 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001647 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001648 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1649 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001650 // We really do want to push this one into the
1651 // user's face, right now.
1652 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001653 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001654 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001655 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1656 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001657 // Caller wants to appear on home activity.
Craig Mautner719e6212014-05-29 16:49:41 +00001658 intentActivity.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001659 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001660 options = null;
1661 }
1662 }
1663 // If the caller has requested that the target task be
1664 // reset, then do so.
1665 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1666 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1667 }
1668 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1669 // We don't need to start a new activity, and
1670 // the client said not to do anything if that
1671 // is the case, so this is it! And for paranoia, make
1672 // sure we have correctly resumed the top activity.
1673 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001674 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001675 } else {
1676 ActivityOptions.abort(options);
1677 }
1678 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1679 }
1680 if ((launchFlags &
1681 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1682 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1683 // The caller has requested to completely replace any
1684 // existing task with its new activity. Well that should
1685 // not be too hard...
1686 reuseTask = intentActivity.task;
1687 reuseTask.performClearTaskLocked();
1688 reuseTask.setIntent(r.intent, r.info);
1689 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
1690 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1691 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1692 // In this situation we want to remove all activities
1693 // from the task up to the one being started. In most
1694 // cases this means we are resetting the task to its
1695 // initial state.
1696 ActivityRecord top =
1697 intentActivity.task.performClearTaskLocked(r, launchFlags);
1698 if (top != null) {
1699 if (top.frontOfTask) {
1700 // Activity aliases may mean we use different
1701 // intents for the top activity, so make sure
1702 // the task now has the identity of the new
1703 // intent.
1704 top.task.setIntent(r.intent, r.info);
1705 }
1706 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1707 r, top.task);
1708 top.deliverNewIntentLocked(callingUid, r.intent);
1709 } else {
1710 // A special case: we need to
1711 // start the activity because it is not currently
1712 // running, and the caller has asked to clear the
1713 // current task to have this activity at the top.
1714 addingToTask = true;
1715 // Now pretend like this activity is being started
1716 // by the top of its task, so it is put in the
1717 // right place.
1718 sourceRecord = intentActivity;
1719 }
1720 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1721 // In this case the top activity on the task is the
1722 // same as the one being launched, so we take that
1723 // as a request to bring the task to the foreground.
1724 // If the top activity in the task is the root
1725 // activity, deliver this new intent to it if it
1726 // desires.
1727 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1728 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1729 && intentActivity.realActivity.equals(r.realActivity)) {
1730 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1731 intentActivity.task);
1732 if (intentActivity.frontOfTask) {
1733 intentActivity.task.setIntent(r.intent, r.info);
1734 }
1735 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1736 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1737 // In this case we are launching the root activity
1738 // of the task, but with a different intent. We
1739 // should start a new instance on top.
1740 addingToTask = true;
1741 sourceRecord = intentActivity;
1742 }
1743 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1744 // In this case an activity is being launched in to an
1745 // existing task, without resetting that task. This
1746 // is typically the situation of launching an activity
1747 // from a notification or shortcut. We want to place
1748 // the new activity on top of the current task.
1749 addingToTask = true;
1750 sourceRecord = intentActivity;
1751 } else if (!intentActivity.task.rootWasReset) {
1752 // In this case we are launching in to an existing task
1753 // that has not yet been started from its front door.
1754 // The current task has been brought to the front.
1755 // Ideally, we'd probably like to place this new task
1756 // at the bottom of its stack, but that's a little hard
1757 // to do with the current organization of the code so
1758 // for now we'll just drop it.
1759 intentActivity.task.setIntent(r.intent, r.info);
1760 }
1761 if (!addingToTask && reuseTask == null) {
1762 // We didn't do anything... but it was needed (a.k.a., client
1763 // don't use that intent!) And for paranoia, make
1764 // sure we have correctly resumed the top activity.
1765 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001766 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001767 } else {
1768 ActivityOptions.abort(options);
1769 }
1770 return ActivityManager.START_TASK_TO_FRONT;
1771 }
1772 }
1773 }
1774 }
1775
1776 //String uri = r.intent.toURI();
1777 //Intent intent2 = new Intent(uri);
1778 //Slog.i(TAG, "Given intent: " + r.intent);
1779 //Slog.i(TAG, "URI is: " + uri);
1780 //Slog.i(TAG, "To intent: " + intent2);
1781
1782 if (r.packageName != null) {
1783 // If the activity being launched is the same as the one currently
1784 // at the top, then we need to check if it should only be launched
1785 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001786 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001787 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001788 if (top != null && r.resultTo == null) {
1789 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1790 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001791 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautner8849a5e2013-04-02 16:41:03 -07001792 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
1793 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1794 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1795 top.task);
1796 // For paranoia, make sure we have correctly
1797 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001798 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001799 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001800 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001801 }
1802 ActivityOptions.abort(options);
1803 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1804 // We don't need to start a new activity, and
1805 // the client said not to do anything if that
1806 // is the case, so this is it!
1807 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1808 }
1809 top.deliverNewIntentLocked(callingUid, r.intent);
1810 return ActivityManager.START_DELIVERED_TO_TOP;
1811 }
1812 }
1813 }
1814 }
1815
1816 } else {
1817 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001818 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1819 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001820 }
1821 ActivityOptions.abort(options);
1822 return ActivityManager.START_CLASS_NOT_FOUND;
1823 }
1824
1825 boolean newTask = false;
1826 boolean keepCurTransition = false;
1827
1828 // Should this be considered a new task?
1829 if (r.resultTo == null && !addingToTask
1830 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001831 if (isLockTaskModeViolation(reuseTask)) {
1832 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1833 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1834 }
Craig Mautner1b4bf852014-05-26 15:06:32 -07001835 newTask = true;
1836 targetStack = adjustStackFocus(r, newTask);
Craig Mautnere0a38842013-12-16 16:14:02 -08001837 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001838 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08001839 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
1840 newTaskInfo != null ? newTaskInfo : r.info,
1841 newTaskIntent != null ? newTaskIntent : intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001842 voiceSession, voiceInteractor, true), null, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001843 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1844 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001845 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001846 r.setTask(reuseTask, reuseTask, true);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001847 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001848 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001849 if ((launchFlags &
1850 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1851 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1852 // Caller wants to appear on home activity, so before starting
1853 // their own activity we will bring home to the front.
Craig Mautner719e6212014-05-29 16:49:41 +00001854 r.task.mOnTopOfHome = r.task.stack.isOnHomeDisplay();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001855 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001856 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001857 } else if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001858 TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08001859 if (isLockTaskModeViolation(sourceTask)) {
1860 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1861 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1862 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001863 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07001864 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07001865 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001866 if (!addingToTask &&
1867 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
1868 // In this case, we are adding the activity to an existing
1869 // task, but the caller has asked to clear that task if the
1870 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001871 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001872 keepCurTransition = true;
1873 if (top != null) {
1874 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1875 top.deliverNewIntentLocked(callingUid, r.intent);
1876 // For paranoia, make sure we have correctly
1877 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001878 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001879 if (doResume) {
1880 targetStack.resumeTopActivityLocked(null);
1881 }
1882 ActivityOptions.abort(options);
1883 return ActivityManager.START_DELIVERED_TO_TOP;
1884 }
1885 } else if (!addingToTask &&
1886 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1887 // In this case, we are launching an activity in our own task
1888 // that may already be running somewhere in the history, and
1889 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001890 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001891 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001892 final TaskRecord task = top.task;
1893 task.moveActivityToFrontLocked(top);
1894 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001895 top.updateOptionsLocked(options);
1896 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001897 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001898 if (doResume) {
1899 targetStack.resumeTopActivityLocked(null);
1900 }
1901 return ActivityManager.START_DELIVERED_TO_TOP;
1902 }
1903 }
1904 // An existing activity is starting this new activity, so we want
1905 // to keep the new one in the same task as the one that is starting
1906 // it.
Craig Mautner525f3d92013-05-07 14:01:50 -07001907 r.setTask(sourceTask, sourceRecord.thumbHolder, false);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001908 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001909 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001910
1911 } else {
1912 // This not being started from an existing activity, and not part
1913 // of a new task... just put it in the top task, though these days
1914 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07001915 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07001916 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07001917 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001918 r.setTask(prev != null ? prev.task
Dianne Hackborn91097de2014-04-04 18:02:06 -07001919 : targetStack.createTaskRecord(getNextTaskId(), r.info, intent, null, null, true),
Craig Mautnerde4ef022013-04-07 19:01:33 -07001920 null, true);
Craig Mautner95e9daa2014-03-17 15:57:20 -07001921 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001922 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
1923 + " in new guessed " + r.task);
1924 }
1925
1926 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001927 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001928
1929 if (newTask) {
1930 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
1931 }
1932 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07001933 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001934 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautner1d001b62013-06-18 16:52:43 -07001935 mService.setFocusedActivityLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001936 return ActivityManager.START_SUCCESS;
1937 }
1938
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001939 void acquireLaunchWakelock() {
1940 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1941 throw new IllegalStateException("Calling must be system uid");
1942 }
1943 mLaunchingActivity.acquire();
1944 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1945 // To be safe, don't allow the wake lock to be held for too long.
1946 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1947 }
1948 }
1949
Craig Mautnerf3333272013-04-22 10:55:53 -07001950 // Checked.
1951 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1952 Configuration config) {
1953 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
1954
Craig Mautnerf3333272013-04-22 10:55:53 -07001955 ArrayList<ActivityRecord> stops = null;
1956 ArrayList<ActivityRecord> finishes = null;
1957 ArrayList<UserStartedState> startingUsers = null;
1958 int NS = 0;
1959 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001960 boolean booting = false;
1961 boolean enableScreen = false;
1962 boolean activityRemoved = false;
1963
1964 ActivityRecord r = ActivityRecord.forToken(token);
1965 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001966 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
1967 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001968 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1969 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001970 if (fromTimeout) {
1971 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001972 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001973
1974 // This is a hack to semi-deal with a race condition
1975 // in the client where it can be constructed with a
1976 // newer configuration from when we asked it to launch.
1977 // We'll update with whatever configuration it now says
1978 // it used to launch.
1979 if (config != null) {
1980 r.configuration = config;
1981 }
1982
1983 // We are now idle. If someone is waiting for a thumbnail from
1984 // us, we can now deliver.
1985 r.idle = true;
1986
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001987 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
1988 if (!mService.mBooted && isFrontStack(r.task.stack)) {
1989 mService.mBooted = true;
1990 enableScreen = true;
1991 }
1992 }
1993
1994 if (allResumedActivitiesIdle()) {
1995 if (r != null) {
1996 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001997 }
1998
1999 if (mLaunchingActivity.isHeld()) {
2000 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2001 if (VALIDATE_WAKE_LOCK_CALLER &&
2002 Binder.getCallingUid() != Process.myUid()) {
2003 throw new IllegalStateException("Calling must be system uid");
2004 }
2005 mLaunchingActivity.release();
2006 }
2007 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002008 }
2009
2010 // Atomically retrieve all of the other things to do.
2011 stops = processStoppingActivitiesLocked(true);
2012 NS = stops != null ? stops.size() : 0;
2013 if ((NF=mFinishingActivities.size()) > 0) {
2014 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2015 mFinishingActivities.clear();
2016 }
2017
Craig Mautnerf3333272013-04-22 10:55:53 -07002018 if (isFrontStack(mHomeStack)) {
2019 booting = mService.mBooting;
2020 mService.mBooting = false;
2021 }
2022
2023 if (mStartingUsers.size() > 0) {
2024 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2025 mStartingUsers.clear();
2026 }
2027
Craig Mautnerf3333272013-04-22 10:55:53 -07002028 // Stop any activities that are scheduled to do so but have been
2029 // waiting for the next one to start.
2030 for (int i = 0; i < NS; i++) {
2031 r = stops.get(i);
2032 final ActivityStack stack = r.task.stack;
2033 if (r.finishing) {
2034 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2035 } else {
2036 stack.stopActivityLocked(r);
2037 }
2038 }
2039
2040 // Finish any activities that are scheduled to do so but have been
2041 // waiting for the next one to start.
2042 for (int i = 0; i < NF; i++) {
2043 r = finishes.get(i);
2044 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, false, "finish-idle");
2045 }
2046
2047 if (booting) {
2048 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002049 } else {
2050 // Complete user switch
2051 if (startingUsers != null) {
2052 for (int i = 0; i < startingUsers.size(); i++) {
2053 mService.finishUserSwitch(startingUsers.get(i));
2054 }
2055 }
2056 // Complete starting up of background users
2057 if (mStartingBackgroundUsers.size() > 0) {
2058 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2059 mStartingBackgroundUsers.clear();
2060 for (int i = 0; i < startingUsers.size(); i++) {
2061 mService.finishUserBoot(startingUsers.get(i));
2062 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002063 }
2064 }
2065
2066 mService.trimApplications();
2067 //dump();
2068 //mWindowManager.dump();
2069
2070 if (enableScreen) {
2071 mService.enableScreenAfterBoot();
2072 }
2073
2074 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002075 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002076 }
2077
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002078 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002079 }
2080
Craig Mautner8e569572013-10-11 17:36:59 -07002081 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002082 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002083 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2084 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002085 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2086 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2087 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002088 }
Craig Mautner19091252013-10-05 00:03:53 -07002089 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002090 }
2091
2092 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002093 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2094 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002095 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2096 stacks.get(stackNdx).closeSystemDialogsLocked();
2097 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002098 }
2099 }
2100
Craig Mautner93529a42013-10-04 15:03:13 -07002101 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002102 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002103 }
2104
Craig Mautner8d341ef2013-03-26 09:03:27 -07002105 /**
2106 * @return true if some activity was finished (or would have finished if doit were true).
2107 */
2108 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2109 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002110 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2111 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002112 final int numStacks = stacks.size();
2113 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2114 final ActivityStack stack = stacks.get(stackNdx);
2115 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2116 didSomething = true;
2117 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002118 }
2119 }
2120 return didSomething;
2121 }
2122
Dianne Hackborna413dc02013-07-12 12:02:55 -07002123 void updatePreviousProcessLocked(ActivityRecord r) {
2124 // Now that this process has stopped, we may want to consider
2125 // it to be the previous app to try to keep around in case
2126 // the user wants to return to it.
2127
2128 // First, found out what is currently the foreground app, so that
2129 // we don't blow away the previous app if this activity is being
2130 // hosted by the process that is actually still the foreground.
2131 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002132 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2133 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002134 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2135 final ActivityStack stack = stacks.get(stackNdx);
2136 if (isFrontStack(stack)) {
2137 if (stack.mResumedActivity != null) {
2138 fgApp = stack.mResumedActivity.app;
2139 } else if (stack.mPausingActivity != null) {
2140 fgApp = stack.mPausingActivity.app;
2141 }
2142 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002143 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002144 }
2145 }
2146
2147 // Now set this one as the previous process, only if that really
2148 // makes sense to.
2149 if (r.app != null && fgApp != null && r.app != fgApp
2150 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002151 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002152 mService.mPreviousProcess = r.app;
2153 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2154 }
2155 }
2156
Craig Mautner05d29032013-05-03 13:40:13 -07002157 boolean resumeTopActivitiesLocked() {
2158 return resumeTopActivitiesLocked(null, null, null);
2159 }
2160
2161 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2162 Bundle targetOptions) {
2163 if (targetStack == null) {
2164 targetStack = getFocusedStack();
2165 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002166 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002167 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002168 if (isFrontStack(targetStack)) {
2169 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2170 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002171 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2172 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002173 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2174 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002175 if (stack == targetStack) {
2176 // Already started above.
2177 continue;
2178 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002179 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002180 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002181 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002182 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002183 }
Craig Mautner05d29032013-05-03 13:40:13 -07002184 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002185 }
2186
2187 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002188 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2189 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002190 final int numStacks = stacks.size();
2191 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2192 final ActivityStack stack = stacks.get(stackNdx);
2193 stack.finishTopRunningActivityLocked(app);
2194 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002195 }
2196 }
2197
Craig Mautneraea74a52014-03-08 14:23:10 -08002198 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2199 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2200 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002201 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002202 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2203 // Caller wants the home activity moved with it. To accomplish this,
2204 // we'll just indicate that this task returns to the home task.
Craig Mautner719e6212014-05-29 16:49:41 +00002205 task.mOnTopOfHome = true;
Craig Mautneraea74a52014-03-08 14:23:10 -08002206 }
2207 task.stack.moveTaskToFrontLocked(task, null, options);
2208 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2209 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002210 }
2211
Craig Mautner967212c2013-04-13 21:10:58 -07002212 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002213 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2214 if (activityContainer != null) {
2215 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002216 }
2217 return null;
2218 }
2219
Craig Mautner967212c2013-04-13 21:10:58 -07002220 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002221 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002222 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2223 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002224 }
2225 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002226 }
2227
Craig Mautner4a1cb222013-12-04 16:14:06 -08002228 IBinder getHomeActivityToken() {
2229 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2230 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2231 final TaskRecord task = tasks.get(taskNdx);
2232 if (task.isHomeTask()) {
2233 final ArrayList<ActivityRecord> activities = task.mActivities;
2234 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2235 final ActivityRecord r = activities.get(activityNdx);
2236 if (r.isHomeActivity()) {
2237 return r.appToken;
2238 }
2239 }
2240 }
2241 }
2242 return null;
2243 }
2244
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002245 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002246 IActivityContainerCallback callback) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002247 ActivityContainer activityContainer = new VirtualActivityContainer(parentActivity, callback);
2248 mActivityContainers.put(activityContainer.mStackId, activityContainer);
2249 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002250 return activityContainer;
2251 }
2252
Craig Mautner34b73df2014-01-12 21:11:08 -08002253 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002254 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2255 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2256 ActivityContainer container = childStacks.remove(containerNdx);
2257 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002258 }
2259 }
2260
Craig Mautner95da1082014-02-24 17:54:35 -08002261 void deleteActivityContainer(IActivityContainer container) {
2262 ActivityContainer activityContainer = (ActivityContainer)container;
2263 if (activityContainer != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002264 activityContainer.mStack.finishAllActivitiesLocked();
Craig Mautner95da1082014-02-24 17:54:35 -08002265 final ActivityRecord parent = activityContainer.mParentActivity;
2266 if (parent != null) {
2267 parent.mChildContainers.remove(activityContainer);
2268 }
2269 final int stackId = activityContainer.mStackId;
2270 mActivityContainers.remove(stackId);
2271 mWindowManager.removeStack(stackId);
2272 }
2273 }
2274
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002275 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002276 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2277 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002278 return -1;
2279 }
2280
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002281 ActivityContainer activityContainer = new ActivityContainer(stackId);
2282 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002283 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002284 return stackId;
2285 }
2286
2287 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002288 while (true) {
2289 if (++mLastStackId <= HOME_STACK_ID) {
2290 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002291 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002292 if (getStack(mLastStackId) == null) {
2293 break;
2294 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002295 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002296 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002297 }
2298
Craig Mautneref73ee12014-04-23 11:45:37 -07002299 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2300 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2301 final ActivityStack stack = getStack(stackId);
2302 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2303 final TaskRecord task = tasks.get(taskNdx);
2304 stack.addTask(task, false, false);
2305 final int taskId = task.taskId;
2306 final ArrayList<ActivityRecord> activities = task.mActivities;
2307 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2308 final ActivityRecord r = activities.get(activityNdx);
2309 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2310 r.info.screenOrientation, r.fullscreen,
2311 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07002312 r.userId, r.info.configChanges, task.voiceSession != null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002313 }
2314 mWindowManager.addTask(taskId, stackId, false);
2315 }
Craig Mautner719e6212014-05-29 16:49:41 +00002316 resumeHomeActivity(null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002317 }
2318
Craig Mautner8d341ef2013-03-26 09:03:27 -07002319 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002320 final TaskRecord task = anyTaskForIdLocked(taskId);
2321 if (task == null) {
2322 return;
2323 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002324 final ActivityStack stack = getStack(stackId);
2325 if (stack == null) {
2326 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2327 return;
2328 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002329 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002330 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002331 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002332 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002333 }
2334
Craig Mautnerac6f8432013-07-17 13:24:59 -07002335 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002336 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002337 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2338 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002339 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2340 final ActivityStack stack = stacks.get(stackNdx);
2341 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002342 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2343 continue;
2344 }
2345 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2346 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2347 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002348 continue;
2349 }
2350 final ActivityRecord ar = stack.findTaskLocked(r);
2351 if (ar != null) {
2352 return ar;
2353 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002354 }
2355 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002356 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002357 return null;
2358 }
2359
2360 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002361 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2362 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002363 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2364 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2365 if (ar != null) {
2366 return ar;
2367 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002368 }
2369 }
2370 return null;
2371 }
2372
Craig Mautner8d341ef2013-03-26 09:03:27 -07002373 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002374 scheduleSleepTimeout();
2375 if (!mGoingToSleep.isHeld()) {
2376 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002377 if (mLaunchingActivity.isHeld()) {
2378 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2379 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002380 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002381 mLaunchingActivity.release();
2382 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002383 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002384 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002385 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002386 }
2387
2388 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002389 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002390
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002391 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002392 final long endTime = System.currentTimeMillis() + timeout;
2393 while (true) {
2394 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002395 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2396 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002397 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2398 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2399 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002400 }
2401 if (cantShutdown) {
2402 long timeRemaining = endTime - System.currentTimeMillis();
2403 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002404 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002405 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002406 } catch (InterruptedException e) {
2407 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002408 } else {
2409 Slog.w(TAG, "Activity manager shutdown timed out");
2410 timedout = true;
2411 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002412 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002413 } else {
2414 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002415 }
2416 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002417
2418 // Force checkReadyForSleep to complete.
2419 mSleepTimeout = true;
2420 checkReadyForSleepLocked();
2421
Craig Mautner8d341ef2013-03-26 09:03:27 -07002422 return timedout;
2423 }
2424
2425 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002426 removeSleepTimeouts();
2427 if (mGoingToSleep.isHeld()) {
2428 mGoingToSleep.release();
2429 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002430 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2431 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002432 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2433 final ActivityStack stack = stacks.get(stackNdx);
2434 stack.awakeFromSleepingLocked();
2435 if (isFrontStack(stack)) {
2436 resumeTopActivitiesLocked();
2437 }
Craig Mautner5314a402013-09-26 12:40:16 -07002438 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002439 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002440 mGoingToSleepActivities.clear();
2441 }
2442
2443 void activitySleptLocked(ActivityRecord r) {
2444 mGoingToSleepActivities.remove(r);
2445 checkReadyForSleepLocked();
2446 }
2447
2448 void checkReadyForSleepLocked() {
2449 if (!mService.isSleepingOrShuttingDown()) {
2450 // Do not care.
2451 return;
2452 }
2453
2454 if (!mSleepTimeout) {
2455 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002456 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2457 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002458 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2459 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2460 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002461 }
2462
2463 if (mStoppingActivities.size() > 0) {
2464 // Still need to tell some activities to stop; can't sleep yet.
2465 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2466 + mStoppingActivities.size() + " activities");
2467 scheduleIdleLocked();
2468 dontSleep = true;
2469 }
2470
2471 if (mGoingToSleepActivities.size() > 0) {
2472 // Still need to tell some activities to sleep; can't sleep yet.
2473 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2474 + mGoingToSleepActivities.size() + " activities");
2475 dontSleep = true;
2476 }
2477
2478 if (dontSleep) {
2479 return;
2480 }
2481 }
2482
Craig Mautnere0a38842013-12-16 16:14:02 -08002483 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2484 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002485 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2486 stacks.get(stackNdx).goToSleep();
2487 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002488 }
2489
2490 removeSleepTimeouts();
2491
2492 if (mGoingToSleep.isHeld()) {
2493 mGoingToSleep.release();
2494 }
2495 if (mService.mShuttingDown) {
2496 mService.notifyAll();
2497 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002498 }
2499
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002500 boolean reportResumedActivityLocked(ActivityRecord r) {
2501 final ActivityStack stack = r.task.stack;
2502 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002503 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002504 }
2505 if (allResumedActivitiesComplete()) {
2506 ensureActivitiesVisibleLocked(null, 0);
2507 mWindowManager.executeAppTransition();
2508 return true;
2509 }
2510 return false;
2511 }
2512
Craig Mautner8d341ef2013-03-26 09:03:27 -07002513 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002514 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2515 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002516 final int numStacks = stacks.size();
2517 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2518 final ActivityStack stack = stacks.get(stackNdx);
2519 stack.handleAppCrashLocked(app);
2520 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002521 }
2522 }
2523
Craig Mautnerde4ef022013-04-07 19:01:33 -07002524 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002525 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002526 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2527 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002528 final int topStackNdx = stacks.size() - 1;
2529 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2530 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002531 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002532 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002533 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002534 }
2535
2536 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002537 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2538 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002539 final int numStacks = stacks.size();
2540 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2541 final ActivityStack stack = stacks.get(stackNdx);
2542 stack.scheduleDestroyActivities(app, false, reason);
2543 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002544 }
2545 }
2546
2547 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002548 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2549 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002550 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002551
Craig Mautner858d8a62013-04-23 17:08:34 -07002552 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002553 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2554 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002555 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002556 final ActivityStack stack = stacks.get(stackNdx);
2557 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002558 TaskRecord task = stack.topTask();
2559 if (task != null) {
2560 mWindowManager.moveTaskToTop(task.taskId);
2561 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002562 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002563 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002564
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002565 ActivityStack stack = getStack(restoreStackId);
2566 if (stack == null) {
2567 stack = mHomeStack;
2568 }
2569 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002570 if (stack.isOnHomeDisplay()) {
2571 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002572 TaskRecord task = stack.topTask();
2573 if (task != null) {
2574 mWindowManager.moveTaskToTop(task.taskId);
2575 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002576 } else {
2577 // Stack was moved to another display while user was swapped out.
Craig Mautner719e6212014-05-29 16:49:41 +00002578 resumeHomeActivity(null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002579 }
Craig Mautner93529a42013-10-04 15:03:13 -07002580 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002581 }
2582
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002583 /**
2584 * Add background users to send boot completed events to.
2585 * @param userId The user being started in the background
2586 * @param uss The state object for the user.
2587 */
2588 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2589 mStartingBackgroundUsers.add(uss);
2590 }
2591
Craig Mautnerde4ef022013-04-07 19:01:33 -07002592 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2593 int N = mStoppingActivities.size();
2594 if (N <= 0) return null;
2595
2596 ArrayList<ActivityRecord> stops = null;
2597
2598 final boolean nowVisible = allResumedActivitiesVisible();
2599 for (int i=0; i<N; i++) {
2600 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002601 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002602 + nowVisible + " waitingVisible=" + s.waitingVisible
2603 + " finishing=" + s.finishing);
2604 if (s.waitingVisible && nowVisible) {
2605 mWaitingVisibleActivities.remove(s);
2606 s.waitingVisible = false;
2607 if (s.finishing) {
2608 // If this activity is finishing, it is sitting on top of
2609 // everyone else but we now know it is no longer needed...
2610 // so get rid of it. Otherwise, we need to go through the
2611 // normal flow and hide it once we determine that it is
2612 // hidden by the activities in front of it.
2613 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002614 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002615 }
2616 }
2617 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2618 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2619 if (stops == null) {
2620 stops = new ArrayList<ActivityRecord>();
2621 }
2622 stops.add(s);
2623 mStoppingActivities.remove(i);
2624 N--;
2625 i--;
2626 }
2627 }
2628
2629 return stops;
2630 }
2631
Craig Mautnercf910b02013-04-23 11:23:27 -07002632 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002633 // FIXME
2634/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2635 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07002636 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002637 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002638 if (isFrontStack(stack)) {
2639 if (r == null) {
2640 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2641 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002642 final ActivityRecord pausing = stack.mPausingActivity;
2643 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002644 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002645 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002646 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002647 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002648 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002649 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002650 }
2651 }
2652 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002653 final ActivityRecord resumed = stack.mResumedActivity;
2654 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002655 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002656 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002657 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002658 if (r != null && (state == ActivityState.INITIALIZING
2659 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002660 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002661 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002662 }
2663 }
2664 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002665*/
Craig Mautner76ea2242013-05-15 11:40:05 -07002666 }
2667
Craig Mautner27084302013-03-25 08:05:25 -07002668 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002669 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity=");
Craig Mautner27084302013-03-25 08:05:25 -07002670 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002671 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002672 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002673 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2674 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2675 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002676 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07002677 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002678
Craig Mautner20e72272013-04-01 13:45:53 -07002679 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002680 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002681 }
2682
Dianne Hackborn390517b2013-05-30 15:03:32 -07002683 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2684 boolean needSep, String prefix) {
2685 if (activity != null) {
2686 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2687 if (needSep) {
2688 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002689 }
2690 pw.print(prefix);
2691 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002692 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002693 }
2694 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002695 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002696 }
2697
Craig Mautner8d341ef2013-03-26 09:03:27 -07002698 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2699 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002700 boolean printed = false;
2701 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002702 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2703 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
2704 pw.print("Display #"); pw.println(activityDisplay.mDisplayId);
2705 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002706 final int numStacks = stacks.size();
2707 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2708 final ActivityStack stack = stacks.get(stackNdx);
2709 StringBuilder stackHeader = new StringBuilder(128);
2710 stackHeader.append(" Stack #");
2711 stackHeader.append(stack.mStackId);
2712 stackHeader.append(":");
2713 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2714 needSep, stackHeader.toString());
2715 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2716 !dumpAll, false, dumpPackage, true,
2717 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002718
Craig Mautner4a1cb222013-12-04 16:14:06 -08002719 needSep = printed;
2720 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2721 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002722 if (pr) {
2723 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002724 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002725 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002726 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2727 " mResumedActivity: ");
2728 if (pr) {
2729 printed = true;
2730 needSep = false;
2731 }
2732 if (dumpAll) {
2733 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2734 " mLastPausedActivity: ");
2735 if (pr) {
2736 printed = true;
2737 needSep = true;
2738 }
2739 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2740 needSep, " mLastNoHistoryActivity: ");
2741 }
2742 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002743 }
2744 }
2745
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002746 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2747 false, dumpPackage, true, " Activities waiting to finish:", null);
2748 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2749 false, dumpPackage, true, " Activities waiting to stop:", null);
2750 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2751 false, dumpPackage, true, " Activities waiting for another to become visible:",
2752 null);
2753 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2754 false, dumpPackage, true, " Activities waiting to sleep:", null);
2755 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2756 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002757
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002758 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002759 }
2760
Dianne Hackborn390517b2013-05-30 15:03:32 -07002761 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002762 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002763 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002764 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002765 String innerPrefix = null;
2766 String[] args = null;
2767 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002768 for (int i=list.size()-1; i>=0; i--) {
2769 final ActivityRecord r = list.get(i);
2770 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2771 continue;
2772 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002773 if (innerPrefix == null) {
2774 innerPrefix = prefix + " ";
2775 args = new String[0];
2776 }
2777 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002778 final boolean full = !brief && (complete || !r.isInHistory());
2779 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002780 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002781 needNL = false;
2782 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002783 if (header1 != null) {
2784 pw.println(header1);
2785 header1 = null;
2786 }
2787 if (header2 != null) {
2788 pw.println(header2);
2789 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002790 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002791 if (lastTask != r.task) {
2792 lastTask = r.task;
2793 pw.print(prefix);
2794 pw.print(full ? "* " : " ");
2795 pw.println(lastTask);
2796 if (full) {
2797 lastTask.dump(pw, prefix + " ");
2798 } else if (complete) {
2799 // Complete + brief == give a summary. Isn't that obvious?!?
2800 if (lastTask.intent != null) {
2801 pw.print(prefix); pw.print(" ");
2802 pw.println(lastTask.intent.toInsecureStringWithClip());
2803 }
2804 }
2805 }
2806 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2807 pw.print(" #"); pw.print(i); pw.print(": ");
2808 pw.println(r);
2809 if (full) {
2810 r.dump(pw, innerPrefix);
2811 } else if (complete) {
2812 // Complete + brief == give a summary. Isn't that obvious?!?
2813 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2814 if (r.app != null) {
2815 pw.print(innerPrefix); pw.println(r.app);
2816 }
2817 }
2818 if (client && r.app != null && r.app.thread != null) {
2819 // flush anything that is already in the PrintWriter since the thread is going
2820 // to write to the file descriptor directly
2821 pw.flush();
2822 try {
2823 TransferPipe tp = new TransferPipe();
2824 try {
2825 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2826 r.appToken, innerPrefix, args);
2827 // Short timeout, since blocking here can
2828 // deadlock with the application.
2829 tp.go(fd, 2000);
2830 } finally {
2831 tp.kill();
2832 }
2833 } catch (IOException e) {
2834 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2835 } catch (RemoteException e) {
2836 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2837 }
2838 needNL = true;
2839 }
2840 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002841 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002842 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002843
Craig Mautnerf3333272013-04-22 10:55:53 -07002844 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002845 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002846 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2847 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002848 }
2849
2850 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002851 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002852 }
2853
2854 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002855 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002856 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2857 }
2858
Craig Mautner05d29032013-05-03 13:40:13 -07002859 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002860 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2861 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2862 }
Craig Mautner05d29032013-05-03 13:40:13 -07002863 }
2864
Craig Mautner0eea92c2013-05-16 13:35:39 -07002865 void removeSleepTimeouts() {
2866 mSleepTimeout = false;
2867 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2868 }
2869
2870 final void scheduleSleepTimeout() {
2871 removeSleepTimeouts();
2872 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2873 }
2874
Craig Mautner4a1cb222013-12-04 16:14:06 -08002875 @Override
2876 public void onDisplayAdded(int displayId) {
2877 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
2878 }
2879
2880 @Override
2881 public void onDisplayRemoved(int displayId) {
2882 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
2883 }
2884
2885 @Override
2886 public void onDisplayChanged(int displayId) {
2887 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
2888 }
2889
2890 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08002891 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002892 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08002893 newDisplay = mActivityDisplays.get(displayId) == null;
2894 if (newDisplay) {
2895 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
2896 mActivityDisplays.put(displayId, activityDisplay);
2897 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002898 }
Craig Mautner4504de52013-12-20 09:06:56 -08002899 if (newDisplay) {
2900 mWindowManager.onDisplayAdded(displayId);
2901 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002902 }
2903
2904 public void handleDisplayRemovedLocked(int displayId) {
2905 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002906 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2907 if (activityDisplay != null) {
2908 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002909 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002910 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002911 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002912 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002913 }
2914 }
2915 mWindowManager.onDisplayRemoved(displayId);
2916 }
2917
2918 public void handleDisplayChangedLocked(int displayId) {
2919 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002920 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2921 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002922 // TODO: Update the bounds.
2923 }
2924 }
2925 mWindowManager.onDisplayChanged(displayId);
2926 }
2927
2928 StackInfo getStackInfo(ActivityStack stack) {
2929 StackInfo info = new StackInfo();
2930 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
2931 info.displayId = Display.DEFAULT_DISPLAY;
2932 info.stackId = stack.mStackId;
2933
2934 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2935 final int numTasks = tasks.size();
2936 int[] taskIds = new int[numTasks];
2937 String[] taskNames = new String[numTasks];
2938 for (int i = 0; i < numTasks; ++i) {
2939 final TaskRecord task = tasks.get(i);
2940 taskIds[i] = task.taskId;
2941 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
2942 : task.realActivity != null ? task.realActivity.flattenToString()
2943 : task.getTopActivity() != null ? task.getTopActivity().packageName
2944 : "unknown";
2945 }
2946 info.taskIds = taskIds;
2947 info.taskNames = taskNames;
2948 return info;
2949 }
2950
2951 StackInfo getStackInfoLocked(int stackId) {
2952 ActivityStack stack = getStack(stackId);
2953 if (stack != null) {
2954 return getStackInfo(stack);
2955 }
2956 return null;
2957 }
2958
2959 ArrayList<StackInfo> getAllStackInfosLocked() {
2960 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002961 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2962 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002963 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
2964 list.add(getStackInfo(stacks.get(ndx)));
2965 }
2966 }
2967 return list;
2968 }
2969
Craig Mautneraea74a52014-03-08 14:23:10 -08002970 void setLockTaskModeLocked(TaskRecord task) {
justinzhang5286d3f2014-05-12 17:06:01 -04002971 final Message lockTaskMsg = Message.obtain();
Craig Mautneraea74a52014-03-08 14:23:10 -08002972 if (task == null) {
2973 // Take out of lock task mode.
2974 mLockTaskModeTask = null;
justinzhang5286d3f2014-05-12 17:06:01 -04002975 lockTaskMsg.what = LOCK_TASK_END_MSG;
2976 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08002977 return;
2978 }
2979 if (isLockTaskModeViolation(task)) {
2980 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
2981 return;
2982 }
2983 mLockTaskModeTask = task;
2984 findTaskToMoveToFrontLocked(task, 0, null);
2985 resumeTopActivitiesLocked();
justinzhang5286d3f2014-05-12 17:06:01 -04002986 lockTaskMsg.what = LOCK_TASK_START_MSG;
2987 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08002988 }
2989
2990 boolean isLockTaskModeViolation(TaskRecord task) {
2991 return mLockTaskModeTask != null && mLockTaskModeTask != task;
2992 }
2993
2994 void endLockTaskModeIfTaskEnding(TaskRecord task) {
2995 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
2996 mLockTaskModeTask = null;
2997 }
2998 }
2999
3000 boolean isInLockTaskMode() {
3001 return mLockTaskModeTask != null;
3002 }
3003
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003004 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003005
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003006 public ActivityStackSupervisorHandler(Looper looper) {
3007 super(looper);
3008 }
3009
Craig Mautnerf3333272013-04-22 10:55:53 -07003010 void activityIdleInternal(ActivityRecord r) {
3011 synchronized (mService) {
3012 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3013 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003014 }
3015
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003016 @Override
3017 public void handleMessage(Message msg) {
3018 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003019 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003020 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003021 if (mService.mDidDexOpt) {
3022 mService.mDidDexOpt = false;
3023 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3024 nmsg.obj = msg.obj;
3025 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3026 return;
3027 }
3028 // We don't at this point know if the activity is fullscreen,
3029 // so we need to be conservative and assume it isn't.
3030 activityIdleInternal((ActivityRecord)msg.obj);
3031 } break;
3032 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003033 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003034 activityIdleInternal((ActivityRecord)msg.obj);
3035 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003036 case RESUME_TOP_ACTIVITY_MSG: {
3037 synchronized (mService) {
3038 resumeTopActivitiesLocked();
3039 }
3040 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003041 case SLEEP_TIMEOUT_MSG: {
3042 synchronized (mService) {
3043 if (mService.isSleepingOrShuttingDown()) {
3044 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3045 mSleepTimeout = true;
3046 checkReadyForSleepLocked();
3047 }
3048 }
3049 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003050 case LAUNCH_TIMEOUT_MSG: {
3051 if (mService.mDidDexOpt) {
3052 mService.mDidDexOpt = false;
3053 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3054 return;
3055 }
3056 synchronized (mService) {
3057 if (mLaunchingActivity.isHeld()) {
3058 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3059 if (VALIDATE_WAKE_LOCK_CALLER
3060 && Binder.getCallingUid() != Process.myUid()) {
3061 throw new IllegalStateException("Calling must be system uid");
3062 }
3063 mLaunchingActivity.release();
3064 }
3065 }
3066 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003067 case HANDLE_DISPLAY_ADDED: {
3068 handleDisplayAddedLocked(msg.arg1);
3069 } break;
3070 case HANDLE_DISPLAY_CHANGED: {
3071 handleDisplayChangedLocked(msg.arg1);
3072 } break;
3073 case HANDLE_DISPLAY_REMOVED: {
3074 handleDisplayRemovedLocked(msg.arg1);
3075 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003076 case CONTAINER_CALLBACK_VISIBILITY: {
3077 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003078 final IActivityContainerCallback callback = container.mCallback;
3079 if (callback != null) {
3080 try {
3081 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3082 } catch (RemoteException e) {
3083 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003084 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003085 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003086 case LOCK_TASK_START_MSG: {
3087 // When lock task starts, we disable the status bars.
3088 try {
3089 if (getStatusBarService() != null) {
3090 getStatusBarService().disable
3091 (StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK,
3092 mToken, mService.mContext.getPackageName());
3093 }
3094 } catch (RemoteException ex) {
3095 throw new RuntimeException(ex);
3096 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003097 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003098 case LOCK_TASK_END_MSG: {
3099 // When lock task ends, we enable the status bars.
3100 try {
3101 if (getStatusBarService() != null) {
3102 getStatusBarService().disable
3103 (StatusBarManager.DISABLE_NONE,
3104 mToken, mService.mContext.getPackageName());
3105 }
3106 } catch (RemoteException ex) {
3107 throw new RuntimeException(ex);
3108 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003109 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003110 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3111 final ActivityContainer container = (ActivityContainer) msg.obj;
3112 final IActivityContainerCallback callback = container.mCallback;
3113 if (callback != null) {
3114 try {
3115 callback.onAllActivitiesComplete(container.asBinder());
3116 } catch (RemoteException e) {
3117 }
3118 }
3119 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003120 }
3121 }
3122 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003123
Ying Wangb081a592014-04-22 15:20:16 -07003124 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003125 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
3126 Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003127 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003128 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003129 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003130 ActivityRecord mParentActivity = null;
3131 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003132
Craig Mautnere3a00d72014-04-16 08:31:19 -07003133 boolean mVisible = true;
3134
Craig Mautner4a1cb222013-12-04 16:14:06 -08003135 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003136 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003137
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003138 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3139 final static int CONTAINER_STATE_NO_SURFACE = 1;
3140 final static int CONTAINER_STATE_FINISHING = 2;
3141 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3142
3143 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003144 synchronized (mService) {
3145 mStackId = stackId;
3146 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003147 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003148 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003149 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003150 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003151
Craig Mautnere0a38842013-12-16 16:14:02 -08003152 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003153 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3154 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003155 mActivityDisplay = activityDisplay;
3156 mStack.mDisplayId = activityDisplay.mDisplayId;
3157 mStack.mStacks = activityDisplay.mStacks;
3158
3159 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003160 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003161 }
3162
3163 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003164 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003165 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003166 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3167 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003168 return;
3169 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003170 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003171 }
3172 }
3173
3174 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003175 public int getDisplayId() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003176 if (mActivityDisplay != null) {
3177 return mActivityDisplay.mDisplayId;
3178 }
3179 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003180 }
3181
Jeff Brownca9bc702014-02-11 14:32:56 -08003182 @Override
3183 public boolean injectEvent(InputEvent event) {
3184 final long origId = Binder.clearCallingIdentity();
3185 try {
3186 if (mActivityDisplay != null) {
3187 return mInputManagerInternal.injectInputEvent(event,
3188 mActivityDisplay.mDisplayId,
3189 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3190 }
3191 return false;
3192 } finally {
3193 Binder.restoreCallingIdentity(origId);
3194 }
3195 }
3196
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003197 @Override
3198 public void release() {
3199 mContainerState = CONTAINER_STATE_FINISHING;
3200 mStack.finishAllActivitiesLocked();
3201 detachLocked();
3202 mWindowManager.removeStack(mStackId);
3203 }
3204
Craig Mautner34b73df2014-01-12 21:11:08 -08003205 private void detachLocked() {
3206 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3207 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003208 if (mActivityDisplay != null) {
3209 mActivityDisplay.detachActivitiesLocked(mStack);
3210 mActivityDisplay = null;
3211 mStack.mDisplayId = -1;
3212 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003213 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003214 }
3215 }
3216
3217 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003218 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003219 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003220 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3221 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3222 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003223 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003224 String mimeType = intent.getType();
3225 if (mimeType == null && intent.getData() != null
3226 && "content".equals(intent.getData().getScheme())) {
3227 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3228 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003229 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0, 0, null,
Craig Mautnere0a38842013-12-16 16:14:02 -08003230 null, null, null, null, userId, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003231 }
3232
3233 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003234 public final int startActivityIntentSender(IIntentSender intentSender) {
3235 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3236
3237 if (!(intentSender instanceof PendingIntentRecord)) {
3238 throw new IllegalArgumentException("Bad PendingIntent object");
3239 }
3240
3241 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003242 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003243 }
3244
Craig Mautner247ab652014-04-25 10:09:00 -07003245 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3246 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3247 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3248 if (resolvedType == null) {
3249 resolvedType = intent.getType();
3250 if (resolvedType == null && intent.getData() != null
3251 && "content".equals(intent.getData().getScheme())) {
3252 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3253 }
3254 }
3255 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003256 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003257 throw new SecurityException(
3258 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3259 }
3260 }
3261
3262 /** Throw a SecurityException if allowEmbedded is not true */
3263 @Override
3264 public final void checkEmbeddedAllowed(Intent intent) {
3265 checkEmbeddedAllowedInner(intent, null);
3266 }
3267
3268 /** Throw a SecurityException if allowEmbedded is not true */
3269 @Override
3270 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3271 if (!(intentSender instanceof PendingIntentRecord)) {
3272 throw new IllegalArgumentException("Bad PendingIntent object");
3273 }
3274 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3275 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3276 pendingIntent.key.requestResolvedType);
3277 }
3278
Craig Mautnerdf88d732014-01-27 09:21:32 -08003279 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003280 public IBinder asBinder() {
3281 return this;
3282 }
3283
Craig Mautner4504de52013-12-20 09:06:56 -08003284 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003285 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003286 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003287 }
3288
Craig Mautner4a1cb222013-12-04 16:14:06 -08003289 ActivityStackSupervisor getOuter() {
3290 return ActivityStackSupervisor.this;
3291 }
3292
3293 boolean isAttached() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003294 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003295 }
3296
3297 void getBounds(Point outBounds) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003298 if (mActivityDisplay != null) {
3299 mActivityDisplay.getBounds(outBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003300 } else {
3301 outBounds.set(0, 0);
3302 }
3303 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003304
Craig Mautner6985bad2014-04-21 15:22:06 -07003305 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003306 void setVisible(boolean visible) {
3307 if (mVisible != visible) {
3308 mVisible = visible;
3309 if (mCallback != null) {
3310 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3311 0 /* unused */, this).sendToTarget();
3312 }
3313 }
3314 }
3315
Craig Mautner6985bad2014-04-21 15:22:06 -07003316 void setDrawn() {
3317 }
3318
Craig Mautner1b4bf852014-05-26 15:06:32 -07003319 // You can always start a new task on a regular ActivityStack.
3320 boolean isEligibleForNewTasks() {
3321 return true;
3322 }
3323
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003324 void onTaskListEmpty() {
3325 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3326 }
3327
Craig Mautner34b73df2014-01-12 21:11:08 -08003328 @Override
3329 public String toString() {
3330 return mIdString + (mActivityDisplay == null ? "N" : "A");
3331 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003332 }
3333
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003334 private class VirtualActivityContainer extends ActivityContainer {
3335 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003336 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003337
3338 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3339 super(getNextStackId());
3340 mParentActivity = parent;
3341 mCallback = callback;
3342 mContainerState = CONTAINER_STATE_NO_SURFACE;
3343 mIdString = "VirtualActivtyContainer{" + mStackId + ", parent=" + mParentActivity + "}";
3344 }
3345
3346 @Override
3347 public void setSurface(Surface surface, int width, int height, int density) {
3348 super.setSurface(surface, width, height, density);
3349
3350 synchronized (mService) {
3351 final long origId = Binder.clearCallingIdentity();
3352 try {
3353 setSurfaceLocked(surface, width, height, density);
3354 } finally {
3355 Binder.restoreCallingIdentity(origId);
3356 }
3357 }
3358 }
3359
3360 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3361 if (mContainerState == CONTAINER_STATE_FINISHING) {
3362 return;
3363 }
3364 VirtualActivityDisplay virtualActivityDisplay =
3365 (VirtualActivityDisplay) mActivityDisplay;
3366 if (virtualActivityDisplay == null) {
3367 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003368 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003369 mActivityDisplay = virtualActivityDisplay;
3370 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3371 attachToDisplayLocked(virtualActivityDisplay);
3372 }
3373
3374 if (mSurface != null) {
3375 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003376 }
3377
Craig Mautner6985bad2014-04-21 15:22:06 -07003378 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003379 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003380 mStack.resumeTopActivityLocked(null);
3381 } else {
3382 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003383 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003384 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
3385 mStack.startPausingLocked(false, true);
3386 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003387 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003388
3389 setSurfaceIfReady();
3390
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003391 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3392 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003393 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003394
Craig Mautner6985bad2014-04-21 15:22:06 -07003395 @Override
Craig Mautnerd13a5582014-05-05 12:07:40 -07003396 boolean isAttached() {
3397 return mSurface != null && super.isAttached();
3398 }
3399
3400 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003401 void setDrawn() {
3402 synchronized (mService) {
3403 mDrawn = true;
3404 setSurfaceIfReady();
3405 }
3406 }
3407
Craig Mautner1b4bf852014-05-26 15:06:32 -07003408 // Never start a new task on an ActivityView if it isn't explicitly specified.
3409 @Override
3410 boolean isEligibleForNewTasks() {
3411 return false;
3412 }
3413
Craig Mautner6985bad2014-04-21 15:22:06 -07003414 private void setSurfaceIfReady() {
3415 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReady: mDrawn=" + mDrawn +
3416 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3417 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3418 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3419 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3420 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003421 }
3422 }
3423
Craig Mautner4a1cb222013-12-04 16:14:06 -08003424 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3425 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003426 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003427 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003428 int mDisplayId;
3429 Display mDisplay;
3430 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003431
Craig Mautner4a1cb222013-12-04 16:14:06 -08003432 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3433 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003434 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003435
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003436 ActivityDisplay() {
3437 }
Craig Mautner4504de52013-12-20 09:06:56 -08003438
Craig Mautnere0a38842013-12-16 16:14:02 -08003439 ActivityDisplay(int displayId) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003440 init(mDisplayManager.getDisplay(displayId));
Craig Mautner4504de52013-12-20 09:06:56 -08003441 }
3442
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003443 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003444 mDisplay = display;
3445 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003446 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003447 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003448
3449 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003450 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3451 + mDisplayId);
3452 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003453 }
3454
Craig Mautnere0a38842013-12-16 16:14:02 -08003455 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003456 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003457 + " from displayId=" + mDisplayId);
3458 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003459 }
3460
3461 void getBounds(Point bounds) {
3462 mDisplay.getDisplayInfo(mDisplayInfo);
3463 bounds.x = mDisplayInfo.appWidth;
3464 bounds.y = mDisplayInfo.appHeight;
3465 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003466
3467 @Override
3468 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003469 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3470 }
3471 }
3472
3473 class VirtualActivityDisplay extends ActivityDisplay {
3474 VirtualDisplay mVirtualDisplay;
3475
Craig Mautner6985bad2014-04-21 15:22:06 -07003476 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003477 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
3478 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, VIRTUAL_DISPLAY_BASE_NAME,
Craig Mautner6985bad2014-04-21 15:22:06 -07003479 width, height, density, null, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003480 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY);
3481
3482 init(mVirtualDisplay.getDisplay());
3483
3484 mWindowManager.handleDisplayAdded(mDisplayId);
3485 }
3486
3487 void setSurface(Surface surface) {
3488 if (mVirtualDisplay != null) {
3489 mVirtualDisplay.setSurface(surface);
3490 }
3491 }
3492
3493 @Override
3494 void detachActivitiesLocked(ActivityStack stack) {
3495 super.detachActivitiesLocked(stack);
3496 if (mVirtualDisplay != null) {
3497 mVirtualDisplay.release();
3498 mVirtualDisplay = null;
3499 }
3500 }
3501
3502 @Override
3503 public String toString() {
3504 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003505 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003506 }
Jose Lima58e66d62014-05-27 20:00:27 -07003507
3508 private boolean isLeanbackOnlyDevice() {
3509 boolean onLeanbackOnly = false;
3510 try {
3511 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
3512 PackageManager.FEATURE_LEANBACK_ONLY);
3513 } catch (RemoteException e) {
3514 // noop
3515 }
3516
3517 return onLeanbackOnly;
3518 }
Craig Mautner27084302013-03-25 08:05:25 -07003519}