blob: 35f8f31d23b03a9849dc4e8a74c2df1f86070c80 [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;
Craig Mautner23ac33b2013-04-01 16:26:35 -070048import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070049import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070051import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070053import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.content.pm.ApplicationInfo;
55import android.content.pm.PackageManager;
56import android.content.pm.ResolveInfo;
57import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080058import android.graphics.Point;
59import android.hardware.display.DisplayManager;
60import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080061import android.hardware.display.DisplayManagerGlobal;
62import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080063import android.hardware.input.InputManager;
64import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070066import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070067import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070068import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070069import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070070import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070071import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070072import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070073import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070074import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070075import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070077import android.os.UserHandle;
Dianne Hackborn91097de2014-04-04 18:02:06 -070078import android.service.voice.IVoiceInteractionSession;
Craig Mautner2420ead2013-04-01 17:13:20 -070079import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070080import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080081import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082
Craig Mautner4a1cb222013-12-04 16:14:06 -080083import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080084import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080085import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080086import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080087import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070088import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070089import com.android.internal.app.IVoiceInteractor;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070090import com.android.internal.os.TransferPipe;
Jeff Brownca9bc702014-02-11 14:32:56 -080091import com.android.server.LocalServices;
Craig Mautner6170f732013-04-02 13:05:23 -070092import com.android.server.am.ActivityManagerService.PendingActivityLaunch;
Craig Mautner2420ead2013-04-01 17:13:20 -070093import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070094import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070095
Craig Mautner8d341ef2013-03-26 09:03:27 -070096import java.io.FileDescriptor;
97import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070098import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -070099import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700101
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700103 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
104 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
105 static final boolean DEBUG_APP = DEBUG || false;
106 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
Craig Mautnera7f2bd42013-10-15 16:13:50 -0700107 static final boolean DEBUG_STATES = DEBUG || false;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700108 static final boolean DEBUG_IDLE = DEBUG || false;
Winson Chung376543b2014-05-21 17:43:28 -0700109 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700110
Craig Mautner2219a1b2013-03-25 09:44:30 -0700111 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700112
Craig Mautnerf3333272013-04-22 10:55:53 -0700113 /** How long we wait until giving up on the last activity telling us it is idle. */
114 static final int IDLE_TIMEOUT = 10*1000;
115
Craig Mautner0eea92c2013-05-16 13:35:39 -0700116 /** How long we can hold the sleep wake lock before giving up. */
117 static final int SLEEP_TIMEOUT = 5*1000;
118
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700119 // How long we can hold the launch wake lock before giving up.
120 static final int LAUNCH_TIMEOUT = 10*1000;
121
Craig Mautner05d29032013-05-03 13:40:13 -0700122 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
123 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
124 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700125 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700126 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800127 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
128 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
129 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700130 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800131
Craig Mautner4504de52013-12-20 09:06:56 -0800132 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700133
134 // For debugging to make sure the caller when acquiring/releasing our
135 // wake lock is the system process.
136 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700137
Craig Mautner27084302013-03-25 08:05:25 -0700138 final ActivityManagerService mService;
139
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700140 final ActivityStackSupervisorHandler mHandler;
141
142 /** Short cut */
143 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800144 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700145
Craig Mautner27084302013-03-25 08:05:25 -0700146 /** Dismiss the keyguard after the next activity is displayed? */
Craig Mautner5314a402013-09-26 12:40:16 -0700147 boolean mDismissKeyguardOnNextActivity = false;
Craig Mautner27084302013-03-25 08:05:25 -0700148
Craig Mautner8d341ef2013-03-26 09:03:27 -0700149 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700150 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700151
152 /** Task identifier that activities are currently being started in. Incremented each time a
153 * new task is created. */
154 private int mCurTaskId = 0;
155
Craig Mautner2420ead2013-04-01 17:13:20 -0700156 /** The current user */
157 private int mCurrentUser;
158
Craig Mautnere0a38842013-12-16 16:14:02 -0800159 /** The stack containing the launcher app. Assumed to always be attached to
160 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700161 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700162
Craig Mautnere0a38842013-12-16 16:14:02 -0800163 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700164 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700165
Craig Mautner4a1cb222013-12-04 16:14:06 -0800166 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
167 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800168 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700170
171 /** List of activities that are waiting for a new activity to become visible before completing
172 * whatever operation they are supposed to do. */
173 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
174
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700175 /** List of processes waiting to find out about the next visible activity. */
176 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
177 new ArrayList<IActivityManager.WaitResult>();
178
179 /** List of processes waiting to find out about the next launched activity. */
180 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
181 new ArrayList<IActivityManager.WaitResult>();
182
Craig Mautnerde4ef022013-04-07 19:01:33 -0700183 /** List of activities that are ready to be stopped, but waiting for the next activity to
184 * settle down before doing so. */
185 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
186
Craig Mautnerf3333272013-04-22 10:55:53 -0700187 /** List of activities that are ready to be finished, but waiting for the previous activity to
188 * settle down before doing so. It contains ActivityRecord objects. */
189 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
190
Craig Mautner0eea92c2013-05-16 13:35:39 -0700191 /** List of activities that are in the process of going to sleep. */
192 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
193
Craig Mautnerf3333272013-04-22 10:55:53 -0700194 /** Used on user changes */
195 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
196
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700197 /** Used to queue up any background users being started */
198 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
199
Craig Mautnerde4ef022013-04-07 19:01:33 -0700200 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
201 * is being brought in front of us. */
202 boolean mUserLeaving = false;
203
Craig Mautner0eea92c2013-05-16 13:35:39 -0700204 /** Set when we have taken too long waiting to go to sleep. */
205 boolean mSleepTimeout = false;
206
Jose Lima58e66d62014-05-27 20:00:27 -0700207 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
208 * setWindowManager is called. **/
209 private boolean mLeanbackOnlyDevice;
210
Craig Mautner0eea92c2013-05-16 13:35:39 -0700211 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212 * We don't want to allow the device to go to sleep while in the process
213 * of launching an activity. This is primarily to allow alarm intent
214 * receivers to launch an activity and get that to run before the device
215 * goes back to sleep.
216 */
217 final PowerManager.WakeLock mLaunchingActivity;
218
219 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700220 * Set when the system is going to sleep, until we have
221 * successfully paused the current activity and released our wake lock.
222 * At that point the system is allowed to actually sleep.
223 */
224 final PowerManager.WakeLock mGoingToSleep;
225
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700226 /** Stack id of the front stack when user switched, indexed by userId. */
227 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700228
Craig Mautner4504de52013-12-20 09:06:56 -0800229 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800230 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700231 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800232
233 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700234 private final SparseArray<ActivityDisplay> mActivityDisplays =
235 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800236
Jeff Brownca9bc702014-02-11 14:32:56 -0800237 InputManagerInternal mInputManagerInternal;
238
Craig Mautneraea74a52014-03-08 14:23:10 -0800239 /** If non-null then the task specified remains in front and no other tasks may be started
240 * until the task exits or #stopLockTaskMode() is called. */
241 private TaskRecord mLockTaskModeTask;
242
Craig Mautner4a1cb222013-12-04 16:14:06 -0800243 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700244 mService = service;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800245 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700246 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800247 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700248 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
249 throw new IllegalStateException("Calling must be system uid");
250 }
251 mLaunchingActivity =
252 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
253 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700254 }
255
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700256 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800257 synchronized (mService) {
258 mWindowManager = wm;
259
260 mDisplayManager =
261 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
262 mDisplayManager.registerDisplayListener(this, null);
263
264 Display[] displays = mDisplayManager.getDisplays();
265 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
266 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800267 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
268 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800269 }
270
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700271 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800272 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800273
274 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700275
276 // Initialize this here, now that we can get a valid reference to PackageManager.
277 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800278 }
Craig Mautner27084302013-03-25 08:05:25 -0700279 }
280
281 void dismissKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700282 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner27084302013-03-25 08:05:25 -0700283 if (mDismissKeyguardOnNextActivity) {
284 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700285 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700286 }
287 }
288
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700289 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800290 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700291 }
292
Craig Mautnerde4ef022013-04-07 19:01:33 -0700293 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800294 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700295 }
296
Craig Mautner4a1cb222013-12-04 16:14:06 -0800297 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
298 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700299 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800300 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
301 if (parent != null) {
302 stack = parent.task.stack;
303 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800304 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800305 if (stacks != null && !stacks.isEmpty()) {
306 return stack == stacks.get(stacks.size() - 1);
307 }
308 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700309 }
310
Craig Mautnerde4ef022013-04-07 19:01:33 -0700311 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800313 int topNdx = stacks.size() - 1;
314 if (topNdx <= 0) {
315 return;
316 }
317 ActivityStack topStack = stacks.get(topNdx);
318 final boolean homeInFront = topStack == mHomeStack;
319 if (homeInFront != toFront) {
320 mLastFocusedStack = topStack;
321 stacks.remove(mHomeStack);
322 stacks.add(toFront ? topNdx : 0, mHomeStack);
323 mFocusedStack = stacks.get(topNdx);
324 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
325 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700326 }
327 }
328
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +0000329 void moveHomeToTop() {
Craig Mautner69ada552013-04-18 13:51:51 -0700330 moveHomeStack(true);
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +0000331 mHomeStack.moveHomeTaskToTop();
Craig Mautner8e569572013-10-11 17:36:59 -0700332 }
333
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +0000334 boolean resumeHomeActivity(ActivityRecord prev) {
335 moveHomeToTop();
Craig Mautnere67a7842014-05-20 19:17:54 -0700336 if (prev != null) {
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +0000337 prev.task.mOnTopOfHome = false;
Craig Mautnere67a7842014-05-20 19:17:54 -0700338 }
Craig Mautnera8a90e02013-06-28 15:24:50 -0700339 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +0000340 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700341 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700342 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700343 }
344 return mService.startHomeActivityLocked(mCurrentUser);
345 }
346
Craig Mautner27084302013-03-25 08:05:25 -0700347 void setDismissKeyguard(boolean dismiss) {
Craig Mautner5314a402013-09-26 12:40:16 -0700348 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen(" dismiss=" + dismiss);
Craig Mautner27084302013-03-25 08:05:25 -0700349 mDismissKeyguardOnNextActivity = dismiss;
350 }
351
Craig Mautner8d341ef2013-03-26 09:03:27 -0700352 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800353 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800354 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800355 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
357 ActivityStack stack = stacks.get(stackNdx);
358 TaskRecord task = stack.taskForIdLocked(id);
359 if (task != null) {
360 return task;
361 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700362 }
363 }
364 return null;
365 }
366
Craig Mautner6170f732013-04-02 13:05:23 -0700367 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800368 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800369 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800370 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800371 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
372 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
373 if (r != null) {
374 return r;
375 }
Craig Mautner6170f732013-04-02 13:05:23 -0700376 }
377 }
378 return null;
379 }
380
Craig Mautner21d24a22014-04-23 11:45:37 -0700381 void setNextTaskId(int taskId) {
382 if (taskId > mCurTaskId) {
383 mCurTaskId = taskId;
384 }
385 }
386
Craig Mautner8d341ef2013-03-26 09:03:27 -0700387 int getNextTaskId() {
388 do {
389 mCurTaskId++;
390 if (mCurTaskId <= 0) {
391 mCurTaskId = 1;
392 }
393 } while (anyTaskForIdLocked(mCurTaskId) != null);
394 return mCurTaskId;
395 }
396
Craig Mautnerde4ef022013-04-07 19:01:33 -0700397 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700398 ActivityStack stack = getFocusedStack();
399 if (stack == null) {
400 return null;
401 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700402 ActivityRecord resumedActivity = stack.mResumedActivity;
403 if (resumedActivity == null || resumedActivity.app == null) {
404 resumedActivity = stack.mPausingActivity;
405 if (resumedActivity == null || resumedActivity.app == null) {
406 resumedActivity = stack.topRunningActivityLocked(null);
407 }
408 }
409 return resumedActivity;
410 }
411
Mike Lockwooded8902d2013-11-15 11:01:47 -0800412 boolean attachApplicationLocked(ProcessRecord app) throws Exception {
Craig Mautner20e72272013-04-01 13:45:53 -0700413 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800414 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800415 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
416 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800417 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
418 final ActivityStack stack = stacks.get(stackNdx);
419 if (!isFrontStack(stack)) {
420 continue;
421 }
422 ActivityRecord hr = stack.topRunningActivityLocked(null);
423 if (hr != null) {
424 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
425 && processName.equals(hr.processName)) {
426 try {
George Mount2c92c972014-03-20 09:38:23 -0700427 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800428 didSomething = true;
429 }
430 } catch (Exception e) {
431 Slog.w(TAG, "Exception in new application when starting activity "
432 + hr.intent.getComponent().flattenToShortString(), e);
433 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700434 }
Craig Mautner20e72272013-04-01 13:45:53 -0700435 }
Craig Mautner20e72272013-04-01 13:45:53 -0700436 }
437 }
438 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700439 if (!didSomething) {
440 ensureActivitiesVisibleLocked(null, 0);
441 }
Craig Mautner20e72272013-04-01 13:45:53 -0700442 return didSomething;
443 }
444
445 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800446 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
447 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800448 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
449 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800450 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800451 continue;
452 }
453 final ActivityRecord resumedActivity = stack.mResumedActivity;
454 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800455 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
456 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800457 return false;
458 }
Craig Mautner20e72272013-04-01 13:45:53 -0700459 }
460 }
461 return true;
462 }
463
Craig Mautnerde4ef022013-04-07 19:01:33 -0700464 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800465 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
466 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800467 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
468 final ActivityStack stack = stacks.get(stackNdx);
469 if (isFrontStack(stack)) {
470 final ActivityRecord r = stack.mResumedActivity;
471 if (r != null && r.state != ActivityState.RESUMED) {
472 return false;
473 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700474 }
475 }
476 }
477 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800478 if (DEBUG_STACK) Slog.d(TAG,
479 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
480 mLastFocusedStack + " to=" + mFocusedStack);
481 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700482 return true;
483 }
484
485 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800486 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
487 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800488 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
489 final ActivityStack stack = stacks.get(stackNdx);
490 final ActivityRecord r = stack.mResumedActivity;
491 if (r != null && (!r.nowVisible || r.waitingVisible)) {
492 return false;
493 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700494 }
495 }
496 return true;
497 }
498
Craig Mautner2acc3892013-09-23 10:28:14 -0700499 /**
500 * Pause all activities in either all of the stacks or just the back stacks.
501 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700502 * @return true if any activity was paused as a result of this call.
503 */
Craig Mautner5314a402013-09-26 12:40:16 -0700504 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700505 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800506 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
507 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
509 final ActivityStack stack = stacks.get(stackNdx);
510 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
511 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
512 " mResumedActivity=" + stack.mResumedActivity);
513 stack.startPausingLocked(userLeaving, false);
514 someActivityPaused = true;
515 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700516 }
517 }
518 return someActivityPaused;
519 }
520
Craig Mautnerde4ef022013-04-07 19:01:33 -0700521 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700522 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800523 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
524 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800525 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
526 final ActivityStack stack = stacks.get(stackNdx);
527 final ActivityRecord r = stack.mPausingActivity;
528 if (r != null && r.state != ActivityState.PAUSED
529 && r.state != ActivityState.STOPPED
530 && r.state != ActivityState.STOPPING) {
531 if (DEBUG_STATES) {
532 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
533 pausing = false;
534 } else {
535 return false;
536 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700537 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700538 }
539 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700540 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700541 }
542
Craig Mautnerdf88d732014-01-27 09:21:32 -0800543 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping) {
John Spurlock8a985d22014-02-25 09:40:05 -0500544 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800545 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
546 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
547 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
548 final ActivityStack stack = stacks.get(stackNdx);
549 if (stack.mResumedActivity != null &&
550 stack.mActivityContainer.mParentActivity == parent) {
551 stack.startPausingLocked(userLeaving, uiSleeping);
552 }
553 }
554 }
555 }
556
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700557 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700558 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700559 WaitResult w = mWaitingActivityVisible.get(i);
560 w.timeout = false;
561 if (r != null) {
562 w.who = new ComponentName(r.info.packageName, r.info.name);
563 }
564 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
565 w.thisTime = w.totalTime;
566 }
567 mService.notifyAll();
568 dismissKeyguard();
569 }
570
571 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
572 long thisTime, long totalTime) {
573 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700574 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700575 w.timeout = timeout;
576 if (r != null) {
577 w.who = new ComponentName(r.info.packageName, r.info.name);
578 }
579 w.thisTime = thisTime;
580 w.totalTime = totalTime;
581 }
582 mService.notifyAll();
583 }
584
Craig Mautner29219d92013-04-16 20:19:12 -0700585 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700586 final ActivityStack focusedStack = getFocusedStack();
587 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
588 if (r != null) {
589 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700590 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700591
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800593 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
595 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700596 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700597 r = stack.topRunningActivityLocked(null);
598 if (r != null) {
599 return r;
600 }
601 }
602 }
603 return null;
604 }
605
Dianne Hackborn09233282014-04-30 11:33:59 -0700606 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700607 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800608 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
609 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800610 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800611 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800612 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
614 final ActivityStack stack = stacks.get(stackNdx);
615 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
616 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700617 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700618 }
619 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700620
621 // The lists are already sorted from most recent to oldest. Just pull the most recent off
622 // each list and add it to list. Stop when all lists are empty or maxNum reached.
623 while (maxNum > 0) {
624 long mostRecentActiveTime = Long.MIN_VALUE;
625 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 final int numTaskLists = runningTaskLists.size();
627 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
628 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700629 if (!stackTaskList.isEmpty()) {
630 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
631 if (lastActiveTime > mostRecentActiveTime) {
632 mostRecentActiveTime = lastActiveTime;
633 selectedStackList = stackTaskList;
634 }
635 }
636 }
637 if (selectedStackList != null) {
638 list.add(selectedStackList.remove(0));
639 --maxNum;
640 } else {
641 break;
642 }
643 }
Craig Mautner20e72272013-04-01 13:45:53 -0700644 }
645
Craig Mautner23ac33b2013-04-01 16:26:35 -0700646 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
647 String profileFile, ParcelFileDescriptor profileFd, int userId) {
648 // Collect information about the target of the Intent.
649 ActivityInfo aInfo;
650 try {
651 ResolveInfo rInfo =
652 AppGlobals.getPackageManager().resolveIntent(
653 intent, resolvedType,
654 PackageManager.MATCH_DEFAULT_ONLY
655 | ActivityManagerService.STOCK_PM_FLAGS, userId);
656 aInfo = rInfo != null ? rInfo.activityInfo : null;
657 } catch (RemoteException e) {
658 aInfo = null;
659 }
660
661 if (aInfo != null) {
662 // Store the found target back into the intent, because now that
663 // we have it we never want to do this again. For example, if the
664 // user navigates back to this point in the history, we should
665 // always restart the exact same activity.
666 intent.setComponent(new ComponentName(
667 aInfo.applicationInfo.packageName, aInfo.name));
668
669 // Don't debug things in the system process
670 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
671 if (!aInfo.processName.equals("system")) {
672 mService.setDebugApp(aInfo.processName, true, false);
673 }
674 }
675
676 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
677 if (!aInfo.processName.equals("system")) {
678 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
679 }
680 }
681
682 if (profileFile != null) {
683 if (!aInfo.processName.equals("system")) {
684 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
685 profileFile, profileFd,
686 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
687 }
688 }
689 }
690 return aInfo;
691 }
692
Craig Mautner2219a1b2013-03-25 09:44:30 -0700693 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +0000694 moveHomeToTop();
Dianne Hackborn91097de2014-04-04 18:02:06 -0700695 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null, 0,
Craig Mautnere0a38842013-12-16 16:14:02 -0800696 null, false, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700697 }
698
Craig Mautner23ac33b2013-04-01 16:26:35 -0700699 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700700 String callingPackage, Intent intent, String resolvedType,
701 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
702 IBinder resultTo, String resultWho, int requestCode, int startFlags, String profileFile,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700703 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
Craig Mautnere0a38842013-12-16 16:14:02 -0800704 Bundle options, int userId, IActivityContainer iContainer) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700705 // Refuse possible leaked file descriptors
706 if (intent != null && intent.hasFileDescriptors()) {
707 throw new IllegalArgumentException("File descriptors passed in Intent");
708 }
709 boolean componentSpecified = intent.getComponent() != null;
710
711 // Don't modify the client's object!
712 intent = new Intent(intent);
713
714 // Collect information about the target of the Intent.
715 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
716 profileFile, profileFd, userId);
717
Craig Mautnere0a38842013-12-16 16:14:02 -0800718 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700719 synchronized (mService) {
720 int callingPid;
721 if (callingUid >= 0) {
722 callingPid = -1;
723 } else if (caller == null) {
724 callingPid = Binder.getCallingPid();
725 callingUid = Binder.getCallingUid();
726 } else {
727 callingPid = callingUid = -1;
728 }
729
Craig Mautnere0a38842013-12-16 16:14:02 -0800730 final ActivityStack stack;
731 if (container == null || container.mStack.isOnHomeDisplay()) {
732 stack = getFocusedStack();
733 } else {
734 stack = container.mStack;
735 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700736 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700737 && mService.mConfiguration.diff(config) != 0;
738 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700739 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700740
741 final long origId = Binder.clearCallingIdentity();
742
743 if (aInfo != null &&
744 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
745 // This may be a heavy-weight process! Check to see if we already
746 // have another, different heavy-weight process running.
747 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
748 if (mService.mHeavyWeightProcess != null &&
749 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
750 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700751 int realCallingUid = callingUid;
752 if (caller != null) {
753 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
754 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700755 realCallingUid = callerApp.info.uid;
756 } else {
757 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700758 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700759 + intent.toString());
760 ActivityOptions.abort(options);
761 return ActivityManager.START_PERMISSION_DENIED;
762 }
763 }
764
765 IIntentSender target = mService.getIntentSenderLocked(
766 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
767 realCallingUid, userId, null, null, 0, new Intent[] { intent },
768 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
769 | PendingIntent.FLAG_ONE_SHOT, null);
770
771 Intent newIntent = new Intent();
772 if (requestCode >= 0) {
773 // Caller is requesting a result.
774 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
775 }
776 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
777 new IntentSender(target));
778 if (mService.mHeavyWeightProcess.activities.size() > 0) {
779 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
780 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
781 hist.packageName);
782 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
783 hist.task.taskId);
784 }
785 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
786 aInfo.packageName);
787 newIntent.setFlags(intent.getFlags());
788 newIntent.setClassName("android",
789 HeavyWeightSwitcherActivity.class.getName());
790 intent = newIntent;
791 resolvedType = null;
792 caller = null;
793 callingUid = Binder.getCallingUid();
794 callingPid = Binder.getCallingPid();
795 componentSpecified = true;
796 try {
797 ResolveInfo rInfo =
798 AppGlobals.getPackageManager().resolveIntent(
799 intent, null,
800 PackageManager.MATCH_DEFAULT_ONLY
801 | ActivityManagerService.STOCK_PM_FLAGS, userId);
802 aInfo = rInfo != null ? rInfo.activityInfo : null;
803 aInfo = mService.getActivityInfoForUser(aInfo, userId);
804 } catch (RemoteException e) {
805 aInfo = null;
806 }
807 }
808 }
809 }
810
Dianne Hackborn91097de2014-04-04 18:02:06 -0700811 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
812 voiceSession, voiceInteractor, resultTo, resultWho,
Craig Mautnere0a38842013-12-16 16:14:02 -0800813 requestCode, callingPid, callingUid, callingPackage, startFlags, options,
814 componentSpecified, null, container);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700815
Craig Mautnerde4ef022013-04-07 19:01:33 -0700816 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700817 // If the caller also wants to switch to a new configuration,
818 // do so now. This allows a clean switch, as we are waiting
819 // for the current activity to pause (so we will not destroy
820 // it), and have not yet started the next activity.
821 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
822 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700823 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700824 if (DEBUG_CONFIGURATION) Slog.v(TAG,
825 "Updating to new configuration after starting activity.");
826 mService.updateConfigurationLocked(config, null, false, false);
827 }
828
829 Binder.restoreCallingIdentity(origId);
830
831 if (outResult != null) {
832 outResult.result = res;
833 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700834 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700835 do {
836 try {
837 mService.wait();
838 } catch (InterruptedException e) {
839 }
840 } while (!outResult.timeout && outResult.who == null);
841 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700842 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700843 if (r.nowVisible) {
844 outResult.timeout = false;
845 outResult.who = new ComponentName(r.info.packageName, r.info.name);
846 outResult.totalTime = 0;
847 outResult.thisTime = 0;
848 } else {
849 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700850 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700851 do {
852 try {
853 mService.wait();
854 } catch (InterruptedException e) {
855 }
856 } while (!outResult.timeout && outResult.who == null);
857 }
858 }
859 }
860
861 return res;
862 }
863 }
864
865 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
866 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
867 Bundle options, int userId) {
868 if (intents == null) {
869 throw new NullPointerException("intents is null");
870 }
871 if (resolvedTypes == null) {
872 throw new NullPointerException("resolvedTypes is null");
873 }
874 if (intents.length != resolvedTypes.length) {
875 throw new IllegalArgumentException("intents are length different than resolvedTypes");
876 }
877
Craig Mautner23ac33b2013-04-01 16:26:35 -0700878
879 int callingPid;
880 if (callingUid >= 0) {
881 callingPid = -1;
882 } else if (caller == null) {
883 callingPid = Binder.getCallingPid();
884 callingUid = Binder.getCallingUid();
885 } else {
886 callingPid = callingUid = -1;
887 }
888 final long origId = Binder.clearCallingIdentity();
889 try {
890 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700891 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700892 for (int i=0; i<intents.length; i++) {
893 Intent intent = intents[i];
894 if (intent == null) {
895 continue;
896 }
897
898 // Refuse possible leaked file descriptors
899 if (intent != null && intent.hasFileDescriptors()) {
900 throw new IllegalArgumentException("File descriptors passed in Intent");
901 }
902
903 boolean componentSpecified = intent.getComponent() != null;
904
905 // Don't modify the client's object!
906 intent = new Intent(intent);
907
908 // Collect information about the target of the Intent.
909 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
910 0, null, null, userId);
911 // TODO: New, check if this is correct
912 aInfo = mService.getActivityInfoForUser(aInfo, userId);
913
914 if (aInfo != null &&
915 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
916 != 0) {
917 throw new IllegalArgumentException(
918 "FLAG_CANT_SAVE_STATE not supported here");
919 }
920
921 Bundle theseOptions;
922 if (options != null && i == intents.length-1) {
923 theseOptions = options;
924 } else {
925 theseOptions = null;
926 }
Craig Mautner6170f732013-04-02 13:05:23 -0700927 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn91097de2014-04-04 18:02:06 -0700928 aInfo, null, null, resultTo, null, -1, callingPid, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -0800929 0, theseOptions, componentSpecified, outActivity, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700930 if (res < 0) {
931 return res;
932 }
933
934 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
935 }
936 }
937 } finally {
938 Binder.restoreCallingIdentity(origId);
939 }
940
941 return ActivityManager.START_SUCCESS;
942 }
943
Craig Mautner2420ead2013-04-01 17:13:20 -0700944 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -0700945 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -0700946 throws RemoteException {
947
948 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -0700949 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700950 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700951
952 // schedule launch ticks to collect information about slow apps.
953 r.startLaunchTickingLocked();
954
955 // Have the window manager re-evaluate the orientation of
956 // the screen based on the new activity order. Note that
957 // as a result of this, it can call back into the activity
958 // manager with a new orientation. We don't care about that,
959 // because the activity is not currently running so we are
960 // just restarting it anyway.
961 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700962 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -0700963 mService.mConfiguration,
964 r.mayFreezeScreenLocked(app) ? r.appToken : null);
965 mService.updateConfigurationLocked(config, r, false, false);
966 }
967
968 r.app = app;
969 app.waitingToKill = null;
970 r.launchCount++;
971 r.lastLaunchTime = SystemClock.uptimeMillis();
972
973 if (localLOGV) Slog.v(TAG, "Launching: " + r);
974
975 int idx = app.activities.indexOf(r);
976 if (idx < 0) {
977 app.activities.add(r);
978 }
Dianne Hackborndb926082013-10-31 16:32:44 -0700979 mService.updateLruProcessLocked(app, true, null);
980 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700981
982 final ActivityStack stack = r.task.stack;
983 try {
984 if (app.thread == null) {
985 throw new RemoteException();
986 }
987 List<ResultInfo> results = null;
988 List<Intent> newIntents = null;
989 if (andResume) {
990 results = r.results;
991 newIntents = r.newIntents;
992 }
993 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
994 + " icicle=" + r.icicle
995 + " with results=" + results + " newIntents=" + newIntents
996 + " andResume=" + andResume);
997 if (andResume) {
998 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
999 r.userId, System.identityHashCode(r),
1000 r.task.taskId, r.shortComponentName);
1001 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001002 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001003 // Home process is the root process of the task.
1004 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001005 }
1006 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1007 r.sleeping = false;
1008 r.forceNewConfig = false;
1009 mService.showAskCompatModeDialogLocked(r);
1010 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1011 String profileFile = null;
1012 ParcelFileDescriptor profileFd = null;
1013 boolean profileAutoStop = false;
1014 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1015 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1016 mService.mProfileProc = app;
1017 profileFile = mService.mProfileFile;
1018 profileFd = mService.mProfileFd;
1019 profileAutoStop = mService.mAutoStopProfiler;
1020 }
1021 }
1022 app.hasShownUi = true;
1023 app.pendingUiClean = true;
1024 if (profileFd != null) {
1025 try {
1026 profileFd = profileFd.dup();
1027 } catch (IOException e) {
1028 if (profileFd != null) {
1029 try {
1030 profileFd.close();
1031 } catch (IOException o) {
1032 }
1033 profileFd = null;
1034 }
1035 }
1036 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001037
Dianne Hackborna413dc02013-07-12 12:02:55 -07001038 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001039 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1040 System.identityHashCode(r), r.info,
Craig Mautnera0026042014-04-23 11:45:37 -07001041 new Configuration(mService.mConfiguration), r.compat, r.task.voiceInteractor,
1042 app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume,
Craig Mautner233ceee2014-05-09 17:05:11 -07001043 mService.isNextTransitionForward(), profileFile, profileFd, profileAutoStop
1044 );
Craig Mautner2420ead2013-04-01 17:13:20 -07001045
1046 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1047 // This may be a heavy-weight process! Note that the package
1048 // manager will ensure that only activity can run in the main
1049 // process of the .apk, which is the only thing that will be
1050 // considered heavy-weight.
1051 if (app.processName.equals(app.info.packageName)) {
1052 if (mService.mHeavyWeightProcess != null
1053 && mService.mHeavyWeightProcess != app) {
1054 Slog.w(TAG, "Starting new heavy weight process " + app
1055 + " when already running "
1056 + mService.mHeavyWeightProcess);
1057 }
1058 mService.mHeavyWeightProcess = app;
1059 Message msg = mService.mHandler.obtainMessage(
1060 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1061 msg.obj = r;
1062 mService.mHandler.sendMessage(msg);
1063 }
1064 }
1065
1066 } catch (RemoteException e) {
1067 if (r.launchFailed) {
1068 // This is the second time we failed -- finish activity
1069 // and give up.
1070 Slog.e(TAG, "Second failure launching "
1071 + r.intent.getComponent().flattenToShortString()
1072 + ", giving up", e);
1073 mService.appDiedLocked(app, app.pid, app.thread);
1074 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1075 "2nd-crash", false);
1076 return false;
1077 }
1078
1079 // This is the first time we failed -- restart process and
1080 // retry.
1081 app.activities.remove(r);
1082 throw e;
1083 }
1084
1085 r.launchFailed = false;
1086 if (stack.updateLRUListLocked(r)) {
1087 Slog.w(TAG, "Activity " + r
1088 + " being launched, but already in LRU list");
1089 }
1090
1091 if (andResume) {
1092 // As part of the process of launching, ActivityThread also performs
1093 // a resume.
1094 stack.minimalResumeActivityLocked(r);
1095 } else {
1096 // This activity is not starting in the resumed state... which
1097 // should look like we asked it to pause+stop (but remain visible),
1098 // and it has done so and reported back the current icicle and
1099 // other state.
1100 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1101 + " (starting in stopped state)");
1102 r.state = ActivityState.STOPPED;
1103 r.stopped = true;
1104 }
1105
1106 // Launch the new version setup screen if needed. We do this -after-
1107 // launching the initial activity (that is, home), so that it can have
1108 // a chance to initialize itself while in the background, making the
1109 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001110 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001111 mService.startSetupActivityLocked();
1112 }
1113
1114 return true;
1115 }
1116
Craig Mautnere79d42682013-04-01 19:01:53 -07001117 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001118 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001119 // Is this activity's application already running?
1120 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001121 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001122
1123 r.task.stack.setLaunchTime(r);
1124
1125 if (app != null && app.thread != null) {
1126 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001127 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1128 || !"android".equals(r.info.packageName)) {
1129 // Don't add this if it is a platform component that is marked
1130 // to run in multiple processes, because this is actually
1131 // part of the framework so doesn't make sense to track as a
1132 // separate apk in the process.
1133 app.addPackage(r.info.packageName, mService.mProcessStats);
1134 }
George Mount2c92c972014-03-20 09:38:23 -07001135 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001136 return;
1137 } catch (RemoteException e) {
1138 Slog.w(TAG, "Exception when starting activity "
1139 + r.intent.getComponent().flattenToShortString(), e);
1140 }
1141
1142 // If a dead object exception was thrown -- fall through to
1143 // restart the application.
1144 }
1145
1146 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001147 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001148 }
1149
Craig Mautner6170f732013-04-02 13:05:23 -07001150 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001151 Intent intent, String resolvedType, ActivityInfo aInfo,
1152 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1153 IBinder resultTo, String resultWho, int requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001154 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Craig Mautnere0a38842013-12-16 16:14:02 -08001155 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container) {
Craig Mautner6170f732013-04-02 13:05:23 -07001156 int err = ActivityManager.START_SUCCESS;
1157
1158 ProcessRecord callerApp = null;
1159 if (caller != null) {
1160 callerApp = mService.getRecordForAppLocked(caller);
1161 if (callerApp != null) {
1162 callingPid = callerApp.pid;
1163 callingUid = callerApp.info.uid;
1164 } else {
1165 Slog.w(TAG, "Unable to find app for caller " + caller
1166 + " (pid=" + callingPid + ") when starting: "
1167 + intent.toString());
1168 err = ActivityManager.START_PERMISSION_DENIED;
1169 }
1170 }
1171
1172 if (err == ActivityManager.START_SUCCESS) {
1173 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1174 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner9ef471f2014-02-07 13:11:47 -08001175 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)
1176 + " on display " + (container == null ? (mFocusedStack == null ?
1177 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1178 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1179 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001180 }
1181
1182 ActivityRecord sourceRecord = null;
1183 ActivityRecord resultRecord = null;
1184 if (resultTo != null) {
1185 sourceRecord = isInAnyStackLocked(resultTo);
1186 if (DEBUG_RESULTS) Slog.v(
1187 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1188 if (sourceRecord != null) {
1189 if (requestCode >= 0 && !sourceRecord.finishing) {
1190 resultRecord = sourceRecord;
1191 }
1192 }
1193 }
1194 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1195
Dianne Hackborn91097de2014-04-04 18:02:06 -07001196 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001197
1198 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1199 && sourceRecord != null) {
1200 // Transfer the result target from the source activity to the new
1201 // one being started, including any failures.
1202 if (requestCode >= 0) {
1203 ActivityOptions.abort(options);
1204 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1205 }
1206 resultRecord = sourceRecord.resultTo;
1207 resultWho = sourceRecord.resultWho;
1208 requestCode = sourceRecord.requestCode;
1209 sourceRecord.resultTo = null;
1210 if (resultRecord != null) {
Craig Mautnerb539b3a2014-05-26 15:06:32 -07001211 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001212 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001213 if (sourceRecord.launchedFromUid == callingUid) {
1214 // The new activity is being launched from the same uid as the previous
1215 // activity in the flow, and asking to forward its result back to the
1216 // previous. In this case the activity is serving as a trampoline between
1217 // the two, so we also want to update its launchedFromPackage to be the
1218 // same as the previous activity. Note that this is safe, since we know
1219 // these two packages come from the same uid; the caller could just as
1220 // well have supplied that same package name itself. This specifially
1221 // deals with the case of an intent picker/chooser being launched in the app
1222 // flow to redirect to an activity picked by the user, where we want the final
1223 // activity to consider it to have been launched by the previous app activity.
1224 callingPackage = sourceRecord.launchedFromPackage;
1225 }
Craig Mautner6170f732013-04-02 13:05:23 -07001226 }
1227
1228 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1229 // We couldn't find a class that can handle the given Intent.
1230 // That's the end of that!
1231 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1232 }
1233
1234 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1235 // We couldn't find the specific class specified in the Intent.
1236 // Also the end of the line.
1237 err = ActivityManager.START_CLASS_NOT_FOUND;
1238 }
1239
Dianne Hackborn91097de2014-04-04 18:02:06 -07001240 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1241 && sourceRecord.task.voiceSession != null) {
1242 // If this activity is being launched as part of a voice session, we need
1243 // to ensure that it is safe to do so. If the upcoming activity will also
1244 // be part of the voice session, we can only launch it if it has explicitly
1245 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001246 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001247 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1248 try {
1249 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1250 intent, resolvedType)) {
1251 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1252 }
1253 } catch (RemoteException e) {
1254 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1255 }
1256 }
1257 }
1258
1259 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1260 // If the caller is starting a new voice session, just make sure the target
1261 // is actually allowing it to run this way.
1262 try {
1263 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1264 intent, resolvedType)) {
1265 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1266 }
1267 } catch (RemoteException e) {
1268 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1269 }
1270 }
1271
Craig Mautner6170f732013-04-02 13:05:23 -07001272 if (err != ActivityManager.START_SUCCESS) {
1273 if (resultRecord != null) {
1274 resultStack.sendActivityResultLocked(-1,
1275 resultRecord, resultWho, requestCode,
1276 Activity.RESULT_CANCELED, null);
1277 }
1278 setDismissKeyguard(false);
1279 ActivityOptions.abort(options);
1280 return err;
1281 }
1282
1283 final int startAnyPerm = mService.checkPermission(
1284 START_ANY_ACTIVITY, callingPid, callingUid);
1285 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1286 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1287 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1288 if (resultRecord != null) {
1289 resultStack.sendActivityResultLocked(-1,
1290 resultRecord, resultWho, requestCode,
1291 Activity.RESULT_CANCELED, null);
1292 }
1293 setDismissKeyguard(false);
1294 String msg;
1295 if (!aInfo.exported) {
1296 msg = "Permission Denial: starting " + intent.toString()
1297 + " from " + callerApp + " (pid=" + callingPid
1298 + ", uid=" + callingUid + ")"
1299 + " not exported from uid " + aInfo.applicationInfo.uid;
1300 } else {
1301 msg = "Permission Denial: starting " + intent.toString()
1302 + " from " + callerApp + " (pid=" + callingPid
1303 + ", uid=" + callingUid + ")"
1304 + " requires " + aInfo.permission;
1305 }
1306 Slog.w(TAG, msg);
1307 throw new SecurityException(msg);
1308 }
1309
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001310 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001311 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001312
Craig Mautner6170f732013-04-02 13:05:23 -07001313 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001314 try {
1315 // The Intent we give to the watcher has the extra data
1316 // stripped off, since it can contain private information.
1317 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001318 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001319 aInfo.applicationInfo.packageName);
1320 } catch (RemoteException e) {
1321 mService.mController = null;
1322 }
Ben Gruver5e207332013-04-03 17:41:37 -07001323 }
Craig Mautner6170f732013-04-02 13:05:23 -07001324
Ben Gruver5e207332013-04-03 17:41:37 -07001325 if (abort) {
1326 if (resultRecord != null) {
1327 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001328 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001329 }
Ben Gruver5e207332013-04-03 17:41:37 -07001330 // We pretend to the caller that it was really started, but
1331 // they will just get a cancel result.
1332 setDismissKeyguard(false);
1333 ActivityOptions.abort(options);
1334 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001335 }
1336
1337 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001338 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001339 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001340 if (outActivity != null) {
1341 outActivity[0] = r;
1342 }
1343
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001344 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001345 if (voiceSession == null && (stack.mResumedActivity == null
1346 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Craig Mautner6170f732013-04-02 13:05:23 -07001347 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1348 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001349 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautner6170f732013-04-02 13:05:23 -07001350 mService.mPendingActivityLaunches.add(pal);
1351 setDismissKeyguard(false);
1352 ActivityOptions.abort(options);
1353 return ActivityManager.START_SWITCHES_CANCELED;
1354 }
1355 }
1356
1357 if (mService.mDidAppSwitch) {
1358 // This is the second allowed switch since we stopped switches,
1359 // so now just generally allow switches. Use case: user presses
1360 // home (switches disabled, switch to home, mDidAppSwitch now true);
1361 // user taps a home icon (coming from home so allowed, we hit here
1362 // and now allow anyone to switch again).
1363 mService.mAppSwitchesAllowedTime = 0;
1364 } else {
1365 mService.mDidAppSwitch = true;
1366 }
1367
1368 mService.doPendingActivityLaunchesLocked(false);
1369
Dianne Hackborn91097de2014-04-04 18:02:06 -07001370 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
1371 startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001372
1373 if (allPausedActivitiesComplete()) {
1374 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001375 // activity start, but we are not actually doing an activity
1376 // switch... just dismiss the keyguard now, because we
1377 // probably want to see whatever is behind it.
1378 dismissKeyguard();
1379 }
1380 return err;
1381 }
1382
Craig Mautnerb539b3a2014-05-26 15:06:32 -07001383 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001384 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001385
1386 // On leanback only devices we should keep all activities in the same stack.
1387 if (!mLeanbackOnlyDevice &&
1388 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001389 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001390 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001391 if (taskStack.isOnHomeDisplay()) {
1392 if (mFocusedStack != taskStack) {
1393 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1394 "focused stack to r=" + r + " task=" + task);
1395 mFocusedStack = taskStack;
1396 } else {
1397 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1398 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1399 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001400 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001401 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001402 }
1403
Craig Mautnere0a38842013-12-16 16:14:02 -08001404 final ActivityContainer container = r.mInitialActivityContainer;
1405 if (container != null) {
1406 // The first time put it on the desired stack, after this put on task stack.
1407 r.mInitialActivityContainer = null;
1408 return container.mStack;
1409 }
1410
Craig Mautnerb539b3a2014-05-26 15:06:32 -07001411 if (mFocusedStack != mHomeStack && (!newTask ||
1412 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001413 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1414 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1415 return mFocusedStack;
1416 }
1417
Craig Mautnere0a38842013-12-16 16:14:02 -08001418 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1419 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1420 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1421 if (!stack.isHomeStack()) {
1422 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1423 "adjustStackFocus: Setting focused stack=" + stack);
1424 mFocusedStack = stack;
1425 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001426 }
1427 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001428
Craig Mautner4a1cb222013-12-04 16:14:06 -08001429 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001430 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001431 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1432 " stackId=" + stackId);
1433 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001434 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001435 }
1436 return mHomeStack;
1437 }
1438
Craig Mautner29219d92013-04-16 20:19:12 -07001439 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001440 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001441 final TaskRecord task = r.task;
1442 boolean isHomeActivity = !r.isApplicationActivity();
1443 if (!isHomeActivity && task != null) {
1444 isHomeActivity = !task.isApplicationTask();
1445 }
1446 if (!isHomeActivity && task != null) {
1447 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1448 isHomeActivity = parent != null && parent.isHomeActivity();
1449 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001450 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001451 }
1452 }
1453
Craig Mautner8849a5e2013-04-02 16:41:03 -07001454 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001455 ActivityRecord sourceRecord,
1456 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
1457 boolean doResume, Bundle options) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001458 final Intent intent = r.intent;
1459 final int callingUid = r.launchedFromUid;
1460
1461 int launchFlags = intent.getFlags();
1462
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001463 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
1464 (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
1465 r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK)) {
1466 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1467 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1468 "\"singleInstance\" or \"singleTask\"");
1469 launchFlags &=
1470 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1471 } else {
1472 switch (r.info.documentLaunchMode) {
1473 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1474 break;
1475 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1476 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1477 break;
1478 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1479 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1480 break;
1481 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1482 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1483 break;
1484 }
1485 }
1486
1487 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1488 // For whatever reason this activity is being launched into a new
1489 // task... yet the caller has requested a result back. Well, that
1490 // is pretty messed up, so instead immediately send back a cancel
1491 // and let the new task continue launched as normal without a
1492 // dependency on its originator.
1493 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1494 r.resultTo.task.stack.sendActivityResultLocked(-1,
1495 r.resultTo, r.resultWho, r.requestCode,
1496 Activity.RESULT_CANCELED, null);
1497 r.resultTo = null;
1498 }
1499
1500 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1501 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1502 }
1503
Craig Mautner8849a5e2013-04-02 16:41:03 -07001504 // We'll invoke onUserLeaving before onPause only if the launching
1505 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001506 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001507 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001508
1509 // If the caller has asked not to resume at this point, we make note
1510 // of this in the record so that we can skip it when trying to find
1511 // the top running activity.
1512 if (!doResume) {
1513 r.delayedResume = true;
1514 }
1515
Craig Mautnera254cd72014-05-25 16:47:39 -07001516 ActivityRecord notTop =
1517 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001518
1519 // If the onlyIfNeeded flag is set, then we can do this if the activity
1520 // being launched is the same as the one making the call... or, as
1521 // a special case, if we do not know the caller then we count the
1522 // current top activity as the caller.
1523 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1524 ActivityRecord checkedCaller = sourceRecord;
1525 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001526 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001527 }
1528 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1529 // Caller is not the same as launcher, so always needed.
1530 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1531 }
1532 }
1533
1534 if (sourceRecord == null) {
1535 // This activity is not being started from another... in this
1536 // case we -always- start a new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001537 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001538 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1539 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001540 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1541 }
1542 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1543 // The original activity who is starting us is running as a single
1544 // instance... this new activity it is starting must go on its
1545 // own task.
1546 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1547 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
1548 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1549 // The activity being started is a single instance... it always
1550 // gets launched into its own task.
1551 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1552 }
1553
Craig Mautner88629292013-11-10 20:39:05 -08001554 ActivityInfo newTaskInfo = null;
1555 Intent newTaskIntent = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001556 final ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001557 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001558 if (sourceRecord.finishing) {
1559 // If the source is finishing, we can't further count it as our source. This
1560 // is because the task it is associated with may now be empty and on its way out,
1561 // so we don't want to blindly throw it in to that task. Instead we will take
Craig Mautner88629292013-11-10 20:39:05 -08001562 // the NEW_TASK flow and try to find a task for it. But save the task information
1563 // so it can be used when creating the new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001564 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001565 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1566 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1567 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautner88629292013-11-10 20:39:05 -08001568 newTaskInfo = sourceRecord.info;
1569 newTaskIntent = sourceRecord.task.intent;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001570 }
1571 sourceRecord = null;
1572 sourceStack = null;
1573 } else {
1574 sourceStack = sourceRecord.task.stack;
1575 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001576 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001577 sourceStack = null;
1578 }
1579
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001580 intent.setFlags(launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001581
1582 boolean addingToTask = false;
1583 boolean movedHome = false;
1584 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001585 ActivityStack targetStack;
Craig Mautnerd00f4742014-03-12 14:17:26 -07001586 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1587 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001588 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1589 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1590 // If bring to front is requested, and no result is requested, and
1591 // we can find a task that was started with this same
1592 // component, then instead of launching bring that one to the front.
1593 if (r.resultTo == null) {
1594 // See if there is a task to bring to the front. If this is
1595 // a SINGLE_INSTANCE activity, there can be one and only one
1596 // instance of it in the history, and it is always in its own
1597 // unique task, so we do a special search.
1598 ActivityRecord intentActivity = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
Craig Mautnerac6f8432013-07-17 13:24:59 -07001599 ? findTaskLocked(r)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001600 : findActivityLocked(intent, r.info);
1601 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001602 if (isLockTaskModeViolation(intentActivity.task)) {
1603 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
1604 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1605 }
Craig Mautner29219d92013-04-16 20:19:12 -07001606 if (r.task == null) {
1607 r.task = intentActivity.task;
1608 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001609 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001610 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001611 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1612 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001613 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001614 if (intentActivity.task.intent == null) {
1615 // This task was started because of movement of
1616 // the activity based on affinity... now that we
1617 // are actually launching it, we can assign the
1618 // base intent.
1619 intentActivity.task.setIntent(intent, r.info);
1620 }
1621 // If the target task is not in the front, then we need
1622 // to bring it to the front... except... well, with
1623 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1624 // to have the same behavior as if a new instance was
1625 // being started, which means not bringing it to the front
1626 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001627 final ActivityStack lastStack = getLastStack();
1628 ActivityRecord curTop = lastStack == null?
1629 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001630 if (curTop != null && (curTop.task != intentActivity.task ||
1631 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001632 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001633 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1634 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001635 // We really do want to push this one into the
1636 // user's face, right now.
1637 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001638 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001639 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001640 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1641 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001642 // Caller wants to appear on home activity.
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +00001643 intentActivity.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001644 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001645 options = null;
1646 }
1647 }
1648 // If the caller has requested that the target task be
1649 // reset, then do so.
1650 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1651 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1652 }
1653 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1654 // We don't need to start a new activity, and
1655 // the client said not to do anything if that
1656 // is the case, so this is it! And for paranoia, make
1657 // sure we have correctly resumed the top activity.
1658 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001659 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001660 } else {
1661 ActivityOptions.abort(options);
1662 }
1663 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1664 }
1665 if ((launchFlags &
1666 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1667 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1668 // The caller has requested to completely replace any
1669 // existing task with its new activity. Well that should
1670 // not be too hard...
1671 reuseTask = intentActivity.task;
1672 reuseTask.performClearTaskLocked();
1673 reuseTask.setIntent(r.intent, r.info);
1674 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
1675 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1676 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1677 // In this situation we want to remove all activities
1678 // from the task up to the one being started. In most
1679 // cases this means we are resetting the task to its
1680 // initial state.
1681 ActivityRecord top =
1682 intentActivity.task.performClearTaskLocked(r, launchFlags);
1683 if (top != null) {
1684 if (top.frontOfTask) {
1685 // Activity aliases may mean we use different
1686 // intents for the top activity, so make sure
1687 // the task now has the identity of the new
1688 // intent.
1689 top.task.setIntent(r.intent, r.info);
1690 }
1691 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1692 r, top.task);
1693 top.deliverNewIntentLocked(callingUid, r.intent);
1694 } else {
1695 // A special case: we need to
1696 // start the activity because it is not currently
1697 // running, and the caller has asked to clear the
1698 // current task to have this activity at the top.
1699 addingToTask = true;
1700 // Now pretend like this activity is being started
1701 // by the top of its task, so it is put in the
1702 // right place.
1703 sourceRecord = intentActivity;
1704 }
1705 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1706 // In this case the top activity on the task is the
1707 // same as the one being launched, so we take that
1708 // as a request to bring the task to the foreground.
1709 // If the top activity in the task is the root
1710 // activity, deliver this new intent to it if it
1711 // desires.
1712 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1713 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1714 && intentActivity.realActivity.equals(r.realActivity)) {
1715 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1716 intentActivity.task);
1717 if (intentActivity.frontOfTask) {
1718 intentActivity.task.setIntent(r.intent, r.info);
1719 }
1720 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1721 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1722 // In this case we are launching the root activity
1723 // of the task, but with a different intent. We
1724 // should start a new instance on top.
1725 addingToTask = true;
1726 sourceRecord = intentActivity;
1727 }
1728 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1729 // In this case an activity is being launched in to an
1730 // existing task, without resetting that task. This
1731 // is typically the situation of launching an activity
1732 // from a notification or shortcut. We want to place
1733 // the new activity on top of the current task.
1734 addingToTask = true;
1735 sourceRecord = intentActivity;
1736 } else if (!intentActivity.task.rootWasReset) {
1737 // In this case we are launching in to an existing task
1738 // that has not yet been started from its front door.
1739 // The current task has been brought to the front.
1740 // Ideally, we'd probably like to place this new task
1741 // at the bottom of its stack, but that's a little hard
1742 // to do with the current organization of the code so
1743 // for now we'll just drop it.
1744 intentActivity.task.setIntent(r.intent, r.info);
1745 }
1746 if (!addingToTask && reuseTask == null) {
1747 // We didn't do anything... but it was needed (a.k.a., client
1748 // don't use that intent!) And for paranoia, make
1749 // sure we have correctly resumed the top activity.
1750 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001751 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001752 } else {
1753 ActivityOptions.abort(options);
1754 }
1755 return ActivityManager.START_TASK_TO_FRONT;
1756 }
1757 }
1758 }
1759 }
1760
1761 //String uri = r.intent.toURI();
1762 //Intent intent2 = new Intent(uri);
1763 //Slog.i(TAG, "Given intent: " + r.intent);
1764 //Slog.i(TAG, "URI is: " + uri);
1765 //Slog.i(TAG, "To intent: " + intent2);
1766
1767 if (r.packageName != null) {
1768 // If the activity being launched is the same as the one currently
1769 // at the top, then we need to check if it should only be launched
1770 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001771 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001772 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001773 if (top != null && r.resultTo == null) {
1774 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1775 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001776 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautner8849a5e2013-04-02 16:41:03 -07001777 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
1778 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1779 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1780 top.task);
1781 // For paranoia, make sure we have correctly
1782 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001783 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001784 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001785 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001786 }
1787 ActivityOptions.abort(options);
1788 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1789 // We don't need to start a new activity, and
1790 // the client said not to do anything if that
1791 // is the case, so this is it!
1792 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1793 }
1794 top.deliverNewIntentLocked(callingUid, r.intent);
1795 return ActivityManager.START_DELIVERED_TO_TOP;
1796 }
1797 }
1798 }
1799 }
1800
1801 } else {
1802 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001803 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1804 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001805 }
1806 ActivityOptions.abort(options);
1807 return ActivityManager.START_CLASS_NOT_FOUND;
1808 }
1809
1810 boolean newTask = false;
1811 boolean keepCurTransition = false;
1812
1813 // Should this be considered a new task?
1814 if (r.resultTo == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001815 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001816 if (isLockTaskModeViolation(reuseTask)) {
1817 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1818 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1819 }
Craig Mautnerb539b3a2014-05-26 15:06:32 -07001820 newTask = true;
1821 targetStack = adjustStackFocus(r, newTask);
Craig Mautnere0a38842013-12-16 16:14:02 -08001822 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001823 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08001824 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
1825 newTaskInfo != null ? newTaskInfo : r.info,
1826 newTaskIntent != null ? newTaskIntent : intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001827 voiceSession, voiceInteractor, true), null, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001828 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1829 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001830 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001831 r.setTask(reuseTask, reuseTask, true);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001832 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001833 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001834 if ((launchFlags &
1835 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1836 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1837 // Caller wants to appear on home activity, so before starting
1838 // their own activity we will bring home to the front.
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +00001839 r.task.mOnTopOfHome = r.task.stack.isOnHomeDisplay();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001840 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001841 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001842 } else if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001843 TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08001844 if (isLockTaskModeViolation(sourceTask)) {
1845 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1846 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1847 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001848 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07001849 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07001850 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001851 if (!addingToTask &&
1852 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
1853 // In this case, we are adding the activity to an existing
1854 // task, but the caller has asked to clear that task if the
1855 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001856 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001857 keepCurTransition = true;
1858 if (top != null) {
1859 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1860 top.deliverNewIntentLocked(callingUid, r.intent);
1861 // For paranoia, make sure we have correctly
1862 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001863 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001864 if (doResume) {
1865 targetStack.resumeTopActivityLocked(null);
1866 }
1867 ActivityOptions.abort(options);
1868 return ActivityManager.START_DELIVERED_TO_TOP;
1869 }
1870 } else if (!addingToTask &&
1871 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1872 // In this case, we are launching an activity in our own task
1873 // that may already be running somewhere in the history, and
1874 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001875 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001876 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001877 final TaskRecord task = top.task;
1878 task.moveActivityToFrontLocked(top);
1879 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001880 top.updateOptionsLocked(options);
1881 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001882 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001883 if (doResume) {
1884 targetStack.resumeTopActivityLocked(null);
1885 }
1886 return ActivityManager.START_DELIVERED_TO_TOP;
1887 }
1888 }
1889 // An existing activity is starting this new activity, so we want
1890 // to keep the new one in the same task as the one that is starting
1891 // it.
Craig Mautner525f3d92013-05-07 14:01:50 -07001892 r.setTask(sourceTask, sourceRecord.thumbHolder, false);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001893 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001894 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001895
1896 } else {
1897 // This not being started from an existing activity, and not part
1898 // of a new task... just put it in the top task, though these days
1899 // this case should never happen.
Craig Mautnerb539b3a2014-05-26 15:06:32 -07001900 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07001901 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07001902 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001903 r.setTask(prev != null ? prev.task
Dianne Hackborn91097de2014-04-04 18:02:06 -07001904 : targetStack.createTaskRecord(getNextTaskId(), r.info, intent, null, null, true),
Craig Mautnerde4ef022013-04-07 19:01:33 -07001905 null, true);
Craig Mautner95e9daa2014-03-17 15:57:20 -07001906 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001907 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
1908 + " in new guessed " + r.task);
1909 }
1910
1911 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
1912 intent, r.getUriPermissionsLocked());
1913
1914 if (newTask) {
1915 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
1916 }
1917 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07001918 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001919 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautner1d001b62013-06-18 16:52:43 -07001920 mService.setFocusedActivityLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001921 return ActivityManager.START_SUCCESS;
1922 }
1923
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001924 void acquireLaunchWakelock() {
1925 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1926 throw new IllegalStateException("Calling must be system uid");
1927 }
1928 mLaunchingActivity.acquire();
1929 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1930 // To be safe, don't allow the wake lock to be held for too long.
1931 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1932 }
1933 }
1934
Craig Mautnerf3333272013-04-22 10:55:53 -07001935 // Checked.
1936 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1937 Configuration config) {
1938 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
1939
Craig Mautnerf3333272013-04-22 10:55:53 -07001940 ArrayList<ActivityRecord> stops = null;
1941 ArrayList<ActivityRecord> finishes = null;
1942 ArrayList<UserStartedState> startingUsers = null;
1943 int NS = 0;
1944 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001945 boolean booting = false;
1946 boolean enableScreen = false;
1947 boolean activityRemoved = false;
1948
1949 ActivityRecord r = ActivityRecord.forToken(token);
1950 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001951 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
1952 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001953 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1954 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001955 if (fromTimeout) {
1956 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001957 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001958
1959 // This is a hack to semi-deal with a race condition
1960 // in the client where it can be constructed with a
1961 // newer configuration from when we asked it to launch.
1962 // We'll update with whatever configuration it now says
1963 // it used to launch.
1964 if (config != null) {
1965 r.configuration = config;
1966 }
1967
1968 // We are now idle. If someone is waiting for a thumbnail from
1969 // us, we can now deliver.
1970 r.idle = true;
1971
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001972 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
1973 if (!mService.mBooted && isFrontStack(r.task.stack)) {
1974 mService.mBooted = true;
1975 enableScreen = true;
1976 }
1977 }
1978
1979 if (allResumedActivitiesIdle()) {
1980 if (r != null) {
1981 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001982 }
1983
1984 if (mLaunchingActivity.isHeld()) {
1985 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1986 if (VALIDATE_WAKE_LOCK_CALLER &&
1987 Binder.getCallingUid() != Process.myUid()) {
1988 throw new IllegalStateException("Calling must be system uid");
1989 }
1990 mLaunchingActivity.release();
1991 }
1992 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07001993 }
1994
1995 // Atomically retrieve all of the other things to do.
1996 stops = processStoppingActivitiesLocked(true);
1997 NS = stops != null ? stops.size() : 0;
1998 if ((NF=mFinishingActivities.size()) > 0) {
1999 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2000 mFinishingActivities.clear();
2001 }
2002
Craig Mautnerf3333272013-04-22 10:55:53 -07002003 if (isFrontStack(mHomeStack)) {
2004 booting = mService.mBooting;
2005 mService.mBooting = false;
2006 }
2007
2008 if (mStartingUsers.size() > 0) {
2009 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2010 mStartingUsers.clear();
2011 }
2012
Craig Mautnerf3333272013-04-22 10:55:53 -07002013 // Stop any activities that are scheduled to do so but have been
2014 // waiting for the next one to start.
2015 for (int i = 0; i < NS; i++) {
2016 r = stops.get(i);
2017 final ActivityStack stack = r.task.stack;
2018 if (r.finishing) {
2019 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2020 } else {
2021 stack.stopActivityLocked(r);
2022 }
2023 }
2024
2025 // Finish any activities that are scheduled to do so but have been
2026 // waiting for the next one to start.
2027 for (int i = 0; i < NF; i++) {
2028 r = finishes.get(i);
2029 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, false, "finish-idle");
2030 }
2031
2032 if (booting) {
2033 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002034 } else {
2035 // Complete user switch
2036 if (startingUsers != null) {
2037 for (int i = 0; i < startingUsers.size(); i++) {
2038 mService.finishUserSwitch(startingUsers.get(i));
2039 }
2040 }
2041 // Complete starting up of background users
2042 if (mStartingBackgroundUsers.size() > 0) {
2043 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2044 mStartingBackgroundUsers.clear();
2045 for (int i = 0; i < startingUsers.size(); i++) {
2046 mService.finishUserBoot(startingUsers.get(i));
2047 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002048 }
2049 }
2050
2051 mService.trimApplications();
2052 //dump();
2053 //mWindowManager.dump();
2054
2055 if (enableScreen) {
2056 mService.enableScreenAfterBoot();
2057 }
2058
2059 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002060 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002061 }
2062
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002063 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002064 }
2065
Craig Mautner8e569572013-10-11 17:36:59 -07002066 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002067 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002068 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2069 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002070 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2071 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2072 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002073 }
Craig Mautner19091252013-10-05 00:03:53 -07002074 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002075 }
2076
2077 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002078 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2079 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002080 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2081 stacks.get(stackNdx).closeSystemDialogsLocked();
2082 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002083 }
2084 }
2085
Craig Mautner93529a42013-10-04 15:03:13 -07002086 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002087 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002088 }
2089
Craig Mautner8d341ef2013-03-26 09:03:27 -07002090 /**
2091 * @return true if some activity was finished (or would have finished if doit were true).
2092 */
2093 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2094 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002095 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2096 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002097 final int numStacks = stacks.size();
2098 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2099 final ActivityStack stack = stacks.get(stackNdx);
2100 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2101 didSomething = true;
2102 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002103 }
2104 }
2105 return didSomething;
2106 }
2107
Dianne Hackborna413dc02013-07-12 12:02:55 -07002108 void updatePreviousProcessLocked(ActivityRecord r) {
2109 // Now that this process has stopped, we may want to consider
2110 // it to be the previous app to try to keep around in case
2111 // the user wants to return to it.
2112
2113 // First, found out what is currently the foreground app, so that
2114 // we don't blow away the previous app if this activity is being
2115 // hosted by the process that is actually still the foreground.
2116 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002117 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2118 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002119 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2120 final ActivityStack stack = stacks.get(stackNdx);
2121 if (isFrontStack(stack)) {
2122 if (stack.mResumedActivity != null) {
2123 fgApp = stack.mResumedActivity.app;
2124 } else if (stack.mPausingActivity != null) {
2125 fgApp = stack.mPausingActivity.app;
2126 }
2127 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002128 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002129 }
2130 }
2131
2132 // Now set this one as the previous process, only if that really
2133 // makes sense to.
2134 if (r.app != null && fgApp != null && r.app != fgApp
2135 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002136 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002137 mService.mPreviousProcess = r.app;
2138 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2139 }
2140 }
2141
Craig Mautner05d29032013-05-03 13:40:13 -07002142 boolean resumeTopActivitiesLocked() {
2143 return resumeTopActivitiesLocked(null, null, null);
2144 }
2145
2146 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2147 Bundle targetOptions) {
2148 if (targetStack == null) {
2149 targetStack = getFocusedStack();
2150 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002151 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002152 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002153 if (isFrontStack(targetStack)) {
2154 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2155 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002156 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2157 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002158 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2159 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002160 if (stack == targetStack) {
2161 // Already started above.
2162 continue;
2163 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002164 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002165 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002166 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002167 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002168 }
Craig Mautner05d29032013-05-03 13:40:13 -07002169 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002170 }
2171
2172 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002173 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2174 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002175 final int numStacks = stacks.size();
2176 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2177 final ActivityStack stack = stacks.get(stackNdx);
2178 stack.finishTopRunningActivityLocked(app);
2179 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002180 }
2181 }
2182
Craig Mautneraea74a52014-03-08 14:23:10 -08002183 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2184 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2185 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002186 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002187 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2188 // Caller wants the home activity moved with it. To accomplish this,
2189 // we'll just indicate that this task returns to the home task.
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +00002190 task.mOnTopOfHome = true;
Craig Mautneraea74a52014-03-08 14:23:10 -08002191 }
2192 task.stack.moveTaskToFrontLocked(task, null, options);
2193 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2194 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002195 }
2196
Craig Mautner967212c2013-04-13 21:10:58 -07002197 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002198 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2199 if (activityContainer != null) {
2200 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002201 }
2202 return null;
2203 }
2204
Craig Mautner967212c2013-04-13 21:10:58 -07002205 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002206 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002207 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2208 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002209 }
2210 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002211 }
2212
Craig Mautner4a1cb222013-12-04 16:14:06 -08002213 IBinder getHomeActivityToken() {
2214 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2215 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2216 final TaskRecord task = tasks.get(taskNdx);
2217 if (task.isHomeTask()) {
2218 final ArrayList<ActivityRecord> activities = task.mActivities;
2219 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2220 final ActivityRecord r = activities.get(activityNdx);
2221 if (r.isHomeActivity()) {
2222 return r.appToken;
2223 }
2224 }
2225 }
2226 }
2227 return null;
2228 }
2229
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002230 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002231 IActivityContainerCallback callback) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002232 ActivityContainer activityContainer = new VirtualActivityContainer(parentActivity, callback);
2233 mActivityContainers.put(activityContainer.mStackId, activityContainer);
2234 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002235 return activityContainer;
2236 }
2237
Craig Mautner34b73df2014-01-12 21:11:08 -08002238 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002239 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2240 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2241 ActivityContainer container = childStacks.remove(containerNdx);
2242 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002243 }
2244 }
2245
Craig Mautner95da1082014-02-24 17:54:35 -08002246 void deleteActivityContainer(IActivityContainer container) {
2247 ActivityContainer activityContainer = (ActivityContainer)container;
2248 if (activityContainer != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002249 activityContainer.mStack.finishAllActivitiesLocked();
Craig Mautner95da1082014-02-24 17:54:35 -08002250 final ActivityRecord parent = activityContainer.mParentActivity;
2251 if (parent != null) {
2252 parent.mChildContainers.remove(activityContainer);
2253 }
2254 final int stackId = activityContainer.mStackId;
2255 mActivityContainers.remove(stackId);
2256 mWindowManager.removeStack(stackId);
2257 }
2258 }
2259
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002260 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002261 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2262 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002263 return -1;
2264 }
2265
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002266 ActivityContainer activityContainer = new ActivityContainer(stackId);
2267 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002268 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002269 return stackId;
2270 }
2271
2272 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002273 while (true) {
2274 if (++mLastStackId <= HOME_STACK_ID) {
2275 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002276 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002277 if (getStack(mLastStackId) == null) {
2278 break;
2279 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002280 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002281 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002282 }
2283
Craig Mautner21d24a22014-04-23 11:45:37 -07002284 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2285 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2286 final ActivityStack stack = getStack(stackId);
2287 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2288 final TaskRecord task = tasks.get(taskNdx);
2289 stack.addTask(task, false, false);
2290 final int taskId = task.taskId;
2291 final ArrayList<ActivityRecord> activities = task.mActivities;
2292 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2293 final ActivityRecord r = activities.get(activityNdx);
2294 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2295 r.info.screenOrientation, r.fullscreen,
2296 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07002297 r.userId, r.info.configChanges, task.voiceSession != null);
Craig Mautner21d24a22014-04-23 11:45:37 -07002298 }
2299 mWindowManager.addTask(taskId, stackId, false);
2300 }
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +00002301 resumeHomeActivity(null);
Craig Mautner21d24a22014-04-23 11:45:37 -07002302 }
2303
Craig Mautner8d341ef2013-03-26 09:03:27 -07002304 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002305 final TaskRecord task = anyTaskForIdLocked(taskId);
2306 if (task == null) {
2307 return;
2308 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002309 final ActivityStack stack = getStack(stackId);
2310 if (stack == null) {
2311 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2312 return;
2313 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002314 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002315 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002316 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002317 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002318 }
2319
Craig Mautnerac6f8432013-07-17 13:24:59 -07002320 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002321 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002322 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2323 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002324 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2325 final ActivityStack stack = stacks.get(stackNdx);
2326 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautnerb539b3a2014-05-26 15:06:32 -07002327 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2328 continue;
2329 }
2330 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2331 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2332 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002333 continue;
2334 }
2335 final ActivityRecord ar = stack.findTaskLocked(r);
2336 if (ar != null) {
2337 return ar;
2338 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002339 }
2340 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002341 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002342 return null;
2343 }
2344
2345 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002346 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2347 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002348 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2349 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2350 if (ar != null) {
2351 return ar;
2352 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002353 }
2354 }
2355 return null;
2356 }
2357
Craig Mautner8d341ef2013-03-26 09:03:27 -07002358 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002359 scheduleSleepTimeout();
2360 if (!mGoingToSleep.isHeld()) {
2361 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002362 if (mLaunchingActivity.isHeld()) {
2363 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2364 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002365 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002366 mLaunchingActivity.release();
2367 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002368 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002369 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002370 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002371 }
2372
2373 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002374 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002375
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002376 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002377 final long endTime = System.currentTimeMillis() + timeout;
2378 while (true) {
2379 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002380 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2381 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002382 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2383 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2384 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002385 }
2386 if (cantShutdown) {
2387 long timeRemaining = endTime - System.currentTimeMillis();
2388 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002389 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002390 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002391 } catch (InterruptedException e) {
2392 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002393 } else {
2394 Slog.w(TAG, "Activity manager shutdown timed out");
2395 timedout = true;
2396 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002397 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002398 } else {
2399 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002400 }
2401 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002402
2403 // Force checkReadyForSleep to complete.
2404 mSleepTimeout = true;
2405 checkReadyForSleepLocked();
2406
Craig Mautner8d341ef2013-03-26 09:03:27 -07002407 return timedout;
2408 }
2409
2410 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002411 removeSleepTimeouts();
2412 if (mGoingToSleep.isHeld()) {
2413 mGoingToSleep.release();
2414 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002415 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2416 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002417 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2418 final ActivityStack stack = stacks.get(stackNdx);
2419 stack.awakeFromSleepingLocked();
2420 if (isFrontStack(stack)) {
2421 resumeTopActivitiesLocked();
2422 }
Craig Mautner5314a402013-09-26 12:40:16 -07002423 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002424 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002425 mGoingToSleepActivities.clear();
2426 }
2427
2428 void activitySleptLocked(ActivityRecord r) {
2429 mGoingToSleepActivities.remove(r);
2430 checkReadyForSleepLocked();
2431 }
2432
2433 void checkReadyForSleepLocked() {
2434 if (!mService.isSleepingOrShuttingDown()) {
2435 // Do not care.
2436 return;
2437 }
2438
2439 if (!mSleepTimeout) {
2440 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002441 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2442 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002443 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2444 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2445 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002446 }
2447
2448 if (mStoppingActivities.size() > 0) {
2449 // Still need to tell some activities to stop; can't sleep yet.
2450 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2451 + mStoppingActivities.size() + " activities");
2452 scheduleIdleLocked();
2453 dontSleep = true;
2454 }
2455
2456 if (mGoingToSleepActivities.size() > 0) {
2457 // Still need to tell some activities to sleep; can't sleep yet.
2458 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2459 + mGoingToSleepActivities.size() + " activities");
2460 dontSleep = true;
2461 }
2462
2463 if (dontSleep) {
2464 return;
2465 }
2466 }
2467
Craig Mautnere0a38842013-12-16 16:14:02 -08002468 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2469 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002470 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2471 stacks.get(stackNdx).goToSleep();
2472 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002473 }
2474
2475 removeSleepTimeouts();
2476
2477 if (mGoingToSleep.isHeld()) {
2478 mGoingToSleep.release();
2479 }
2480 if (mService.mShuttingDown) {
2481 mService.notifyAll();
2482 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002483 }
2484
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002485 boolean reportResumedActivityLocked(ActivityRecord r) {
2486 final ActivityStack stack = r.task.stack;
2487 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002488 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002489 }
2490 if (allResumedActivitiesComplete()) {
2491 ensureActivitiesVisibleLocked(null, 0);
2492 mWindowManager.executeAppTransition();
2493 return true;
2494 }
2495 return false;
2496 }
2497
Craig Mautner8d341ef2013-03-26 09:03:27 -07002498 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002499 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2500 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002501 final int numStacks = stacks.size();
2502 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2503 final ActivityStack stack = stacks.get(stackNdx);
2504 stack.handleAppCrashLocked(app);
2505 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002506 }
2507 }
2508
Craig Mautnerde4ef022013-04-07 19:01:33 -07002509 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002510 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002511 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2512 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002513 final int topStackNdx = stacks.size() - 1;
2514 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2515 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002516 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002517 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002518 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002519 }
2520
2521 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002522 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2523 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002524 final int numStacks = stacks.size();
2525 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2526 final ActivityStack stack = stacks.get(stackNdx);
2527 stack.scheduleDestroyActivities(app, false, reason);
2528 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002529 }
2530 }
2531
2532 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002533 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2534 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002535 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002536
Craig Mautner858d8a62013-04-23 17:08:34 -07002537 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002538 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2539 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002540 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002541 final ActivityStack stack = stacks.get(stackNdx);
2542 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002543 TaskRecord task = stack.topTask();
2544 if (task != null) {
2545 mWindowManager.moveTaskToTop(task.taskId);
2546 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002547 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002548 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002549
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002550 ActivityStack stack = getStack(restoreStackId);
2551 if (stack == null) {
2552 stack = mHomeStack;
2553 }
2554 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002555 if (stack.isOnHomeDisplay()) {
2556 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002557 TaskRecord task = stack.topTask();
2558 if (task != null) {
2559 mWindowManager.moveTaskToTop(task.taskId);
2560 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002561 } else {
2562 // Stack was moved to another display while user was swapped out.
Craig Mautnerb9a6c8a2014-05-29 16:50:59 +00002563 resumeHomeActivity(null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002564 }
Craig Mautner93529a42013-10-04 15:03:13 -07002565 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002566 }
2567
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002568 /**
2569 * Add background users to send boot completed events to.
2570 * @param userId The user being started in the background
2571 * @param uss The state object for the user.
2572 */
2573 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2574 mStartingBackgroundUsers.add(uss);
2575 }
2576
Craig Mautnerde4ef022013-04-07 19:01:33 -07002577 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2578 int N = mStoppingActivities.size();
2579 if (N <= 0) return null;
2580
2581 ArrayList<ActivityRecord> stops = null;
2582
2583 final boolean nowVisible = allResumedActivitiesVisible();
2584 for (int i=0; i<N; i++) {
2585 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002586 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002587 + nowVisible + " waitingVisible=" + s.waitingVisible
2588 + " finishing=" + s.finishing);
2589 if (s.waitingVisible && nowVisible) {
2590 mWaitingVisibleActivities.remove(s);
2591 s.waitingVisible = false;
2592 if (s.finishing) {
2593 // If this activity is finishing, it is sitting on top of
2594 // everyone else but we now know it is no longer needed...
2595 // so get rid of it. Otherwise, we need to go through the
2596 // normal flow and hide it once we determine that it is
2597 // hidden by the activities in front of it.
2598 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002599 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002600 }
2601 }
2602 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2603 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2604 if (stops == null) {
2605 stops = new ArrayList<ActivityRecord>();
2606 }
2607 stops.add(s);
2608 mStoppingActivities.remove(i);
2609 N--;
2610 i--;
2611 }
2612 }
2613
2614 return stops;
2615 }
2616
Craig Mautnercf910b02013-04-23 11:23:27 -07002617 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002618 // FIXME
2619/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2620 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07002621 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002622 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002623 if (isFrontStack(stack)) {
2624 if (r == null) {
2625 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2626 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002627 final ActivityRecord pausing = stack.mPausingActivity;
2628 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002629 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002630 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002631 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002632 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002633 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002634 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002635 }
2636 }
2637 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002638 final ActivityRecord resumed = stack.mResumedActivity;
2639 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002640 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002641 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002642 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002643 if (r != null && (state == ActivityState.INITIALIZING
2644 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002645 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002646 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002647 }
2648 }
2649 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002650*/
Craig Mautner76ea2242013-05-15 11:40:05 -07002651 }
2652
Craig Mautner27084302013-03-25 08:05:25 -07002653 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002654 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity=");
Craig Mautner27084302013-03-25 08:05:25 -07002655 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002656 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002657 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002658 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2659 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2660 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002661 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07002662 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002663
Craig Mautner20e72272013-04-01 13:45:53 -07002664 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002665 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002666 }
2667
Dianne Hackborn390517b2013-05-30 15:03:32 -07002668 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2669 boolean needSep, String prefix) {
2670 if (activity != null) {
2671 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2672 if (needSep) {
2673 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002674 }
2675 pw.print(prefix);
2676 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002677 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002678 }
2679 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002680 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002681 }
2682
Craig Mautner8d341ef2013-03-26 09:03:27 -07002683 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2684 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002685 boolean printed = false;
2686 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002687 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2688 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
2689 pw.print("Display #"); pw.println(activityDisplay.mDisplayId);
2690 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002691 final int numStacks = stacks.size();
2692 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2693 final ActivityStack stack = stacks.get(stackNdx);
2694 StringBuilder stackHeader = new StringBuilder(128);
2695 stackHeader.append(" Stack #");
2696 stackHeader.append(stack.mStackId);
2697 stackHeader.append(":");
2698 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2699 needSep, stackHeader.toString());
2700 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2701 !dumpAll, false, dumpPackage, true,
2702 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002703
Craig Mautner4a1cb222013-12-04 16:14:06 -08002704 needSep = printed;
2705 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2706 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002707 if (pr) {
2708 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002709 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002710 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002711 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2712 " mResumedActivity: ");
2713 if (pr) {
2714 printed = true;
2715 needSep = false;
2716 }
2717 if (dumpAll) {
2718 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2719 " mLastPausedActivity: ");
2720 if (pr) {
2721 printed = true;
2722 needSep = true;
2723 }
2724 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2725 needSep, " mLastNoHistoryActivity: ");
2726 }
2727 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002728 }
2729 }
2730
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002731 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2732 false, dumpPackage, true, " Activities waiting to finish:", null);
2733 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2734 false, dumpPackage, true, " Activities waiting to stop:", null);
2735 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2736 false, dumpPackage, true, " Activities waiting for another to become visible:",
2737 null);
2738 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2739 false, dumpPackage, true, " Activities waiting to sleep:", null);
2740 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2741 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002742
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002743 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002744 }
2745
Dianne Hackborn390517b2013-05-30 15:03:32 -07002746 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002747 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002748 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002749 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002750 String innerPrefix = null;
2751 String[] args = null;
2752 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002753 for (int i=list.size()-1; i>=0; i--) {
2754 final ActivityRecord r = list.get(i);
2755 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2756 continue;
2757 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002758 if (innerPrefix == null) {
2759 innerPrefix = prefix + " ";
2760 args = new String[0];
2761 }
2762 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002763 final boolean full = !brief && (complete || !r.isInHistory());
2764 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002765 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002766 needNL = false;
2767 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002768 if (header1 != null) {
2769 pw.println(header1);
2770 header1 = null;
2771 }
2772 if (header2 != null) {
2773 pw.println(header2);
2774 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002775 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002776 if (lastTask != r.task) {
2777 lastTask = r.task;
2778 pw.print(prefix);
2779 pw.print(full ? "* " : " ");
2780 pw.println(lastTask);
2781 if (full) {
2782 lastTask.dump(pw, prefix + " ");
2783 } else if (complete) {
2784 // Complete + brief == give a summary. Isn't that obvious?!?
2785 if (lastTask.intent != null) {
2786 pw.print(prefix); pw.print(" ");
2787 pw.println(lastTask.intent.toInsecureStringWithClip());
2788 }
2789 }
2790 }
2791 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2792 pw.print(" #"); pw.print(i); pw.print(": ");
2793 pw.println(r);
2794 if (full) {
2795 r.dump(pw, innerPrefix);
2796 } else if (complete) {
2797 // Complete + brief == give a summary. Isn't that obvious?!?
2798 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2799 if (r.app != null) {
2800 pw.print(innerPrefix); pw.println(r.app);
2801 }
2802 }
2803 if (client && r.app != null && r.app.thread != null) {
2804 // flush anything that is already in the PrintWriter since the thread is going
2805 // to write to the file descriptor directly
2806 pw.flush();
2807 try {
2808 TransferPipe tp = new TransferPipe();
2809 try {
2810 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2811 r.appToken, innerPrefix, args);
2812 // Short timeout, since blocking here can
2813 // deadlock with the application.
2814 tp.go(fd, 2000);
2815 } finally {
2816 tp.kill();
2817 }
2818 } catch (IOException e) {
2819 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2820 } catch (RemoteException e) {
2821 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2822 }
2823 needNL = true;
2824 }
2825 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002826 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002827 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002828
Craig Mautnerf3333272013-04-22 10:55:53 -07002829 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002830 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002831 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2832 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002833 }
2834
2835 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002836 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002837 }
2838
2839 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002840 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002841 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2842 }
2843
Craig Mautner05d29032013-05-03 13:40:13 -07002844 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002845 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2846 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2847 }
Craig Mautner05d29032013-05-03 13:40:13 -07002848 }
2849
Craig Mautner0eea92c2013-05-16 13:35:39 -07002850 void removeSleepTimeouts() {
2851 mSleepTimeout = false;
2852 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2853 }
2854
2855 final void scheduleSleepTimeout() {
2856 removeSleepTimeouts();
2857 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2858 }
2859
Craig Mautner4a1cb222013-12-04 16:14:06 -08002860 @Override
2861 public void onDisplayAdded(int displayId) {
2862 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
2863 }
2864
2865 @Override
2866 public void onDisplayRemoved(int displayId) {
2867 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
2868 }
2869
2870 @Override
2871 public void onDisplayChanged(int displayId) {
2872 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
2873 }
2874
2875 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08002876 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002877 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08002878 newDisplay = mActivityDisplays.get(displayId) == null;
2879 if (newDisplay) {
2880 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
2881 mActivityDisplays.put(displayId, activityDisplay);
2882 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002883 }
Craig Mautner4504de52013-12-20 09:06:56 -08002884 if (newDisplay) {
2885 mWindowManager.onDisplayAdded(displayId);
2886 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002887 }
2888
2889 public void handleDisplayRemovedLocked(int displayId) {
2890 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002891 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2892 if (activityDisplay != null) {
2893 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002894 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002895 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002896 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002897 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002898 }
2899 }
2900 mWindowManager.onDisplayRemoved(displayId);
2901 }
2902
2903 public void handleDisplayChangedLocked(int displayId) {
2904 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002905 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2906 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002907 // TODO: Update the bounds.
2908 }
2909 }
2910 mWindowManager.onDisplayChanged(displayId);
2911 }
2912
2913 StackInfo getStackInfo(ActivityStack stack) {
2914 StackInfo info = new StackInfo();
2915 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
2916 info.displayId = Display.DEFAULT_DISPLAY;
2917 info.stackId = stack.mStackId;
2918
2919 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2920 final int numTasks = tasks.size();
2921 int[] taskIds = new int[numTasks];
2922 String[] taskNames = new String[numTasks];
2923 for (int i = 0; i < numTasks; ++i) {
2924 final TaskRecord task = tasks.get(i);
2925 taskIds[i] = task.taskId;
2926 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
2927 : task.realActivity != null ? task.realActivity.flattenToString()
2928 : task.getTopActivity() != null ? task.getTopActivity().packageName
2929 : "unknown";
2930 }
2931 info.taskIds = taskIds;
2932 info.taskNames = taskNames;
2933 return info;
2934 }
2935
2936 StackInfo getStackInfoLocked(int stackId) {
2937 ActivityStack stack = getStack(stackId);
2938 if (stack != null) {
2939 return getStackInfo(stack);
2940 }
2941 return null;
2942 }
2943
2944 ArrayList<StackInfo> getAllStackInfosLocked() {
2945 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002946 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2947 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002948 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
2949 list.add(getStackInfo(stacks.get(ndx)));
2950 }
2951 }
2952 return list;
2953 }
2954
Craig Mautneraea74a52014-03-08 14:23:10 -08002955 void setLockTaskModeLocked(TaskRecord task) {
2956 if (task == null) {
2957 // Take out of lock task mode.
2958 mLockTaskModeTask = null;
2959 return;
2960 }
2961 if (isLockTaskModeViolation(task)) {
2962 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
2963 return;
2964 }
2965 mLockTaskModeTask = task;
2966 findTaskToMoveToFrontLocked(task, 0, null);
2967 resumeTopActivitiesLocked();
2968 }
2969
2970 boolean isLockTaskModeViolation(TaskRecord task) {
2971 return mLockTaskModeTask != null && mLockTaskModeTask != task;
2972 }
2973
2974 void endLockTaskModeIfTaskEnding(TaskRecord task) {
2975 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
2976 mLockTaskModeTask = null;
2977 }
2978 }
2979
2980 boolean isInLockTaskMode() {
2981 return mLockTaskModeTask != null;
2982 }
2983
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002984 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07002985
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002986 public ActivityStackSupervisorHandler(Looper looper) {
2987 super(looper);
2988 }
2989
Craig Mautnerf3333272013-04-22 10:55:53 -07002990 void activityIdleInternal(ActivityRecord r) {
2991 synchronized (mService) {
2992 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
2993 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002994 }
2995
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002996 @Override
2997 public void handleMessage(Message msg) {
2998 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002999 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003000 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003001 if (mService.mDidDexOpt) {
3002 mService.mDidDexOpt = false;
3003 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3004 nmsg.obj = msg.obj;
3005 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3006 return;
3007 }
3008 // We don't at this point know if the activity is fullscreen,
3009 // so we need to be conservative and assume it isn't.
3010 activityIdleInternal((ActivityRecord)msg.obj);
3011 } break;
3012 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003013 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003014 activityIdleInternal((ActivityRecord)msg.obj);
3015 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003016 case RESUME_TOP_ACTIVITY_MSG: {
3017 synchronized (mService) {
3018 resumeTopActivitiesLocked();
3019 }
3020 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003021 case SLEEP_TIMEOUT_MSG: {
3022 synchronized (mService) {
3023 if (mService.isSleepingOrShuttingDown()) {
3024 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3025 mSleepTimeout = true;
3026 checkReadyForSleepLocked();
3027 }
3028 }
3029 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003030 case LAUNCH_TIMEOUT_MSG: {
3031 if (mService.mDidDexOpt) {
3032 mService.mDidDexOpt = false;
3033 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3034 return;
3035 }
3036 synchronized (mService) {
3037 if (mLaunchingActivity.isHeld()) {
3038 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3039 if (VALIDATE_WAKE_LOCK_CALLER
3040 && Binder.getCallingUid() != Process.myUid()) {
3041 throw new IllegalStateException("Calling must be system uid");
3042 }
3043 mLaunchingActivity.release();
3044 }
3045 }
3046 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003047 case HANDLE_DISPLAY_ADDED: {
3048 handleDisplayAddedLocked(msg.arg1);
3049 } break;
3050 case HANDLE_DISPLAY_CHANGED: {
3051 handleDisplayChangedLocked(msg.arg1);
3052 } break;
3053 case HANDLE_DISPLAY_REMOVED: {
3054 handleDisplayRemovedLocked(msg.arg1);
3055 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003056 case CONTAINER_CALLBACK_VISIBILITY: {
3057 final ActivityContainer container = (ActivityContainer) msg.obj;
3058 try {
3059 // We only send this message if mCallback is non-null.
3060 container.mCallback.setVisible(container.asBinder(), msg.arg1 == 1);
3061 } catch (RemoteException e) {
3062 }
3063 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003064 }
3065 }
3066 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003067
Ying Wangb081a592014-04-22 15:20:16 -07003068 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003069 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
3070 Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003071 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003072 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003073 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003074 ActivityRecord mParentActivity = null;
3075 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003076
Craig Mautnere3a00d72014-04-16 08:31:19 -07003077 boolean mVisible = true;
3078
Craig Mautner4a1cb222013-12-04 16:14:06 -08003079 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003080 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003081
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003082 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3083 final static int CONTAINER_STATE_NO_SURFACE = 1;
3084 final static int CONTAINER_STATE_FINISHING = 2;
3085 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3086
3087 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003088 synchronized (mService) {
3089 mStackId = stackId;
3090 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003091 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003092 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003093 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003094 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003095
Craig Mautnere0a38842013-12-16 16:14:02 -08003096 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003097 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3098 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003099 mActivityDisplay = activityDisplay;
3100 mStack.mDisplayId = activityDisplay.mDisplayId;
3101 mStack.mStacks = activityDisplay.mStacks;
3102
3103 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003104 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003105 }
3106
3107 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003108 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003109 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003110 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3111 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003112 return;
3113 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003114 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003115 }
3116 }
3117
3118 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003119 public int getDisplayId() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003120 if (mActivityDisplay != null) {
3121 return mActivityDisplay.mDisplayId;
3122 }
3123 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003124 }
3125
Jeff Brownca9bc702014-02-11 14:32:56 -08003126 @Override
3127 public boolean injectEvent(InputEvent event) {
3128 final long origId = Binder.clearCallingIdentity();
3129 try {
3130 if (mActivityDisplay != null) {
3131 return mInputManagerInternal.injectInputEvent(event,
3132 mActivityDisplay.mDisplayId,
3133 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3134 }
3135 return false;
3136 } finally {
3137 Binder.restoreCallingIdentity(origId);
3138 }
3139 }
3140
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003141 @Override
3142 public void release() {
3143 mContainerState = CONTAINER_STATE_FINISHING;
3144 mStack.finishAllActivitiesLocked();
3145 detachLocked();
3146 mWindowManager.removeStack(mStackId);
3147 }
3148
Craig Mautner34b73df2014-01-12 21:11:08 -08003149 private void detachLocked() {
3150 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3151 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003152 if (mActivityDisplay != null) {
3153 mActivityDisplay.detachActivitiesLocked(mStack);
3154 mActivityDisplay = null;
3155 mStack.mDisplayId = -1;
3156 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003157 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003158 }
3159 }
3160
3161 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003162 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003163 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003164 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3165 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3166 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003167 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003168 String mimeType = intent.getType();
3169 if (mimeType == null && intent.getData() != null
3170 && "content".equals(intent.getData().getScheme())) {
3171 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3172 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003173 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0, 0, null,
Craig Mautnere0a38842013-12-16 16:14:02 -08003174 null, null, null, null, userId, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003175 }
3176
3177 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003178 public final int startActivityIntentSender(IIntentSender intentSender) {
3179 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3180
3181 if (!(intentSender instanceof PendingIntentRecord)) {
3182 throw new IllegalArgumentException("Bad PendingIntent object");
3183 }
3184
3185 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003186 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003187 }
3188
Craig Mautner247ab652014-04-25 10:09:00 -07003189 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3190 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3191 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3192 if (resolvedType == null) {
3193 resolvedType = intent.getType();
3194 if (resolvedType == null && intent.getData() != null
3195 && "content".equals(intent.getData().getScheme())) {
3196 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3197 }
3198 }
3199 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003200 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003201 throw new SecurityException(
3202 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3203 }
3204 }
3205
3206 /** Throw a SecurityException if allowEmbedded is not true */
3207 @Override
3208 public final void checkEmbeddedAllowed(Intent intent) {
3209 checkEmbeddedAllowedInner(intent, null);
3210 }
3211
3212 /** Throw a SecurityException if allowEmbedded is not true */
3213 @Override
3214 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3215 if (!(intentSender instanceof PendingIntentRecord)) {
3216 throw new IllegalArgumentException("Bad PendingIntent object");
3217 }
3218 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3219 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3220 pendingIntent.key.requestResolvedType);
3221 }
3222
Craig Mautnerdf88d732014-01-27 09:21:32 -08003223 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003224 public IBinder asBinder() {
3225 return this;
3226 }
3227
Craig Mautner4504de52013-12-20 09:06:56 -08003228 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003229 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003230 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003231 }
3232
Craig Mautner4a1cb222013-12-04 16:14:06 -08003233 ActivityStackSupervisor getOuter() {
3234 return ActivityStackSupervisor.this;
3235 }
3236
3237 boolean isAttached() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003238 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003239 }
3240
3241 void getBounds(Point outBounds) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003242 if (mActivityDisplay != null) {
3243 mActivityDisplay.getBounds(outBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003244 } else {
3245 outBounds.set(0, 0);
3246 }
3247 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003248
Craig Mautner6985bad2014-04-21 15:22:06 -07003249 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003250 void setVisible(boolean visible) {
3251 if (mVisible != visible) {
3252 mVisible = visible;
3253 if (mCallback != null) {
3254 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3255 0 /* unused */, this).sendToTarget();
3256 }
3257 }
3258 }
3259
Craig Mautner6985bad2014-04-21 15:22:06 -07003260 void setDrawn() {
3261 }
3262
Craig Mautnerb539b3a2014-05-26 15:06:32 -07003263 // You can always start a new task on a regular ActivityStack.
3264 boolean isEligibleForNewTasks() {
3265 return true;
3266 }
3267
Craig Mautner34b73df2014-01-12 21:11:08 -08003268 @Override
3269 public String toString() {
3270 return mIdString + (mActivityDisplay == null ? "N" : "A");
3271 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003272 }
3273
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003274 private class VirtualActivityContainer extends ActivityContainer {
3275 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003276 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003277
3278 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3279 super(getNextStackId());
3280 mParentActivity = parent;
3281 mCallback = callback;
3282 mContainerState = CONTAINER_STATE_NO_SURFACE;
3283 mIdString = "VirtualActivtyContainer{" + mStackId + ", parent=" + mParentActivity + "}";
3284 }
3285
3286 @Override
3287 public void setSurface(Surface surface, int width, int height, int density) {
3288 super.setSurface(surface, width, height, density);
3289
3290 synchronized (mService) {
3291 final long origId = Binder.clearCallingIdentity();
3292 try {
3293 setSurfaceLocked(surface, width, height, density);
3294 } finally {
3295 Binder.restoreCallingIdentity(origId);
3296 }
3297 }
3298 }
3299
3300 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3301 if (mContainerState == CONTAINER_STATE_FINISHING) {
3302 return;
3303 }
3304 VirtualActivityDisplay virtualActivityDisplay =
3305 (VirtualActivityDisplay) mActivityDisplay;
3306 if (virtualActivityDisplay == null) {
3307 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003308 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003309 mActivityDisplay = virtualActivityDisplay;
3310 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3311 attachToDisplayLocked(virtualActivityDisplay);
3312 }
3313
3314 if (mSurface != null) {
3315 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003316 }
3317
Craig Mautner6985bad2014-04-21 15:22:06 -07003318 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003319 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003320 mStack.resumeTopActivityLocked(null);
3321 } else {
3322 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003323 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003324 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
3325 mStack.startPausingLocked(false, true);
3326 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003327 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003328
3329 setSurfaceIfReady();
3330
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003331 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3332 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003333 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003334
Craig Mautner6985bad2014-04-21 15:22:06 -07003335 @Override
Craig Mautnerd13a5582014-05-05 12:07:40 -07003336 boolean isAttached() {
3337 return mSurface != null && super.isAttached();
3338 }
3339
3340 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003341 void setDrawn() {
3342 synchronized (mService) {
3343 mDrawn = true;
3344 setSurfaceIfReady();
3345 }
3346 }
3347
Craig Mautnerb539b3a2014-05-26 15:06:32 -07003348 // Never start a new task on an ActivityView if it isn't explicitly specified.
3349 @Override
3350 boolean isEligibleForNewTasks() {
3351 return false;
3352 }
3353
Craig Mautner6985bad2014-04-21 15:22:06 -07003354 private void setSurfaceIfReady() {
3355 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReady: mDrawn=" + mDrawn +
3356 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3357 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3358 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3359 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3360 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003361 }
3362 }
3363
Craig Mautner4a1cb222013-12-04 16:14:06 -08003364 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3365 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003366 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003367 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003368 int mDisplayId;
3369 Display mDisplay;
3370 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003371
Craig Mautner4a1cb222013-12-04 16:14:06 -08003372 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3373 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003374 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003375
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003376 ActivityDisplay() {
3377 }
Craig Mautner4504de52013-12-20 09:06:56 -08003378
Craig Mautnere0a38842013-12-16 16:14:02 -08003379 ActivityDisplay(int displayId) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003380 init(mDisplayManager.getDisplay(displayId));
Craig Mautner4504de52013-12-20 09:06:56 -08003381 }
3382
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003383 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003384 mDisplay = display;
3385 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003386 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003387 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003388
3389 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003390 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3391 + mDisplayId);
3392 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003393 }
3394
Craig Mautnere0a38842013-12-16 16:14:02 -08003395 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003396 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003397 + " from displayId=" + mDisplayId);
3398 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003399 }
3400
3401 void getBounds(Point bounds) {
3402 mDisplay.getDisplayInfo(mDisplayInfo);
3403 bounds.x = mDisplayInfo.appWidth;
3404 bounds.y = mDisplayInfo.appHeight;
3405 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003406
3407 @Override
3408 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003409 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3410 }
3411 }
3412
3413 class VirtualActivityDisplay extends ActivityDisplay {
3414 VirtualDisplay mVirtualDisplay;
3415
Craig Mautner6985bad2014-04-21 15:22:06 -07003416 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003417 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
3418 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, VIRTUAL_DISPLAY_BASE_NAME,
Craig Mautner6985bad2014-04-21 15:22:06 -07003419 width, height, density, null, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003420 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY);
3421
3422 init(mVirtualDisplay.getDisplay());
3423
3424 mWindowManager.handleDisplayAdded(mDisplayId);
3425 }
3426
3427 void setSurface(Surface surface) {
3428 if (mVirtualDisplay != null) {
3429 mVirtualDisplay.setSurface(surface);
3430 }
3431 }
3432
3433 @Override
3434 void detachActivitiesLocked(ActivityStack stack) {
3435 super.detachActivitiesLocked(stack);
3436 if (mVirtualDisplay != null) {
3437 mVirtualDisplay.release();
3438 mVirtualDisplay = null;
3439 }
3440 }
3441
3442 @Override
3443 public String toString() {
3444 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003445 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003446 }
Jose Lima58e66d62014-05-27 20:00:27 -07003447
3448 private boolean isLeanbackOnlyDevice() {
3449 boolean onLeanbackOnly = false;
3450 try {
3451 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
3452 PackageManager.FEATURE_LEANBACK_ONLY);
3453 } catch (RemoteException e) {
3454 // noop
3455 }
3456
3457 return onLeanbackOnly;
3458 }
Craig Mautner27084302013-03-25 08:05:25 -07003459}