blob: 6f2996c735016b348cf534625ad2821903bd0a47 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner29219d92013-04-16 20:19:12 -070020import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070022import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Craig Mautner2420ead2013-04-01 17:13:20 -070023import static com.android.server.am.ActivityManagerService.localLOGV;
Craig Mautner23ac33b2013-04-01 16:26:35 -070024import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070025import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Craig Mautner6170f732013-04-02 13:05:23 -070027import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070029import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070030import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
31import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070032import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070033import static com.android.server.am.ActivityManagerService.TAG;
34
Craig Mautner2420ead2013-04-01 17:13:20 -070035import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070036import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080037import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070038import android.app.ActivityOptions;
39import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080040import android.app.IActivityContainer;
41import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070042import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.PendingIntent;
Craig Mautner20e72272013-04-01 13:45:53 -070045import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070047import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040048import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040049import android.app.admin.DevicePolicyManager;
50import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070052import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070054import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070056import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.pm.ApplicationInfo;
58import android.content.pm.PackageManager;
59import android.content.pm.ResolveInfo;
60import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080061import android.graphics.Point;
62import android.hardware.display.DisplayManager;
63import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080064import android.hardware.display.DisplayManagerGlobal;
65import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080066import android.hardware.input.InputManager;
67import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070068import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070069import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070070import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070071import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070072import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070073import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070074import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070075import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070076import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070077import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040079import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070081import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040082import android.provider.Settings;
83import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070084import android.service.voice.IVoiceInteractionSession;
Craig Mautner2420ead2013-04-01 17:13:20 -070085import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070086import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080087import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070088
Craig Mautner4a1cb222013-12-04 16:14:06 -080089import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080090import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080091import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080092import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080093import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070094import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070095import com.android.internal.app.IVoiceInteractor;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070096import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040097import com.android.internal.statusbar.IStatusBarService;
Jeff Brownca9bc702014-02-11 14:32:56 -080098import com.android.server.LocalServices;
Craig Mautner6170f732013-04-02 13:05:23 -070099import com.android.server.am.ActivityManagerService.PendingActivityLaunch;
Craig Mautner2420ead2013-04-01 17:13:20 -0700100import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700101import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102
justinzhang5286d3f2014-05-12 17:06:01 -0400103
Craig Mautner8d341ef2013-03-26 09:03:27 -0700104import java.io.FileDescriptor;
105import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700106import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700107import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700108import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700109
Craig Mautner4a1cb222013-12-04 16:14:06 -0800110public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700111 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
112 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
113 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700114 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700115 static final boolean DEBUG_IDLE = DEBUG || false;
116 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
117 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
118 static final boolean DEBUG_STATES = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700119
Craig Mautner2219a1b2013-03-25 09:44:30 -0700120 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700121
Craig Mautnerf3333272013-04-22 10:55:53 -0700122 /** How long we wait until giving up on the last activity telling us it is idle. */
123 static final int IDLE_TIMEOUT = 10*1000;
124
Craig Mautner0eea92c2013-05-16 13:35:39 -0700125 /** How long we can hold the sleep wake lock before giving up. */
126 static final int SLEEP_TIMEOUT = 5*1000;
127
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700128 // How long we can hold the launch wake lock before giving up.
129 static final int LAUNCH_TIMEOUT = 10*1000;
130
Craig Mautner05d29032013-05-03 13:40:13 -0700131 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
132 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
133 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700134 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700135 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800136 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
137 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
138 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700139 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400140 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
141 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700142 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700143 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800144
Craig Mautner4504de52013-12-20 09:06:56 -0800145 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700146
Jason Monk62515be2014-05-21 16:06:19 -0400147 private static final String LOCK_TASK_TAG = "Lock-to-App";
148
justinzhang5286d3f2014-05-12 17:06:01 -0400149 /** Status Bar Service **/
150 private IBinder mToken = new Binder();
151 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400152 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400153
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700154 // For debugging to make sure the caller when acquiring/releasing our
155 // wake lock is the system process.
156 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700157
Craig Mautner27084302013-03-25 08:05:25 -0700158 final ActivityManagerService mService;
159
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700160 final ActivityStackSupervisorHandler mHandler;
161
162 /** Short cut */
163 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800164 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700165
Craig Mautner27084302013-03-25 08:05:25 -0700166 /** Dismiss the keyguard after the next activity is displayed? */
Craig Mautner5314a402013-09-26 12:40:16 -0700167 boolean mDismissKeyguardOnNextActivity = false;
Craig Mautner27084302013-03-25 08:05:25 -0700168
Craig Mautner8d341ef2013-03-26 09:03:27 -0700169 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700170 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700171
172 /** Task identifier that activities are currently being started in. Incremented each time a
173 * new task is created. */
174 private int mCurTaskId = 0;
175
Craig Mautner2420ead2013-04-01 17:13:20 -0700176 /** The current user */
177 private int mCurrentUser;
178
Craig Mautnere0a38842013-12-16 16:14:02 -0800179 /** The stack containing the launcher app. Assumed to always be attached to
180 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700181 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700182
Craig Mautnere0a38842013-12-16 16:14:02 -0800183 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700184 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700185
Craig Mautner4a1cb222013-12-04 16:14:06 -0800186 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
187 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800188 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800189 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700190
191 /** List of activities that are waiting for a new activity to become visible before completing
192 * whatever operation they are supposed to do. */
193 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
194
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700195 /** List of processes waiting to find out about the next visible activity. */
196 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
197 new ArrayList<IActivityManager.WaitResult>();
198
199 /** List of processes waiting to find out about the next launched activity. */
200 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
201 new ArrayList<IActivityManager.WaitResult>();
202
Craig Mautnerde4ef022013-04-07 19:01:33 -0700203 /** List of activities that are ready to be stopped, but waiting for the next activity to
204 * settle down before doing so. */
205 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
206
Craig Mautnerf3333272013-04-22 10:55:53 -0700207 /** List of activities that are ready to be finished, but waiting for the previous activity to
208 * settle down before doing so. It contains ActivityRecord objects. */
209 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
210
Craig Mautner0eea92c2013-05-16 13:35:39 -0700211 /** List of activities that are in the process of going to sleep. */
212 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
213
Craig Mautnerf3333272013-04-22 10:55:53 -0700214 /** Used on user changes */
215 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
216
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700217 /** Used to queue up any background users being started */
218 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
219
Craig Mautnerde4ef022013-04-07 19:01:33 -0700220 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
221 * is being brought in front of us. */
222 boolean mUserLeaving = false;
223
Craig Mautner0eea92c2013-05-16 13:35:39 -0700224 /** Set when we have taken too long waiting to go to sleep. */
225 boolean mSleepTimeout = false;
226
Jose Lima58e66d62014-05-27 20:00:27 -0700227 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
228 * setWindowManager is called. **/
229 private boolean mLeanbackOnlyDevice;
230
Craig Mautner0eea92c2013-05-16 13:35:39 -0700231 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700232 * We don't want to allow the device to go to sleep while in the process
233 * of launching an activity. This is primarily to allow alarm intent
234 * receivers to launch an activity and get that to run before the device
235 * goes back to sleep.
236 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700237 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700238
239 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700240 * Set when the system is going to sleep, until we have
241 * successfully paused the current activity and released our wake lock.
242 * At that point the system is allowed to actually sleep.
243 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700244 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700245
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700246 /** Stack id of the front stack when user switched, indexed by userId. */
247 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700248
Craig Mautner4504de52013-12-20 09:06:56 -0800249 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800250 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700251 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800252
253 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700254 private final SparseArray<ActivityDisplay> mActivityDisplays =
255 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800256
Jeff Brownca9bc702014-02-11 14:32:56 -0800257 InputManagerInternal mInputManagerInternal;
258
Craig Mautneraea74a52014-03-08 14:23:10 -0800259 /** If non-null then the task specified remains in front and no other tasks may be started
260 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400261 TaskRecord mLockTaskModeTask;
Jason Monk62515be2014-05-21 16:06:19 -0400262 /**
263 * Notifies the user when entering/exiting lock-task.
264 */
265 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800266
Craig Mautner4a1cb222013-12-04 16:14:06 -0800267 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700268 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700269 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
270 }
271
272 /**
273 * At the time when the constructor runs, the power manager has not yet been
274 * initialized. So we initialize our wakelocks afterwards.
275 */
276 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800277 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700278 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700279 mLaunchingActivity =
280 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
281 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700282 }
283
justinzhang5286d3f2014-05-12 17:06:01 -0400284 // This function returns a IStatusBarService. The value is from ServiceManager.
285 // getService and is cached.
286 private IStatusBarService getStatusBarService() {
287 synchronized (mService) {
288 if (mStatusBarService == null) {
289 mStatusBarService = IStatusBarService.Stub.asInterface(
290 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
291 if (mStatusBarService == null) {
292 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
293 }
294 }
295 return mStatusBarService;
296 }
297 }
298
Jason Monk35c62a42014-06-17 10:24:47 -0400299 private IDevicePolicyManager getDevicePolicyManager() {
300 synchronized (mService) {
301 if (mDevicePolicyManager == null) {
302 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
303 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
304 if (mDevicePolicyManager == null) {
305 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
306 }
307 }
308 return mDevicePolicyManager;
309 }
310 }
311
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700312 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800313 synchronized (mService) {
314 mWindowManager = wm;
315
316 mDisplayManager =
317 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
318 mDisplayManager.registerDisplayListener(this, null);
319
320 Display[] displays = mDisplayManager.getDisplays();
321 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
322 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800323 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
324 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800325 }
326
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700327 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800328 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800329
330 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700331
332 // Initialize this here, now that we can get a valid reference to PackageManager.
333 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800334 }
Craig Mautner27084302013-03-25 08:05:25 -0700335 }
336
337 void dismissKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700338 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner27084302013-03-25 08:05:25 -0700339 if (mDismissKeyguardOnNextActivity) {
340 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700341 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700342 }
343 }
344
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700345 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800346 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700347 }
348
Craig Mautnerde4ef022013-04-07 19:01:33 -0700349 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800350 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700351 }
352
Craig Mautner4a1cb222013-12-04 16:14:06 -0800353 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
354 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700355 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800356 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
357 if (parent != null) {
358 stack = parent.task.stack;
359 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800360 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800361 if (stacks != null && !stacks.isEmpty()) {
362 return stack == stacks.get(stacks.size() - 1);
363 }
364 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700365 }
366
Craig Mautnerde4ef022013-04-07 19:01:33 -0700367 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800368 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800369 int topNdx = stacks.size() - 1;
370 if (topNdx <= 0) {
371 return;
372 }
373 ActivityStack topStack = stacks.get(topNdx);
374 final boolean homeInFront = topStack == mHomeStack;
375 if (homeInFront != toFront) {
376 mLastFocusedStack = topStack;
377 stacks.remove(mHomeStack);
378 stacks.add(toFront ? topNdx : 0, mHomeStack);
379 mFocusedStack = stacks.get(topNdx);
380 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
381 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700382 }
383 }
384
Craig Mautner719e6212014-05-29 16:49:41 +0000385 void moveHomeToTop() {
Craig Mautner69ada552013-04-18 13:51:51 -0700386 moveHomeStack(true);
Craig Mautner719e6212014-05-29 16:49:41 +0000387 mHomeStack.moveHomeTaskToTop();
Craig Mautner8e569572013-10-11 17:36:59 -0700388 }
389
Craig Mautner719e6212014-05-29 16:49:41 +0000390 boolean resumeHomeActivity(ActivityRecord prev) {
391 moveHomeToTop();
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700392 if (prev != null) {
Craig Mautner719e6212014-05-29 16:49:41 +0000393 prev.task.mOnTopOfHome = false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700394 }
Craig Mautnera8a90e02013-06-28 15:24:50 -0700395 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner719e6212014-05-29 16:49:41 +0000396 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700397 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700398 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700399 }
400 return mService.startHomeActivityLocked(mCurrentUser);
401 }
402
Craig Mautner27084302013-03-25 08:05:25 -0700403 void setDismissKeyguard(boolean dismiss) {
Craig Mautner5314a402013-09-26 12:40:16 -0700404 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen(" dismiss=" + dismiss);
Craig Mautner27084302013-03-25 08:05:25 -0700405 mDismissKeyguardOnNextActivity = dismiss;
406 }
407
Craig Mautner8d341ef2013-03-26 09:03:27 -0700408 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800409 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800410 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800411 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800412 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
413 ActivityStack stack = stacks.get(stackNdx);
414 TaskRecord task = stack.taskForIdLocked(id);
415 if (task != null) {
416 return task;
417 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700418 }
419 }
420 return null;
421 }
422
Craig Mautner6170f732013-04-02 13:05:23 -0700423 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800424 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800425 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800426 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800427 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
428 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
429 if (r != null) {
430 return r;
431 }
Craig Mautner6170f732013-04-02 13:05:23 -0700432 }
433 }
434 return null;
435 }
436
Craig Mautneref73ee12014-04-23 11:45:37 -0700437 void setNextTaskId(int taskId) {
438 if (taskId > mCurTaskId) {
439 mCurTaskId = taskId;
440 }
441 }
442
Craig Mautner8d341ef2013-03-26 09:03:27 -0700443 int getNextTaskId() {
444 do {
445 mCurTaskId++;
446 if (mCurTaskId <= 0) {
447 mCurTaskId = 1;
448 }
449 } while (anyTaskForIdLocked(mCurTaskId) != null);
450 return mCurTaskId;
451 }
452
Craig Mautnerde4ef022013-04-07 19:01:33 -0700453 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700454 ActivityStack stack = getFocusedStack();
455 if (stack == null) {
456 return null;
457 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700458 ActivityRecord resumedActivity = stack.mResumedActivity;
459 if (resumedActivity == null || resumedActivity.app == null) {
460 resumedActivity = stack.mPausingActivity;
461 if (resumedActivity == null || resumedActivity.app == null) {
462 resumedActivity = stack.topRunningActivityLocked(null);
463 }
464 }
465 return resumedActivity;
466 }
467
Mike Lockwooded8902d2013-11-15 11:01:47 -0800468 boolean attachApplicationLocked(ProcessRecord app) throws Exception {
Craig Mautner20e72272013-04-01 13:45:53 -0700469 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800470 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800471 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
472 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800473 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
474 final ActivityStack stack = stacks.get(stackNdx);
475 if (!isFrontStack(stack)) {
476 continue;
477 }
478 ActivityRecord hr = stack.topRunningActivityLocked(null);
479 if (hr != null) {
480 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
481 && processName.equals(hr.processName)) {
482 try {
George Mount2c92c972014-03-20 09:38:23 -0700483 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800484 didSomething = true;
485 }
486 } catch (Exception e) {
487 Slog.w(TAG, "Exception in new application when starting activity "
488 + hr.intent.getComponent().flattenToShortString(), e);
489 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700490 }
Craig Mautner20e72272013-04-01 13:45:53 -0700491 }
Craig Mautner20e72272013-04-01 13:45:53 -0700492 }
493 }
494 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700495 if (!didSomething) {
496 ensureActivitiesVisibleLocked(null, 0);
497 }
Craig Mautner20e72272013-04-01 13:45:53 -0700498 return didSomething;
499 }
500
501 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800502 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
503 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800504 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
505 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800506 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800507 continue;
508 }
509 final ActivityRecord resumedActivity = stack.mResumedActivity;
510 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800511 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
512 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800513 return false;
514 }
Craig Mautner20e72272013-04-01 13:45:53 -0700515 }
516 }
517 return true;
518 }
519
Craig Mautnerde4ef022013-04-07 19:01:33 -0700520 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800521 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
522 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800523 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
524 final ActivityStack stack = stacks.get(stackNdx);
525 if (isFrontStack(stack)) {
526 final ActivityRecord r = stack.mResumedActivity;
527 if (r != null && r.state != ActivityState.RESUMED) {
528 return false;
529 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700530 }
531 }
532 }
533 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800534 if (DEBUG_STACK) Slog.d(TAG,
535 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
536 mLastFocusedStack + " to=" + mFocusedStack);
537 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700538 return true;
539 }
540
541 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800542 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
543 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800544 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
545 final ActivityStack stack = stacks.get(stackNdx);
546 final ActivityRecord r = stack.mResumedActivity;
547 if (r != null && (!r.nowVisible || r.waitingVisible)) {
548 return false;
549 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700550 }
551 }
552 return true;
553 }
554
Craig Mautner2acc3892013-09-23 10:28:14 -0700555 /**
556 * Pause all activities in either all of the stacks or just the back stacks.
557 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700558 * @return true if any activity was paused as a result of this call.
559 */
Craig Mautner5314a402013-09-26 12:40:16 -0700560 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700561 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800562 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
563 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800564 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
565 final ActivityStack stack = stacks.get(stackNdx);
566 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
567 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
568 " mResumedActivity=" + stack.mResumedActivity);
569 stack.startPausingLocked(userLeaving, false);
570 someActivityPaused = true;
571 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700572 }
573 }
574 return someActivityPaused;
575 }
576
Craig Mautnerde4ef022013-04-07 19:01:33 -0700577 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700578 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800579 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
580 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800581 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
582 final ActivityStack stack = stacks.get(stackNdx);
583 final ActivityRecord r = stack.mPausingActivity;
584 if (r != null && r.state != ActivityState.PAUSED
585 && r.state != ActivityState.STOPPED
586 && r.state != ActivityState.STOPPING) {
587 if (DEBUG_STATES) {
588 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
589 pausing = false;
590 } else {
591 return false;
592 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700593 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700594 }
595 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700596 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700597 }
598
Craig Mautnerdf88d732014-01-27 09:21:32 -0800599 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping) {
John Spurlock8a985d22014-02-25 09:40:05 -0500600 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800601 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
602 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
603 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
604 final ActivityStack stack = stacks.get(stackNdx);
605 if (stack.mResumedActivity != null &&
606 stack.mActivityContainer.mParentActivity == parent) {
607 stack.startPausingLocked(userLeaving, uiSleeping);
608 }
609 }
610 }
611 }
612
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700613 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700614 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700615 WaitResult w = mWaitingActivityVisible.get(i);
616 w.timeout = false;
617 if (r != null) {
618 w.who = new ComponentName(r.info.packageName, r.info.name);
619 }
620 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
621 w.thisTime = w.totalTime;
622 }
623 mService.notifyAll();
624 dismissKeyguard();
625 }
626
627 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
628 long thisTime, long totalTime) {
629 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700630 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700631 w.timeout = timeout;
632 if (r != null) {
633 w.who = new ComponentName(r.info.packageName, r.info.name);
634 }
635 w.thisTime = thisTime;
636 w.totalTime = totalTime;
637 }
638 mService.notifyAll();
639 }
640
Craig Mautner29219d92013-04-16 20:19:12 -0700641 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700642 final ActivityStack focusedStack = getFocusedStack();
643 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
644 if (r != null) {
645 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700646 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700647
Craig Mautner4a1cb222013-12-04 16:14:06 -0800648 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800649 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
651 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700652 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700653 r = stack.topRunningActivityLocked(null);
654 if (r != null) {
655 return r;
656 }
657 }
658 }
659 return null;
660 }
661
Dianne Hackborn09233282014-04-30 11:33:59 -0700662 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700663 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
665 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800666 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800667 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800668 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800669 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
670 final ActivityStack stack = stacks.get(stackNdx);
671 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
672 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700673 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700674 }
675 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700676
677 // The lists are already sorted from most recent to oldest. Just pull the most recent off
678 // each list and add it to list. Stop when all lists are empty or maxNum reached.
679 while (maxNum > 0) {
680 long mostRecentActiveTime = Long.MIN_VALUE;
681 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800682 final int numTaskLists = runningTaskLists.size();
683 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
684 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700685 if (!stackTaskList.isEmpty()) {
686 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
687 if (lastActiveTime > mostRecentActiveTime) {
688 mostRecentActiveTime = lastActiveTime;
689 selectedStackList = stackTaskList;
690 }
691 }
692 }
693 if (selectedStackList != null) {
694 list.add(selectedStackList.remove(0));
695 --maxNum;
696 } else {
697 break;
698 }
699 }
Craig Mautner20e72272013-04-01 13:45:53 -0700700 }
701
Craig Mautner23ac33b2013-04-01 16:26:35 -0700702 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
703 String profileFile, ParcelFileDescriptor profileFd, int userId) {
704 // Collect information about the target of the Intent.
705 ActivityInfo aInfo;
706 try {
707 ResolveInfo rInfo =
708 AppGlobals.getPackageManager().resolveIntent(
709 intent, resolvedType,
710 PackageManager.MATCH_DEFAULT_ONLY
711 | ActivityManagerService.STOCK_PM_FLAGS, userId);
712 aInfo = rInfo != null ? rInfo.activityInfo : null;
713 } catch (RemoteException e) {
714 aInfo = null;
715 }
716
717 if (aInfo != null) {
718 // Store the found target back into the intent, because now that
719 // we have it we never want to do this again. For example, if the
720 // user navigates back to this point in the history, we should
721 // always restart the exact same activity.
722 intent.setComponent(new ComponentName(
723 aInfo.applicationInfo.packageName, aInfo.name));
724
725 // Don't debug things in the system process
726 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
727 if (!aInfo.processName.equals("system")) {
728 mService.setDebugApp(aInfo.processName, true, false);
729 }
730 }
731
732 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
733 if (!aInfo.processName.equals("system")) {
734 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
735 }
736 }
737
738 if (profileFile != null) {
739 if (!aInfo.processName.equals("system")) {
740 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
741 profileFile, profileFd,
742 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
743 }
744 }
745 }
746 return aInfo;
747 }
748
Craig Mautner2219a1b2013-03-25 09:44:30 -0700749 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner719e6212014-05-29 16:49:41 +0000750 moveHomeToTop();
Dianne Hackborn91097de2014-04-04 18:02:06 -0700751 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null, 0,
Craig Mautnere0a38842013-12-16 16:14:02 -0800752 null, false, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700753 }
754
Craig Mautner23ac33b2013-04-01 16:26:35 -0700755 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700756 String callingPackage, Intent intent, String resolvedType,
757 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
758 IBinder resultTo, String resultWho, int requestCode, int startFlags, String profileFile,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700759 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
Craig Mautnere0a38842013-12-16 16:14:02 -0800760 Bundle options, int userId, IActivityContainer iContainer) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700761 // Refuse possible leaked file descriptors
762 if (intent != null && intent.hasFileDescriptors()) {
763 throw new IllegalArgumentException("File descriptors passed in Intent");
764 }
765 boolean componentSpecified = intent.getComponent() != null;
766
767 // Don't modify the client's object!
768 intent = new Intent(intent);
769
770 // Collect information about the target of the Intent.
771 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
772 profileFile, profileFd, userId);
773
Craig Mautnere0a38842013-12-16 16:14:02 -0800774 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700775 synchronized (mService) {
776 int callingPid;
777 if (callingUid >= 0) {
778 callingPid = -1;
779 } else if (caller == null) {
780 callingPid = Binder.getCallingPid();
781 callingUid = Binder.getCallingUid();
782 } else {
783 callingPid = callingUid = -1;
784 }
785
Craig Mautnere0a38842013-12-16 16:14:02 -0800786 final ActivityStack stack;
787 if (container == null || container.mStack.isOnHomeDisplay()) {
788 stack = getFocusedStack();
789 } else {
790 stack = container.mStack;
791 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700792 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700793 && mService.mConfiguration.diff(config) != 0;
794 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700795 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700796
797 final long origId = Binder.clearCallingIdentity();
798
799 if (aInfo != null &&
800 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
801 // This may be a heavy-weight process! Check to see if we already
802 // have another, different heavy-weight process running.
803 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
804 if (mService.mHeavyWeightProcess != null &&
805 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
806 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700807 int realCallingUid = callingUid;
808 if (caller != null) {
809 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
810 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700811 realCallingUid = callerApp.info.uid;
812 } else {
813 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700814 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700815 + intent.toString());
816 ActivityOptions.abort(options);
817 return ActivityManager.START_PERMISSION_DENIED;
818 }
819 }
820
821 IIntentSender target = mService.getIntentSenderLocked(
822 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
823 realCallingUid, userId, null, null, 0, new Intent[] { intent },
824 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
825 | PendingIntent.FLAG_ONE_SHOT, null);
826
827 Intent newIntent = new Intent();
828 if (requestCode >= 0) {
829 // Caller is requesting a result.
830 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
831 }
832 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
833 new IntentSender(target));
834 if (mService.mHeavyWeightProcess.activities.size() > 0) {
835 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
836 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
837 hist.packageName);
838 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
839 hist.task.taskId);
840 }
841 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
842 aInfo.packageName);
843 newIntent.setFlags(intent.getFlags());
844 newIntent.setClassName("android",
845 HeavyWeightSwitcherActivity.class.getName());
846 intent = newIntent;
847 resolvedType = null;
848 caller = null;
849 callingUid = Binder.getCallingUid();
850 callingPid = Binder.getCallingPid();
851 componentSpecified = true;
852 try {
853 ResolveInfo rInfo =
854 AppGlobals.getPackageManager().resolveIntent(
855 intent, null,
856 PackageManager.MATCH_DEFAULT_ONLY
857 | ActivityManagerService.STOCK_PM_FLAGS, userId);
858 aInfo = rInfo != null ? rInfo.activityInfo : null;
859 aInfo = mService.getActivityInfoForUser(aInfo, userId);
860 } catch (RemoteException e) {
861 aInfo = null;
862 }
863 }
864 }
865 }
866
Dianne Hackborn91097de2014-04-04 18:02:06 -0700867 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
868 voiceSession, voiceInteractor, resultTo, resultWho,
Craig Mautnere0a38842013-12-16 16:14:02 -0800869 requestCode, callingPid, callingUid, callingPackage, startFlags, options,
870 componentSpecified, null, container);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700871
Craig Mautnerde4ef022013-04-07 19:01:33 -0700872 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700873 // If the caller also wants to switch to a new configuration,
874 // do so now. This allows a clean switch, as we are waiting
875 // for the current activity to pause (so we will not destroy
876 // it), and have not yet started the next activity.
877 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
878 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700879 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700880 if (DEBUG_CONFIGURATION) Slog.v(TAG,
881 "Updating to new configuration after starting activity.");
882 mService.updateConfigurationLocked(config, null, false, false);
883 }
884
885 Binder.restoreCallingIdentity(origId);
886
887 if (outResult != null) {
888 outResult.result = res;
889 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700890 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700891 do {
892 try {
893 mService.wait();
894 } catch (InterruptedException e) {
895 }
896 } while (!outResult.timeout && outResult.who == null);
897 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700898 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700899 if (r.nowVisible) {
900 outResult.timeout = false;
901 outResult.who = new ComponentName(r.info.packageName, r.info.name);
902 outResult.totalTime = 0;
903 outResult.thisTime = 0;
904 } else {
905 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700906 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700907 do {
908 try {
909 mService.wait();
910 } catch (InterruptedException e) {
911 }
912 } while (!outResult.timeout && outResult.who == null);
913 }
914 }
915 }
916
917 return res;
918 }
919 }
920
921 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
922 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
923 Bundle options, int userId) {
924 if (intents == null) {
925 throw new NullPointerException("intents is null");
926 }
927 if (resolvedTypes == null) {
928 throw new NullPointerException("resolvedTypes is null");
929 }
930 if (intents.length != resolvedTypes.length) {
931 throw new IllegalArgumentException("intents are length different than resolvedTypes");
932 }
933
Craig Mautner23ac33b2013-04-01 16:26:35 -0700934
935 int callingPid;
936 if (callingUid >= 0) {
937 callingPid = -1;
938 } else if (caller == null) {
939 callingPid = Binder.getCallingPid();
940 callingUid = Binder.getCallingUid();
941 } else {
942 callingPid = callingUid = -1;
943 }
944 final long origId = Binder.clearCallingIdentity();
945 try {
946 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700947 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700948 for (int i=0; i<intents.length; i++) {
949 Intent intent = intents[i];
950 if (intent == null) {
951 continue;
952 }
953
954 // Refuse possible leaked file descriptors
955 if (intent != null && intent.hasFileDescriptors()) {
956 throw new IllegalArgumentException("File descriptors passed in Intent");
957 }
958
959 boolean componentSpecified = intent.getComponent() != null;
960
961 // Don't modify the client's object!
962 intent = new Intent(intent);
963
964 // Collect information about the target of the Intent.
965 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
966 0, null, null, userId);
967 // TODO: New, check if this is correct
968 aInfo = mService.getActivityInfoForUser(aInfo, userId);
969
970 if (aInfo != null &&
971 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
972 != 0) {
973 throw new IllegalArgumentException(
974 "FLAG_CANT_SAVE_STATE not supported here");
975 }
976
977 Bundle theseOptions;
978 if (options != null && i == intents.length-1) {
979 theseOptions = options;
980 } else {
981 theseOptions = null;
982 }
Craig Mautner6170f732013-04-02 13:05:23 -0700983 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn91097de2014-04-04 18:02:06 -0700984 aInfo, null, null, resultTo, null, -1, callingPid, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -0800985 0, theseOptions, componentSpecified, outActivity, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700986 if (res < 0) {
987 return res;
988 }
989
990 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
991 }
992 }
993 } finally {
994 Binder.restoreCallingIdentity(origId);
995 }
996
997 return ActivityManager.START_SUCCESS;
998 }
999
Craig Mautner2420ead2013-04-01 17:13:20 -07001000 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001001 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001002 throws RemoteException {
1003
1004 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001005 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001006 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001007
1008 // schedule launch ticks to collect information about slow apps.
1009 r.startLaunchTickingLocked();
1010
1011 // Have the window manager re-evaluate the orientation of
1012 // the screen based on the new activity order. Note that
1013 // as a result of this, it can call back into the activity
1014 // manager with a new orientation. We don't care about that,
1015 // because the activity is not currently running so we are
1016 // just restarting it anyway.
1017 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001018 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001019 mService.mConfiguration,
1020 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1021 mService.updateConfigurationLocked(config, r, false, false);
1022 }
1023
1024 r.app = app;
1025 app.waitingToKill = null;
1026 r.launchCount++;
1027 r.lastLaunchTime = SystemClock.uptimeMillis();
1028
1029 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1030
1031 int idx = app.activities.indexOf(r);
1032 if (idx < 0) {
1033 app.activities.add(r);
1034 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001035 mService.updateLruProcessLocked(app, true, null);
1036 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001037
1038 final ActivityStack stack = r.task.stack;
1039 try {
1040 if (app.thread == null) {
1041 throw new RemoteException();
1042 }
1043 List<ResultInfo> results = null;
1044 List<Intent> newIntents = null;
1045 if (andResume) {
1046 results = r.results;
1047 newIntents = r.newIntents;
1048 }
1049 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1050 + " icicle=" + r.icicle
1051 + " with results=" + results + " newIntents=" + newIntents
1052 + " andResume=" + andResume);
1053 if (andResume) {
1054 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1055 r.userId, System.identityHashCode(r),
1056 r.task.taskId, r.shortComponentName);
1057 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001058 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001059 // Home process is the root process of the task.
1060 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001061 }
1062 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1063 r.sleeping = false;
1064 r.forceNewConfig = false;
1065 mService.showAskCompatModeDialogLocked(r);
1066 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1067 String profileFile = null;
1068 ParcelFileDescriptor profileFd = null;
1069 boolean profileAutoStop = false;
1070 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1071 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1072 mService.mProfileProc = app;
1073 profileFile = mService.mProfileFile;
1074 profileFd = mService.mProfileFd;
1075 profileAutoStop = mService.mAutoStopProfiler;
1076 }
1077 }
1078 app.hasShownUi = true;
1079 app.pendingUiClean = true;
1080 if (profileFd != null) {
1081 try {
1082 profileFd = profileFd.dup();
1083 } catch (IOException e) {
1084 if (profileFd != null) {
1085 try {
1086 profileFd.close();
1087 } catch (IOException o) {
1088 }
1089 profileFd = null;
1090 }
1091 }
1092 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001093
Dianne Hackborna413dc02013-07-12 12:02:55 -07001094 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001095 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1096 System.identityHashCode(r), r.info,
Craig Mautnera0026042014-04-23 11:45:37 -07001097 new Configuration(mService.mConfiguration), r.compat, r.task.voiceInteractor,
1098 app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume,
Craig Mautner233ceee2014-05-09 17:05:11 -07001099 mService.isNextTransitionForward(), profileFile, profileFd, profileAutoStop
1100 );
Craig Mautner2420ead2013-04-01 17:13:20 -07001101
1102 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1103 // This may be a heavy-weight process! Note that the package
1104 // manager will ensure that only activity can run in the main
1105 // process of the .apk, which is the only thing that will be
1106 // considered heavy-weight.
1107 if (app.processName.equals(app.info.packageName)) {
1108 if (mService.mHeavyWeightProcess != null
1109 && mService.mHeavyWeightProcess != app) {
1110 Slog.w(TAG, "Starting new heavy weight process " + app
1111 + " when already running "
1112 + mService.mHeavyWeightProcess);
1113 }
1114 mService.mHeavyWeightProcess = app;
1115 Message msg = mService.mHandler.obtainMessage(
1116 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1117 msg.obj = r;
1118 mService.mHandler.sendMessage(msg);
1119 }
1120 }
1121
1122 } catch (RemoteException e) {
1123 if (r.launchFailed) {
1124 // This is the second time we failed -- finish activity
1125 // and give up.
1126 Slog.e(TAG, "Second failure launching "
1127 + r.intent.getComponent().flattenToShortString()
1128 + ", giving up", e);
1129 mService.appDiedLocked(app, app.pid, app.thread);
1130 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1131 "2nd-crash", false);
1132 return false;
1133 }
1134
1135 // This is the first time we failed -- restart process and
1136 // retry.
1137 app.activities.remove(r);
1138 throw e;
1139 }
1140
1141 r.launchFailed = false;
1142 if (stack.updateLRUListLocked(r)) {
1143 Slog.w(TAG, "Activity " + r
1144 + " being launched, but already in LRU list");
1145 }
1146
1147 if (andResume) {
1148 // As part of the process of launching, ActivityThread also performs
1149 // a resume.
1150 stack.minimalResumeActivityLocked(r);
1151 } else {
1152 // This activity is not starting in the resumed state... which
1153 // should look like we asked it to pause+stop (but remain visible),
1154 // and it has done so and reported back the current icicle and
1155 // other state.
1156 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1157 + " (starting in stopped state)");
1158 r.state = ActivityState.STOPPED;
1159 r.stopped = true;
1160 }
1161
1162 // Launch the new version setup screen if needed. We do this -after-
1163 // launching the initial activity (that is, home), so that it can have
1164 // a chance to initialize itself while in the background, making the
1165 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001166 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001167 mService.startSetupActivityLocked();
1168 }
1169
1170 return true;
1171 }
1172
Craig Mautnere79d42682013-04-01 19:01:53 -07001173 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001174 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001175 // Is this activity's application already running?
1176 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001177 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001178
1179 r.task.stack.setLaunchTime(r);
1180
1181 if (app != null && app.thread != null) {
1182 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001183 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1184 || !"android".equals(r.info.packageName)) {
1185 // Don't add this if it is a platform component that is marked
1186 // to run in multiple processes, because this is actually
1187 // part of the framework so doesn't make sense to track as a
1188 // separate apk in the process.
1189 app.addPackage(r.info.packageName, mService.mProcessStats);
1190 }
George Mount2c92c972014-03-20 09:38:23 -07001191 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001192 return;
1193 } catch (RemoteException e) {
1194 Slog.w(TAG, "Exception when starting activity "
1195 + r.intent.getComponent().flattenToShortString(), e);
1196 }
1197
1198 // If a dead object exception was thrown -- fall through to
1199 // restart the application.
1200 }
1201
1202 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001203 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001204 }
1205
Craig Mautner6170f732013-04-02 13:05:23 -07001206 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001207 Intent intent, String resolvedType, ActivityInfo aInfo,
1208 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1209 IBinder resultTo, String resultWho, int requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001210 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Craig Mautnere0a38842013-12-16 16:14:02 -08001211 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container) {
Craig Mautner6170f732013-04-02 13:05:23 -07001212 int err = ActivityManager.START_SUCCESS;
1213
1214 ProcessRecord callerApp = null;
1215 if (caller != null) {
1216 callerApp = mService.getRecordForAppLocked(caller);
1217 if (callerApp != null) {
1218 callingPid = callerApp.pid;
1219 callingUid = callerApp.info.uid;
1220 } else {
1221 Slog.w(TAG, "Unable to find app for caller " + caller
1222 + " (pid=" + callingPid + ") when starting: "
1223 + intent.toString());
1224 err = ActivityManager.START_PERMISSION_DENIED;
1225 }
1226 }
1227
1228 if (err == ActivityManager.START_SUCCESS) {
1229 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1230 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner9ef471f2014-02-07 13:11:47 -08001231 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)
1232 + " on display " + (container == null ? (mFocusedStack == null ?
1233 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1234 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1235 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001236 }
1237
1238 ActivityRecord sourceRecord = null;
1239 ActivityRecord resultRecord = null;
1240 if (resultTo != null) {
1241 sourceRecord = isInAnyStackLocked(resultTo);
1242 if (DEBUG_RESULTS) Slog.v(
1243 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1244 if (sourceRecord != null) {
1245 if (requestCode >= 0 && !sourceRecord.finishing) {
1246 resultRecord = sourceRecord;
1247 }
1248 }
1249 }
1250 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1251
Dianne Hackborn91097de2014-04-04 18:02:06 -07001252 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001253
1254 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1255 && sourceRecord != null) {
1256 // Transfer the result target from the source activity to the new
1257 // one being started, including any failures.
1258 if (requestCode >= 0) {
1259 ActivityOptions.abort(options);
1260 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1261 }
1262 resultRecord = sourceRecord.resultTo;
1263 resultWho = sourceRecord.resultWho;
1264 requestCode = sourceRecord.requestCode;
1265 sourceRecord.resultTo = null;
1266 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001267 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001268 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001269 if (sourceRecord.launchedFromUid == callingUid) {
1270 // The new activity is being launched from the same uid as the previous
1271 // activity in the flow, and asking to forward its result back to the
1272 // previous. In this case the activity is serving as a trampoline between
1273 // the two, so we also want to update its launchedFromPackage to be the
1274 // same as the previous activity. Note that this is safe, since we know
1275 // these two packages come from the same uid; the caller could just as
1276 // well have supplied that same package name itself. This specifially
1277 // deals with the case of an intent picker/chooser being launched in the app
1278 // flow to redirect to an activity picked by the user, where we want the final
1279 // activity to consider it to have been launched by the previous app activity.
1280 callingPackage = sourceRecord.launchedFromPackage;
1281 }
Craig Mautner6170f732013-04-02 13:05:23 -07001282 }
1283
1284 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1285 // We couldn't find a class that can handle the given Intent.
1286 // That's the end of that!
1287 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1288 }
1289
1290 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1291 // We couldn't find the specific class specified in the Intent.
1292 // Also the end of the line.
1293 err = ActivityManager.START_CLASS_NOT_FOUND;
1294 }
1295
Dianne Hackborn91097de2014-04-04 18:02:06 -07001296 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1297 && sourceRecord.task.voiceSession != null) {
1298 // If this activity is being launched as part of a voice session, we need
1299 // to ensure that it is safe to do so. If the upcoming activity will also
1300 // be part of the voice session, we can only launch it if it has explicitly
1301 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001302 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001303 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1304 try {
1305 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1306 intent, resolvedType)) {
1307 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1308 }
1309 } catch (RemoteException e) {
1310 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1311 }
1312 }
1313 }
1314
1315 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1316 // If the caller is starting a new voice session, just make sure the target
1317 // is actually allowing it to run this way.
1318 try {
1319 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1320 intent, resolvedType)) {
1321 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1322 }
1323 } catch (RemoteException e) {
1324 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1325 }
1326 }
1327
Craig Mautner6170f732013-04-02 13:05:23 -07001328 if (err != ActivityManager.START_SUCCESS) {
1329 if (resultRecord != null) {
1330 resultStack.sendActivityResultLocked(-1,
1331 resultRecord, resultWho, requestCode,
1332 Activity.RESULT_CANCELED, null);
1333 }
1334 setDismissKeyguard(false);
1335 ActivityOptions.abort(options);
1336 return err;
1337 }
1338
1339 final int startAnyPerm = mService.checkPermission(
1340 START_ANY_ACTIVITY, callingPid, callingUid);
1341 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1342 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1343 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1344 if (resultRecord != null) {
1345 resultStack.sendActivityResultLocked(-1,
1346 resultRecord, resultWho, requestCode,
1347 Activity.RESULT_CANCELED, null);
1348 }
1349 setDismissKeyguard(false);
1350 String msg;
1351 if (!aInfo.exported) {
1352 msg = "Permission Denial: starting " + intent.toString()
1353 + " from " + callerApp + " (pid=" + callingPid
1354 + ", uid=" + callingUid + ")"
1355 + " not exported from uid " + aInfo.applicationInfo.uid;
1356 } else {
1357 msg = "Permission Denial: starting " + intent.toString()
1358 + " from " + callerApp + " (pid=" + callingPid
1359 + ", uid=" + callingUid + ")"
1360 + " requires " + aInfo.permission;
1361 }
1362 Slog.w(TAG, msg);
1363 throw new SecurityException(msg);
1364 }
1365
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001366 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001367 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001368
Craig Mautner6170f732013-04-02 13:05:23 -07001369 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001370 try {
1371 // The Intent we give to the watcher has the extra data
1372 // stripped off, since it can contain private information.
1373 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001374 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001375 aInfo.applicationInfo.packageName);
1376 } catch (RemoteException e) {
1377 mService.mController = null;
1378 }
Ben Gruver5e207332013-04-03 17:41:37 -07001379 }
Craig Mautner6170f732013-04-02 13:05:23 -07001380
Ben Gruver5e207332013-04-03 17:41:37 -07001381 if (abort) {
1382 if (resultRecord != null) {
1383 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001384 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001385 }
Ben Gruver5e207332013-04-03 17:41:37 -07001386 // We pretend to the caller that it was really started, but
1387 // they will just get a cancel result.
1388 setDismissKeyguard(false);
1389 ActivityOptions.abort(options);
1390 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001391 }
1392
1393 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001394 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001395 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001396 if (outActivity != null) {
1397 outActivity[0] = r;
1398 }
1399
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001400 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001401 if (voiceSession == null && (stack.mResumedActivity == null
1402 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Craig Mautner6170f732013-04-02 13:05:23 -07001403 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1404 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001405 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautner6170f732013-04-02 13:05:23 -07001406 mService.mPendingActivityLaunches.add(pal);
1407 setDismissKeyguard(false);
1408 ActivityOptions.abort(options);
1409 return ActivityManager.START_SWITCHES_CANCELED;
1410 }
1411 }
1412
1413 if (mService.mDidAppSwitch) {
1414 // This is the second allowed switch since we stopped switches,
1415 // so now just generally allow switches. Use case: user presses
1416 // home (switches disabled, switch to home, mDidAppSwitch now true);
1417 // user taps a home icon (coming from home so allowed, we hit here
1418 // and now allow anyone to switch again).
1419 mService.mAppSwitchesAllowedTime = 0;
1420 } else {
1421 mService.mDidAppSwitch = true;
1422 }
1423
1424 mService.doPendingActivityLaunchesLocked(false);
1425
Dianne Hackborn91097de2014-04-04 18:02:06 -07001426 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
1427 startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001428
1429 if (allPausedActivitiesComplete()) {
1430 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001431 // activity start, but we are not actually doing an activity
1432 // switch... just dismiss the keyguard now, because we
1433 // probably want to see whatever is behind it.
1434 dismissKeyguard();
1435 }
1436 return err;
1437 }
1438
Craig Mautner1b4bf852014-05-26 15:06:32 -07001439 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001440 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001441
1442 // On leanback only devices we should keep all activities in the same stack.
1443 if (!mLeanbackOnlyDevice &&
1444 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001445 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001446 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001447 if (taskStack.isOnHomeDisplay()) {
1448 if (mFocusedStack != taskStack) {
1449 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1450 "focused stack to r=" + r + " task=" + task);
1451 mFocusedStack = taskStack;
1452 } else {
1453 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1454 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1455 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001456 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001457 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001458 }
1459
Craig Mautnere0a38842013-12-16 16:14:02 -08001460 final ActivityContainer container = r.mInitialActivityContainer;
1461 if (container != null) {
1462 // The first time put it on the desired stack, after this put on task stack.
1463 r.mInitialActivityContainer = null;
1464 return container.mStack;
1465 }
1466
Craig Mautner1b4bf852014-05-26 15:06:32 -07001467 if (mFocusedStack != mHomeStack && (!newTask ||
1468 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001469 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1470 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1471 return mFocusedStack;
1472 }
1473
Craig Mautnere0a38842013-12-16 16:14:02 -08001474 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1475 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1476 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1477 if (!stack.isHomeStack()) {
1478 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1479 "adjustStackFocus: Setting focused stack=" + stack);
1480 mFocusedStack = stack;
1481 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001482 }
1483 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001484
Craig Mautner4a1cb222013-12-04 16:14:06 -08001485 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001486 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001487 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1488 " stackId=" + stackId);
1489 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001490 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001491 }
1492 return mHomeStack;
1493 }
1494
Craig Mautner29219d92013-04-16 20:19:12 -07001495 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001496 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001497 final TaskRecord task = r.task;
1498 boolean isHomeActivity = !r.isApplicationActivity();
1499 if (!isHomeActivity && task != null) {
1500 isHomeActivity = !task.isApplicationTask();
1501 }
1502 if (!isHomeActivity && task != null) {
1503 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1504 isHomeActivity = parent != null && parent.isHomeActivity();
1505 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001506 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001507 }
1508 }
1509
Craig Mautner8849a5e2013-04-02 16:41:03 -07001510 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001511 ActivityRecord sourceRecord,
1512 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
1513 boolean doResume, Bundle options) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001514 final Intent intent = r.intent;
1515 final int callingUid = r.launchedFromUid;
1516
1517 int launchFlags = intent.getFlags();
1518
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001519 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
1520 (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
1521 r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK)) {
1522 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1523 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1524 "\"singleInstance\" or \"singleTask\"");
1525 launchFlags &=
1526 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1527 } else {
1528 switch (r.info.documentLaunchMode) {
1529 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1530 break;
1531 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1532 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1533 break;
1534 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1535 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1536 break;
1537 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1538 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1539 break;
1540 }
1541 }
1542
1543 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1544 // For whatever reason this activity is being launched into a new
1545 // task... yet the caller has requested a result back. Well, that
1546 // is pretty messed up, so instead immediately send back a cancel
1547 // and let the new task continue launched as normal without a
1548 // dependency on its originator.
1549 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1550 r.resultTo.task.stack.sendActivityResultLocked(-1,
1551 r.resultTo, r.resultWho, r.requestCode,
1552 Activity.RESULT_CANCELED, null);
1553 r.resultTo = null;
1554 }
1555
1556 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1557 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1558 }
1559
Craig Mautner8849a5e2013-04-02 16:41:03 -07001560 // We'll invoke onUserLeaving before onPause only if the launching
1561 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001562 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001563 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001564
1565 // If the caller has asked not to resume at this point, we make note
1566 // of this in the record so that we can skip it when trying to find
1567 // the top running activity.
1568 if (!doResume) {
1569 r.delayedResume = true;
1570 }
1571
Craig Mautnera254cd72014-05-25 16:47:39 -07001572 ActivityRecord notTop =
1573 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001574
1575 // If the onlyIfNeeded flag is set, then we can do this if the activity
1576 // being launched is the same as the one making the call... or, as
1577 // a special case, if we do not know the caller then we count the
1578 // current top activity as the caller.
1579 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1580 ActivityRecord checkedCaller = sourceRecord;
1581 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001582 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001583 }
1584 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1585 // Caller is not the same as launcher, so always needed.
1586 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1587 }
1588 }
1589
1590 if (sourceRecord == null) {
1591 // This activity is not being started from another... in this
1592 // case we -always- start a new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001593 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001594 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1595 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001596 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1597 }
1598 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1599 // The original activity who is starting us is running as a single
1600 // instance... this new activity it is starting must go on its
1601 // own task.
1602 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1603 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
1604 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1605 // The activity being started is a single instance... it always
1606 // gets launched into its own task.
1607 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1608 }
1609
Craig Mautner88629292013-11-10 20:39:05 -08001610 ActivityInfo newTaskInfo = null;
1611 Intent newTaskIntent = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001612 final ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001613 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001614 if (sourceRecord.finishing) {
1615 // If the source is finishing, we can't further count it as our source. This
1616 // is because the task it is associated with may now be empty and on its way out,
1617 // so we don't want to blindly throw it in to that task. Instead we will take
Craig Mautner88629292013-11-10 20:39:05 -08001618 // the NEW_TASK flow and try to find a task for it. But save the task information
1619 // so it can be used when creating the new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001620 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001621 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1622 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1623 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautner88629292013-11-10 20:39:05 -08001624 newTaskInfo = sourceRecord.info;
1625 newTaskIntent = sourceRecord.task.intent;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001626 }
1627 sourceRecord = null;
1628 sourceStack = null;
1629 } else {
1630 sourceStack = sourceRecord.task.stack;
1631 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001632 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001633 sourceStack = null;
1634 }
1635
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001636 intent.setFlags(launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001637
1638 boolean addingToTask = false;
1639 boolean movedHome = false;
1640 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001641 ActivityStack targetStack;
Craig Mautnerd00f4742014-03-12 14:17:26 -07001642 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1643 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001644 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1645 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1646 // If bring to front is requested, and no result is requested, and
1647 // we can find a task that was started with this same
1648 // component, then instead of launching bring that one to the front.
1649 if (r.resultTo == null) {
1650 // See if there is a task to bring to the front. If this is
1651 // a SINGLE_INSTANCE activity, there can be one and only one
1652 // instance of it in the history, and it is always in its own
1653 // unique task, so we do a special search.
1654 ActivityRecord intentActivity = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
Craig Mautnerac6f8432013-07-17 13:24:59 -07001655 ? findTaskLocked(r)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001656 : findActivityLocked(intent, r.info);
1657 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001658 if (isLockTaskModeViolation(intentActivity.task)) {
1659 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
1660 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1661 }
Craig Mautner29219d92013-04-16 20:19:12 -07001662 if (r.task == null) {
1663 r.task = intentActivity.task;
1664 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001665 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001666 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001667 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1668 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001669 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001670 if (intentActivity.task.intent == null) {
1671 // This task was started because of movement of
1672 // the activity based on affinity... now that we
1673 // are actually launching it, we can assign the
1674 // base intent.
1675 intentActivity.task.setIntent(intent, r.info);
1676 }
1677 // If the target task is not in the front, then we need
1678 // to bring it to the front... except... well, with
1679 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1680 // to have the same behavior as if a new instance was
1681 // being started, which means not bringing it to the front
1682 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001683 final ActivityStack lastStack = getLastStack();
1684 ActivityRecord curTop = lastStack == null?
1685 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001686 if (curTop != null && (curTop.task != intentActivity.task ||
1687 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001688 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001689 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1690 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001691 // We really do want to push this one into the
1692 // user's face, right now.
1693 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001694 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001695 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001696 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1697 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001698 // Caller wants to appear on home activity.
Craig Mautner719e6212014-05-29 16:49:41 +00001699 intentActivity.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001700 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001701 options = null;
1702 }
1703 }
1704 // If the caller has requested that the target task be
1705 // reset, then do so.
1706 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1707 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1708 }
1709 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1710 // We don't need to start a new activity, and
1711 // the client said not to do anything if that
1712 // is the case, so this is it! And for paranoia, make
1713 // sure we have correctly resumed the top activity.
1714 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001715 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001716 } else {
1717 ActivityOptions.abort(options);
1718 }
1719 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1720 }
1721 if ((launchFlags &
1722 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1723 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1724 // The caller has requested to completely replace any
1725 // existing task with its new activity. Well that should
1726 // not be too hard...
1727 reuseTask = intentActivity.task;
1728 reuseTask.performClearTaskLocked();
1729 reuseTask.setIntent(r.intent, r.info);
1730 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
1731 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1732 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1733 // In this situation we want to remove all activities
1734 // from the task up to the one being started. In most
1735 // cases this means we are resetting the task to its
1736 // initial state.
1737 ActivityRecord top =
1738 intentActivity.task.performClearTaskLocked(r, launchFlags);
1739 if (top != null) {
1740 if (top.frontOfTask) {
1741 // Activity aliases may mean we use different
1742 // intents for the top activity, so make sure
1743 // the task now has the identity of the new
1744 // intent.
1745 top.task.setIntent(r.intent, r.info);
1746 }
1747 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1748 r, top.task);
1749 top.deliverNewIntentLocked(callingUid, r.intent);
1750 } else {
1751 // A special case: we need to
1752 // start the activity because it is not currently
1753 // running, and the caller has asked to clear the
1754 // current task to have this activity at the top.
1755 addingToTask = true;
1756 // Now pretend like this activity is being started
1757 // by the top of its task, so it is put in the
1758 // right place.
1759 sourceRecord = intentActivity;
1760 }
1761 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1762 // In this case the top activity on the task is the
1763 // same as the one being launched, so we take that
1764 // as a request to bring the task to the foreground.
1765 // If the top activity in the task is the root
1766 // activity, deliver this new intent to it if it
1767 // desires.
1768 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1769 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1770 && intentActivity.realActivity.equals(r.realActivity)) {
1771 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1772 intentActivity.task);
1773 if (intentActivity.frontOfTask) {
1774 intentActivity.task.setIntent(r.intent, r.info);
1775 }
1776 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1777 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1778 // In this case we are launching the root activity
1779 // of the task, but with a different intent. We
1780 // should start a new instance on top.
1781 addingToTask = true;
1782 sourceRecord = intentActivity;
1783 }
1784 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1785 // In this case an activity is being launched in to an
1786 // existing task, without resetting that task. This
1787 // is typically the situation of launching an activity
1788 // from a notification or shortcut. We want to place
1789 // the new activity on top of the current task.
1790 addingToTask = true;
1791 sourceRecord = intentActivity;
1792 } else if (!intentActivity.task.rootWasReset) {
1793 // In this case we are launching in to an existing task
1794 // that has not yet been started from its front door.
1795 // The current task has been brought to the front.
1796 // Ideally, we'd probably like to place this new task
1797 // at the bottom of its stack, but that's a little hard
1798 // to do with the current organization of the code so
1799 // for now we'll just drop it.
1800 intentActivity.task.setIntent(r.intent, r.info);
1801 }
1802 if (!addingToTask && reuseTask == null) {
1803 // We didn't do anything... but it was needed (a.k.a., client
1804 // don't use that intent!) And for paranoia, make
1805 // sure we have correctly resumed the top activity.
1806 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001807 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001808 } else {
1809 ActivityOptions.abort(options);
1810 }
1811 return ActivityManager.START_TASK_TO_FRONT;
1812 }
1813 }
1814 }
1815 }
1816
1817 //String uri = r.intent.toURI();
1818 //Intent intent2 = new Intent(uri);
1819 //Slog.i(TAG, "Given intent: " + r.intent);
1820 //Slog.i(TAG, "URI is: " + uri);
1821 //Slog.i(TAG, "To intent: " + intent2);
1822
1823 if (r.packageName != null) {
1824 // If the activity being launched is the same as the one currently
1825 // at the top, then we need to check if it should only be launched
1826 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001827 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001828 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001829 if (top != null && r.resultTo == null) {
1830 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1831 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001832 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautner8849a5e2013-04-02 16:41:03 -07001833 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
1834 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1835 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1836 top.task);
1837 // For paranoia, make sure we have correctly
1838 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001839 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001840 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001841 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001842 }
1843 ActivityOptions.abort(options);
1844 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1845 // We don't need to start a new activity, and
1846 // the client said not to do anything if that
1847 // is the case, so this is it!
1848 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1849 }
1850 top.deliverNewIntentLocked(callingUid, r.intent);
1851 return ActivityManager.START_DELIVERED_TO_TOP;
1852 }
1853 }
1854 }
1855 }
1856
1857 } else {
1858 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001859 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1860 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001861 }
1862 ActivityOptions.abort(options);
1863 return ActivityManager.START_CLASS_NOT_FOUND;
1864 }
1865
1866 boolean newTask = false;
1867 boolean keepCurTransition = false;
1868
1869 // Should this be considered a new task?
1870 if (r.resultTo == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001871 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001872 if (isLockTaskModeViolation(reuseTask)) {
1873 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1874 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1875 }
Craig Mautner1b4bf852014-05-26 15:06:32 -07001876 newTask = true;
1877 targetStack = adjustStackFocus(r, newTask);
Craig Mautnere0a38842013-12-16 16:14:02 -08001878 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001879 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08001880 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
1881 newTaskInfo != null ? newTaskInfo : r.info,
1882 newTaskIntent != null ? newTaskIntent : intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001883 voiceSession, voiceInteractor, true), null, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001884 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1885 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001886 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001887 r.setTask(reuseTask, reuseTask, true);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001888 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001889 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001890 if ((launchFlags &
1891 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1892 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1893 // Caller wants to appear on home activity, so before starting
1894 // their own activity we will bring home to the front.
Craig Mautner719e6212014-05-29 16:49:41 +00001895 r.task.mOnTopOfHome = r.task.stack.isOnHomeDisplay();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001896 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001897 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001898 } else if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001899 TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08001900 if (isLockTaskModeViolation(sourceTask)) {
1901 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1902 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1903 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001904 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07001905 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07001906 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001907 if (!addingToTask &&
1908 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
1909 // In this case, we are adding the activity to an existing
1910 // task, but the caller has asked to clear that task if the
1911 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001912 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001913 keepCurTransition = true;
1914 if (top != null) {
1915 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1916 top.deliverNewIntentLocked(callingUid, r.intent);
1917 // For paranoia, make sure we have correctly
1918 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001919 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920 if (doResume) {
1921 targetStack.resumeTopActivityLocked(null);
1922 }
1923 ActivityOptions.abort(options);
1924 return ActivityManager.START_DELIVERED_TO_TOP;
1925 }
1926 } else if (!addingToTask &&
1927 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1928 // In this case, we are launching an activity in our own task
1929 // that may already be running somewhere in the history, and
1930 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001931 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001932 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001933 final TaskRecord task = top.task;
1934 task.moveActivityToFrontLocked(top);
1935 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001936 top.updateOptionsLocked(options);
1937 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001938 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001939 if (doResume) {
1940 targetStack.resumeTopActivityLocked(null);
1941 }
1942 return ActivityManager.START_DELIVERED_TO_TOP;
1943 }
1944 }
1945 // An existing activity is starting this new activity, so we want
1946 // to keep the new one in the same task as the one that is starting
1947 // it.
Craig Mautner525f3d92013-05-07 14:01:50 -07001948 r.setTask(sourceTask, sourceRecord.thumbHolder, false);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001949 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001950 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001951
1952 } else {
1953 // This not being started from an existing activity, and not part
1954 // of a new task... just put it in the top task, though these days
1955 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07001956 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07001957 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07001958 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001959 r.setTask(prev != null ? prev.task
Dianne Hackborn91097de2014-04-04 18:02:06 -07001960 : targetStack.createTaskRecord(getNextTaskId(), r.info, intent, null, null, true),
Craig Mautnerde4ef022013-04-07 19:01:33 -07001961 null, true);
Craig Mautner95e9daa2014-03-17 15:57:20 -07001962 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001963 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
1964 + " in new guessed " + r.task);
1965 }
1966
1967 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001968 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001969
1970 if (newTask) {
1971 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
1972 }
1973 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07001974 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001975 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautner1d001b62013-06-18 16:52:43 -07001976 mService.setFocusedActivityLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001977 return ActivityManager.START_SUCCESS;
1978 }
1979
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001980 void acquireLaunchWakelock() {
1981 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1982 throw new IllegalStateException("Calling must be system uid");
1983 }
1984 mLaunchingActivity.acquire();
1985 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1986 // To be safe, don't allow the wake lock to be held for too long.
1987 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1988 }
1989 }
1990
Craig Mautnerf3333272013-04-22 10:55:53 -07001991 // Checked.
1992 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1993 Configuration config) {
1994 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
1995
Craig Mautnerf3333272013-04-22 10:55:53 -07001996 ArrayList<ActivityRecord> stops = null;
1997 ArrayList<ActivityRecord> finishes = null;
1998 ArrayList<UserStartedState> startingUsers = null;
1999 int NS = 0;
2000 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002001 boolean booting = false;
2002 boolean enableScreen = false;
2003 boolean activityRemoved = false;
2004
2005 ActivityRecord r = ActivityRecord.forToken(token);
2006 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002007 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2008 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002009 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2010 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002011 if (fromTimeout) {
2012 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002013 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002014
2015 // This is a hack to semi-deal with a race condition
2016 // in the client where it can be constructed with a
2017 // newer configuration from when we asked it to launch.
2018 // We'll update with whatever configuration it now says
2019 // it used to launch.
2020 if (config != null) {
2021 r.configuration = config;
2022 }
2023
2024 // We are now idle. If someone is waiting for a thumbnail from
2025 // us, we can now deliver.
2026 r.idle = true;
2027
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002028 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
2029 if (!mService.mBooted && isFrontStack(r.task.stack)) {
2030 mService.mBooted = true;
2031 enableScreen = true;
2032 }
2033 }
2034
2035 if (allResumedActivitiesIdle()) {
2036 if (r != null) {
2037 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002038 }
2039
2040 if (mLaunchingActivity.isHeld()) {
2041 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2042 if (VALIDATE_WAKE_LOCK_CALLER &&
2043 Binder.getCallingUid() != Process.myUid()) {
2044 throw new IllegalStateException("Calling must be system uid");
2045 }
2046 mLaunchingActivity.release();
2047 }
2048 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002049 }
2050
2051 // Atomically retrieve all of the other things to do.
2052 stops = processStoppingActivitiesLocked(true);
2053 NS = stops != null ? stops.size() : 0;
2054 if ((NF=mFinishingActivities.size()) > 0) {
2055 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2056 mFinishingActivities.clear();
2057 }
2058
Craig Mautnerf3333272013-04-22 10:55:53 -07002059 if (isFrontStack(mHomeStack)) {
2060 booting = mService.mBooting;
2061 mService.mBooting = false;
2062 }
2063
2064 if (mStartingUsers.size() > 0) {
2065 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2066 mStartingUsers.clear();
2067 }
2068
Craig Mautnerf3333272013-04-22 10:55:53 -07002069 // Stop any activities that are scheduled to do so but have been
2070 // waiting for the next one to start.
2071 for (int i = 0; i < NS; i++) {
2072 r = stops.get(i);
2073 final ActivityStack stack = r.task.stack;
2074 if (r.finishing) {
2075 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2076 } else {
2077 stack.stopActivityLocked(r);
2078 }
2079 }
2080
2081 // Finish any activities that are scheduled to do so but have been
2082 // waiting for the next one to start.
2083 for (int i = 0; i < NF; i++) {
2084 r = finishes.get(i);
2085 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, false, "finish-idle");
2086 }
2087
2088 if (booting) {
2089 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002090 } else {
2091 // Complete user switch
2092 if (startingUsers != null) {
2093 for (int i = 0; i < startingUsers.size(); i++) {
2094 mService.finishUserSwitch(startingUsers.get(i));
2095 }
2096 }
2097 // Complete starting up of background users
2098 if (mStartingBackgroundUsers.size() > 0) {
2099 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2100 mStartingBackgroundUsers.clear();
2101 for (int i = 0; i < startingUsers.size(); i++) {
2102 mService.finishUserBoot(startingUsers.get(i));
2103 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002104 }
2105 }
2106
2107 mService.trimApplications();
2108 //dump();
2109 //mWindowManager.dump();
2110
2111 if (enableScreen) {
2112 mService.enableScreenAfterBoot();
2113 }
2114
2115 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002116 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002117 }
2118
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002119 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002120 }
2121
Craig Mautner8e569572013-10-11 17:36:59 -07002122 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002123 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002124 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2125 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002126 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2127 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2128 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002129 }
Craig Mautner19091252013-10-05 00:03:53 -07002130 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002131 }
2132
2133 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002134 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2135 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002136 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2137 stacks.get(stackNdx).closeSystemDialogsLocked();
2138 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002139 }
2140 }
2141
Craig Mautner93529a42013-10-04 15:03:13 -07002142 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002143 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002144 }
2145
Craig Mautner8d341ef2013-03-26 09:03:27 -07002146 /**
2147 * @return true if some activity was finished (or would have finished if doit were true).
2148 */
2149 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2150 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002151 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2152 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002153 final int numStacks = stacks.size();
2154 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2155 final ActivityStack stack = stacks.get(stackNdx);
2156 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2157 didSomething = true;
2158 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002159 }
2160 }
2161 return didSomething;
2162 }
2163
Dianne Hackborna413dc02013-07-12 12:02:55 -07002164 void updatePreviousProcessLocked(ActivityRecord r) {
2165 // Now that this process has stopped, we may want to consider
2166 // it to be the previous app to try to keep around in case
2167 // the user wants to return to it.
2168
2169 // First, found out what is currently the foreground app, so that
2170 // we don't blow away the previous app if this activity is being
2171 // hosted by the process that is actually still the foreground.
2172 ProcessRecord fgApp = null;
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 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2176 final ActivityStack stack = stacks.get(stackNdx);
2177 if (isFrontStack(stack)) {
2178 if (stack.mResumedActivity != null) {
2179 fgApp = stack.mResumedActivity.app;
2180 } else if (stack.mPausingActivity != null) {
2181 fgApp = stack.mPausingActivity.app;
2182 }
2183 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002184 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002185 }
2186 }
2187
2188 // Now set this one as the previous process, only if that really
2189 // makes sense to.
2190 if (r.app != null && fgApp != null && r.app != fgApp
2191 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002192 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002193 mService.mPreviousProcess = r.app;
2194 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2195 }
2196 }
2197
Craig Mautner05d29032013-05-03 13:40:13 -07002198 boolean resumeTopActivitiesLocked() {
2199 return resumeTopActivitiesLocked(null, null, null);
2200 }
2201
2202 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2203 Bundle targetOptions) {
2204 if (targetStack == null) {
2205 targetStack = getFocusedStack();
2206 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002207 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002208 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002209 if (isFrontStack(targetStack)) {
2210 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2211 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002212 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2213 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002214 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2215 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002216 if (stack == targetStack) {
2217 // Already started above.
2218 continue;
2219 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002220 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002221 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002222 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002223 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002224 }
Craig Mautner05d29032013-05-03 13:40:13 -07002225 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002226 }
2227
2228 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002229 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2230 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002231 final int numStacks = stacks.size();
2232 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2233 final ActivityStack stack = stacks.get(stackNdx);
2234 stack.finishTopRunningActivityLocked(app);
2235 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002236 }
2237 }
2238
Craig Mautneraea74a52014-03-08 14:23:10 -08002239 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2240 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2241 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002242 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002243 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2244 // Caller wants the home activity moved with it. To accomplish this,
2245 // we'll just indicate that this task returns to the home task.
Craig Mautner719e6212014-05-29 16:49:41 +00002246 task.mOnTopOfHome = true;
Craig Mautneraea74a52014-03-08 14:23:10 -08002247 }
2248 task.stack.moveTaskToFrontLocked(task, null, options);
2249 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2250 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002251 }
2252
Craig Mautner967212c2013-04-13 21:10:58 -07002253 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002254 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2255 if (activityContainer != null) {
2256 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002257 }
2258 return null;
2259 }
2260
Craig Mautner967212c2013-04-13 21:10:58 -07002261 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002262 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002263 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2264 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002265 }
2266 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002267 }
2268
Craig Mautner4a1cb222013-12-04 16:14:06 -08002269 IBinder getHomeActivityToken() {
2270 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2271 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2272 final TaskRecord task = tasks.get(taskNdx);
2273 if (task.isHomeTask()) {
2274 final ArrayList<ActivityRecord> activities = task.mActivities;
2275 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2276 final ActivityRecord r = activities.get(activityNdx);
2277 if (r.isHomeActivity()) {
2278 return r.appToken;
2279 }
2280 }
2281 }
2282 }
2283 return null;
2284 }
2285
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002286 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002287 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002288 ActivityContainer activityContainer =
2289 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002290 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002291 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002292 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002293 return activityContainer;
2294 }
2295
Craig Mautner34b73df2014-01-12 21:11:08 -08002296 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002297 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2298 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2299 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002300 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2301 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002302 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002303 }
2304 }
2305
Craig Mautner95da1082014-02-24 17:54:35 -08002306 void deleteActivityContainer(IActivityContainer container) {
2307 ActivityContainer activityContainer = (ActivityContainer)container;
2308 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002309 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2310 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002311 final int stackId = activityContainer.mStackId;
2312 mActivityContainers.remove(stackId);
2313 mWindowManager.removeStack(stackId);
2314 }
2315 }
2316
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002317 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002318 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2319 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002320 return -1;
2321 }
2322
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002323 ActivityContainer activityContainer = new ActivityContainer(stackId);
2324 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002325 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002326 return stackId;
2327 }
2328
2329 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002330 while (true) {
2331 if (++mLastStackId <= HOME_STACK_ID) {
2332 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002333 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002334 if (getStack(mLastStackId) == null) {
2335 break;
2336 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002337 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002338 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002339 }
2340
Craig Mautneref73ee12014-04-23 11:45:37 -07002341 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2342 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2343 final ActivityStack stack = getStack(stackId);
2344 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2345 final TaskRecord task = tasks.get(taskNdx);
2346 stack.addTask(task, false, false);
2347 final int taskId = task.taskId;
2348 final ArrayList<ActivityRecord> activities = task.mActivities;
2349 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2350 final ActivityRecord r = activities.get(activityNdx);
2351 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2352 r.info.screenOrientation, r.fullscreen,
2353 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07002354 r.userId, r.info.configChanges, task.voiceSession != null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002355 }
2356 mWindowManager.addTask(taskId, stackId, false);
2357 }
Craig Mautner719e6212014-05-29 16:49:41 +00002358 resumeHomeActivity(null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002359 }
2360
Craig Mautner8d341ef2013-03-26 09:03:27 -07002361 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002362 final TaskRecord task = anyTaskForIdLocked(taskId);
2363 if (task == null) {
2364 return;
2365 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002366 final ActivityStack stack = getStack(stackId);
2367 if (stack == null) {
2368 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2369 return;
2370 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002371 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002372 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002373 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002374 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002375 }
2376
Craig Mautnerac6f8432013-07-17 13:24:59 -07002377 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002378 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002379 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2380 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002381 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2382 final ActivityStack stack = stacks.get(stackNdx);
2383 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002384 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2385 continue;
2386 }
2387 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2388 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2389 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002390 continue;
2391 }
2392 final ActivityRecord ar = stack.findTaskLocked(r);
2393 if (ar != null) {
2394 return ar;
2395 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002396 }
2397 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002398 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002399 return null;
2400 }
2401
2402 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002403 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2404 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002405 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2406 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2407 if (ar != null) {
2408 return ar;
2409 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002410 }
2411 }
2412 return null;
2413 }
2414
Craig Mautner8d341ef2013-03-26 09:03:27 -07002415 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002416 scheduleSleepTimeout();
2417 if (!mGoingToSleep.isHeld()) {
2418 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002419 if (mLaunchingActivity.isHeld()) {
2420 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2421 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002422 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002423 mLaunchingActivity.release();
2424 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002425 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002426 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002427 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002428 }
2429
2430 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002431 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002432
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002433 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002434 final long endTime = System.currentTimeMillis() + timeout;
2435 while (true) {
2436 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002437 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2438 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002439 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2440 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2441 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002442 }
2443 if (cantShutdown) {
2444 long timeRemaining = endTime - System.currentTimeMillis();
2445 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002446 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002447 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002448 } catch (InterruptedException e) {
2449 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002450 } else {
2451 Slog.w(TAG, "Activity manager shutdown timed out");
2452 timedout = true;
2453 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002454 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002455 } else {
2456 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002457 }
2458 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002459
2460 // Force checkReadyForSleep to complete.
2461 mSleepTimeout = true;
2462 checkReadyForSleepLocked();
2463
Craig Mautner8d341ef2013-03-26 09:03:27 -07002464 return timedout;
2465 }
2466
2467 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002468 removeSleepTimeouts();
2469 if (mGoingToSleep.isHeld()) {
2470 mGoingToSleep.release();
2471 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002472 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2473 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002474 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2475 final ActivityStack stack = stacks.get(stackNdx);
2476 stack.awakeFromSleepingLocked();
2477 if (isFrontStack(stack)) {
2478 resumeTopActivitiesLocked();
2479 }
Craig Mautner5314a402013-09-26 12:40:16 -07002480 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002481 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002482 mGoingToSleepActivities.clear();
2483 }
2484
2485 void activitySleptLocked(ActivityRecord r) {
2486 mGoingToSleepActivities.remove(r);
2487 checkReadyForSleepLocked();
2488 }
2489
2490 void checkReadyForSleepLocked() {
2491 if (!mService.isSleepingOrShuttingDown()) {
2492 // Do not care.
2493 return;
2494 }
2495
2496 if (!mSleepTimeout) {
2497 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002498 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2499 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002500 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2501 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2502 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002503 }
2504
2505 if (mStoppingActivities.size() > 0) {
2506 // Still need to tell some activities to stop; can't sleep yet.
2507 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2508 + mStoppingActivities.size() + " activities");
2509 scheduleIdleLocked();
2510 dontSleep = true;
2511 }
2512
2513 if (mGoingToSleepActivities.size() > 0) {
2514 // Still need to tell some activities to sleep; can't sleep yet.
2515 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2516 + mGoingToSleepActivities.size() + " activities");
2517 dontSleep = true;
2518 }
2519
2520 if (dontSleep) {
2521 return;
2522 }
2523 }
2524
Craig Mautnere0a38842013-12-16 16:14:02 -08002525 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2526 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002527 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2528 stacks.get(stackNdx).goToSleep();
2529 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002530 }
2531
2532 removeSleepTimeouts();
2533
2534 if (mGoingToSleep.isHeld()) {
2535 mGoingToSleep.release();
2536 }
2537 if (mService.mShuttingDown) {
2538 mService.notifyAll();
2539 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002540 }
2541
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002542 boolean reportResumedActivityLocked(ActivityRecord r) {
2543 final ActivityStack stack = r.task.stack;
2544 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002545 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002546 }
2547 if (allResumedActivitiesComplete()) {
2548 ensureActivitiesVisibleLocked(null, 0);
2549 mWindowManager.executeAppTransition();
2550 return true;
2551 }
2552 return false;
2553 }
2554
Craig Mautner8d341ef2013-03-26 09:03:27 -07002555 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002556 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2557 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002558 final int numStacks = stacks.size();
2559 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2560 final ActivityStack stack = stacks.get(stackNdx);
2561 stack.handleAppCrashLocked(app);
2562 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002563 }
2564 }
2565
Craig Mautnerde4ef022013-04-07 19:01:33 -07002566 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002567 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002568 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2569 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002570 final int topStackNdx = stacks.size() - 1;
2571 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2572 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002573 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002574 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002575 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002576 }
2577
2578 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002579 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2580 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002581 final int numStacks = stacks.size();
2582 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2583 final ActivityStack stack = stacks.get(stackNdx);
2584 stack.scheduleDestroyActivities(app, false, reason);
2585 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002586 }
2587 }
2588
2589 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002590 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2591 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002592 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002593
Craig Mautner858d8a62013-04-23 17:08:34 -07002594 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002595 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2596 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002597 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002598 final ActivityStack stack = stacks.get(stackNdx);
2599 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002600 TaskRecord task = stack.topTask();
2601 if (task != null) {
2602 mWindowManager.moveTaskToTop(task.taskId);
2603 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002604 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002605 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002606
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002607 ActivityStack stack = getStack(restoreStackId);
2608 if (stack == null) {
2609 stack = mHomeStack;
2610 }
2611 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002612 if (stack.isOnHomeDisplay()) {
2613 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002614 TaskRecord task = stack.topTask();
2615 if (task != null) {
2616 mWindowManager.moveTaskToTop(task.taskId);
2617 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002618 } else {
2619 // Stack was moved to another display while user was swapped out.
Craig Mautner719e6212014-05-29 16:49:41 +00002620 resumeHomeActivity(null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002621 }
Craig Mautner93529a42013-10-04 15:03:13 -07002622 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002623 }
2624
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002625 /**
2626 * Add background users to send boot completed events to.
2627 * @param userId The user being started in the background
2628 * @param uss The state object for the user.
2629 */
2630 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2631 mStartingBackgroundUsers.add(uss);
2632 }
2633
Craig Mautnerde4ef022013-04-07 19:01:33 -07002634 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2635 int N = mStoppingActivities.size();
2636 if (N <= 0) return null;
2637
2638 ArrayList<ActivityRecord> stops = null;
2639
2640 final boolean nowVisible = allResumedActivitiesVisible();
2641 for (int i=0; i<N; i++) {
2642 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002643 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002644 + nowVisible + " waitingVisible=" + s.waitingVisible
2645 + " finishing=" + s.finishing);
2646 if (s.waitingVisible && nowVisible) {
2647 mWaitingVisibleActivities.remove(s);
2648 s.waitingVisible = false;
2649 if (s.finishing) {
2650 // If this activity is finishing, it is sitting on top of
2651 // everyone else but we now know it is no longer needed...
2652 // so get rid of it. Otherwise, we need to go through the
2653 // normal flow and hide it once we determine that it is
2654 // hidden by the activities in front of it.
2655 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002656 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002657 }
2658 }
2659 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2660 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2661 if (stops == null) {
2662 stops = new ArrayList<ActivityRecord>();
2663 }
2664 stops.add(s);
2665 mStoppingActivities.remove(i);
2666 N--;
2667 i--;
2668 }
2669 }
2670
2671 return stops;
2672 }
2673
Craig Mautnercf910b02013-04-23 11:23:27 -07002674 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002675 // FIXME
2676/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2677 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07002678 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002679 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002680 if (isFrontStack(stack)) {
2681 if (r == null) {
2682 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2683 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002684 final ActivityRecord pausing = stack.mPausingActivity;
2685 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002686 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002687 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002688 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002689 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002690 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002691 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002692 }
2693 }
2694 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002695 final ActivityRecord resumed = stack.mResumedActivity;
2696 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002697 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002698 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002699 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002700 if (r != null && (state == ActivityState.INITIALIZING
2701 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002702 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002703 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002704 }
2705 }
2706 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002707*/
Craig Mautner76ea2242013-05-15 11:40:05 -07002708 }
2709
Craig Mautner27084302013-03-25 08:05:25 -07002710 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002711 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity=");
Craig Mautner27084302013-03-25 08:05:25 -07002712 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002713 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002714 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002715 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2716 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2717 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002718 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07002719 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002720
Craig Mautner20e72272013-04-01 13:45:53 -07002721 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002722 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002723 }
2724
Dianne Hackborn390517b2013-05-30 15:03:32 -07002725 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2726 boolean needSep, String prefix) {
2727 if (activity != null) {
2728 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2729 if (needSep) {
2730 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002731 }
2732 pw.print(prefix);
2733 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002734 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002735 }
2736 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002737 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002738 }
2739
Craig Mautner8d341ef2013-03-26 09:03:27 -07002740 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2741 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002742 boolean printed = false;
2743 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002744 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2745 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
2746 pw.print("Display #"); pw.println(activityDisplay.mDisplayId);
2747 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002748 final int numStacks = stacks.size();
2749 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2750 final ActivityStack stack = stacks.get(stackNdx);
2751 StringBuilder stackHeader = new StringBuilder(128);
2752 stackHeader.append(" Stack #");
2753 stackHeader.append(stack.mStackId);
2754 stackHeader.append(":");
2755 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2756 needSep, stackHeader.toString());
2757 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2758 !dumpAll, false, dumpPackage, true,
2759 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002760
Craig Mautner4a1cb222013-12-04 16:14:06 -08002761 needSep = printed;
2762 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2763 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002764 if (pr) {
2765 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002766 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002767 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002768 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2769 " mResumedActivity: ");
2770 if (pr) {
2771 printed = true;
2772 needSep = false;
2773 }
2774 if (dumpAll) {
2775 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2776 " mLastPausedActivity: ");
2777 if (pr) {
2778 printed = true;
2779 needSep = true;
2780 }
2781 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2782 needSep, " mLastNoHistoryActivity: ");
2783 }
2784 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002785 }
2786 }
2787
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002788 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2789 false, dumpPackage, true, " Activities waiting to finish:", null);
2790 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2791 false, dumpPackage, true, " Activities waiting to stop:", null);
2792 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2793 false, dumpPackage, true, " Activities waiting for another to become visible:",
2794 null);
2795 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2796 false, dumpPackage, true, " Activities waiting to sleep:", null);
2797 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2798 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002799
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002800 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002801 }
2802
Dianne Hackborn390517b2013-05-30 15:03:32 -07002803 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002804 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002805 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002806 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002807 String innerPrefix = null;
2808 String[] args = null;
2809 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002810 for (int i=list.size()-1; i>=0; i--) {
2811 final ActivityRecord r = list.get(i);
2812 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2813 continue;
2814 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002815 if (innerPrefix == null) {
2816 innerPrefix = prefix + " ";
2817 args = new String[0];
2818 }
2819 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002820 final boolean full = !brief && (complete || !r.isInHistory());
2821 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002822 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002823 needNL = false;
2824 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002825 if (header1 != null) {
2826 pw.println(header1);
2827 header1 = null;
2828 }
2829 if (header2 != null) {
2830 pw.println(header2);
2831 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002832 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002833 if (lastTask != r.task) {
2834 lastTask = r.task;
2835 pw.print(prefix);
2836 pw.print(full ? "* " : " ");
2837 pw.println(lastTask);
2838 if (full) {
2839 lastTask.dump(pw, prefix + " ");
2840 } else if (complete) {
2841 // Complete + brief == give a summary. Isn't that obvious?!?
2842 if (lastTask.intent != null) {
2843 pw.print(prefix); pw.print(" ");
2844 pw.println(lastTask.intent.toInsecureStringWithClip());
2845 }
2846 }
2847 }
2848 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2849 pw.print(" #"); pw.print(i); pw.print(": ");
2850 pw.println(r);
2851 if (full) {
2852 r.dump(pw, innerPrefix);
2853 } else if (complete) {
2854 // Complete + brief == give a summary. Isn't that obvious?!?
2855 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2856 if (r.app != null) {
2857 pw.print(innerPrefix); pw.println(r.app);
2858 }
2859 }
2860 if (client && r.app != null && r.app.thread != null) {
2861 // flush anything that is already in the PrintWriter since the thread is going
2862 // to write to the file descriptor directly
2863 pw.flush();
2864 try {
2865 TransferPipe tp = new TransferPipe();
2866 try {
2867 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2868 r.appToken, innerPrefix, args);
2869 // Short timeout, since blocking here can
2870 // deadlock with the application.
2871 tp.go(fd, 2000);
2872 } finally {
2873 tp.kill();
2874 }
2875 } catch (IOException e) {
2876 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2877 } catch (RemoteException e) {
2878 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2879 }
2880 needNL = true;
2881 }
2882 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002883 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002884 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002885
Craig Mautnerf3333272013-04-22 10:55:53 -07002886 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002887 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002888 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2889 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002890 }
2891
2892 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002893 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002894 }
2895
2896 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002897 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002898 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2899 }
2900
Craig Mautner05d29032013-05-03 13:40:13 -07002901 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002902 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2903 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2904 }
Craig Mautner05d29032013-05-03 13:40:13 -07002905 }
2906
Craig Mautner0eea92c2013-05-16 13:35:39 -07002907 void removeSleepTimeouts() {
2908 mSleepTimeout = false;
2909 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2910 }
2911
2912 final void scheduleSleepTimeout() {
2913 removeSleepTimeouts();
2914 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2915 }
2916
Craig Mautner4a1cb222013-12-04 16:14:06 -08002917 @Override
2918 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002919 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002920 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
2921 }
2922
2923 @Override
2924 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002925 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
2927 }
2928
2929 @Override
2930 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002931 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002932 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
2933 }
2934
2935 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08002936 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002937 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08002938 newDisplay = mActivityDisplays.get(displayId) == null;
2939 if (newDisplay) {
2940 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
2941 mActivityDisplays.put(displayId, activityDisplay);
2942 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002943 }
Craig Mautner4504de52013-12-20 09:06:56 -08002944 if (newDisplay) {
2945 mWindowManager.onDisplayAdded(displayId);
2946 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002947 }
2948
2949 public void handleDisplayRemovedLocked(int displayId) {
2950 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002951 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2952 if (activityDisplay != null) {
2953 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002954 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002955 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002956 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002957 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002958 }
2959 }
2960 mWindowManager.onDisplayRemoved(displayId);
2961 }
2962
2963 public void handleDisplayChangedLocked(int displayId) {
2964 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002965 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2966 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002967 // TODO: Update the bounds.
2968 }
2969 }
2970 mWindowManager.onDisplayChanged(displayId);
2971 }
2972
2973 StackInfo getStackInfo(ActivityStack stack) {
2974 StackInfo info = new StackInfo();
2975 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
2976 info.displayId = Display.DEFAULT_DISPLAY;
2977 info.stackId = stack.mStackId;
2978
2979 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2980 final int numTasks = tasks.size();
2981 int[] taskIds = new int[numTasks];
2982 String[] taskNames = new String[numTasks];
2983 for (int i = 0; i < numTasks; ++i) {
2984 final TaskRecord task = tasks.get(i);
2985 taskIds[i] = task.taskId;
2986 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
2987 : task.realActivity != null ? task.realActivity.flattenToString()
2988 : task.getTopActivity() != null ? task.getTopActivity().packageName
2989 : "unknown";
2990 }
2991 info.taskIds = taskIds;
2992 info.taskNames = taskNames;
2993 return info;
2994 }
2995
2996 StackInfo getStackInfoLocked(int stackId) {
2997 ActivityStack stack = getStack(stackId);
2998 if (stack != null) {
2999 return getStackInfo(stack);
3000 }
3001 return null;
3002 }
3003
3004 ArrayList<StackInfo> getAllStackInfosLocked() {
3005 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003006 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3007 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003008 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3009 list.add(getStackInfo(stacks.get(ndx)));
3010 }
3011 }
3012 return list;
3013 }
3014
Jason Monk62515be2014-05-21 16:06:19 -04003015 void setLockTaskModeLocked(TaskRecord task, boolean showHomeRecents) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003016 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003017 // Take out of lock task mode if necessary
3018 if (mLockTaskModeTask != null) {
3019 final Message lockTaskMsg = Message.obtain();
3020 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3021 lockTaskMsg.what = LOCK_TASK_END_MSG;
3022 mLockTaskModeTask = null;
3023 mHandler.sendMessage(lockTaskMsg);
3024 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003025 return;
3026 }
3027 if (isLockTaskModeViolation(task)) {
3028 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3029 return;
3030 }
3031 mLockTaskModeTask = task;
3032 findTaskToMoveToFrontLocked(task, 0, null);
3033 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003034
3035 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003036 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3037 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003038 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monk62515be2014-05-21 16:06:19 -04003039 lockTaskMsg.arg2 = showHomeRecents ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003040 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003041 }
3042
3043 boolean isLockTaskModeViolation(TaskRecord task) {
3044 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3045 }
3046
3047 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3048 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
3049 mLockTaskModeTask = null;
3050 }
3051 }
3052
3053 boolean isInLockTaskMode() {
3054 return mLockTaskModeTask != null;
3055 }
3056
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003057 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003058
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003059 public ActivityStackSupervisorHandler(Looper looper) {
3060 super(looper);
3061 }
3062
Craig Mautnerf3333272013-04-22 10:55:53 -07003063 void activityIdleInternal(ActivityRecord r) {
3064 synchronized (mService) {
3065 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3066 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003067 }
3068
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003069 @Override
3070 public void handleMessage(Message msg) {
3071 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003072 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003073 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003074 if (mService.mDidDexOpt) {
3075 mService.mDidDexOpt = false;
3076 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3077 nmsg.obj = msg.obj;
3078 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3079 return;
3080 }
3081 // We don't at this point know if the activity is fullscreen,
3082 // so we need to be conservative and assume it isn't.
3083 activityIdleInternal((ActivityRecord)msg.obj);
3084 } break;
3085 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003086 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003087 activityIdleInternal((ActivityRecord)msg.obj);
3088 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003089 case RESUME_TOP_ACTIVITY_MSG: {
3090 synchronized (mService) {
3091 resumeTopActivitiesLocked();
3092 }
3093 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003094 case SLEEP_TIMEOUT_MSG: {
3095 synchronized (mService) {
3096 if (mService.isSleepingOrShuttingDown()) {
3097 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3098 mSleepTimeout = true;
3099 checkReadyForSleepLocked();
3100 }
3101 }
3102 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003103 case LAUNCH_TIMEOUT_MSG: {
3104 if (mService.mDidDexOpt) {
3105 mService.mDidDexOpt = false;
3106 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3107 return;
3108 }
3109 synchronized (mService) {
3110 if (mLaunchingActivity.isHeld()) {
3111 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3112 if (VALIDATE_WAKE_LOCK_CALLER
3113 && Binder.getCallingUid() != Process.myUid()) {
3114 throw new IllegalStateException("Calling must be system uid");
3115 }
3116 mLaunchingActivity.release();
3117 }
3118 }
3119 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003120 case HANDLE_DISPLAY_ADDED: {
3121 handleDisplayAddedLocked(msg.arg1);
3122 } break;
3123 case HANDLE_DISPLAY_CHANGED: {
3124 handleDisplayChangedLocked(msg.arg1);
3125 } break;
3126 case HANDLE_DISPLAY_REMOVED: {
3127 handleDisplayRemovedLocked(msg.arg1);
3128 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003129 case CONTAINER_CALLBACK_VISIBILITY: {
3130 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003131 final IActivityContainerCallback callback = container.mCallback;
3132 if (callback != null) {
3133 try {
3134 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3135 } catch (RemoteException e) {
3136 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003137 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003138 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003139 case LOCK_TASK_START_MSG: {
3140 // When lock task starts, we disable the status bars.
3141 try {
Jason Monk62515be2014-05-21 16:06:19 -04003142 if (mLockTaskNotify == null) {
3143 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003144 }
Jason Monk62515be2014-05-21 16:06:19 -04003145 mLockTaskNotify.show(true);
3146 if (getStatusBarService() != null) {
3147 int flags =
3148 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
3149 if (msg.arg2 != 0) {
3150 flags ^= StatusBarManager.DISABLE_HOME
3151 | StatusBarManager.DISABLE_RECENT;
3152 }
3153 getStatusBarService().disable(flags, mToken,
3154 mService.mContext.getPackageName());
3155 }
3156 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003157 if (getDevicePolicyManager() != null) {
3158 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003159 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003160 }
justinzhang5286d3f2014-05-12 17:06:01 -04003161 } catch (RemoteException ex) {
3162 throw new RuntimeException(ex);
3163 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003164 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003165 case LOCK_TASK_END_MSG: {
3166 // When lock task ends, we enable the status bars.
3167 try {
Jason Monk62515be2014-05-21 16:06:19 -04003168 if (getStatusBarService() != null) {
3169 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3170 mService.mContext.getPackageName());
3171 }
3172 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003173 if (getDevicePolicyManager() != null) {
3174 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3175 msg.arg1);
3176 }
Jason Monk62515be2014-05-21 16:06:19 -04003177 if (mLockTaskNotify == null) {
3178 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3179 }
3180 mLockTaskNotify.show(false);
3181 try {
3182 boolean shouldLockKeyguard = Settings.System.getInt(
3183 mService.mContext.getContentResolver(),
3184 Settings.System.LOCK_TO_APP_EXIT_LOCKED) != 0;
3185 if (shouldLockKeyguard) {
3186 mWindowManager.lockNow(null);
3187 }
3188 } catch (SettingNotFoundException e) {
3189 // No setting, don't lock.
3190 }
justinzhang5286d3f2014-05-12 17:06:01 -04003191 } catch (RemoteException ex) {
3192 throw new RuntimeException(ex);
3193 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003194 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003195 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3196 final ActivityContainer container = (ActivityContainer) msg.obj;
3197 final IActivityContainerCallback callback = container.mCallback;
3198 if (callback != null) {
3199 try {
3200 callback.onAllActivitiesComplete(container.asBinder());
3201 } catch (RemoteException e) {
3202 }
3203 }
3204 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003205 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3206 synchronized (mService) {
3207 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3208 msg.obj);
3209 ((ActivityContainer) msg.obj).onTaskListEmptyLocked();
3210 }
3211 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003212 }
3213 }
3214 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003215
Ying Wangb081a592014-04-22 15:20:16 -07003216 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003217 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003218 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003219 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003220 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003221 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003222 ActivityRecord mParentActivity = null;
3223 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003224
Craig Mautnere3a00d72014-04-16 08:31:19 -07003225 boolean mVisible = true;
3226
Craig Mautner4a1cb222013-12-04 16:14:06 -08003227 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003228 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003229
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003230 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3231 final static int CONTAINER_STATE_NO_SURFACE = 1;
3232 final static int CONTAINER_STATE_FINISHING = 2;
3233 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3234
3235 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003236 synchronized (mService) {
3237 mStackId = stackId;
3238 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003239 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003240 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003241 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003242 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003243
Craig Mautnere0a38842013-12-16 16:14:02 -08003244 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003245 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3246 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003247 mActivityDisplay = activityDisplay;
3248 mStack.mDisplayId = activityDisplay.mDisplayId;
3249 mStack.mStacks = activityDisplay.mStacks;
3250
3251 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003252 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003253 }
3254
3255 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003256 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003257 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003258 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3259 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003260 return;
3261 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003262 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003263 }
3264 }
3265
3266 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003267 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003268 synchronized (mService) {
3269 if (mActivityDisplay != null) {
3270 return mActivityDisplay.mDisplayId;
3271 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003272 }
3273 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003274 }
3275
Jeff Brownca9bc702014-02-11 14:32:56 -08003276 @Override
3277 public boolean injectEvent(InputEvent event) {
3278 final long origId = Binder.clearCallingIdentity();
3279 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003280 synchronized (mService) {
3281 if (mActivityDisplay != null) {
3282 return mInputManagerInternal.injectInputEvent(event,
3283 mActivityDisplay.mDisplayId,
3284 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3285 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003286 }
3287 return false;
3288 } finally {
3289 Binder.restoreCallingIdentity(origId);
3290 }
3291 }
3292
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003293 @Override
3294 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003295 synchronized (mService) {
3296 if (mContainerState == CONTAINER_STATE_FINISHING) {
3297 return;
3298 }
3299 mContainerState = CONTAINER_STATE_FINISHING;
3300
3301 final Message msg =
3302 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3303 mHandler.sendMessageDelayed(msg, 1000);
3304
3305 long origId = Binder.clearCallingIdentity();
3306 try {
3307 mStack.finishAllActivitiesLocked();
3308 } finally {
3309 Binder.restoreCallingIdentity(origId);
3310 }
3311 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003312 }
3313
Craig Mautner34b73df2014-01-12 21:11:08 -08003314 private void detachLocked() {
3315 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3316 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003317 if (mActivityDisplay != null) {
3318 mActivityDisplay.detachActivitiesLocked(mStack);
3319 mActivityDisplay = null;
3320 mStack.mDisplayId = -1;
3321 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003322 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003323 }
3324 }
3325
3326 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003327 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003328 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003329 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3330 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3331 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003332 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003333 String mimeType = intent.getType();
3334 if (mimeType == null && intent.getData() != null
3335 && "content".equals(intent.getData().getScheme())) {
3336 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3337 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003338 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0, 0, null,
Craig Mautnere0a38842013-12-16 16:14:02 -08003339 null, null, null, null, userId, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003340 }
3341
3342 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003343 public final int startActivityIntentSender(IIntentSender intentSender) {
3344 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3345
3346 if (!(intentSender instanceof PendingIntentRecord)) {
3347 throw new IllegalArgumentException("Bad PendingIntent object");
3348 }
3349
3350 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003351 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003352 }
3353
Craig Mautner247ab652014-04-25 10:09:00 -07003354 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3355 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3356 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3357 if (resolvedType == null) {
3358 resolvedType = intent.getType();
3359 if (resolvedType == null && intent.getData() != null
3360 && "content".equals(intent.getData().getScheme())) {
3361 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3362 }
3363 }
3364 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003365 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003366 throw new SecurityException(
3367 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3368 }
3369 }
3370
3371 /** Throw a SecurityException if allowEmbedded is not true */
3372 @Override
3373 public final void checkEmbeddedAllowed(Intent intent) {
3374 checkEmbeddedAllowedInner(intent, null);
3375 }
3376
3377 /** Throw a SecurityException if allowEmbedded is not true */
3378 @Override
3379 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3380 if (!(intentSender instanceof PendingIntentRecord)) {
3381 throw new IllegalArgumentException("Bad PendingIntent object");
3382 }
3383 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3384 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3385 pendingIntent.key.requestResolvedType);
3386 }
3387
Craig Mautnerdf88d732014-01-27 09:21:32 -08003388 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003389 public IBinder asBinder() {
3390 return this;
3391 }
3392
Craig Mautner4504de52013-12-20 09:06:56 -08003393 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003394 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003395 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003396 }
3397
Craig Mautner4a1cb222013-12-04 16:14:06 -08003398 ActivityStackSupervisor getOuter() {
3399 return ActivityStackSupervisor.this;
3400 }
3401
Craig Mautnerd163e752014-06-13 17:18:47 -07003402 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003403 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003404 }
3405
3406 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003407 synchronized (mService) {
3408 if (mActivityDisplay != null) {
3409 mActivityDisplay.getBounds(outBounds);
3410 } else {
3411 outBounds.set(0, 0);
3412 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003413 }
3414 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003415
Craig Mautner6985bad2014-04-21 15:22:06 -07003416 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003417 void setVisible(boolean visible) {
3418 if (mVisible != visible) {
3419 mVisible = visible;
3420 if (mCallback != null) {
3421 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3422 0 /* unused */, this).sendToTarget();
3423 }
3424 }
3425 }
3426
Craig Mautner6985bad2014-04-21 15:22:06 -07003427 void setDrawn() {
3428 }
3429
Craig Mautner1b4bf852014-05-26 15:06:32 -07003430 // You can always start a new task on a regular ActivityStack.
3431 boolean isEligibleForNewTasks() {
3432 return true;
3433 }
3434
Craig Mautnerd163e752014-06-13 17:18:47 -07003435 void onTaskListEmptyLocked() {
3436 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3437 if (!mStack.isHomeStack()) {
3438 detachLocked();
3439 deleteActivityContainer(this);
3440 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003441 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3442 }
3443
Craig Mautner34b73df2014-01-12 21:11:08 -08003444 @Override
3445 public String toString() {
3446 return mIdString + (mActivityDisplay == null ? "N" : "A");
3447 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003448 }
3449
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003450 private class VirtualActivityContainer extends ActivityContainer {
3451 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003452 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003453
3454 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3455 super(getNextStackId());
3456 mParentActivity = parent;
3457 mCallback = callback;
3458 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003459 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003460 }
3461
3462 @Override
3463 public void setSurface(Surface surface, int width, int height, int density) {
3464 super.setSurface(surface, width, height, density);
3465
3466 synchronized (mService) {
3467 final long origId = Binder.clearCallingIdentity();
3468 try {
3469 setSurfaceLocked(surface, width, height, density);
3470 } finally {
3471 Binder.restoreCallingIdentity(origId);
3472 }
3473 }
3474 }
3475
3476 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3477 if (mContainerState == CONTAINER_STATE_FINISHING) {
3478 return;
3479 }
3480 VirtualActivityDisplay virtualActivityDisplay =
3481 (VirtualActivityDisplay) mActivityDisplay;
3482 if (virtualActivityDisplay == null) {
3483 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003484 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003485 mActivityDisplay = virtualActivityDisplay;
3486 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3487 attachToDisplayLocked(virtualActivityDisplay);
3488 }
3489
3490 if (mSurface != null) {
3491 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003492 }
3493
Craig Mautner6985bad2014-04-21 15:22:06 -07003494 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003495 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003496 mStack.resumeTopActivityLocked(null);
3497 } else {
3498 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003499 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003500 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
3501 mStack.startPausingLocked(false, true);
3502 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003503 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003504
Craig Mautnerd163e752014-06-13 17:18:47 -07003505 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003506
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003507 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3508 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003509 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003510
Craig Mautner6985bad2014-04-21 15:22:06 -07003511 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003512 boolean isAttachedLocked() {
3513 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003514 }
3515
3516 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003517 void setDrawn() {
3518 synchronized (mService) {
3519 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003520 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003521 }
3522 }
3523
Craig Mautner1b4bf852014-05-26 15:06:32 -07003524 // Never start a new task on an ActivityView if it isn't explicitly specified.
3525 @Override
3526 boolean isEligibleForNewTasks() {
3527 return false;
3528 }
3529
Craig Mautnerd163e752014-06-13 17:18:47 -07003530 private void setSurfaceIfReadyLocked() {
3531 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003532 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3533 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3534 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3535 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3536 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003537 }
3538 }
3539
Craig Mautner4a1cb222013-12-04 16:14:06 -08003540 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3541 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003542 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003543 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003544 int mDisplayId;
3545 Display mDisplay;
3546 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003547
Craig Mautner4a1cb222013-12-04 16:14:06 -08003548 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3549 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003550 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003551
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003552 ActivityDisplay() {
3553 }
Craig Mautner4504de52013-12-20 09:06:56 -08003554
Craig Mautnere0a38842013-12-16 16:14:02 -08003555 ActivityDisplay(int displayId) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003556 init(mDisplayManager.getDisplay(displayId));
Craig Mautner4504de52013-12-20 09:06:56 -08003557 }
3558
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003559 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003560 mDisplay = display;
3561 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003562 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003563 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003564
3565 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003566 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3567 + mDisplayId);
3568 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003569 }
3570
Craig Mautnere0a38842013-12-16 16:14:02 -08003571 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003572 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003573 + " from displayId=" + mDisplayId);
3574 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003575 }
3576
3577 void getBounds(Point bounds) {
3578 mDisplay.getDisplayInfo(mDisplayInfo);
3579 bounds.x = mDisplayInfo.appWidth;
3580 bounds.y = mDisplayInfo.appHeight;
3581 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003582
3583 @Override
3584 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003585 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3586 }
3587 }
3588
3589 class VirtualActivityDisplay extends ActivityDisplay {
3590 VirtualDisplay mVirtualDisplay;
3591
Craig Mautner6985bad2014-04-21 15:22:06 -07003592 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003593 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
3594 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, VIRTUAL_DISPLAY_BASE_NAME,
Craig Mautner6985bad2014-04-21 15:22:06 -07003595 width, height, density, null, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003596 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY);
3597
3598 init(mVirtualDisplay.getDisplay());
3599
3600 mWindowManager.handleDisplayAdded(mDisplayId);
3601 }
3602
3603 void setSurface(Surface surface) {
3604 if (mVirtualDisplay != null) {
3605 mVirtualDisplay.setSurface(surface);
3606 }
3607 }
3608
3609 @Override
3610 void detachActivitiesLocked(ActivityStack stack) {
3611 super.detachActivitiesLocked(stack);
3612 if (mVirtualDisplay != null) {
3613 mVirtualDisplay.release();
3614 mVirtualDisplay = null;
3615 }
3616 }
3617
3618 @Override
3619 public String toString() {
3620 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003621 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003622 }
Jose Lima58e66d62014-05-27 20:00:27 -07003623
3624 private boolean isLeanbackOnlyDevice() {
3625 boolean onLeanbackOnly = false;
3626 try {
3627 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
3628 PackageManager.FEATURE_LEANBACK_ONLY);
3629 } catch (RemoteException e) {
3630 // noop
3631 }
3632
3633 return onLeanbackOnly;
3634 }
Craig Mautner27084302013-03-25 08:05:25 -07003635}