blob: 2d6f3cf5079bd56242d9b47052b95cb31e1d7ff8 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner29219d92013-04-16 20:19:12 -070020import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070022import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Craig Mautner2420ead2013-04-01 17:13:20 -070023import static com.android.server.am.ActivityManagerService.localLOGV;
Craig Mautner23ac33b2013-04-01 16:26:35 -070024import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070025import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Wale Ogunwale7de05352014-12-12 15:21:33 -080027import static com.android.server.am.ActivityManagerService.DEBUG_RECENTS;
Craig Mautner6170f732013-04-02 13:05:23 -070028import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070029import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070030import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070031import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
32import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070033import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070034import static com.android.server.am.ActivityManagerService.TAG;
Craig Mautner84984fa2014-06-19 11:19:20 -070035import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
37import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070038
Craig Mautner2420ead2013-04-01 17:13:20 -070039import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070040import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080041import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.app.ActivityOptions;
43import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080044import android.app.IActivityContainer;
45import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070048import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070049import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070050import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070052import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040053import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040054import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070056import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.pm.ApplicationInfo;
62import android.content.pm.PackageManager;
63import android.content.pm.ResolveInfo;
64import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080065import android.graphics.Point;
66import android.hardware.display.DisplayManager;
67import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080068import android.hardware.display.DisplayManagerGlobal;
69import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080070import android.hardware.input.InputManager;
71import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070072import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070073import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070074import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070075import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070077import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070078import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070079import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070080import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070081import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070082import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040083import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070085import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040086import android.provider.Settings;
87import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070088import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070089import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070090import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070091import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080092import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070093
Craig Mautner4a1cb222013-12-04 16:14:06 -080094import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080095import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080096import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080097import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080098import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700100import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800101import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700102import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400103import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400104import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800105import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700106import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700107import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108
justinzhang5286d3f2014-05-12 17:06:01 -0400109
Craig Mautner8d341ef2013-03-26 09:03:27 -0700110import java.io.FileDescriptor;
111import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700112import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700113import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700114import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700115
Craig Mautner4a1cb222013-12-04 16:14:06 -0800116public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700117 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
118 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
119 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700120 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700121 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700122 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700123 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
124 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner30bd3bb2014-11-13 00:55:53 +0000125 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700126 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700127
Craig Mautner2219a1b2013-03-25 09:44:30 -0700128 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700129
Craig Mautnerf3333272013-04-22 10:55:53 -0700130 /** How long we wait until giving up on the last activity telling us it is idle. */
131 static final int IDLE_TIMEOUT = 10*1000;
132
Craig Mautner0eea92c2013-05-16 13:35:39 -0700133 /** How long we can hold the sleep wake lock before giving up. */
134 static final int SLEEP_TIMEOUT = 5*1000;
135
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700136 // How long we can hold the launch wake lock before giving up.
137 static final int LAUNCH_TIMEOUT = 10*1000;
138
Craig Mautner05d29032013-05-03 13:40:13 -0700139 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
140 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
141 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700142 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700143 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800144 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
145 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
146 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700147 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400148 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
149 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700150 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700151 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700152 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800153
Craig Mautner4504de52013-12-20 09:06:56 -0800154 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700155
Jason Monk62515be2014-05-21 16:06:19 -0400156 private static final String LOCK_TASK_TAG = "Lock-to-App";
157
justinzhang5286d3f2014-05-12 17:06:01 -0400158 /** Status Bar Service **/
159 private IBinder mToken = new Binder();
160 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400161 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400162
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700163 // For debugging to make sure the caller when acquiring/releasing our
164 // wake lock is the system process.
165 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700166
Craig Mautner27084302013-03-25 08:05:25 -0700167 final ActivityManagerService mService;
168
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800169 private final RecentTasks mRecentTasks;
170
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700171 final ActivityStackSupervisorHandler mHandler;
172
173 /** Short cut */
174 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800175 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700176
Craig Mautner8d341ef2013-03-26 09:03:27 -0700177 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700178 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700179
180 /** Task identifier that activities are currently being started in. Incremented each time a
181 * new task is created. */
182 private int mCurTaskId = 0;
183
Craig Mautner2420ead2013-04-01 17:13:20 -0700184 /** The current user */
185 private int mCurrentUser;
186
Craig Mautnere0a38842013-12-16 16:14:02 -0800187 /** The stack containing the launcher app. Assumed to always be attached to
188 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700189 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700190
Craig Mautnere0a38842013-12-16 16:14:02 -0800191 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700192 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700193
Craig Mautner4a1cb222013-12-04 16:14:06 -0800194 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
195 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800196 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800197 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700198
199 /** List of activities that are waiting for a new activity to become visible before completing
200 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800201 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700202
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700203 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800204 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700205
206 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800207 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700208
Craig Mautnerde4ef022013-04-07 19:01:33 -0700209 /** List of activities that are ready to be stopped, but waiting for the next activity to
210 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800211 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700212
Craig Mautnerf3333272013-04-22 10:55:53 -0700213 /** List of activities that are ready to be finished, but waiting for the previous activity to
214 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800215 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700216
Craig Mautner0eea92c2013-05-16 13:35:39 -0700217 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800218 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700219
Craig Mautnerf3333272013-04-22 10:55:53 -0700220 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800221 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700222
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700223 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800224 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700225
Craig Mautnerde4ef022013-04-07 19:01:33 -0700226 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
227 * is being brought in front of us. */
228 boolean mUserLeaving = false;
229
Craig Mautner0eea92c2013-05-16 13:35:39 -0700230 /** Set when we have taken too long waiting to go to sleep. */
231 boolean mSleepTimeout = false;
232
Jose Lima58e66d62014-05-27 20:00:27 -0700233 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
234 * setWindowManager is called. **/
235 private boolean mLeanbackOnlyDevice;
236
Craig Mautner0eea92c2013-05-16 13:35:39 -0700237 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700238 * We don't want to allow the device to go to sleep while in the process
239 * of launching an activity. This is primarily to allow alarm intent
240 * receivers to launch an activity and get that to run before the device
241 * goes back to sleep.
242 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700243 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700244
245 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700246 * Set when the system is going to sleep, until we have
247 * successfully paused the current activity and released our wake lock.
248 * At that point the system is allowed to actually sleep.
249 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700250 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700251
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700252 /** Stack id of the front stack when user switched, indexed by userId. */
253 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700254
Craig Mautner4504de52013-12-20 09:06:56 -0800255 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800256 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700257 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800258
259 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700260 private final SparseArray<ActivityDisplay> mActivityDisplays =
261 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800262
Jeff Brownca9bc702014-02-11 14:32:56 -0800263 InputManagerInternal mInputManagerInternal;
264
Craig Mautneraea74a52014-03-08 14:23:10 -0800265 /** If non-null then the task specified remains in front and no other tasks may be started
266 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400267 TaskRecord mLockTaskModeTask;
Jason Monka8f569c2014-07-07 12:15:21 -0400268 /** Whether lock task has been entered by an authorized app and cannot
269 * be exited. */
270 private boolean mLockTaskIsLocked;
Jason Monk62515be2014-05-21 16:06:19 -0400271 /**
272 * Notifies the user when entering/exiting lock-task.
273 */
274 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800275
Craig Mautneree36c772014-07-16 14:56:05 -0700276 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
277 = new ArrayList<PendingActivityLaunch>();
278
Craig Mautner42d04db2014-11-06 12:13:23 -0800279 /** Used to keep resumeTopActivityLocked() from being entered recursively */
280 boolean inResumeTopActivity;
281
Craig Mautneree36c772014-07-16 14:56:05 -0700282 /**
283 * Description of a request to start a new activity, which has been held
284 * due to app switches being disabled.
285 */
286 static class PendingActivityLaunch {
287 final ActivityRecord r;
288 final ActivityRecord sourceRecord;
289 final int startFlags;
290 final ActivityStack stack;
291
292 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
293 int _startFlags, ActivityStack _stack) {
294 r = _r;
295 sourceRecord = _sourceRecord;
296 startFlags = _startFlags;
297 stack = _stack;
298 }
299 }
300
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800301 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700302 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800303 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700304 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
305 }
306
307 /**
308 * At the time when the constructor runs, the power manager has not yet been
309 * initialized. So we initialize our wakelocks afterwards.
310 */
311 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800312 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700313 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700314 mLaunchingActivity =
315 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
316 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700317 }
318
justinzhang5286d3f2014-05-12 17:06:01 -0400319 // This function returns a IStatusBarService. The value is from ServiceManager.
320 // getService and is cached.
321 private IStatusBarService getStatusBarService() {
322 synchronized (mService) {
323 if (mStatusBarService == null) {
324 mStatusBarService = IStatusBarService.Stub.asInterface(
325 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
326 if (mStatusBarService == null) {
327 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
328 }
329 }
330 return mStatusBarService;
331 }
332 }
333
Jason Monk35c62a42014-06-17 10:24:47 -0400334 private IDevicePolicyManager getDevicePolicyManager() {
335 synchronized (mService) {
336 if (mDevicePolicyManager == null) {
337 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
338 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
339 if (mDevicePolicyManager == null) {
340 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
341 }
342 }
343 return mDevicePolicyManager;
344 }
345 }
346
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700347 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800348 synchronized (mService) {
349 mWindowManager = wm;
350
351 mDisplayManager =
352 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
353 mDisplayManager.registerDisplayListener(this, null);
354
355 Display[] displays = mDisplayManager.getDisplays();
356 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
357 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800358 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700359 if (activityDisplay.mDisplay == null) {
360 throw new IllegalStateException("Default Display does not exist");
361 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800362 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800363 }
364
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700365 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800366 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800367
368 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700369
370 // Initialize this here, now that we can get a valid reference to PackageManager.
371 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800372 }
Craig Mautner27084302013-03-25 08:05:25 -0700373 }
374
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200375 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700376 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200377 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700378 }
379
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700380 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700382 }
383
Craig Mautnerde4ef022013-04-07 19:01:33 -0700384 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800385 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700386 }
387
Craig Mautner4a1cb222013-12-04 16:14:06 -0800388 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
389 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700390 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800391 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
392 if (parent != null) {
393 stack = parent.task.stack;
394 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800395 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800396 if (stacks != null && !stacks.isEmpty()) {
397 return stack == stacks.get(stacks.size() - 1);
398 }
399 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700400 }
401
Craig Mautnerde4ef022013-04-07 19:01:33 -0700402 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800403 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800404 int topNdx = stacks.size() - 1;
405 if (topNdx <= 0) {
406 return;
407 }
408 ActivityStack topStack = stacks.get(topNdx);
409 final boolean homeInFront = topStack == mHomeStack;
410 if (homeInFront != toFront) {
411 mLastFocusedStack = topStack;
412 stacks.remove(mHomeStack);
413 stacks.add(toFront ? topNdx : 0, mHomeStack);
414 mFocusedStack = stacks.get(topNdx);
415 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
416 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700417 }
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800418 if (mService.mBooting || !mService.mBooted) {
419 final ActivityRecord r = topRunningActivityLocked();
420 if (r != null && r.idle) {
421 checkFinishBootingLocked();
422 }
423 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700424 }
425
Craig Mautner84984fa2014-06-19 11:19:20 -0700426 void moveHomeStackTaskToTop(int homeStackTaskType) {
427 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
428 mWindowManager.showRecentApps();
429 return;
430 }
Craig Mautner69ada552013-04-18 13:51:51 -0700431 moveHomeStack(true);
Craig Mautner84984fa2014-06-19 11:19:20 -0700432 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700433 }
434
Craig Mautner84984fa2014-06-19 11:19:20 -0700435 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700436 if (!mService.mBooting && !mService.mBooted) {
437 // Not ready yet!
438 return false;
439 }
440
Craig Mautner84984fa2014-06-19 11:19:20 -0700441 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
442 mWindowManager.showRecentApps();
443 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700444 }
Craig Mautner84984fa2014-06-19 11:19:20 -0700445 moveHomeStackTaskToTop(homeStackTaskType);
446 if (prev != null) {
447 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
448 }
449
Craig Mautnera8a90e02013-06-28 15:24:50 -0700450 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700451 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000452 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700453 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700454 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700455 }
456 return mService.startHomeActivityLocked(mCurrentUser);
457 }
458
Craig Mautner8d341ef2013-03-26 09:03:27 -0700459 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800460 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800461 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800462 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800463 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
464 ActivityStack stack = stacks.get(stackNdx);
465 TaskRecord task = stack.taskForIdLocked(id);
466 if (task != null) {
467 return task;
468 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700469 }
470 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800471
472 // Don't give up! Look in recents.
473 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800474 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800475 if (task == null) {
476 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
477 return null;
478 }
479
480 if (!restoreRecentTaskLocked(task)) {
481 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
482 return null;
483 }
484 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
485 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700486 }
487
Craig Mautner6170f732013-04-02 13:05:23 -0700488 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800489 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800490 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800491 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800492 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
493 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
494 if (r != null) {
495 return r;
496 }
Craig Mautner6170f732013-04-02 13:05:23 -0700497 }
498 }
499 return null;
500 }
501
Craig Mautneref73ee12014-04-23 11:45:37 -0700502 void setNextTaskId(int taskId) {
503 if (taskId > mCurTaskId) {
504 mCurTaskId = taskId;
505 }
506 }
507
Craig Mautner8d341ef2013-03-26 09:03:27 -0700508 int getNextTaskId() {
509 do {
510 mCurTaskId++;
511 if (mCurTaskId <= 0) {
512 mCurTaskId = 1;
513 }
514 } while (anyTaskForIdLocked(mCurTaskId) != null);
515 return mCurTaskId;
516 }
517
Craig Mautnerde4ef022013-04-07 19:01:33 -0700518 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700519 ActivityStack stack = getFocusedStack();
520 if (stack == null) {
521 return null;
522 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700523 ActivityRecord resumedActivity = stack.mResumedActivity;
524 if (resumedActivity == null || resumedActivity.app == null) {
525 resumedActivity = stack.mPausingActivity;
526 if (resumedActivity == null || resumedActivity.app == null) {
527 resumedActivity = stack.topRunningActivityLocked(null);
528 }
529 }
530 return resumedActivity;
531 }
532
Dianne Hackbornff072722014-09-24 10:56:28 -0700533 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700534 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800535 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800536 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
537 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800538 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
539 final ActivityStack stack = stacks.get(stackNdx);
540 if (!isFrontStack(stack)) {
541 continue;
542 }
543 ActivityRecord hr = stack.topRunningActivityLocked(null);
544 if (hr != null) {
545 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
546 && processName.equals(hr.processName)) {
547 try {
George Mount2c92c972014-03-20 09:38:23 -0700548 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800549 didSomething = true;
550 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700551 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800552 Slog.w(TAG, "Exception in new application when starting activity "
553 + hr.intent.getComponent().flattenToShortString(), e);
554 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700555 }
Craig Mautner20e72272013-04-01 13:45:53 -0700556 }
Craig Mautner20e72272013-04-01 13:45:53 -0700557 }
558 }
559 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700560 if (!didSomething) {
561 ensureActivitiesVisibleLocked(null, 0);
562 }
Craig Mautner20e72272013-04-01 13:45:53 -0700563 return didSomething;
564 }
565
566 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800567 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
568 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800569 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
570 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800571 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 continue;
573 }
574 final ActivityRecord resumedActivity = stack.mResumedActivity;
575 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800576 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
577 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800578 return false;
579 }
Craig Mautner20e72272013-04-01 13:45:53 -0700580 }
581 }
582 return true;
583 }
584
Craig Mautnerde4ef022013-04-07 19:01:33 -0700585 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800586 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
587 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800588 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
589 final ActivityStack stack = stacks.get(stackNdx);
590 if (isFrontStack(stack)) {
591 final ActivityRecord r = stack.mResumedActivity;
592 if (r != null && r.state != ActivityState.RESUMED) {
593 return false;
594 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700595 }
596 }
597 }
598 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800599 if (DEBUG_STACK) Slog.d(TAG,
600 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
601 mLastFocusedStack + " to=" + mFocusedStack);
602 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700603 return true;
604 }
605
606 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800607 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
608 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
610 final ActivityStack stack = stacks.get(stackNdx);
611 final ActivityRecord r = stack.mResumedActivity;
Craig Mautner8c14c152015-01-15 17:32:07 -0800612 if (r != null && (!r.nowVisible || mWaitingVisibleActivities.contains(r))) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613 return false;
614 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700615 }
616 }
617 return true;
618 }
619
Craig Mautner2acc3892013-09-23 10:28:14 -0700620 /**
621 * Pause all activities in either all of the stacks or just the back stacks.
622 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700623 * @return true if any activity was paused as a result of this call.
624 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700625 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700626 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800627 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
628 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
630 final ActivityStack stack = stacks.get(stackNdx);
631 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
632 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
633 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700634 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
635 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800636 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700637 }
638 }
639 return someActivityPaused;
640 }
641
Craig Mautnerde4ef022013-04-07 19:01:33 -0700642 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700643 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800644 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
645 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800646 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
647 final ActivityStack stack = stacks.get(stackNdx);
648 final ActivityRecord r = stack.mPausingActivity;
649 if (r != null && r.state != ActivityState.PAUSED
650 && r.state != ActivityState.STOPPED
651 && r.state != ActivityState.STOPPING) {
652 if (DEBUG_STATES) {
653 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
654 pausing = false;
655 } else {
656 return false;
657 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700658 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700659 }
660 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700661 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700662 }
663
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700664 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
665 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500666 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800667 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
668 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
669 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
670 final ActivityStack stack = stacks.get(stackNdx);
671 if (stack.mResumedActivity != null &&
672 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700673 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800674 }
675 }
676 }
677 }
678
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700679 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700680 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700681 }
682
683 void sendWaitingVisibleReportLocked(ActivityRecord r) {
684 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700685 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700686 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700687 if (w.who == null) {
688 changed = true;
689 w.timeout = false;
690 if (r != null) {
691 w.who = new ComponentName(r.info.packageName, r.info.name);
692 }
693 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
694 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700695 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700696 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700697 if (changed) {
698 mService.notifyAll();
699 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700700 }
701
702 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
703 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700704 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700705 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700706 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700707 if (w.who == null) {
708 changed = true;
709 w.timeout = timeout;
710 if (r != null) {
711 w.who = new ComponentName(r.info.packageName, r.info.name);
712 }
713 w.thisTime = thisTime;
714 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700715 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700716 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700717 if (changed) {
718 mService.notifyAll();
719 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700720 }
721
Craig Mautner29219d92013-04-16 20:19:12 -0700722 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700723 final ActivityStack focusedStack = getFocusedStack();
724 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
725 if (r != null) {
726 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700727 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700728
Craig Mautner4a1cb222013-12-04 16:14:06 -0800729 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800730 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800731 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
732 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700733 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700734 r = stack.topRunningActivityLocked(null);
735 if (r != null) {
736 return r;
737 }
738 }
739 }
740 return null;
741 }
742
Dianne Hackborn09233282014-04-30 11:33:59 -0700743 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700744 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800745 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
746 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800747 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800749 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800750 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
751 final ActivityStack stack = stacks.get(stackNdx);
752 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
753 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700754 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700755 }
756 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700757
758 // The lists are already sorted from most recent to oldest. Just pull the most recent off
759 // each list and add it to list. Stop when all lists are empty or maxNum reached.
760 while (maxNum > 0) {
761 long mostRecentActiveTime = Long.MIN_VALUE;
762 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800763 final int numTaskLists = runningTaskLists.size();
764 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
765 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700766 if (!stackTaskList.isEmpty()) {
767 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
768 if (lastActiveTime > mostRecentActiveTime) {
769 mostRecentActiveTime = lastActiveTime;
770 selectedStackList = stackTaskList;
771 }
772 }
773 }
774 if (selectedStackList != null) {
775 list.add(selectedStackList.remove(0));
776 --maxNum;
777 } else {
778 break;
779 }
780 }
Craig Mautner20e72272013-04-01 13:45:53 -0700781 }
782
Craig Mautner23ac33b2013-04-01 16:26:35 -0700783 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700784 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700785 // Collect information about the target of the Intent.
786 ActivityInfo aInfo;
787 try {
788 ResolveInfo rInfo =
789 AppGlobals.getPackageManager().resolveIntent(
790 intent, resolvedType,
791 PackageManager.MATCH_DEFAULT_ONLY
792 | ActivityManagerService.STOCK_PM_FLAGS, userId);
793 aInfo = rInfo != null ? rInfo.activityInfo : null;
794 } catch (RemoteException e) {
795 aInfo = null;
796 }
797
798 if (aInfo != null) {
799 // Store the found target back into the intent, because now that
800 // we have it we never want to do this again. For example, if the
801 // user navigates back to this point in the history, we should
802 // always restart the exact same activity.
803 intent.setComponent(new ComponentName(
804 aInfo.applicationInfo.packageName, aInfo.name));
805
806 // Don't debug things in the system process
807 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
808 if (!aInfo.processName.equals("system")) {
809 mService.setDebugApp(aInfo.processName, true, false);
810 }
811 }
812
813 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
814 if (!aInfo.processName.equals("system")) {
815 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
816 }
817 }
818
Jeff Hao1b012d32014-08-20 10:35:34 -0700819 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700820 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700821 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700822 }
823 }
824 }
825 return aInfo;
826 }
827
Craig Mautner2219a1b2013-03-25 09:44:30 -0700828 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700829 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE);
Dianne Hackborn95465202014-09-15 16:21:55 -0700830 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
831 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700832 }
833
Craig Mautner23ac33b2013-04-01 16:26:35 -0700834 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700835 String callingPackage, Intent intent, String resolvedType,
836 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700837 IBinder resultTo, String resultWho, int requestCode, int startFlags,
838 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700839 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700840 // Refuse possible leaked file descriptors
841 if (intent != null && intent.hasFileDescriptors()) {
842 throw new IllegalArgumentException("File descriptors passed in Intent");
843 }
844 boolean componentSpecified = intent.getComponent() != null;
845
846 // Don't modify the client's object!
847 intent = new Intent(intent);
848
849 // Collect information about the target of the Intent.
850 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700851 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700852
Craig Mautnere0a38842013-12-16 16:14:02 -0800853 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700854 synchronized (mService) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700855 final int realCallingPid = Binder.getCallingPid();
856 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700857 int callingPid;
858 if (callingUid >= 0) {
859 callingPid = -1;
860 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700861 callingPid = realCallingPid;
862 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700863 } else {
864 callingPid = callingUid = -1;
865 }
866
Craig Mautnere0a38842013-12-16 16:14:02 -0800867 final ActivityStack stack;
868 if (container == null || container.mStack.isOnHomeDisplay()) {
869 stack = getFocusedStack();
870 } else {
871 stack = container.mStack;
872 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700873 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700874 && mService.mConfiguration.diff(config) != 0;
875 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700876 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700877
878 final long origId = Binder.clearCallingIdentity();
879
880 if (aInfo != null &&
881 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
882 // This may be a heavy-weight process! Check to see if we already
883 // have another, different heavy-weight process running.
884 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
885 if (mService.mHeavyWeightProcess != null &&
886 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
887 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700888 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700889 if (caller != null) {
890 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
891 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700892 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700893 } else {
894 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700895 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700896 + intent.toString());
897 ActivityOptions.abort(options);
898 return ActivityManager.START_PERMISSION_DENIED;
899 }
900 }
901
902 IIntentSender target = mService.getIntentSenderLocked(
903 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700904 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700905 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
906 | PendingIntent.FLAG_ONE_SHOT, null);
907
908 Intent newIntent = new Intent();
909 if (requestCode >= 0) {
910 // Caller is requesting a result.
911 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
912 }
913 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
914 new IntentSender(target));
915 if (mService.mHeavyWeightProcess.activities.size() > 0) {
916 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
917 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
918 hist.packageName);
919 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
920 hist.task.taskId);
921 }
922 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
923 aInfo.packageName);
924 newIntent.setFlags(intent.getFlags());
925 newIntent.setClassName("android",
926 HeavyWeightSwitcherActivity.class.getName());
927 intent = newIntent;
928 resolvedType = null;
929 caller = null;
930 callingUid = Binder.getCallingUid();
931 callingPid = Binder.getCallingPid();
932 componentSpecified = true;
933 try {
934 ResolveInfo rInfo =
935 AppGlobals.getPackageManager().resolveIntent(
936 intent, null,
937 PackageManager.MATCH_DEFAULT_ONLY
938 | ActivityManagerService.STOCK_PM_FLAGS, userId);
939 aInfo = rInfo != null ? rInfo.activityInfo : null;
940 aInfo = mService.getActivityInfoForUser(aInfo, userId);
941 } catch (RemoteException e) {
942 aInfo = null;
943 }
944 }
945 }
946 }
947
Dianne Hackborn91097de2014-04-04 18:02:06 -0700948 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
949 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700950 requestCode, callingPid, callingUid, callingPackage,
951 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700952 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700953
Craig Mautner85c11a82014-07-17 12:38:18 -0700954 Binder.restoreCallingIdentity(origId);
955
Craig Mautnerde4ef022013-04-07 19:01:33 -0700956 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700957 // If the caller also wants to switch to a new configuration,
958 // do so now. This allows a clean switch, as we are waiting
959 // for the current activity to pause (so we will not destroy
960 // it), and have not yet started the next activity.
961 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
962 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700963 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700964 if (DEBUG_CONFIGURATION) Slog.v(TAG,
965 "Updating to new configuration after starting activity.");
966 mService.updateConfigurationLocked(config, null, false, false);
967 }
968
Craig Mautner23ac33b2013-04-01 16:26:35 -0700969 if (outResult != null) {
970 outResult.result = res;
971 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700972 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700973 do {
974 try {
975 mService.wait();
976 } catch (InterruptedException e) {
977 }
978 } while (!outResult.timeout && outResult.who == null);
979 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700980 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700981 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700982 outResult.timeout = false;
983 outResult.who = new ComponentName(r.info.packageName, r.info.name);
984 outResult.totalTime = 0;
985 outResult.thisTime = 0;
986 } else {
987 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700988 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700989 do {
990 try {
991 mService.wait();
992 } catch (InterruptedException e) {
993 }
994 } while (!outResult.timeout && outResult.who == null);
995 }
996 }
997 }
998
999 return res;
1000 }
1001 }
1002
1003 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1004 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1005 Bundle options, int userId) {
1006 if (intents == null) {
1007 throw new NullPointerException("intents is null");
1008 }
1009 if (resolvedTypes == null) {
1010 throw new NullPointerException("resolvedTypes is null");
1011 }
1012 if (intents.length != resolvedTypes.length) {
1013 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1014 }
1015
Craig Mautner23ac33b2013-04-01 16:26:35 -07001016
1017 int callingPid;
1018 if (callingUid >= 0) {
1019 callingPid = -1;
1020 } else if (caller == null) {
1021 callingPid = Binder.getCallingPid();
1022 callingUid = Binder.getCallingUid();
1023 } else {
1024 callingPid = callingUid = -1;
1025 }
1026 final long origId = Binder.clearCallingIdentity();
1027 try {
1028 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001029 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001030 for (int i=0; i<intents.length; i++) {
1031 Intent intent = intents[i];
1032 if (intent == null) {
1033 continue;
1034 }
1035
1036 // Refuse possible leaked file descriptors
1037 if (intent != null && intent.hasFileDescriptors()) {
1038 throw new IllegalArgumentException("File descriptors passed in Intent");
1039 }
1040
1041 boolean componentSpecified = intent.getComponent() != null;
1042
1043 // Don't modify the client's object!
1044 intent = new Intent(intent);
1045
1046 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001047 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001048 // TODO: New, check if this is correct
1049 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1050
1051 if (aInfo != null &&
1052 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
1053 != 0) {
1054 throw new IllegalArgumentException(
1055 "FLAG_CANT_SAVE_STATE not supported here");
1056 }
1057
1058 Bundle theseOptions;
1059 if (options != null && i == intents.length-1) {
1060 theseOptions = options;
1061 } else {
1062 theseOptions = null;
1063 }
Craig Mautner6170f732013-04-02 13:05:23 -07001064 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001065 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1066 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001067 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001068 if (res < 0) {
1069 return res;
1070 }
1071
1072 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1073 }
1074 }
1075 } finally {
1076 Binder.restoreCallingIdentity(origId);
1077 }
1078
1079 return ActivityManager.START_SUCCESS;
1080 }
1081
Craig Mautner2420ead2013-04-01 17:13:20 -07001082 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001083 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001084 throws RemoteException {
1085
1086 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001087 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001088 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001089
1090 // schedule launch ticks to collect information about slow apps.
1091 r.startLaunchTickingLocked();
1092
1093 // Have the window manager re-evaluate the orientation of
1094 // the screen based on the new activity order. Note that
1095 // as a result of this, it can call back into the activity
1096 // manager with a new orientation. We don't care about that,
1097 // because the activity is not currently running so we are
1098 // just restarting it anyway.
1099 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001100 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001101 mService.mConfiguration,
1102 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1103 mService.updateConfigurationLocked(config, r, false, false);
1104 }
1105
1106 r.app = app;
1107 app.waitingToKill = null;
1108 r.launchCount++;
1109 r.lastLaunchTime = SystemClock.uptimeMillis();
1110
1111 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1112
1113 int idx = app.activities.indexOf(r);
1114 if (idx < 0) {
1115 app.activities.add(r);
1116 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001117 mService.updateLruProcessLocked(app, true, null);
1118 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001119
1120 final ActivityStack stack = r.task.stack;
1121 try {
1122 if (app.thread == null) {
1123 throw new RemoteException();
1124 }
1125 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001126 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001127 if (andResume) {
1128 results = r.results;
1129 newIntents = r.newIntents;
1130 }
1131 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1132 + " icicle=" + r.icicle
1133 + " with results=" + results + " newIntents=" + newIntents
1134 + " andResume=" + andResume);
1135 if (andResume) {
1136 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1137 r.userId, System.identityHashCode(r),
1138 r.task.taskId, r.shortComponentName);
1139 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001140 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001141 // Home process is the root process of the task.
1142 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001143 }
1144 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1145 r.sleeping = false;
1146 r.forceNewConfig = false;
1147 mService.showAskCompatModeDialogLocked(r);
1148 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1149 String profileFile = null;
1150 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001151 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1152 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1153 mService.mProfileProc = app;
1154 profileFile = mService.mProfileFile;
1155 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001156 }
1157 }
1158 app.hasShownUi = true;
1159 app.pendingUiClean = true;
1160 if (profileFd != null) {
1161 try {
1162 profileFd = profileFd.dup();
1163 } catch (IOException e) {
1164 if (profileFd != null) {
1165 try {
1166 profileFd.close();
1167 } catch (IOException o) {
1168 }
1169 profileFd = null;
1170 }
1171 }
1172 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001173
Jeff Hao1b012d32014-08-20 10:35:34 -07001174 ProfilerInfo profilerInfo = profileFile != null
1175 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1176 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001177 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001178 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001179 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001180 r.compat, r.launchedFromPackage, r.task.voiceInteractor, app.repProcState,
1181 r.icicle, r.persistentState, results, newIntents, !andResume,
1182 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001183
1184 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1185 // This may be a heavy-weight process! Note that the package
1186 // manager will ensure that only activity can run in the main
1187 // process of the .apk, which is the only thing that will be
1188 // considered heavy-weight.
1189 if (app.processName.equals(app.info.packageName)) {
1190 if (mService.mHeavyWeightProcess != null
1191 && mService.mHeavyWeightProcess != app) {
1192 Slog.w(TAG, "Starting new heavy weight process " + app
1193 + " when already running "
1194 + mService.mHeavyWeightProcess);
1195 }
1196 mService.mHeavyWeightProcess = app;
1197 Message msg = mService.mHandler.obtainMessage(
1198 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1199 msg.obj = r;
1200 mService.mHandler.sendMessage(msg);
1201 }
1202 }
1203
1204 } catch (RemoteException e) {
1205 if (r.launchFailed) {
1206 // This is the second time we failed -- finish activity
1207 // and give up.
1208 Slog.e(TAG, "Second failure launching "
1209 + r.intent.getComponent().flattenToShortString()
1210 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001211 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001212 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1213 "2nd-crash", false);
1214 return false;
1215 }
1216
1217 // This is the first time we failed -- restart process and
1218 // retry.
1219 app.activities.remove(r);
1220 throw e;
1221 }
1222
1223 r.launchFailed = false;
1224 if (stack.updateLRUListLocked(r)) {
1225 Slog.w(TAG, "Activity " + r
1226 + " being launched, but already in LRU list");
1227 }
1228
1229 if (andResume) {
1230 // As part of the process of launching, ActivityThread also performs
1231 // a resume.
1232 stack.minimalResumeActivityLocked(r);
1233 } else {
1234 // This activity is not starting in the resumed state... which
1235 // should look like we asked it to pause+stop (but remain visible),
1236 // and it has done so and reported back the current icicle and
1237 // other state.
1238 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1239 + " (starting in stopped state)");
1240 r.state = ActivityState.STOPPED;
1241 r.stopped = true;
1242 }
1243
1244 // Launch the new version setup screen if needed. We do this -after-
1245 // launching the initial activity (that is, home), so that it can have
1246 // a chance to initialize itself while in the background, making the
1247 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001248 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 mService.startSetupActivityLocked();
1250 }
1251
Dianne Hackborn465fa392014-09-14 14:21:18 -07001252 // Update any services we are bound to that might care about whether
1253 // their client may have activities.
1254 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1255
Craig Mautner2420ead2013-04-01 17:13:20 -07001256 return true;
1257 }
1258
Craig Mautnere79d42682013-04-01 19:01:53 -07001259 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001260 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001261 // Is this activity's application already running?
1262 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001263 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001264
1265 r.task.stack.setLaunchTime(r);
1266
1267 if (app != null && app.thread != null) {
1268 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001269 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1270 || !"android".equals(r.info.packageName)) {
1271 // Don't add this if it is a platform component that is marked
1272 // to run in multiple processes, because this is actually
1273 // part of the framework so doesn't make sense to track as a
1274 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001275 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1276 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001277 }
George Mount2c92c972014-03-20 09:38:23 -07001278 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001279 return;
1280 } catch (RemoteException e) {
1281 Slog.w(TAG, "Exception when starting activity "
1282 + r.intent.getComponent().flattenToShortString(), e);
1283 }
1284
1285 // If a dead object exception was thrown -- fall through to
1286 // restart the application.
1287 }
1288
1289 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001290 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001291 }
1292
Craig Mautner6170f732013-04-02 13:05:23 -07001293 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001294 Intent intent, String resolvedType, ActivityInfo aInfo,
1295 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1296 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001297 int callingPid, int callingUid, String callingPackage,
1298 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001299 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1300 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001301 int err = ActivityManager.START_SUCCESS;
1302
1303 ProcessRecord callerApp = null;
1304 if (caller != null) {
1305 callerApp = mService.getRecordForAppLocked(caller);
1306 if (callerApp != null) {
1307 callingPid = callerApp.pid;
1308 callingUid = callerApp.info.uid;
1309 } else {
1310 Slog.w(TAG, "Unable to find app for caller " + caller
1311 + " (pid=" + callingPid + ") when starting: "
1312 + intent.toString());
1313 err = ActivityManager.START_PERMISSION_DENIED;
1314 }
1315 }
1316
1317 if (err == ActivityManager.START_SUCCESS) {
1318 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1319 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001320 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001321 + " on display " + (container == null ? (mFocusedStack == null ?
1322 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1323 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1324 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001325 }
1326
1327 ActivityRecord sourceRecord = null;
1328 ActivityRecord resultRecord = null;
1329 if (resultTo != null) {
1330 sourceRecord = isInAnyStackLocked(resultTo);
1331 if (DEBUG_RESULTS) Slog.v(
1332 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1333 if (sourceRecord != null) {
1334 if (requestCode >= 0 && !sourceRecord.finishing) {
1335 resultRecord = sourceRecord;
1336 }
1337 }
1338 }
Craig Mautner6170f732013-04-02 13:05:23 -07001339
Dianne Hackborn91097de2014-04-04 18:02:06 -07001340 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001341
Dianne Hackborn95465202014-09-15 16:21:55 -07001342 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001343 // Transfer the result target from the source activity to the new
1344 // one being started, including any failures.
1345 if (requestCode >= 0) {
1346 ActivityOptions.abort(options);
1347 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1348 }
1349 resultRecord = sourceRecord.resultTo;
1350 resultWho = sourceRecord.resultWho;
1351 requestCode = sourceRecord.requestCode;
1352 sourceRecord.resultTo = null;
1353 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001354 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001355 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001356 if (sourceRecord.launchedFromUid == callingUid) {
1357 // The new activity is being launched from the same uid as the previous
1358 // activity in the flow, and asking to forward its result back to the
1359 // previous. In this case the activity is serving as a trampoline between
1360 // the two, so we also want to update its launchedFromPackage to be the
1361 // same as the previous activity. Note that this is safe, since we know
1362 // these two packages come from the same uid; the caller could just as
1363 // well have supplied that same package name itself. This specifially
1364 // deals with the case of an intent picker/chooser being launched in the app
1365 // flow to redirect to an activity picked by the user, where we want the final
1366 // activity to consider it to have been launched by the previous app activity.
1367 callingPackage = sourceRecord.launchedFromPackage;
1368 }
Craig Mautner6170f732013-04-02 13:05:23 -07001369 }
1370
1371 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1372 // We couldn't find a class that can handle the given Intent.
1373 // That's the end of that!
1374 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1375 }
1376
1377 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1378 // We couldn't find the specific class specified in the Intent.
1379 // Also the end of the line.
1380 err = ActivityManager.START_CLASS_NOT_FOUND;
1381 }
1382
Dianne Hackborn91097de2014-04-04 18:02:06 -07001383 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1384 && sourceRecord.task.voiceSession != null) {
1385 // If this activity is being launched as part of a voice session, we need
1386 // to ensure that it is safe to do so. If the upcoming activity will also
1387 // be part of the voice session, we can only launch it if it has explicitly
1388 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001389 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001390 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1391 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001392 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1393 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001394 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1395 }
1396 } catch (RemoteException e) {
1397 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1398 }
1399 }
1400 }
1401
1402 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1403 // If the caller is starting a new voice session, just make sure the target
1404 // is actually allowing it to run this way.
1405 try {
1406 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1407 intent, resolvedType)) {
1408 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1409 }
1410 } catch (RemoteException e) {
1411 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1412 }
1413 }
1414
louis_changcd5d1982014-08-01 10:09:08 +08001415 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1416
Craig Mautner6170f732013-04-02 13:05:23 -07001417 if (err != ActivityManager.START_SUCCESS) {
1418 if (resultRecord != null) {
1419 resultStack.sendActivityResultLocked(-1,
1420 resultRecord, resultWho, requestCode,
1421 Activity.RESULT_CANCELED, null);
1422 }
Craig Mautner6170f732013-04-02 13:05:23 -07001423 ActivityOptions.abort(options);
1424 return err;
1425 }
1426
1427 final int startAnyPerm = mService.checkPermission(
1428 START_ANY_ACTIVITY, callingPid, callingUid);
1429 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1430 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1431 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1432 if (resultRecord != null) {
1433 resultStack.sendActivityResultLocked(-1,
1434 resultRecord, resultWho, requestCode,
1435 Activity.RESULT_CANCELED, null);
1436 }
Craig Mautner6170f732013-04-02 13:05:23 -07001437 String msg;
1438 if (!aInfo.exported) {
1439 msg = "Permission Denial: starting " + intent.toString()
1440 + " from " + callerApp + " (pid=" + callingPid
1441 + ", uid=" + callingUid + ")"
1442 + " not exported from uid " + aInfo.applicationInfo.uid;
1443 } else {
1444 msg = "Permission Denial: starting " + intent.toString()
1445 + " from " + callerApp + " (pid=" + callingPid
1446 + ", uid=" + callingUid + ")"
1447 + " requires " + aInfo.permission;
1448 }
1449 Slog.w(TAG, msg);
1450 throw new SecurityException(msg);
1451 }
1452
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001453 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001454 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001455
Craig Mautner6170f732013-04-02 13:05:23 -07001456 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001457 try {
1458 // The Intent we give to the watcher has the extra data
1459 // stripped off, since it can contain private information.
1460 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001461 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001462 aInfo.applicationInfo.packageName);
1463 } catch (RemoteException e) {
1464 mService.mController = null;
1465 }
Ben Gruver5e207332013-04-03 17:41:37 -07001466 }
Craig Mautner6170f732013-04-02 13:05:23 -07001467
Ben Gruver5e207332013-04-03 17:41:37 -07001468 if (abort) {
1469 if (resultRecord != null) {
1470 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001471 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001472 }
Ben Gruver5e207332013-04-03 17:41:37 -07001473 // We pretend to the caller that it was really started, but
1474 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001475 ActivityOptions.abort(options);
1476 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001477 }
1478
1479 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001480 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001481 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001482 if (outActivity != null) {
1483 outActivity[0] = r;
1484 }
1485
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001486 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001487 if (voiceSession == null && (stack.mResumedActivity == null
1488 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001489 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1490 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001491 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001492 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001493 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001494 ActivityOptions.abort(options);
1495 return ActivityManager.START_SWITCHES_CANCELED;
1496 }
1497 }
1498
1499 if (mService.mDidAppSwitch) {
1500 // This is the second allowed switch since we stopped switches,
1501 // so now just generally allow switches. Use case: user presses
1502 // home (switches disabled, switch to home, mDidAppSwitch now true);
1503 // user taps a home icon (coming from home so allowed, we hit here
1504 // and now allow anyone to switch again).
1505 mService.mAppSwitchesAllowedTime = 0;
1506 } else {
1507 mService.mDidAppSwitch = true;
1508 }
1509
Craig Mautneree36c772014-07-16 14:56:05 -07001510 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001511
Dianne Hackborn91097de2014-04-04 18:02:06 -07001512 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001513 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001514
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001515 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001516 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001517 // activity start, but we are not actually doing an activity
1518 // switch... just dismiss the keyguard now, because we
1519 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001520 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001521 }
1522 return err;
1523 }
1524
Craig Mautner1b4bf852014-05-26 15:06:32 -07001525 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001526 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001527
1528 // On leanback only devices we should keep all activities in the same stack.
1529 if (!mLeanbackOnlyDevice &&
1530 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001531 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001532 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001533 if (taskStack.isOnHomeDisplay()) {
1534 if (mFocusedStack != taskStack) {
1535 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1536 "focused stack to r=" + r + " task=" + task);
1537 mFocusedStack = taskStack;
1538 } else {
1539 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1540 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1541 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001542 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001543 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001544 }
1545
Craig Mautnere0a38842013-12-16 16:14:02 -08001546 final ActivityContainer container = r.mInitialActivityContainer;
1547 if (container != null) {
1548 // The first time put it on the desired stack, after this put on task stack.
1549 r.mInitialActivityContainer = null;
1550 return container.mStack;
1551 }
1552
Craig Mautner1b4bf852014-05-26 15:06:32 -07001553 if (mFocusedStack != mHomeStack && (!newTask ||
1554 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001555 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1556 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1557 return mFocusedStack;
1558 }
1559
Craig Mautnere0a38842013-12-16 16:14:02 -08001560 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1561 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1562 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1563 if (!stack.isHomeStack()) {
1564 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1565 "adjustStackFocus: Setting focused stack=" + stack);
1566 mFocusedStack = stack;
1567 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001568 }
1569 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001570
Craig Mautner4a1cb222013-12-04 16:14:06 -08001571 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001572 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001573 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1574 " stackId=" + stackId);
1575 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001576 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001577 }
1578 return mHomeStack;
1579 }
1580
Craig Mautner29219d92013-04-16 20:19:12 -07001581 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001582 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001583 final TaskRecord task = r.task;
1584 boolean isHomeActivity = !r.isApplicationActivity();
1585 if (!isHomeActivity && task != null) {
1586 isHomeActivity = !task.isApplicationTask();
1587 }
1588 if (!isHomeActivity && task != null) {
1589 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1590 isHomeActivity = parent != null && parent.isHomeActivity();
1591 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001592 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001593 }
1594 }
1595
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001596 final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001597 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001598 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001599 final Intent intent = r.intent;
1600 final int callingUid = r.launchedFromUid;
1601
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001602 // In some flows in to this function, we retrieve the task record and hold on to it
1603 // without a lock before calling back in to here... so the task at this point may
1604 // not actually be in recents. Check for that, and if it isn't in recents just
1605 // consider it invalid.
1606 if (inTask != null && !inTask.inRecents) {
1607 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1608 inTask = null;
1609 }
1610
Craig Mautnerad400af2014-08-13 16:41:36 -07001611 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001612 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1613 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001614
Craig Mautnera228ae92014-07-09 05:44:55 -07001615 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001616 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001617 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001618 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1619 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1620 "\"singleInstance\" or \"singleTask\"");
1621 launchFlags &=
1622 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1623 } else {
1624 switch (r.info.documentLaunchMode) {
1625 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1626 break;
1627 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1628 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1629 break;
1630 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1631 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1632 break;
1633 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1634 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1635 break;
1636 }
1637 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001638
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001639 final boolean launchTaskBehind = r.mLaunchTaskBehind
1640 && !launchSingleTask && !launchSingleInstance
1641 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001642
1643 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1644 // For whatever reason this activity is being launched into a new
1645 // task... yet the caller has requested a result back. Well, that
1646 // is pretty messed up, so instead immediately send back a cancel
1647 // and let the new task continue launched as normal without a
1648 // dependency on its originator.
1649 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1650 r.resultTo.task.stack.sendActivityResultLocked(-1,
1651 r.resultTo, r.resultWho, r.requestCode,
1652 Activity.RESULT_CANCELED, null);
1653 r.resultTo = null;
1654 }
1655
1656 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1657 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1658 }
1659
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001660 // If we are actually going to launch in to a new task, there are some cases where
1661 // we further want to do multiple task.
1662 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1663 if (launchTaskBehind
1664 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1665 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1666 }
1667 }
1668
Craig Mautner8849a5e2013-04-02 16:41:03 -07001669 // We'll invoke onUserLeaving before onPause only if the launching
1670 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001671 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001672 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001673
1674 // If the caller has asked not to resume at this point, we make note
1675 // of this in the record so that we can skip it when trying to find
1676 // the top running activity.
1677 if (!doResume) {
1678 r.delayedResume = true;
1679 }
1680
Craig Mautnera254cd72014-05-25 16:47:39 -07001681 ActivityRecord notTop =
1682 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001683
1684 // If the onlyIfNeeded flag is set, then we can do this if the activity
1685 // being launched is the same as the one making the call... or, as
1686 // a special case, if we do not know the caller then we count the
1687 // current top activity as the caller.
1688 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1689 ActivityRecord checkedCaller = sourceRecord;
1690 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001691 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001692 }
1693 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1694 // Caller is not the same as launcher, so always needed.
1695 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1696 }
1697 }
1698
Craig Mautner8849a5e2013-04-02 16:41:03 -07001699 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001700 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001701
1702 // If the caller is not coming from another activity, but has given us an
1703 // explicit task into which they would like us to launch the new activity,
1704 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001705 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1706 final Intent baseIntent = inTask.getBaseIntent();
1707 final ActivityRecord root = inTask.getRootActivity();
1708 if (baseIntent == null) {
1709 ActivityOptions.abort(options);
1710 throw new IllegalArgumentException("Launching into task without base intent: "
1711 + inTask);
1712 }
1713
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001714 // If this task is empty, then we are adding the first activity -- it
1715 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001716 if (launchSingleInstance || launchSingleTask) {
1717 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1718 ActivityOptions.abort(options);
1719 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1720 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001721 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001722 if (root != null) {
1723 ActivityOptions.abort(options);
1724 throw new IllegalArgumentException("Caller with inTask " + inTask
1725 + " has root " + root + " but target is singleInstance/Task");
1726 }
1727 }
1728
1729 // If task is empty, then adopt the interesting intent launch flags in to the
1730 // activity being started.
1731 if (root == null) {
1732 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1733 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1734 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1735 launchFlags = (launchFlags&~flagsOfInterest)
1736 | (baseIntent.getFlags()&flagsOfInterest);
1737 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001738 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001739 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001740
Dianne Hackborn962d5352014-08-29 16:27:40 -07001741 // If the task is not empty and the caller is asking to start it as the root
1742 // of a new task, then we don't actually want to start this on the task. We
1743 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001744 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001745 addingToTask = false;
1746
1747 } else {
1748 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001749 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001750
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001751 reuseTask = inTask;
1752 } else {
1753 inTask = null;
1754 }
1755
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001756 if (inTask == null) {
1757 if (sourceRecord == null) {
1758 // This activity is not being started from another... in this
1759 // case we -always- start a new task.
1760 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1761 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1762 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1763 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1764 }
1765 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1766 // The original activity who is starting us is running as a single
1767 // instance... this new activity it is starting must go on its
1768 // own task.
1769 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1770 } else if (launchSingleInstance || launchSingleTask) {
1771 // The activity being started is a single instance... it always
1772 // gets launched into its own task.
1773 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1774 }
1775 }
1776
1777 ActivityInfo newTaskInfo = null;
1778 Intent newTaskIntent = null;
1779 ActivityStack sourceStack;
1780 if (sourceRecord != null) {
1781 if (sourceRecord.finishing) {
1782 // If the source is finishing, we can't further count it as our source. This
1783 // is because the task it is associated with may now be empty and on its way out,
1784 // so we don't want to blindly throw it in to that task. Instead we will take
1785 // the NEW_TASK flow and try to find a task for it. But save the task information
1786 // so it can be used when creating the new task.
1787 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1788 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1789 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1790 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1791 newTaskInfo = sourceRecord.info;
1792 newTaskIntent = sourceRecord.task.intent;
1793 }
1794 sourceRecord = null;
1795 sourceStack = null;
1796 } else {
1797 sourceStack = sourceRecord.task.stack;
1798 }
1799 } else {
1800 sourceStack = null;
1801 }
1802
1803 boolean movedHome = false;
1804 ActivityStack targetStack;
1805
1806 intent.setFlags(launchFlags);
1807
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001808 // We may want to try to place the new activity in to an existing task. We always
1809 // do this if the target activity is singleTask or singleInstance; we will also do
1810 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1811 // us to still place it in a new task: multi task, always doc mode, or being asked to
1812 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001813 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1814 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001815 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001816 // If bring to front is requested, and no result is requested and we have not
1817 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001818 // we can find a task that was started with this same
1819 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001820 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001821 // See if there is a task to bring to the front. If this is
1822 // a SINGLE_INSTANCE activity, there can be one and only one
1823 // instance of it in the history, and it is always in its own
1824 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001825 ActivityRecord intentActivity = !launchSingleInstance ?
1826 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001827 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001828 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001829 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001830 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001831 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1832 }
Craig Mautner29219d92013-04-16 20:19:12 -07001833 if (r.task == null) {
1834 r.task = intentActivity.task;
1835 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001836 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001837 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001838 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1839 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001840 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001841 if (intentActivity.task.intent == null) {
1842 // This task was started because of movement of
1843 // the activity based on affinity... now that we
1844 // are actually launching it, we can assign the
1845 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001846 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001847 }
1848 // If the target task is not in the front, then we need
1849 // to bring it to the front... except... well, with
1850 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1851 // to have the same behavior as if a new instance was
1852 // being started, which means not bringing it to the front
1853 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001854 final ActivityStack lastStack = getLastStack();
1855 ActivityRecord curTop = lastStack == null?
1856 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001857 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001858 if (curTop != null && (curTop.task != intentActivity.task ||
1859 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001860 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001861 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1862 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001863 // We really do want to push this one into the
1864 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001865 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001866 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1867 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001868 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001869 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001870 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001871 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1872 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001873 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001874 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001875 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001876 options = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001877 movedToFront = true;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001878 }
1879 }
1880 // If the caller has requested that the target task be
1881 // reset, then do so.
1882 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1883 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1884 }
1885 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1886 // We don't need to start a new activity, and
1887 // the client said not to do anything if that
1888 // is the case, so this is it! And for paranoia, make
1889 // sure we have correctly resumed the top activity.
1890 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001891 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001892
1893 // Make sure to notify Keyguard as well if we are not running an app
1894 // transition later.
1895 if (!movedToFront) {
1896 notifyActivityDrawnForKeyguard();
1897 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001898 } else {
1899 ActivityOptions.abort(options);
1900 }
1901 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1902 }
1903 if ((launchFlags &
1904 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1905 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1906 // The caller has requested to completely replace any
1907 // existing task with its new activity. Well that should
1908 // not be too hard...
1909 reuseTask = intentActivity.task;
1910 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001911 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001912 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001913 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001914 // In this situation we want to remove all activities
1915 // from the task up to the one being started. In most
1916 // cases this means we are resetting the task to its
1917 // initial state.
1918 ActivityRecord top =
1919 intentActivity.task.performClearTaskLocked(r, launchFlags);
1920 if (top != null) {
1921 if (top.frontOfTask) {
1922 // Activity aliases may mean we use different
1923 // intents for the top activity, so make sure
1924 // the task now has the identity of the new
1925 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001926 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001927 }
1928 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1929 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001930 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001931 } else {
1932 // A special case: we need to
1933 // start the activity because it is not currently
1934 // running, and the caller has asked to clear the
1935 // current task to have this activity at the top.
1936 addingToTask = true;
1937 // Now pretend like this activity is being started
1938 // by the top of its task, so it is put in the
1939 // right place.
1940 sourceRecord = intentActivity;
1941 }
1942 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1943 // In this case the top activity on the task is the
1944 // same as the one being launched, so we take that
1945 // as a request to bring the task to the foreground.
1946 // If the top activity in the task is the root
1947 // activity, deliver this new intent to it if it
1948 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001949 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001950 && intentActivity.realActivity.equals(r.realActivity)) {
1951 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1952 intentActivity.task);
1953 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001954 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001955 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001956 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
1957 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001958 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1959 // In this case we are launching the root activity
1960 // of the task, but with a different intent. We
1961 // should start a new instance on top.
1962 addingToTask = true;
1963 sourceRecord = intentActivity;
1964 }
1965 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1966 // In this case an activity is being launched in to an
1967 // existing task, without resetting that task. This
1968 // is typically the situation of launching an activity
1969 // from a notification or shortcut. We want to place
1970 // the new activity on top of the current task.
1971 addingToTask = true;
1972 sourceRecord = intentActivity;
1973 } else if (!intentActivity.task.rootWasReset) {
1974 // In this case we are launching in to an existing task
1975 // that has not yet been started from its front door.
1976 // The current task has been brought to the front.
1977 // Ideally, we'd probably like to place this new task
1978 // at the bottom of its stack, but that's a little hard
1979 // to do with the current organization of the code so
1980 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07001981 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001982 }
1983 if (!addingToTask && reuseTask == null) {
1984 // We didn't do anything... but it was needed (a.k.a., client
1985 // don't use that intent!) And for paranoia, make
1986 // sure we have correctly resumed the top activity.
1987 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001988 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001989 if (!movedToFront) {
1990 // Make sure to notify Keyguard as well if we are not running an app
1991 // transition later.
1992 notifyActivityDrawnForKeyguard();
1993 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001994 } else {
1995 ActivityOptions.abort(options);
1996 }
1997 return ActivityManager.START_TASK_TO_FRONT;
1998 }
1999 }
2000 }
2001 }
2002
2003 //String uri = r.intent.toURI();
2004 //Intent intent2 = new Intent(uri);
2005 //Slog.i(TAG, "Given intent: " + r.intent);
2006 //Slog.i(TAG, "URI is: " + uri);
2007 //Slog.i(TAG, "To intent: " + intent2);
2008
2009 if (r.packageName != null) {
2010 // If the activity being launched is the same as the one currently
2011 // at the top, then we need to check if it should only be launched
2012 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002013 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002014 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002015 if (top != null && r.resultTo == null) {
2016 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2017 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002018 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002019 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002020 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2021 top.task);
2022 // For paranoia, make sure we have correctly
2023 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002024 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002025 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002026 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002027 }
2028 ActivityOptions.abort(options);
2029 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2030 // We don't need to start a new activity, and
2031 // the client said not to do anything if that
2032 // is the case, so this is it!
2033 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2034 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002035 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002036 return ActivityManager.START_DELIVERED_TO_TOP;
2037 }
2038 }
2039 }
2040 }
2041
2042 } else {
2043 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002044 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2045 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002046 }
2047 ActivityOptions.abort(options);
2048 return ActivityManager.START_CLASS_NOT_FOUND;
2049 }
2050
2051 boolean newTask = false;
2052 boolean keepCurTransition = false;
2053
Craig Mautnerbb742462014-07-07 15:28:55 -07002054 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002055 sourceRecord.task : null;
2056
Craig Mautner8849a5e2013-04-02 16:41:03 -07002057 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002058 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002059 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002060 if (isLockTaskModeViolation(reuseTask)) {
2061 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2062 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2063 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002064 newTask = true;
2065 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerbb742462014-07-07 15:28:55 -07002066 if (!launchTaskBehind) {
2067 targetStack.moveToFront();
2068 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002069 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002070 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2071 newTaskInfo != null ? newTaskInfo : r.info,
2072 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002073 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2074 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002075 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2076 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002077 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002078 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002079 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002080 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002081 if ((launchFlags &
2082 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2083 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2084 // Caller wants to appear on home activity, so before starting
2085 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002086 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002087 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002088 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002089 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002090 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002091 if (isLockTaskModeViolation(sourceTask)) {
2092 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2093 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2094 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002095 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07002096 targetStack.moveToFront();
Craig Mautner737fae22014-10-15 12:52:10 -07002097 final TaskRecord topTask = targetStack.topTask();
2098 if (topTask != sourceTask) {
2099 targetStack.moveTaskToFrontLocked(sourceTask, r, options);
Craig Mautner737fae22014-10-15 12:52:10 -07002100 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002101 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002102 // In this case, we are adding the activity to an existing
2103 // task, but the caller has asked to clear that task if the
2104 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002105 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002106 keepCurTransition = true;
2107 if (top != null) {
2108 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002109 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002110 // For paranoia, make sure we have correctly
2111 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002112 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002113 if (doResume) {
2114 targetStack.resumeTopActivityLocked(null);
2115 }
2116 ActivityOptions.abort(options);
2117 return ActivityManager.START_DELIVERED_TO_TOP;
2118 }
2119 } else if (!addingToTask &&
2120 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2121 // In this case, we are launching an activity in our own task
2122 // that may already be running somewhere in the history, and
2123 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002124 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002125 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002126 final TaskRecord task = top.task;
2127 task.moveActivityToFrontLocked(top);
2128 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002129 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002130 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002131 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002132 if (doResume) {
2133 targetStack.resumeTopActivityLocked(null);
2134 }
2135 return ActivityManager.START_DELIVERED_TO_TOP;
2136 }
2137 }
2138 // An existing activity is starting this new activity, so we want
2139 // to keep the new one in the same task as the one that is starting
2140 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002141 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002142 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002143 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002144
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002145 } else if (inTask != null) {
2146 // The calling is asking that the new activity be started in an explicit
2147 // task it has provided to us.
2148 if (isLockTaskModeViolation(inTask)) {
2149 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2150 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2151 }
2152 targetStack = inTask.stack;
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002153 targetStack.moveTaskToFrontLocked(inTask, r, options);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002154
2155 // Check whether we should actually launch the new activity in to the task,
2156 // or just reuse the current activity on top.
2157 ActivityRecord top = inTask.getTopActivity();
2158 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2159 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2160 || launchSingleTop || launchSingleTask) {
2161 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2162 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2163 // We don't need to start a new activity, and
2164 // the client said not to do anything if that
2165 // is the case, so this is it!
2166 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2167 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002168 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002169 return ActivityManager.START_DELIVERED_TO_TOP;
2170 }
2171 }
2172
Dianne Hackborn962d5352014-08-29 16:27:40 -07002173 if (!addingToTask) {
2174 // We don't actually want to have this activity added to the task, so just
2175 // stop here but still tell the caller that we consumed the intent.
2176 ActivityOptions.abort(options);
2177 return ActivityManager.START_TASK_TO_FRONT;
2178 }
2179
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002180 r.setTask(inTask, null);
2181 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2182 + " in explicit task " + r.task);
2183
Craig Mautner8849a5e2013-04-02 16:41:03 -07002184 } else {
2185 // This not being started from an existing activity, and not part
2186 // of a new task... just put it in the top task, though these days
2187 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002188 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07002189 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07002190 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002191 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002192 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002193 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002194 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2195 + " in new guessed " + r.task);
2196 }
2197
2198 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002199 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002200
Craig Mautner76e2a762014-06-24 09:05:43 -07002201 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2202 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2203 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002204 if (newTask) {
2205 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2206 }
2207 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002208 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002209 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002210 if (!launchTaskBehind) {
2211 // Don't set focus on an activity that's going to the back.
2212 mService.setFocusedActivityLocked(r);
2213 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002214 return ActivityManager.START_SUCCESS;
2215 }
2216
Craig Mautneree36c772014-07-16 14:56:05 -07002217 final void doPendingActivityLaunchesLocked(boolean doResume) {
2218 while (!mPendingActivityLaunches.isEmpty()) {
2219 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002220 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002221 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002222 }
2223 }
2224
Craig Mautner7f13ed32014-07-28 14:00:35 -07002225 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002226 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2227 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002228 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002229 mPendingActivityLaunches.remove(palNdx);
2230 }
2231 }
2232 }
2233
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002234 void acquireLaunchWakelock() {
2235 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2236 throw new IllegalStateException("Calling must be system uid");
2237 }
2238 mLaunchingActivity.acquire();
2239 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2240 // To be safe, don't allow the wake lock to be held for too long.
2241 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2242 }
2243 }
2244
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002245 /**
2246 * Called when the frontmost task is idle.
2247 * @return the state of mService.mBooting before this was called.
2248 */
2249 private boolean checkFinishBootingLocked() {
2250 final boolean booting = mService.mBooting;
2251 boolean enableScreen = false;
2252 mService.mBooting = false;
2253 if (!mService.mBooted) {
2254 mService.mBooted = true;
2255 enableScreen = true;
2256 }
2257 if (booting || enableScreen) {
2258 mService.postFinishBooting(booting, enableScreen);
2259 }
2260 return booting;
2261 }
2262
Craig Mautnerf3333272013-04-22 10:55:53 -07002263 // Checked.
2264 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2265 Configuration config) {
2266 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2267
Craig Mautnerf3333272013-04-22 10:55:53 -07002268 ArrayList<ActivityRecord> stops = null;
2269 ArrayList<ActivityRecord> finishes = null;
2270 ArrayList<UserStartedState> startingUsers = null;
2271 int NS = 0;
2272 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002273 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002274 boolean activityRemoved = false;
2275
2276 ActivityRecord r = ActivityRecord.forToken(token);
2277 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002278 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2279 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002280 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2281 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002282 if (fromTimeout) {
2283 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002284 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002285
2286 // This is a hack to semi-deal with a race condition
2287 // in the client where it can be constructed with a
2288 // newer configuration from when we asked it to launch.
2289 // We'll update with whatever configuration it now says
2290 // it used to launch.
2291 if (config != null) {
2292 r.configuration = config;
2293 }
2294
2295 // We are now idle. If someone is waiting for a thumbnail from
2296 // us, we can now deliver.
2297 r.idle = true;
2298
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002299 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002300 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002301 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002302 }
2303 }
2304
2305 if (allResumedActivitiesIdle()) {
2306 if (r != null) {
2307 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002308 }
2309
2310 if (mLaunchingActivity.isHeld()) {
2311 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2312 if (VALIDATE_WAKE_LOCK_CALLER &&
2313 Binder.getCallingUid() != Process.myUid()) {
2314 throw new IllegalStateException("Calling must be system uid");
2315 }
2316 mLaunchingActivity.release();
2317 }
2318 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002319 }
2320
2321 // Atomically retrieve all of the other things to do.
2322 stops = processStoppingActivitiesLocked(true);
2323 NS = stops != null ? stops.size() : 0;
2324 if ((NF=mFinishingActivities.size()) > 0) {
2325 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2326 mFinishingActivities.clear();
2327 }
2328
Craig Mautnerf3333272013-04-22 10:55:53 -07002329 if (mStartingUsers.size() > 0) {
2330 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2331 mStartingUsers.clear();
2332 }
2333
Craig Mautnerf3333272013-04-22 10:55:53 -07002334 // Stop any activities that are scheduled to do so but have been
2335 // waiting for the next one to start.
2336 for (int i = 0; i < NS; i++) {
2337 r = stops.get(i);
2338 final ActivityStack stack = r.task.stack;
2339 if (r.finishing) {
2340 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2341 } else {
2342 stack.stopActivityLocked(r);
2343 }
2344 }
2345
2346 // Finish any activities that are scheduled to do so but have been
2347 // waiting for the next one to start.
2348 for (int i = 0; i < NF; i++) {
2349 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002350 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002351 }
2352
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002353 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002354 // Complete user switch
2355 if (startingUsers != null) {
2356 for (int i = 0; i < startingUsers.size(); i++) {
2357 mService.finishUserSwitch(startingUsers.get(i));
2358 }
2359 }
2360 // Complete starting up of background users
2361 if (mStartingBackgroundUsers.size() > 0) {
2362 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2363 mStartingBackgroundUsers.clear();
2364 for (int i = 0; i < startingUsers.size(); i++) {
2365 mService.finishUserBoot(startingUsers.get(i));
2366 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002367 }
2368 }
2369
2370 mService.trimApplications();
2371 //dump();
2372 //mWindowManager.dump();
2373
Craig Mautnerf3333272013-04-22 10:55:53 -07002374 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002375 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002376 }
2377
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002378 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002379 }
2380
Craig Mautner8e569572013-10-11 17:36:59 -07002381 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002382 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002383 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2384 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002385 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2386 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2387 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002388 }
Craig Mautner19091252013-10-05 00:03:53 -07002389 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002390 }
2391
2392 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002393 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2394 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002395 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2396 stacks.get(stackNdx).closeSystemDialogsLocked();
2397 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002398 }
2399 }
2400
Craig Mautner93529a42013-10-04 15:03:13 -07002401 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002402 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002403 }
2404
Craig Mautner8d341ef2013-03-26 09:03:27 -07002405 /**
2406 * @return true if some activity was finished (or would have finished if doit were true).
2407 */
2408 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2409 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002410 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2411 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002412 final int numStacks = stacks.size();
2413 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2414 final ActivityStack stack = stacks.get(stackNdx);
2415 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2416 didSomething = true;
2417 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002418 }
2419 }
2420 return didSomething;
2421 }
2422
Dianne Hackborna413dc02013-07-12 12:02:55 -07002423 void updatePreviousProcessLocked(ActivityRecord r) {
2424 // Now that this process has stopped, we may want to consider
2425 // it to be the previous app to try to keep around in case
2426 // the user wants to return to it.
2427
2428 // First, found out what is currently the foreground app, so that
2429 // we don't blow away the previous app if this activity is being
2430 // hosted by the process that is actually still the foreground.
2431 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002432 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2433 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002434 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2435 final ActivityStack stack = stacks.get(stackNdx);
2436 if (isFrontStack(stack)) {
2437 if (stack.mResumedActivity != null) {
2438 fgApp = stack.mResumedActivity.app;
2439 } else if (stack.mPausingActivity != null) {
2440 fgApp = stack.mPausingActivity.app;
2441 }
2442 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002443 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002444 }
2445 }
2446
2447 // Now set this one as the previous process, only if that really
2448 // makes sense to.
2449 if (r.app != null && fgApp != null && r.app != fgApp
2450 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002451 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002452 mService.mPreviousProcess = r.app;
2453 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2454 }
2455 }
2456
Craig Mautner05d29032013-05-03 13:40:13 -07002457 boolean resumeTopActivitiesLocked() {
2458 return resumeTopActivitiesLocked(null, null, null);
2459 }
2460
2461 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2462 Bundle targetOptions) {
2463 if (targetStack == null) {
2464 targetStack = getFocusedStack();
2465 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002466 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002467 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002468 if (isFrontStack(targetStack)) {
2469 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2470 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002471 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2472 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002473 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2474 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002475 if (stack == targetStack) {
2476 // Already started above.
2477 continue;
2478 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002479 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002480 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002481 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002482 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002483 }
Craig Mautner05d29032013-05-03 13:40:13 -07002484 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002485 }
2486
Todd Kennedy539db512014-12-15 09:57:55 -08002487 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002488 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2489 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002490 final int numStacks = stacks.size();
2491 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2492 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002493 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002494 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002495 }
2496 }
2497
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002498 void finishVoiceTask(IVoiceInteractionSession session) {
2499 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2500 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2501 final int numStacks = stacks.size();
2502 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2503 final ActivityStack stack = stacks.get(stackNdx);
2504 stack.finishVoiceTask(session);
2505 }
2506 }
2507 }
2508
Craig Mautneraea74a52014-03-08 14:23:10 -08002509 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2510 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2511 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002512 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002513 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2514 // Caller wants the home activity moved with it. To accomplish this,
2515 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002516 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002517 }
2518 task.stack.moveTaskToFrontLocked(task, null, options);
2519 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2520 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002521 }
2522
Craig Mautner967212c2013-04-13 21:10:58 -07002523 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002524 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2525 if (activityContainer != null) {
2526 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002527 }
2528 return null;
2529 }
2530
Craig Mautner967212c2013-04-13 21:10:58 -07002531 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002532 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002533 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2534 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002535 }
2536 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002537 }
2538
Craig Mautner4a1cb222013-12-04 16:14:06 -08002539 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002540 ActivityRecord homeActivity = getHomeActivity();
2541 if (homeActivity != null) {
2542 return homeActivity.appToken;
2543 }
2544 return null;
2545 }
2546
2547 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002548 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2549 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2550 final TaskRecord task = tasks.get(taskNdx);
2551 if (task.isHomeTask()) {
2552 final ArrayList<ActivityRecord> activities = task.mActivities;
2553 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2554 final ActivityRecord r = activities.get(activityNdx);
2555 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002556 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002557 }
2558 }
2559 }
2560 }
2561 return null;
2562 }
2563
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002564 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002565 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002566 ActivityContainer activityContainer =
2567 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002568 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002569 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002570 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002571 return activityContainer;
2572 }
2573
Craig Mautner34b73df2014-01-12 21:11:08 -08002574 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002575 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2576 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2577 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002578 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2579 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002580 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002581 }
2582 }
2583
Craig Mautner95da1082014-02-24 17:54:35 -08002584 void deleteActivityContainer(IActivityContainer container) {
2585 ActivityContainer activityContainer = (ActivityContainer)container;
2586 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002587 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2588 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002589 final int stackId = activityContainer.mStackId;
2590 mActivityContainers.remove(stackId);
2591 mWindowManager.removeStack(stackId);
2592 }
2593 }
2594
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002595 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002596 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2597 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002598 return -1;
2599 }
2600
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002601 ActivityContainer activityContainer = new ActivityContainer(stackId);
2602 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002603 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002604 return stackId;
2605 }
2606
2607 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002608 while (true) {
2609 if (++mLastStackId <= HOME_STACK_ID) {
2610 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002611 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002612 if (getStack(mLastStackId) == null) {
2613 break;
2614 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002615 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002616 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002617 }
2618
Wale Ogunwale7de05352014-12-12 15:21:33 -08002619 private boolean restoreRecentTaskLocked(TaskRecord task) {
2620 ActivityStack stack = null;
2621 // Determine stack to restore task to.
2622 if (mLeanbackOnlyDevice) {
2623 // There is only one stack for lean back devices.
2624 stack = mHomeStack;
2625 } else {
2626 // Look for the top stack on the home display that isn't the home stack.
2627 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2628 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2629 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
2630 if (!tmpStack.isHomeStack()) {
2631 stack = tmpStack;
2632 break;
2633 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002634 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002635 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002636
2637 if (stack == null) {
2638 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2639 // before an application stack is created...Go ahead and create one on the default
2640 // display.
2641 stack = getStack(createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY));
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002642 // Restore home stack to top.
2643 moveHomeStack(true);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002644 if (DEBUG_RECENTS)
2645 Slog.v(TAG, "Created stack=" + stack + " for recents restoration.");
2646 }
2647
2648 if (stack == null) {
2649 // What does this mean??? Not sure how we would get here...
2650 if (DEBUG_RECENTS)
2651 Slog.v(TAG, "Unable to find/create stack to restore recent task=" + task);
2652 return false;
2653 }
2654
2655 stack.addTask(task, false, false);
2656 if (DEBUG_RECENTS)
2657 Slog.v(TAG, "Added restored task=" + task + " to stack=" + stack);
2658 final ArrayList<ActivityRecord> activities = task.mActivities;
2659 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2660 final ActivityRecord r = activities.get(activityNdx);
2661 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2662 r.info.screenOrientation, r.fullscreen,
2663 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2664 r.userId, r.info.configChanges, task.voiceSession != null,
2665 r.mLaunchTaskBehind);
2666 }
2667 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002668 }
2669
Craig Mautner8d341ef2013-03-26 09:03:27 -07002670 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002671 final TaskRecord task = anyTaskForIdLocked(taskId);
2672 if (task == null) {
2673 return;
2674 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002675 final ActivityStack stack = getStack(stackId);
2676 if (stack == null) {
2677 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2678 return;
2679 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002680 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002681 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002682 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002683 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002684 }
2685
Craig Mautnerac6f8432013-07-17 13:24:59 -07002686 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002687 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002688 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2689 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002690 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2691 final ActivityStack stack = stacks.get(stackNdx);
2692 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002693 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2694 continue;
2695 }
2696 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2697 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2698 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002699 continue;
2700 }
2701 final ActivityRecord ar = stack.findTaskLocked(r);
2702 if (ar != null) {
2703 return ar;
2704 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002705 }
2706 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002707 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002708 return null;
2709 }
2710
2711 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002712 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2713 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002714 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2715 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2716 if (ar != null) {
2717 return ar;
2718 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002719 }
2720 }
2721 return null;
2722 }
2723
Craig Mautner8d341ef2013-03-26 09:03:27 -07002724 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002725 scheduleSleepTimeout();
2726 if (!mGoingToSleep.isHeld()) {
2727 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002728 if (mLaunchingActivity.isHeld()) {
2729 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2730 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002731 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002732 mLaunchingActivity.release();
2733 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002734 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002735 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002736 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002737 }
2738
2739 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002740 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002741
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002742 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002743 final long endTime = System.currentTimeMillis() + timeout;
2744 while (true) {
2745 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002746 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2747 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002748 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2749 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2750 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002751 }
2752 if (cantShutdown) {
2753 long timeRemaining = endTime - System.currentTimeMillis();
2754 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002755 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002756 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002757 } catch (InterruptedException e) {
2758 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002759 } else {
2760 Slog.w(TAG, "Activity manager shutdown timed out");
2761 timedout = true;
2762 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002763 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002764 } else {
2765 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002766 }
2767 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002768
2769 // Force checkReadyForSleep to complete.
2770 mSleepTimeout = true;
2771 checkReadyForSleepLocked();
2772
Craig Mautner8d341ef2013-03-26 09:03:27 -07002773 return timedout;
2774 }
2775
2776 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002777 removeSleepTimeouts();
2778 if (mGoingToSleep.isHeld()) {
2779 mGoingToSleep.release();
2780 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002781 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2782 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002783 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2784 final ActivityStack stack = stacks.get(stackNdx);
2785 stack.awakeFromSleepingLocked();
2786 if (isFrontStack(stack)) {
2787 resumeTopActivitiesLocked();
2788 }
Craig Mautner5314a402013-09-26 12:40:16 -07002789 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002790 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002791 mGoingToSleepActivities.clear();
2792 }
2793
2794 void activitySleptLocked(ActivityRecord r) {
2795 mGoingToSleepActivities.remove(r);
2796 checkReadyForSleepLocked();
2797 }
2798
2799 void checkReadyForSleepLocked() {
2800 if (!mService.isSleepingOrShuttingDown()) {
2801 // Do not care.
2802 return;
2803 }
2804
2805 if (!mSleepTimeout) {
2806 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2808 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002809 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2810 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2811 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002812 }
2813
2814 if (mStoppingActivities.size() > 0) {
2815 // Still need to tell some activities to stop; can't sleep yet.
2816 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2817 + mStoppingActivities.size() + " activities");
2818 scheduleIdleLocked();
2819 dontSleep = true;
2820 }
2821
2822 if (mGoingToSleepActivities.size() > 0) {
2823 // Still need to tell some activities to sleep; can't sleep yet.
2824 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2825 + mGoingToSleepActivities.size() + " activities");
2826 dontSleep = true;
2827 }
2828
2829 if (dontSleep) {
2830 return;
2831 }
2832 }
2833
Craig Mautnere0a38842013-12-16 16:14:02 -08002834 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2835 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002836 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2837 stacks.get(stackNdx).goToSleep();
2838 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002839 }
2840
2841 removeSleepTimeouts();
2842
2843 if (mGoingToSleep.isHeld()) {
2844 mGoingToSleep.release();
2845 }
2846 if (mService.mShuttingDown) {
2847 mService.notifyAll();
2848 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002849 }
2850
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002851 boolean reportResumedActivityLocked(ActivityRecord r) {
2852 final ActivityStack stack = r.task.stack;
2853 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002854 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002855 }
2856 if (allResumedActivitiesComplete()) {
2857 ensureActivitiesVisibleLocked(null, 0);
2858 mWindowManager.executeAppTransition();
2859 return true;
2860 }
2861 return false;
2862 }
2863
Craig Mautner8d341ef2013-03-26 09:03:27 -07002864 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002865 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2866 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002867 final int numStacks = stacks.size();
2868 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2869 final ActivityStack stack = stacks.get(stackNdx);
2870 stack.handleAppCrashLocked(app);
2871 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002872 }
2873 }
2874
Jose Lima4b6c6692014-08-12 17:41:12 -07002875 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002876 final ActivityStack stack = r.task.stack;
2877 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002878 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2879 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002880 return false;
2881 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002882 final boolean isVisible = stack.hasVisibleBehindActivity();
2883 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2884 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002885
2886 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002887 if (top == null || top == r || (visible == isVisible)) {
2888 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2889 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002890 return true;
2891 }
2892
2893 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002894 if (visible && top.fullscreen) {
2895 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07002896 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07002897 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07002898 " top.app.thread=" + top.app.thread);
2899 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002900 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2901 // Only the activity set as currently visible behind should actively reset its
2902 // visible behind state.
2903 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
2904 + visible + " stack.getVisibleBehindActivity()=" +
2905 stack.getVisibleBehindActivity() + " r=" + r);
2906 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002907 }
2908
Jose Lima4b6c6692014-08-12 17:41:12 -07002909 stack.setVisibleBehindActivity(visible ? r : null);
2910 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002911 // Make the activity immediately above r opaque.
2912 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2913 if (next != null) {
2914 mService.convertFromTranslucent(next.appToken);
2915 }
2916 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002917 if (top.app != null && top.app.thread != null) {
2918 // Notify the top app of the change.
2919 try {
2920 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2921 } catch (RemoteException e) {
2922 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002923 }
2924 return true;
2925 }
2926
Craig Mautnerbb742462014-07-07 15:28:55 -07002927 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2928 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2929 r.mLaunchTaskBehind = false;
2930 final TaskRecord task = r.task;
2931 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002932 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002933 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002934 mWindowManager.setAppVisibility(r.appToken, false);
2935 }
2936
2937 void scheduleLaunchTaskBehindComplete(IBinder token) {
2938 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2939 }
2940
Craig Mautnerde4ef022013-04-07 19:01:33 -07002941 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002942 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002943 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2944 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002945 final int topStackNdx = stacks.size() - 1;
2946 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2947 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002948 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002949 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002950 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002951 }
2952
2953 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002954 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2955 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002956 final int numStacks = stacks.size();
2957 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2958 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002959 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002960 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002961 }
2962 }
2963
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002964 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2965 // Examine all activities currently running in the process.
2966 TaskRecord firstTask = null;
2967 // Tasks is non-null only if two or more tasks are found.
2968 ArraySet<TaskRecord> tasks = null;
2969 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
2970 for (int i=0; i<app.activities.size(); i++) {
2971 ActivityRecord r = app.activities.get(i);
2972 // First, if we find an activity that is in the process of being destroyed,
2973 // then we just aren't going to do anything for now; we want things to settle
2974 // down before we try to prune more activities.
2975 if (r.finishing || r.state == ActivityState.DESTROYING
2976 || r.state == ActivityState.DESTROYED) {
2977 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
2978 return;
2979 }
2980 // Don't consider any activies that are currently not in a state where they
2981 // can be destroyed.
2982 if (r.visible || !r.stopped || !r.haveState
2983 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
2984 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
2985 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
2986 continue;
2987 }
2988 if (r.task != null) {
2989 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
2990 + " from " + r);
2991 if (firstTask == null) {
2992 firstTask = r.task;
2993 } else if (firstTask != r.task) {
2994 if (tasks == null) {
2995 tasks = new ArraySet<>();
2996 tasks.add(firstTask);
2997 }
2998 tasks.add(r.task);
2999 }
3000 }
3001 }
3002 if (tasks == null) {
3003 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3004 return;
3005 }
3006 // If we have activities in multiple tasks that are in a position to be destroyed,
3007 // let's iterate through the tasks and release the oldest one.
3008 final int numDisplays = mActivityDisplays.size();
3009 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3010 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3011 // Step through all stacks starting from behind, to hit the oldest things first.
3012 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3013 final ActivityStack stack = stacks.get(stackNdx);
3014 // Try to release activities in this stack; if we manage to, we are done.
3015 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3016 return;
3017 }
3018 }
3019 }
3020 }
3021
Craig Mautner8d341ef2013-03-26 09:03:27 -07003022 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003023 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
3024 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003025 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003026
Craig Mautner858d8a62013-04-23 17:08:34 -07003027 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003028 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3029 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003030 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003031 final ActivityStack stack = stacks.get(stackNdx);
3032 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003033 TaskRecord task = stack.topTask();
3034 if (task != null) {
3035 mWindowManager.moveTaskToTop(task.taskId);
3036 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003037 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003038 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003039
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003040 ActivityStack stack = getStack(restoreStackId);
3041 if (stack == null) {
3042 stack = mHomeStack;
3043 }
3044 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003045 if (stack.isOnHomeDisplay()) {
3046 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003047 TaskRecord task = stack.topTask();
3048 if (task != null) {
3049 mWindowManager.moveTaskToTop(task.taskId);
3050 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003051 } else {
3052 // Stack was moved to another display while user was swapped out.
Craig Mautner84984fa2014-06-19 11:19:20 -07003053 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003054 }
Craig Mautner93529a42013-10-04 15:03:13 -07003055 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003056 }
3057
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003058 /**
3059 * Add background users to send boot completed events to.
3060 * @param userId The user being started in the background
3061 * @param uss The state object for the user.
3062 */
3063 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3064 mStartingBackgroundUsers.add(uss);
3065 }
3066
Craig Mautnerde4ef022013-04-07 19:01:33 -07003067 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003068 ArrayList<ActivityRecord> stops = null;
3069
3070 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003071 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3072 ActivityRecord s = mStoppingActivities.get(activityNdx);
3073 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
3074 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
3075 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3076 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003077 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003078 if (s.finishing) {
3079 // If this activity is finishing, it is sitting on top of
3080 // everyone else but we now know it is no longer needed...
3081 // so get rid of it. Otherwise, we need to go through the
3082 // normal flow and hide it once we determine that it is
3083 // hidden by the activities in front of it.
3084 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003085 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003086 }
3087 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003088 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003089 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3090 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003091 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003092 }
3093 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003094 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003095 }
3096 }
3097
3098 return stops;
3099 }
3100
Craig Mautnercf910b02013-04-23 11:23:27 -07003101 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003102 // FIXME
3103/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3104 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003105 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003106 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003107 if (isFrontStack(stack)) {
3108 if (r == null) {
3109 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3110 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003111 final ActivityRecord pausing = stack.mPausingActivity;
3112 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003113 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003114 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003115 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003116 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003117 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003118 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003119 }
3120 }
3121 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003122 final ActivityRecord resumed = stack.mResumedActivity;
3123 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003124 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003125 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003126 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003127 if (r != null && (state == ActivityState.INITIALIZING
3128 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003129 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003130 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003131 }
3132 }
3133 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003134*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003135 }
3136
Craig Mautner27084302013-03-25 08:05:25 -07003137 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003138 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003139 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003140 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3141 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3142 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003143 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003144 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003145
Craig Mautner20e72272013-04-01 13:45:53 -07003146 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003147 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003148 }
3149
Dianne Hackborn390517b2013-05-30 15:03:32 -07003150 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3151 boolean needSep, String prefix) {
3152 if (activity != null) {
3153 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3154 if (needSep) {
3155 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003156 }
3157 pw.print(prefix);
3158 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003159 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003160 }
3161 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003162 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003163 }
3164
Craig Mautner8d341ef2013-03-26 09:03:27 -07003165 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3166 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003167 boolean printed = false;
3168 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003169 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3170 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003171 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003172 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003173 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003174 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003175 final ActivityStack stack = stacks.get(stackNdx);
3176 StringBuilder stackHeader = new StringBuilder(128);
3177 stackHeader.append(" Stack #");
3178 stackHeader.append(stack.mStackId);
3179 stackHeader.append(":");
3180 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3181 needSep, stackHeader.toString());
3182 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3183 !dumpAll, false, dumpPackage, true,
3184 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003185
Craig Mautner4a1cb222013-12-04 16:14:06 -08003186 needSep = printed;
3187 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3188 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003189 if (pr) {
3190 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003191 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003192 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003193 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3194 " mResumedActivity: ");
3195 if (pr) {
3196 printed = true;
3197 needSep = false;
3198 }
3199 if (dumpAll) {
3200 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3201 " mLastPausedActivity: ");
3202 if (pr) {
3203 printed = true;
3204 needSep = true;
3205 }
3206 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3207 needSep, " mLastNoHistoryActivity: ");
3208 }
3209 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003210 }
3211 }
3212
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003213 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3214 false, dumpPackage, true, " Activities waiting to finish:", null);
3215 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3216 false, dumpPackage, true, " Activities waiting to stop:", null);
3217 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3218 false, dumpPackage, true, " Activities waiting for another to become visible:",
3219 null);
3220 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3221 false, dumpPackage, true, " Activities waiting to sleep:", null);
3222 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3223 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003224
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003225 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003226 }
3227
Dianne Hackborn390517b2013-05-30 15:03:32 -07003228 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003229 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003230 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003231 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003232 String innerPrefix = null;
3233 String[] args = null;
3234 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003235 for (int i=list.size()-1; i>=0; i--) {
3236 final ActivityRecord r = list.get(i);
3237 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3238 continue;
3239 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003240 if (innerPrefix == null) {
3241 innerPrefix = prefix + " ";
3242 args = new String[0];
3243 }
3244 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003245 final boolean full = !brief && (complete || !r.isInHistory());
3246 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003247 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003248 needNL = false;
3249 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003250 if (header1 != null) {
3251 pw.println(header1);
3252 header1 = null;
3253 }
3254 if (header2 != null) {
3255 pw.println(header2);
3256 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003257 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003258 if (lastTask != r.task) {
3259 lastTask = r.task;
3260 pw.print(prefix);
3261 pw.print(full ? "* " : " ");
3262 pw.println(lastTask);
3263 if (full) {
3264 lastTask.dump(pw, prefix + " ");
3265 } else if (complete) {
3266 // Complete + brief == give a summary. Isn't that obvious?!?
3267 if (lastTask.intent != null) {
3268 pw.print(prefix); pw.print(" ");
3269 pw.println(lastTask.intent.toInsecureStringWithClip());
3270 }
3271 }
3272 }
3273 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3274 pw.print(" #"); pw.print(i); pw.print(": ");
3275 pw.println(r);
3276 if (full) {
3277 r.dump(pw, innerPrefix);
3278 } else if (complete) {
3279 // Complete + brief == give a summary. Isn't that obvious?!?
3280 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3281 if (r.app != null) {
3282 pw.print(innerPrefix); pw.println(r.app);
3283 }
3284 }
3285 if (client && r.app != null && r.app.thread != null) {
3286 // flush anything that is already in the PrintWriter since the thread is going
3287 // to write to the file descriptor directly
3288 pw.flush();
3289 try {
3290 TransferPipe tp = new TransferPipe();
3291 try {
3292 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3293 r.appToken, innerPrefix, args);
3294 // Short timeout, since blocking here can
3295 // deadlock with the application.
3296 tp.go(fd, 2000);
3297 } finally {
3298 tp.kill();
3299 }
3300 } catch (IOException e) {
3301 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3302 } catch (RemoteException e) {
3303 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3304 }
3305 needNL = true;
3306 }
3307 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003308 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003309 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003310
Craig Mautnerf3333272013-04-22 10:55:53 -07003311 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003312 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003313 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3314 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003315 }
3316
3317 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003318 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003319 }
3320
3321 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003322 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003323 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3324 }
3325
Craig Mautner05d29032013-05-03 13:40:13 -07003326 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003327 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3328 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3329 }
Craig Mautner05d29032013-05-03 13:40:13 -07003330 }
3331
Craig Mautner0eea92c2013-05-16 13:35:39 -07003332 void removeSleepTimeouts() {
3333 mSleepTimeout = false;
3334 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3335 }
3336
3337 final void scheduleSleepTimeout() {
3338 removeSleepTimeouts();
3339 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3340 }
3341
Craig Mautner4a1cb222013-12-04 16:14:06 -08003342 @Override
3343 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003344 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003345 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3346 }
3347
3348 @Override
3349 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003350 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003351 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3352 }
3353
3354 @Override
3355 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003356 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003357 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3358 }
3359
3360 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003361 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003362 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003363 newDisplay = mActivityDisplays.get(displayId) == null;
3364 if (newDisplay) {
3365 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003366 if (activityDisplay.mDisplay == null) {
3367 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3368 return;
3369 }
Craig Mautner4504de52013-12-20 09:06:56 -08003370 mActivityDisplays.put(displayId, activityDisplay);
3371 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003372 }
Craig Mautner4504de52013-12-20 09:06:56 -08003373 if (newDisplay) {
3374 mWindowManager.onDisplayAdded(displayId);
3375 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003376 }
3377
3378 public void handleDisplayRemovedLocked(int displayId) {
3379 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003380 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3381 if (activityDisplay != null) {
3382 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003383 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003384 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003385 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003386 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003387 }
3388 }
3389 mWindowManager.onDisplayRemoved(displayId);
3390 }
3391
3392 public void handleDisplayChangedLocked(int displayId) {
3393 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003394 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3395 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003396 // TODO: Update the bounds.
3397 }
3398 }
3399 mWindowManager.onDisplayChanged(displayId);
3400 }
3401
3402 StackInfo getStackInfo(ActivityStack stack) {
3403 StackInfo info = new StackInfo();
3404 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3405 info.displayId = Display.DEFAULT_DISPLAY;
3406 info.stackId = stack.mStackId;
3407
3408 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3409 final int numTasks = tasks.size();
3410 int[] taskIds = new int[numTasks];
3411 String[] taskNames = new String[numTasks];
3412 for (int i = 0; i < numTasks; ++i) {
3413 final TaskRecord task = tasks.get(i);
3414 taskIds[i] = task.taskId;
3415 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3416 : task.realActivity != null ? task.realActivity.flattenToString()
3417 : task.getTopActivity() != null ? task.getTopActivity().packageName
3418 : "unknown";
3419 }
3420 info.taskIds = taskIds;
3421 info.taskNames = taskNames;
3422 return info;
3423 }
3424
3425 StackInfo getStackInfoLocked(int stackId) {
3426 ActivityStack stack = getStack(stackId);
3427 if (stack != null) {
3428 return getStackInfo(stack);
3429 }
3430 return null;
3431 }
3432
3433 ArrayList<StackInfo> getAllStackInfosLocked() {
3434 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003435 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3436 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003437 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3438 list.add(getStackInfo(stacks.get(ndx)));
3439 }
3440 }
3441 return list;
3442 }
3443
Jason Monka8f569c2014-07-07 12:15:21 -04003444 void showLockTaskToast() {
3445 mLockTaskNotify.showToast(mLockTaskIsLocked);
3446 }
3447
3448 void setLockTaskModeLocked(TaskRecord task, boolean isLocked) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003449 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003450 // Take out of lock task mode if necessary
3451 if (mLockTaskModeTask != null) {
3452 final Message lockTaskMsg = Message.obtain();
3453 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3454 lockTaskMsg.what = LOCK_TASK_END_MSG;
3455 mLockTaskModeTask = null;
3456 mHandler.sendMessage(lockTaskMsg);
3457 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003458 return;
3459 }
3460 if (isLockTaskModeViolation(task)) {
3461 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3462 return;
3463 }
3464 mLockTaskModeTask = task;
3465 findTaskToMoveToFrontLocked(task, 0, null);
3466 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003467
3468 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003469 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3470 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003471 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003472 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003473 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003474 }
3475
3476 boolean isLockTaskModeViolation(TaskRecord task) {
3477 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3478 }
3479
3480 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3481 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003482 final Message lockTaskMsg = Message.obtain();
3483 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3484 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003485 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003486 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003487 }
3488 }
3489
3490 boolean isInLockTaskMode() {
3491 return mLockTaskModeTask != null;
3492 }
3493
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003494 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003495
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003496 public ActivityStackSupervisorHandler(Looper looper) {
3497 super(looper);
3498 }
3499
Craig Mautnerf3333272013-04-22 10:55:53 -07003500 void activityIdleInternal(ActivityRecord r) {
3501 synchronized (mService) {
3502 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3503 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003504 }
3505
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003506 @Override
3507 public void handleMessage(Message msg) {
3508 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003509 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003510 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003511 if (mService.mDidDexOpt) {
3512 mService.mDidDexOpt = false;
3513 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3514 nmsg.obj = msg.obj;
3515 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3516 return;
3517 }
3518 // We don't at this point know if the activity is fullscreen,
3519 // so we need to be conservative and assume it isn't.
3520 activityIdleInternal((ActivityRecord)msg.obj);
3521 } break;
3522 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003523 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003524 activityIdleInternal((ActivityRecord)msg.obj);
3525 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003526 case RESUME_TOP_ACTIVITY_MSG: {
3527 synchronized (mService) {
3528 resumeTopActivitiesLocked();
3529 }
3530 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003531 case SLEEP_TIMEOUT_MSG: {
3532 synchronized (mService) {
3533 if (mService.isSleepingOrShuttingDown()) {
3534 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3535 mSleepTimeout = true;
3536 checkReadyForSleepLocked();
3537 }
3538 }
3539 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003540 case LAUNCH_TIMEOUT_MSG: {
3541 if (mService.mDidDexOpt) {
3542 mService.mDidDexOpt = false;
3543 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3544 return;
3545 }
3546 synchronized (mService) {
3547 if (mLaunchingActivity.isHeld()) {
3548 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3549 if (VALIDATE_WAKE_LOCK_CALLER
3550 && Binder.getCallingUid() != Process.myUid()) {
3551 throw new IllegalStateException("Calling must be system uid");
3552 }
3553 mLaunchingActivity.release();
3554 }
3555 }
3556 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003557 case HANDLE_DISPLAY_ADDED: {
3558 handleDisplayAddedLocked(msg.arg1);
3559 } break;
3560 case HANDLE_DISPLAY_CHANGED: {
3561 handleDisplayChangedLocked(msg.arg1);
3562 } break;
3563 case HANDLE_DISPLAY_REMOVED: {
3564 handleDisplayRemovedLocked(msg.arg1);
3565 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003566 case CONTAINER_CALLBACK_VISIBILITY: {
3567 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003568 final IActivityContainerCallback callback = container.mCallback;
3569 if (callback != null) {
3570 try {
3571 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3572 } catch (RemoteException e) {
3573 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003574 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003575 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003576 case LOCK_TASK_START_MSG: {
3577 // When lock task starts, we disable the status bars.
3578 try {
Jason Monk62515be2014-05-21 16:06:19 -04003579 if (mLockTaskNotify == null) {
3580 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003581 }
Jason Monk62515be2014-05-21 16:06:19 -04003582 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003583 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003584 if (getStatusBarService() != null) {
3585 int flags =
3586 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003587 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003588 flags ^= StatusBarManager.DISABLE_HOME
3589 | StatusBarManager.DISABLE_RECENT;
3590 }
3591 getStatusBarService().disable(flags, mToken,
3592 mService.mContext.getPackageName());
3593 }
3594 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003595 if (getDevicePolicyManager() != null) {
3596 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003597 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003598 }
justinzhang5286d3f2014-05-12 17:06:01 -04003599 } catch (RemoteException ex) {
3600 throw new RuntimeException(ex);
3601 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003602 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003603 case LOCK_TASK_END_MSG: {
3604 // When lock task ends, we enable the status bars.
3605 try {
Jason Monk62515be2014-05-21 16:06:19 -04003606 if (getStatusBarService() != null) {
3607 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3608 mService.mContext.getPackageName());
3609 }
3610 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003611 if (getDevicePolicyManager() != null) {
3612 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3613 msg.arg1);
3614 }
Jason Monk62515be2014-05-21 16:06:19 -04003615 if (mLockTaskNotify == null) {
3616 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3617 }
3618 mLockTaskNotify.show(false);
3619 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003620 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003621 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003622 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003623 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003624 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003625 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003626 new LockPatternUtils(mService.mContext)
3627 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003628 }
3629 } catch (SettingNotFoundException e) {
3630 // No setting, don't lock.
3631 }
justinzhang5286d3f2014-05-12 17:06:01 -04003632 } catch (RemoteException ex) {
3633 throw new RuntimeException(ex);
3634 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003635 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003636 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3637 final ActivityContainer container = (ActivityContainer) msg.obj;
3638 final IActivityContainerCallback callback = container.mCallback;
3639 if (callback != null) {
3640 try {
3641 callback.onAllActivitiesComplete(container.asBinder());
3642 } catch (RemoteException e) {
3643 }
3644 }
3645 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003646 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3647 synchronized (mService) {
3648 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3649 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003650 final ActivityContainer container = (ActivityContainer) msg.obj;
3651 container.mStack.finishAllActivitiesLocked(true);
3652 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003653 }
3654 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003655 case LAUNCH_TASK_BEHIND_COMPLETE: {
3656 synchronized (mService) {
3657 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3658 if (r != null) {
3659 handleLaunchTaskBehindCompleteLocked(r);
3660 }
3661 }
3662 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003663 }
3664 }
3665 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003666
Ying Wangb081a592014-04-22 15:20:16 -07003667 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003668 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003669 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003670 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003671 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003672 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003673 ActivityRecord mParentActivity = null;
3674 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003675
Craig Mautnere3a00d72014-04-16 08:31:19 -07003676 boolean mVisible = true;
3677
Craig Mautner4a1cb222013-12-04 16:14:06 -08003678 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003679 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003680
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003681 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3682 final static int CONTAINER_STATE_NO_SURFACE = 1;
3683 final static int CONTAINER_STATE_FINISHING = 2;
3684 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3685
3686 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003687 synchronized (mService) {
3688 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003689 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003690 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003691 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003692 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003693 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003694
Craig Mautnere0a38842013-12-16 16:14:02 -08003695 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003696 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3697 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003698 mActivityDisplay = activityDisplay;
3699 mStack.mDisplayId = activityDisplay.mDisplayId;
3700 mStack.mStacks = activityDisplay.mStacks;
3701
3702 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003703 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003704 }
3705
3706 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003707 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003708 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003709 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3710 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003711 return;
3712 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003713 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003714 }
3715 }
3716
3717 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003718 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003719 synchronized (mService) {
3720 if (mActivityDisplay != null) {
3721 return mActivityDisplay.mDisplayId;
3722 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003723 }
3724 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003725 }
3726
Jeff Brownca9bc702014-02-11 14:32:56 -08003727 @Override
3728 public boolean injectEvent(InputEvent event) {
3729 final long origId = Binder.clearCallingIdentity();
3730 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003731 synchronized (mService) {
3732 if (mActivityDisplay != null) {
3733 return mInputManagerInternal.injectInputEvent(event,
3734 mActivityDisplay.mDisplayId,
3735 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3736 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003737 }
3738 return false;
3739 } finally {
3740 Binder.restoreCallingIdentity(origId);
3741 }
3742 }
3743
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003744 @Override
3745 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003746 synchronized (mService) {
3747 if (mContainerState == CONTAINER_STATE_FINISHING) {
3748 return;
3749 }
3750 mContainerState = CONTAINER_STATE_FINISHING;
3751
3752 final Message msg =
3753 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003754 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003755
3756 long origId = Binder.clearCallingIdentity();
3757 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003758 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003759 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003760 } finally {
3761 Binder.restoreCallingIdentity(origId);
3762 }
3763 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003764 }
3765
Craig Mautner60257702014-09-17 15:02:33 -07003766 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003767 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3768 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003769 if (mActivityDisplay != null) {
3770 mActivityDisplay.detachActivitiesLocked(mStack);
3771 mActivityDisplay = null;
3772 mStack.mDisplayId = -1;
3773 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003774 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003775 }
3776 }
3777
3778 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003779 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003780 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003781 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003782 Binder.getCallingUid(), mCurrentUser, false,
3783 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003784 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003785 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003786 String mimeType = intent.getType();
3787 if (mimeType == null && intent.getData() != null
3788 && "content".equals(intent.getData().getScheme())) {
3789 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3790 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003791 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0,
3792 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003793 }
3794
3795 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003796 public final int startActivityIntentSender(IIntentSender intentSender) {
3797 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3798
3799 if (!(intentSender instanceof PendingIntentRecord)) {
3800 throw new IllegalArgumentException("Bad PendingIntent object");
3801 }
3802
3803 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003804 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003805 }
3806
Craig Mautner247ab652014-04-25 10:09:00 -07003807 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3808 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003809 Binder.getCallingUid(), mCurrentUser, false,
3810 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003811 if (resolvedType == null) {
3812 resolvedType = intent.getType();
3813 if (resolvedType == null && intent.getData() != null
3814 && "content".equals(intent.getData().getScheme())) {
3815 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3816 }
3817 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003818 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003819 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003820 throw new SecurityException(
3821 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3822 }
3823 }
3824
3825 /** Throw a SecurityException if allowEmbedded is not true */
3826 @Override
3827 public final void checkEmbeddedAllowed(Intent intent) {
3828 checkEmbeddedAllowedInner(intent, null);
3829 }
3830
3831 /** Throw a SecurityException if allowEmbedded is not true */
3832 @Override
3833 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3834 if (!(intentSender instanceof PendingIntentRecord)) {
3835 throw new IllegalArgumentException("Bad PendingIntent object");
3836 }
3837 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3838 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3839 pendingIntent.key.requestResolvedType);
3840 }
3841
Craig Mautnerdf88d732014-01-27 09:21:32 -08003842 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003843 public IBinder asBinder() {
3844 return this;
3845 }
3846
Craig Mautner4504de52013-12-20 09:06:56 -08003847 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003848 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003849 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003850 }
3851
Craig Mautner4a1cb222013-12-04 16:14:06 -08003852 ActivityStackSupervisor getOuter() {
3853 return ActivityStackSupervisor.this;
3854 }
3855
Craig Mautnerd163e752014-06-13 17:18:47 -07003856 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003857 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003858 }
3859
3860 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003861 synchronized (mService) {
3862 if (mActivityDisplay != null) {
3863 mActivityDisplay.getBounds(outBounds);
3864 } else {
3865 outBounds.set(0, 0);
3866 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003867 }
3868 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003869
Craig Mautner6985bad2014-04-21 15:22:06 -07003870 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003871 void setVisible(boolean visible) {
3872 if (mVisible != visible) {
3873 mVisible = visible;
3874 if (mCallback != null) {
3875 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3876 0 /* unused */, this).sendToTarget();
3877 }
3878 }
3879 }
3880
Craig Mautner6985bad2014-04-21 15:22:06 -07003881 void setDrawn() {
3882 }
3883
Craig Mautner1b4bf852014-05-26 15:06:32 -07003884 // You can always start a new task on a regular ActivityStack.
3885 boolean isEligibleForNewTasks() {
3886 return true;
3887 }
3888
Craig Mautnerd163e752014-06-13 17:18:47 -07003889 void onTaskListEmptyLocked() {
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003890 }
3891
Craig Mautner34b73df2014-01-12 21:11:08 -08003892 @Override
3893 public String toString() {
3894 return mIdString + (mActivityDisplay == null ? "N" : "A");
3895 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003896 }
3897
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003898 private class VirtualActivityContainer extends ActivityContainer {
3899 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003900 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003901
3902 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3903 super(getNextStackId());
3904 mParentActivity = parent;
3905 mCallback = callback;
3906 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003907 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003908 }
3909
3910 @Override
3911 public void setSurface(Surface surface, int width, int height, int density) {
3912 super.setSurface(surface, width, height, density);
3913
3914 synchronized (mService) {
3915 final long origId = Binder.clearCallingIdentity();
3916 try {
3917 setSurfaceLocked(surface, width, height, density);
3918 } finally {
3919 Binder.restoreCallingIdentity(origId);
3920 }
3921 }
3922 }
3923
3924 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3925 if (mContainerState == CONTAINER_STATE_FINISHING) {
3926 return;
3927 }
3928 VirtualActivityDisplay virtualActivityDisplay =
3929 (VirtualActivityDisplay) mActivityDisplay;
3930 if (virtualActivityDisplay == null) {
3931 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003932 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003933 mActivityDisplay = virtualActivityDisplay;
3934 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3935 attachToDisplayLocked(virtualActivityDisplay);
3936 }
3937
3938 if (mSurface != null) {
3939 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003940 }
3941
Craig Mautner6985bad2014-04-21 15:22:06 -07003942 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003943 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003944 mStack.resumeTopActivityLocked(null);
3945 } else {
3946 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003947 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003948 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003949 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003950 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003951 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003952
Craig Mautnerd163e752014-06-13 17:18:47 -07003953 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003954
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003955 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3956 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003957 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003958
Craig Mautner6985bad2014-04-21 15:22:06 -07003959 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003960 boolean isAttachedLocked() {
3961 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003962 }
3963
3964 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003965 void setDrawn() {
3966 synchronized (mService) {
3967 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003968 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003969 }
3970 }
3971
Craig Mautner1b4bf852014-05-26 15:06:32 -07003972 // Never start a new task on an ActivityView if it isn't explicitly specified.
3973 @Override
3974 boolean isEligibleForNewTasks() {
3975 return false;
3976 }
3977
Craig Mautner60257702014-09-17 15:02:33 -07003978 void onTaskListEmptyLocked() {
3979 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3980 detachLocked();
3981 deleteActivityContainer(this);
3982 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3983 }
3984
Craig Mautnerd163e752014-06-13 17:18:47 -07003985 private void setSurfaceIfReadyLocked() {
3986 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003987 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3988 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3989 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3990 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3991 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003992 }
3993 }
3994
Craig Mautner4a1cb222013-12-04 16:14:06 -08003995 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3996 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003997 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003998 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003999 int mDisplayId;
4000 Display mDisplay;
4001 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004002
Craig Mautner4a1cb222013-12-04 16:14:06 -08004003 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4004 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004005 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004006
Jose Lima4b6c6692014-08-12 17:41:12 -07004007 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004008
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004009 ActivityDisplay() {
4010 }
Craig Mautner4504de52013-12-20 09:06:56 -08004011
Craig Mautner1a70a162014-09-13 12:09:31 -07004012 // After instantiation, check that mDisplay is not null before using this. The alternative
4013 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004014 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004015 final Display display = mDisplayManager.getDisplay(displayId);
4016 if (display == null) {
4017 return;
4018 }
4019 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004020 }
4021
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004022 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004023 mDisplay = display;
4024 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004025 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004026 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004027
4028 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004029 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4030 + mDisplayId);
4031 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004032 }
4033
Craig Mautnere0a38842013-12-16 16:14:02 -08004034 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004035 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004036 + " from displayId=" + mDisplayId);
4037 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004038 }
4039
4040 void getBounds(Point bounds) {
4041 mDisplay.getDisplayInfo(mDisplayInfo);
4042 bounds.x = mDisplayInfo.appWidth;
4043 bounds.y = mDisplayInfo.appHeight;
4044 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004045
Jose Lima4b6c6692014-08-12 17:41:12 -07004046 void setVisibleBehindActivity(ActivityRecord r) {
4047 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004048 }
4049
Jose Lima4b6c6692014-08-12 17:41:12 -07004050 boolean hasVisibleBehindActivity() {
4051 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004052 }
4053
Craig Mautner34b73df2014-01-12 21:11:08 -08004054 @Override
4055 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004056 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4057 }
4058 }
4059
4060 class VirtualActivityDisplay extends ActivityDisplay {
4061 VirtualDisplay mVirtualDisplay;
4062
Craig Mautner6985bad2014-04-21 15:22:06 -07004063 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004064 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004065 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4066 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4067 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4068 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004069
4070 init(mVirtualDisplay.getDisplay());
4071
4072 mWindowManager.handleDisplayAdded(mDisplayId);
4073 }
4074
4075 void setSurface(Surface surface) {
4076 if (mVirtualDisplay != null) {
4077 mVirtualDisplay.setSurface(surface);
4078 }
4079 }
4080
4081 @Override
4082 void detachActivitiesLocked(ActivityStack stack) {
4083 super.detachActivitiesLocked(stack);
4084 if (mVirtualDisplay != null) {
4085 mVirtualDisplay.release();
4086 mVirtualDisplay = null;
4087 }
4088 }
4089
4090 @Override
4091 public String toString() {
4092 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004093 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004094 }
Jose Lima58e66d62014-05-27 20:00:27 -07004095
4096 private boolean isLeanbackOnlyDevice() {
4097 boolean onLeanbackOnly = false;
4098 try {
4099 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4100 PackageManager.FEATURE_LEANBACK_ONLY);
4101 } catch (RemoteException e) {
4102 // noop
4103 }
4104
4105 return onLeanbackOnly;
4106 }
Craig Mautner27084302013-03-25 08:05:25 -07004107}