blob: c68430eb165cef5405557373eb64a1324a970cbf [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 Mautner299f9602015-01-26 09:47:33 -0800402 void moveHomeStack(boolean toFront, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800403 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800404 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800405 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 Mautnerde313752015-01-22 14:28:03 -0800418 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
419 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800420 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800421
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800422 if (mService.mBooting || !mService.mBooted) {
423 final ActivityRecord r = topRunningActivityLocked();
424 if (r != null && r.idle) {
425 checkFinishBootingLocked();
426 }
427 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700428 }
429
Craig Mautner299f9602015-01-26 09:47:33 -0800430 void moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700431 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
432 mWindowManager.showRecentApps();
433 return;
434 }
Craig Mautner299f9602015-01-26 09:47:33 -0800435 moveHomeStack(true, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700436 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700437 }
438
Craig Mautner299f9602015-01-26 09:47:33 -0800439 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700440 if (!mService.mBooting && !mService.mBooted) {
441 // Not ready yet!
442 return false;
443 }
444
Craig Mautner84984fa2014-06-19 11:19:20 -0700445 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
446 mWindowManager.showRecentApps();
447 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700448 }
Craig Mautner299f9602015-01-26 09:47:33 -0800449 moveHomeStackTaskToTop(homeStackTaskType, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700450 if (prev != null) {
451 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
452 }
453
Craig Mautnera8a90e02013-06-28 15:24:50 -0700454 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700455 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000456 if (r != null && r.isHomeActivity()) {
Craig Mautner299f9602015-01-26 09:47:33 -0800457 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700458 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700459 }
Craig Mautner299f9602015-01-26 09:47:33 -0800460 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700461 }
462
Craig Mautner8d341ef2013-03-26 09:03:27 -0700463 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800464 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800465 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800466 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800467 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
468 ActivityStack stack = stacks.get(stackNdx);
469 TaskRecord task = stack.taskForIdLocked(id);
470 if (task != null) {
471 return task;
472 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700473 }
474 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800475
476 // Don't give up! Look in recents.
477 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800478 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800479 if (task == null) {
480 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
481 return null;
482 }
483
484 if (!restoreRecentTaskLocked(task)) {
485 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
486 return null;
487 }
488 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
489 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700490 }
491
Craig Mautner6170f732013-04-02 13:05:23 -0700492 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800493 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800494 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800495 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800496 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
497 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
498 if (r != null) {
499 return r;
500 }
Craig Mautner6170f732013-04-02 13:05:23 -0700501 }
502 }
503 return null;
504 }
505
Craig Mautneref73ee12014-04-23 11:45:37 -0700506 void setNextTaskId(int taskId) {
507 if (taskId > mCurTaskId) {
508 mCurTaskId = taskId;
509 }
510 }
511
Craig Mautner8d341ef2013-03-26 09:03:27 -0700512 int getNextTaskId() {
513 do {
514 mCurTaskId++;
515 if (mCurTaskId <= 0) {
516 mCurTaskId = 1;
517 }
518 } while (anyTaskForIdLocked(mCurTaskId) != null);
519 return mCurTaskId;
520 }
521
Craig Mautnerde4ef022013-04-07 19:01:33 -0700522 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700523 ActivityStack stack = getFocusedStack();
524 if (stack == null) {
525 return null;
526 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700527 ActivityRecord resumedActivity = stack.mResumedActivity;
528 if (resumedActivity == null || resumedActivity.app == null) {
529 resumedActivity = stack.mPausingActivity;
530 if (resumedActivity == null || resumedActivity.app == null) {
531 resumedActivity = stack.topRunningActivityLocked(null);
532 }
533 }
534 return resumedActivity;
535 }
536
Dianne Hackbornff072722014-09-24 10:56:28 -0700537 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700538 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800539 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800540 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
541 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
543 final ActivityStack stack = stacks.get(stackNdx);
544 if (!isFrontStack(stack)) {
545 continue;
546 }
547 ActivityRecord hr = stack.topRunningActivityLocked(null);
548 if (hr != null) {
549 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
550 && processName.equals(hr.processName)) {
551 try {
George Mount2c92c972014-03-20 09:38:23 -0700552 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800553 didSomething = true;
554 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700555 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800556 Slog.w(TAG, "Exception in new application when starting activity "
557 + hr.intent.getComponent().flattenToShortString(), e);
558 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700559 }
Craig Mautner20e72272013-04-01 13:45:53 -0700560 }
Craig Mautner20e72272013-04-01 13:45:53 -0700561 }
562 }
563 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700564 if (!didSomething) {
565 ensureActivitiesVisibleLocked(null, 0);
566 }
Craig Mautner20e72272013-04-01 13:45:53 -0700567 return didSomething;
568 }
569
570 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800571 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
572 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800573 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
574 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800575 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800576 continue;
577 }
578 final ActivityRecord resumedActivity = stack.mResumedActivity;
579 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800580 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
581 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800582 return false;
583 }
Craig Mautner20e72272013-04-01 13:45:53 -0700584 }
585 }
586 return true;
587 }
588
Craig Mautnerde4ef022013-04-07 19:01:33 -0700589 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800590 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
591 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
593 final ActivityStack stack = stacks.get(stackNdx);
594 if (isFrontStack(stack)) {
595 final ActivityRecord r = stack.mResumedActivity;
596 if (r != null && r.state != ActivityState.RESUMED) {
597 return false;
598 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700599 }
600 }
601 }
602 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800603 if (DEBUG_STACK) Slog.d(TAG,
604 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
605 mLastFocusedStack + " to=" + mFocusedStack);
606 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700607 return true;
608 }
609
610 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800611 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
612 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
614 final ActivityStack stack = stacks.get(stackNdx);
615 final ActivityRecord r = stack.mResumedActivity;
Craig Mautner8c14c152015-01-15 17:32:07 -0800616 if (r != null && (!r.nowVisible || mWaitingVisibleActivities.contains(r))) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800617 return false;
618 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700619 }
620 }
621 return true;
622 }
623
Craig Mautner2acc3892013-09-23 10:28:14 -0700624 /**
625 * Pause all activities in either all of the stacks or just the back stacks.
626 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700627 * @return true if any activity was paused as a result of this call.
628 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700629 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700630 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800631 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
632 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800633 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
634 final ActivityStack stack = stacks.get(stackNdx);
635 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
636 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
637 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700638 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
639 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800640 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700641 }
642 }
643 return someActivityPaused;
644 }
645
Craig Mautnerde4ef022013-04-07 19:01:33 -0700646 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700647 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800648 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
649 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
651 final ActivityStack stack = stacks.get(stackNdx);
652 final ActivityRecord r = stack.mPausingActivity;
653 if (r != null && r.state != ActivityState.PAUSED
654 && r.state != ActivityState.STOPPED
655 && r.state != ActivityState.STOPPING) {
656 if (DEBUG_STATES) {
657 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
658 pausing = false;
659 } else {
660 return false;
661 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700662 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700663 }
664 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700665 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700666 }
667
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700668 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
669 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500670 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800671 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
672 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
673 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
674 final ActivityStack stack = stacks.get(stackNdx);
675 if (stack.mResumedActivity != null &&
676 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700677 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800678 }
679 }
680 }
681 }
682
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700683 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700684 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700685 }
686
687 void sendWaitingVisibleReportLocked(ActivityRecord r) {
688 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700689 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700690 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700691 if (w.who == null) {
692 changed = true;
693 w.timeout = false;
694 if (r != null) {
695 w.who = new ComponentName(r.info.packageName, r.info.name);
696 }
697 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
698 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700699 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700700 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700701 if (changed) {
702 mService.notifyAll();
703 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700704 }
705
706 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
707 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700708 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700709 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700710 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700711 if (w.who == null) {
712 changed = true;
713 w.timeout = timeout;
714 if (r != null) {
715 w.who = new ComponentName(r.info.packageName, r.info.name);
716 }
717 w.thisTime = thisTime;
718 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700719 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700720 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700721 if (changed) {
722 mService.notifyAll();
723 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700724 }
725
Craig Mautner29219d92013-04-16 20:19:12 -0700726 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700727 final ActivityStack focusedStack = getFocusedStack();
728 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
729 if (r != null) {
730 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700731 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700732
Craig Mautner4a1cb222013-12-04 16:14:06 -0800733 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800734 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800735 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
736 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700737 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700738 r = stack.topRunningActivityLocked(null);
739 if (r != null) {
740 return r;
741 }
742 }
743 }
744 return null;
745 }
746
Dianne Hackborn09233282014-04-30 11:33:59 -0700747 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700748 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800749 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
750 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800751 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800752 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800753 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800754 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
755 final ActivityStack stack = stacks.get(stackNdx);
756 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
757 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700758 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700759 }
760 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700761
762 // The lists are already sorted from most recent to oldest. Just pull the most recent off
763 // each list and add it to list. Stop when all lists are empty or maxNum reached.
764 while (maxNum > 0) {
765 long mostRecentActiveTime = Long.MIN_VALUE;
766 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 final int numTaskLists = runningTaskLists.size();
768 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
769 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700770 if (!stackTaskList.isEmpty()) {
771 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
772 if (lastActiveTime > mostRecentActiveTime) {
773 mostRecentActiveTime = lastActiveTime;
774 selectedStackList = stackTaskList;
775 }
776 }
777 }
778 if (selectedStackList != null) {
779 list.add(selectedStackList.remove(0));
780 --maxNum;
781 } else {
782 break;
783 }
784 }
Craig Mautner20e72272013-04-01 13:45:53 -0700785 }
786
Craig Mautner23ac33b2013-04-01 16:26:35 -0700787 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700788 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700789 // Collect information about the target of the Intent.
790 ActivityInfo aInfo;
791 try {
792 ResolveInfo rInfo =
793 AppGlobals.getPackageManager().resolveIntent(
794 intent, resolvedType,
795 PackageManager.MATCH_DEFAULT_ONLY
796 | ActivityManagerService.STOCK_PM_FLAGS, userId);
797 aInfo = rInfo != null ? rInfo.activityInfo : null;
798 } catch (RemoteException e) {
799 aInfo = null;
800 }
801
802 if (aInfo != null) {
803 // Store the found target back into the intent, because now that
804 // we have it we never want to do this again. For example, if the
805 // user navigates back to this point in the history, we should
806 // always restart the exact same activity.
807 intent.setComponent(new ComponentName(
808 aInfo.applicationInfo.packageName, aInfo.name));
809
810 // Don't debug things in the system process
811 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
812 if (!aInfo.processName.equals("system")) {
813 mService.setDebugApp(aInfo.processName, true, false);
814 }
815 }
816
817 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
818 if (!aInfo.processName.equals("system")) {
819 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
820 }
821 }
822
Jeff Hao1b012d32014-08-20 10:35:34 -0700823 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700824 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700825 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700826 }
827 }
828 }
829 return aInfo;
830 }
831
Craig Mautner299f9602015-01-26 09:47:33 -0800832 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
833 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700834 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
835 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700836 }
837
Craig Mautner23ac33b2013-04-01 16:26:35 -0700838 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700839 String callingPackage, Intent intent, String resolvedType,
840 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700841 IBinder resultTo, String resultWho, int requestCode, int startFlags,
842 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700843 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700844 // Refuse possible leaked file descriptors
845 if (intent != null && intent.hasFileDescriptors()) {
846 throw new IllegalArgumentException("File descriptors passed in Intent");
847 }
848 boolean componentSpecified = intent.getComponent() != null;
849
850 // Don't modify the client's object!
851 intent = new Intent(intent);
852
853 // Collect information about the target of the Intent.
854 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700855 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700856
Craig Mautnere0a38842013-12-16 16:14:02 -0800857 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700858 synchronized (mService) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700859 final int realCallingPid = Binder.getCallingPid();
860 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700861 int callingPid;
862 if (callingUid >= 0) {
863 callingPid = -1;
864 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700865 callingPid = realCallingPid;
866 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700867 } else {
868 callingPid = callingUid = -1;
869 }
870
Craig Mautnere0a38842013-12-16 16:14:02 -0800871 final ActivityStack stack;
872 if (container == null || container.mStack.isOnHomeDisplay()) {
873 stack = getFocusedStack();
874 } else {
875 stack = container.mStack;
876 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700877 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700878 && mService.mConfiguration.diff(config) != 0;
879 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700880 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700881
882 final long origId = Binder.clearCallingIdentity();
883
884 if (aInfo != null &&
885 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
886 // This may be a heavy-weight process! Check to see if we already
887 // have another, different heavy-weight process running.
888 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
889 if (mService.mHeavyWeightProcess != null &&
890 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
891 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700892 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700893 if (caller != null) {
894 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
895 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700896 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700897 } else {
898 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700899 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700900 + intent.toString());
901 ActivityOptions.abort(options);
902 return ActivityManager.START_PERMISSION_DENIED;
903 }
904 }
905
906 IIntentSender target = mService.getIntentSenderLocked(
907 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700908 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700909 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
910 | PendingIntent.FLAG_ONE_SHOT, null);
911
912 Intent newIntent = new Intent();
913 if (requestCode >= 0) {
914 // Caller is requesting a result.
915 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
916 }
917 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
918 new IntentSender(target));
919 if (mService.mHeavyWeightProcess.activities.size() > 0) {
920 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
921 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
922 hist.packageName);
923 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
924 hist.task.taskId);
925 }
926 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
927 aInfo.packageName);
928 newIntent.setFlags(intent.getFlags());
929 newIntent.setClassName("android",
930 HeavyWeightSwitcherActivity.class.getName());
931 intent = newIntent;
932 resolvedType = null;
933 caller = null;
934 callingUid = Binder.getCallingUid();
935 callingPid = Binder.getCallingPid();
936 componentSpecified = true;
937 try {
938 ResolveInfo rInfo =
939 AppGlobals.getPackageManager().resolveIntent(
940 intent, null,
941 PackageManager.MATCH_DEFAULT_ONLY
942 | ActivityManagerService.STOCK_PM_FLAGS, userId);
943 aInfo = rInfo != null ? rInfo.activityInfo : null;
944 aInfo = mService.getActivityInfoForUser(aInfo, userId);
945 } catch (RemoteException e) {
946 aInfo = null;
947 }
948 }
949 }
950 }
951
Dianne Hackborn91097de2014-04-04 18:02:06 -0700952 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
953 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700954 requestCode, callingPid, callingUid, callingPackage,
955 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700956 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700957
Craig Mautner85c11a82014-07-17 12:38:18 -0700958 Binder.restoreCallingIdentity(origId);
959
Craig Mautnerde4ef022013-04-07 19:01:33 -0700960 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700961 // If the caller also wants to switch to a new configuration,
962 // do so now. This allows a clean switch, as we are waiting
963 // for the current activity to pause (so we will not destroy
964 // it), and have not yet started the next activity.
965 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
966 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700967 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700968 if (DEBUG_CONFIGURATION) Slog.v(TAG,
969 "Updating to new configuration after starting activity.");
970 mService.updateConfigurationLocked(config, null, false, false);
971 }
972
Craig Mautner23ac33b2013-04-01 16:26:35 -0700973 if (outResult != null) {
974 outResult.result = res;
975 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700976 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700977 do {
978 try {
979 mService.wait();
980 } catch (InterruptedException e) {
981 }
982 } while (!outResult.timeout && outResult.who == null);
983 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700984 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700985 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700986 outResult.timeout = false;
987 outResult.who = new ComponentName(r.info.packageName, r.info.name);
988 outResult.totalTime = 0;
989 outResult.thisTime = 0;
990 } else {
991 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700992 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700993 do {
994 try {
995 mService.wait();
996 } catch (InterruptedException e) {
997 }
998 } while (!outResult.timeout && outResult.who == null);
999 }
1000 }
1001 }
1002
1003 return res;
1004 }
1005 }
1006
1007 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1008 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1009 Bundle options, int userId) {
1010 if (intents == null) {
1011 throw new NullPointerException("intents is null");
1012 }
1013 if (resolvedTypes == null) {
1014 throw new NullPointerException("resolvedTypes is null");
1015 }
1016 if (intents.length != resolvedTypes.length) {
1017 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1018 }
1019
Craig Mautner23ac33b2013-04-01 16:26:35 -07001020
1021 int callingPid;
1022 if (callingUid >= 0) {
1023 callingPid = -1;
1024 } else if (caller == null) {
1025 callingPid = Binder.getCallingPid();
1026 callingUid = Binder.getCallingUid();
1027 } else {
1028 callingPid = callingUid = -1;
1029 }
1030 final long origId = Binder.clearCallingIdentity();
1031 try {
1032 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001033 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001034 for (int i=0; i<intents.length; i++) {
1035 Intent intent = intents[i];
1036 if (intent == null) {
1037 continue;
1038 }
1039
1040 // Refuse possible leaked file descriptors
1041 if (intent != null && intent.hasFileDescriptors()) {
1042 throw new IllegalArgumentException("File descriptors passed in Intent");
1043 }
1044
1045 boolean componentSpecified = intent.getComponent() != null;
1046
1047 // Don't modify the client's object!
1048 intent = new Intent(intent);
1049
1050 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001051 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001052 // TODO: New, check if this is correct
1053 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1054
1055 if (aInfo != null &&
1056 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
1057 != 0) {
1058 throw new IllegalArgumentException(
1059 "FLAG_CANT_SAVE_STATE not supported here");
1060 }
1061
1062 Bundle theseOptions;
1063 if (options != null && i == intents.length-1) {
1064 theseOptions = options;
1065 } else {
1066 theseOptions = null;
1067 }
Craig Mautner6170f732013-04-02 13:05:23 -07001068 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001069 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1070 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001071 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001072 if (res < 0) {
1073 return res;
1074 }
1075
1076 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1077 }
1078 }
1079 } finally {
1080 Binder.restoreCallingIdentity(origId);
1081 }
1082
1083 return ActivityManager.START_SUCCESS;
1084 }
1085
Craig Mautner2420ead2013-04-01 17:13:20 -07001086 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001087 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001088 throws RemoteException {
1089
1090 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001091 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001092 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001093
1094 // schedule launch ticks to collect information about slow apps.
1095 r.startLaunchTickingLocked();
1096
1097 // Have the window manager re-evaluate the orientation of
1098 // the screen based on the new activity order. Note that
1099 // as a result of this, it can call back into the activity
1100 // manager with a new orientation. We don't care about that,
1101 // because the activity is not currently running so we are
1102 // just restarting it anyway.
1103 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001104 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001105 mService.mConfiguration,
1106 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1107 mService.updateConfigurationLocked(config, r, false, false);
1108 }
1109
1110 r.app = app;
1111 app.waitingToKill = null;
1112 r.launchCount++;
1113 r.lastLaunchTime = SystemClock.uptimeMillis();
1114
1115 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1116
1117 int idx = app.activities.indexOf(r);
1118 if (idx < 0) {
1119 app.activities.add(r);
1120 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001121 mService.updateLruProcessLocked(app, true, null);
1122 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001123
1124 final ActivityStack stack = r.task.stack;
1125 try {
1126 if (app.thread == null) {
1127 throw new RemoteException();
1128 }
1129 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001130 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001131 if (andResume) {
1132 results = r.results;
1133 newIntents = r.newIntents;
1134 }
1135 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1136 + " icicle=" + r.icicle
1137 + " with results=" + results + " newIntents=" + newIntents
1138 + " andResume=" + andResume);
1139 if (andResume) {
1140 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1141 r.userId, System.identityHashCode(r),
1142 r.task.taskId, r.shortComponentName);
1143 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001144 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001145 // Home process is the root process of the task.
1146 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001147 }
1148 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1149 r.sleeping = false;
1150 r.forceNewConfig = false;
1151 mService.showAskCompatModeDialogLocked(r);
1152 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1153 String profileFile = null;
1154 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001155 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1156 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1157 mService.mProfileProc = app;
1158 profileFile = mService.mProfileFile;
1159 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001160 }
1161 }
1162 app.hasShownUi = true;
1163 app.pendingUiClean = true;
1164 if (profileFd != null) {
1165 try {
1166 profileFd = profileFd.dup();
1167 } catch (IOException e) {
1168 if (profileFd != null) {
1169 try {
1170 profileFd.close();
1171 } catch (IOException o) {
1172 }
1173 profileFd = null;
1174 }
1175 }
1176 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001177
Jeff Hao1b012d32014-08-20 10:35:34 -07001178 ProfilerInfo profilerInfo = profileFile != null
1179 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1180 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001181 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001182 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001183 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001184 r.compat, r.launchedFromPackage, r.task.voiceInteractor, app.repProcState,
1185 r.icicle, r.persistentState, results, newIntents, !andResume,
1186 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001187
1188 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1189 // This may be a heavy-weight process! Note that the package
1190 // manager will ensure that only activity can run in the main
1191 // process of the .apk, which is the only thing that will be
1192 // considered heavy-weight.
1193 if (app.processName.equals(app.info.packageName)) {
1194 if (mService.mHeavyWeightProcess != null
1195 && mService.mHeavyWeightProcess != app) {
1196 Slog.w(TAG, "Starting new heavy weight process " + app
1197 + " when already running "
1198 + mService.mHeavyWeightProcess);
1199 }
1200 mService.mHeavyWeightProcess = app;
1201 Message msg = mService.mHandler.obtainMessage(
1202 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1203 msg.obj = r;
1204 mService.mHandler.sendMessage(msg);
1205 }
1206 }
1207
1208 } catch (RemoteException e) {
1209 if (r.launchFailed) {
1210 // This is the second time we failed -- finish activity
1211 // and give up.
1212 Slog.e(TAG, "Second failure launching "
1213 + r.intent.getComponent().flattenToShortString()
1214 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001215 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001216 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1217 "2nd-crash", false);
1218 return false;
1219 }
1220
1221 // This is the first time we failed -- restart process and
1222 // retry.
1223 app.activities.remove(r);
1224 throw e;
1225 }
1226
1227 r.launchFailed = false;
1228 if (stack.updateLRUListLocked(r)) {
1229 Slog.w(TAG, "Activity " + r
1230 + " being launched, but already in LRU list");
1231 }
1232
1233 if (andResume) {
1234 // As part of the process of launching, ActivityThread also performs
1235 // a resume.
1236 stack.minimalResumeActivityLocked(r);
1237 } else {
1238 // This activity is not starting in the resumed state... which
1239 // should look like we asked it to pause+stop (but remain visible),
1240 // and it has done so and reported back the current icicle and
1241 // other state.
1242 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1243 + " (starting in stopped state)");
1244 r.state = ActivityState.STOPPED;
1245 r.stopped = true;
1246 }
1247
1248 // Launch the new version setup screen if needed. We do this -after-
1249 // launching the initial activity (that is, home), so that it can have
1250 // a chance to initialize itself while in the background, making the
1251 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001252 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 mService.startSetupActivityLocked();
1254 }
1255
Dianne Hackborn465fa392014-09-14 14:21:18 -07001256 // Update any services we are bound to that might care about whether
1257 // their client may have activities.
1258 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1259
Craig Mautner2420ead2013-04-01 17:13:20 -07001260 return true;
1261 }
1262
Craig Mautnere79d42682013-04-01 19:01:53 -07001263 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001264 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001265 // Is this activity's application already running?
1266 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001267 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001268
1269 r.task.stack.setLaunchTime(r);
1270
1271 if (app != null && app.thread != null) {
1272 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001273 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1274 || !"android".equals(r.info.packageName)) {
1275 // Don't add this if it is a platform component that is marked
1276 // to run in multiple processes, because this is actually
1277 // part of the framework so doesn't make sense to track as a
1278 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001279 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1280 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001281 }
George Mount2c92c972014-03-20 09:38:23 -07001282 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001283 return;
1284 } catch (RemoteException e) {
1285 Slog.w(TAG, "Exception when starting activity "
1286 + r.intent.getComponent().flattenToShortString(), e);
1287 }
1288
1289 // If a dead object exception was thrown -- fall through to
1290 // restart the application.
1291 }
1292
1293 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001294 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001295 }
1296
Craig Mautner6170f732013-04-02 13:05:23 -07001297 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001298 Intent intent, String resolvedType, ActivityInfo aInfo,
1299 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1300 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001301 int callingPid, int callingUid, String callingPackage,
1302 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001303 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1304 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001305 int err = ActivityManager.START_SUCCESS;
1306
1307 ProcessRecord callerApp = null;
1308 if (caller != null) {
1309 callerApp = mService.getRecordForAppLocked(caller);
1310 if (callerApp != null) {
1311 callingPid = callerApp.pid;
1312 callingUid = callerApp.info.uid;
1313 } else {
1314 Slog.w(TAG, "Unable to find app for caller " + caller
1315 + " (pid=" + callingPid + ") when starting: "
1316 + intent.toString());
1317 err = ActivityManager.START_PERMISSION_DENIED;
1318 }
1319 }
1320
1321 if (err == ActivityManager.START_SUCCESS) {
1322 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1323 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001324 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001325 + " on display " + (container == null ? (mFocusedStack == null ?
1326 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1327 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1328 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001329 }
1330
1331 ActivityRecord sourceRecord = null;
1332 ActivityRecord resultRecord = null;
1333 if (resultTo != null) {
1334 sourceRecord = isInAnyStackLocked(resultTo);
1335 if (DEBUG_RESULTS) Slog.v(
1336 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1337 if (sourceRecord != null) {
1338 if (requestCode >= 0 && !sourceRecord.finishing) {
1339 resultRecord = sourceRecord;
1340 }
1341 }
1342 }
Craig Mautner6170f732013-04-02 13:05:23 -07001343
Dianne Hackborn91097de2014-04-04 18:02:06 -07001344 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001345
Dianne Hackborn95465202014-09-15 16:21:55 -07001346 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001347 // Transfer the result target from the source activity to the new
1348 // one being started, including any failures.
1349 if (requestCode >= 0) {
1350 ActivityOptions.abort(options);
1351 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1352 }
1353 resultRecord = sourceRecord.resultTo;
1354 resultWho = sourceRecord.resultWho;
1355 requestCode = sourceRecord.requestCode;
1356 sourceRecord.resultTo = null;
1357 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001358 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001359 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001360 if (sourceRecord.launchedFromUid == callingUid) {
1361 // The new activity is being launched from the same uid as the previous
1362 // activity in the flow, and asking to forward its result back to the
1363 // previous. In this case the activity is serving as a trampoline between
1364 // the two, so we also want to update its launchedFromPackage to be the
1365 // same as the previous activity. Note that this is safe, since we know
1366 // these two packages come from the same uid; the caller could just as
1367 // well have supplied that same package name itself. This specifially
1368 // deals with the case of an intent picker/chooser being launched in the app
1369 // flow to redirect to an activity picked by the user, where we want the final
1370 // activity to consider it to have been launched by the previous app activity.
1371 callingPackage = sourceRecord.launchedFromPackage;
1372 }
Craig Mautner6170f732013-04-02 13:05:23 -07001373 }
1374
1375 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1376 // We couldn't find a class that can handle the given Intent.
1377 // That's the end of that!
1378 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1379 }
1380
1381 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1382 // We couldn't find the specific class specified in the Intent.
1383 // Also the end of the line.
1384 err = ActivityManager.START_CLASS_NOT_FOUND;
1385 }
1386
Dianne Hackborn91097de2014-04-04 18:02:06 -07001387 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1388 && sourceRecord.task.voiceSession != null) {
1389 // If this activity is being launched as part of a voice session, we need
1390 // to ensure that it is safe to do so. If the upcoming activity will also
1391 // be part of the voice session, we can only launch it if it has explicitly
1392 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001393 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001394 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1395 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001396 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1397 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001398 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1399 }
1400 } catch (RemoteException e) {
1401 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1402 }
1403 }
1404 }
1405
1406 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1407 // If the caller is starting a new voice session, just make sure the target
1408 // is actually allowing it to run this way.
1409 try {
1410 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1411 intent, resolvedType)) {
1412 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1413 }
1414 } catch (RemoteException e) {
1415 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1416 }
1417 }
1418
louis_changcd5d1982014-08-01 10:09:08 +08001419 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1420
Craig Mautner6170f732013-04-02 13:05:23 -07001421 if (err != ActivityManager.START_SUCCESS) {
1422 if (resultRecord != null) {
1423 resultStack.sendActivityResultLocked(-1,
1424 resultRecord, resultWho, requestCode,
1425 Activity.RESULT_CANCELED, null);
1426 }
Craig Mautner6170f732013-04-02 13:05:23 -07001427 ActivityOptions.abort(options);
1428 return err;
1429 }
1430
1431 final int startAnyPerm = mService.checkPermission(
1432 START_ANY_ACTIVITY, callingPid, callingUid);
1433 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1434 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1435 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1436 if (resultRecord != null) {
1437 resultStack.sendActivityResultLocked(-1,
1438 resultRecord, resultWho, requestCode,
1439 Activity.RESULT_CANCELED, null);
1440 }
Craig Mautner6170f732013-04-02 13:05:23 -07001441 String msg;
1442 if (!aInfo.exported) {
1443 msg = "Permission Denial: starting " + intent.toString()
1444 + " from " + callerApp + " (pid=" + callingPid
1445 + ", uid=" + callingUid + ")"
1446 + " not exported from uid " + aInfo.applicationInfo.uid;
1447 } else {
1448 msg = "Permission Denial: starting " + intent.toString()
1449 + " from " + callerApp + " (pid=" + callingPid
1450 + ", uid=" + callingUid + ")"
1451 + " requires " + aInfo.permission;
1452 }
1453 Slog.w(TAG, msg);
1454 throw new SecurityException(msg);
1455 }
1456
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001457 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001458 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001459
Craig Mautner6170f732013-04-02 13:05:23 -07001460 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001461 try {
1462 // The Intent we give to the watcher has the extra data
1463 // stripped off, since it can contain private information.
1464 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001465 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001466 aInfo.applicationInfo.packageName);
1467 } catch (RemoteException e) {
1468 mService.mController = null;
1469 }
Ben Gruver5e207332013-04-03 17:41:37 -07001470 }
Craig Mautner6170f732013-04-02 13:05:23 -07001471
Ben Gruver5e207332013-04-03 17:41:37 -07001472 if (abort) {
1473 if (resultRecord != null) {
1474 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001475 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001476 }
Ben Gruver5e207332013-04-03 17:41:37 -07001477 // We pretend to the caller that it was really started, but
1478 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001479 ActivityOptions.abort(options);
1480 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001481 }
1482
1483 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001484 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001485 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001486 if (outActivity != null) {
1487 outActivity[0] = r;
1488 }
1489
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001490 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001491 if (voiceSession == null && (stack.mResumedActivity == null
1492 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001493 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1494 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001495 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001496 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001497 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001498 ActivityOptions.abort(options);
1499 return ActivityManager.START_SWITCHES_CANCELED;
1500 }
1501 }
1502
1503 if (mService.mDidAppSwitch) {
1504 // This is the second allowed switch since we stopped switches,
1505 // so now just generally allow switches. Use case: user presses
1506 // home (switches disabled, switch to home, mDidAppSwitch now true);
1507 // user taps a home icon (coming from home so allowed, we hit here
1508 // and now allow anyone to switch again).
1509 mService.mAppSwitchesAllowedTime = 0;
1510 } else {
1511 mService.mDidAppSwitch = true;
1512 }
1513
Craig Mautneree36c772014-07-16 14:56:05 -07001514 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001515
Dianne Hackborn91097de2014-04-04 18:02:06 -07001516 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001517 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001518
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001519 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001520 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001521 // activity start, but we are not actually doing an activity
1522 // switch... just dismiss the keyguard now, because we
1523 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001524 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001525 }
1526 return err;
1527 }
1528
Craig Mautner1b4bf852014-05-26 15:06:32 -07001529 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001530 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001531
1532 // On leanback only devices we should keep all activities in the same stack.
1533 if (!mLeanbackOnlyDevice &&
1534 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001535 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001536 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001537 if (taskStack.isOnHomeDisplay()) {
1538 if (mFocusedStack != taskStack) {
1539 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1540 "focused stack to r=" + r + " task=" + task);
1541 mFocusedStack = taskStack;
1542 } else {
1543 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1544 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1545 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001546 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001547 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001548 }
1549
Craig Mautnere0a38842013-12-16 16:14:02 -08001550 final ActivityContainer container = r.mInitialActivityContainer;
1551 if (container != null) {
1552 // The first time put it on the desired stack, after this put on task stack.
1553 r.mInitialActivityContainer = null;
1554 return container.mStack;
1555 }
1556
Craig Mautner1b4bf852014-05-26 15:06:32 -07001557 if (mFocusedStack != mHomeStack && (!newTask ||
1558 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001559 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1560 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1561 return mFocusedStack;
1562 }
1563
Craig Mautnere0a38842013-12-16 16:14:02 -08001564 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1565 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1566 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1567 if (!stack.isHomeStack()) {
1568 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1569 "adjustStackFocus: Setting focused stack=" + stack);
1570 mFocusedStack = stack;
1571 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001572 }
1573 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001574
Craig Mautner4a1cb222013-12-04 16:14:06 -08001575 // Need to create an app stack for this user.
Todd Kennedy4900bf92015-01-16 16:05:14 -08001576 mFocusedStack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001577 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
Todd Kennedy4900bf92015-01-16 16:05:14 -08001578 " stackId=" + mFocusedStack.mStackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001579 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001580 }
1581 return mHomeStack;
1582 }
1583
Craig Mautner299f9602015-01-26 09:47:33 -08001584 void setFocusedStack(ActivityRecord r, String reason) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001585 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001586 final TaskRecord task = r.task;
1587 boolean isHomeActivity = !r.isApplicationActivity();
1588 if (!isHomeActivity && task != null) {
1589 isHomeActivity = !task.isApplicationTask();
1590 }
1591 if (!isHomeActivity && task != null) {
1592 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1593 isHomeActivity = parent != null && parent.isHomeActivity();
1594 }
Craig Mautner299f9602015-01-26 09:47:33 -08001595 moveHomeStack(isHomeActivity, reason);
Craig Mautner29219d92013-04-16 20:19:12 -07001596 }
1597 }
1598
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001599 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001600 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001601 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001602 final Intent intent = r.intent;
1603 final int callingUid = r.launchedFromUid;
1604
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001605 // In some flows in to this function, we retrieve the task record and hold on to it
1606 // without a lock before calling back in to here... so the task at this point may
1607 // not actually be in recents. Check for that, and if it isn't in recents just
1608 // consider it invalid.
1609 if (inTask != null && !inTask.inRecents) {
1610 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1611 inTask = null;
1612 }
1613
Craig Mautnerad400af2014-08-13 16:41:36 -07001614 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001615 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1616 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001617
Craig Mautnera228ae92014-07-09 05:44:55 -07001618 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001619 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001620 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001621 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1622 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1623 "\"singleInstance\" or \"singleTask\"");
1624 launchFlags &=
1625 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1626 } else {
1627 switch (r.info.documentLaunchMode) {
1628 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1629 break;
1630 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1631 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1632 break;
1633 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1634 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1635 break;
1636 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1637 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1638 break;
1639 }
1640 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001641
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001642 final boolean launchTaskBehind = r.mLaunchTaskBehind
1643 && !launchSingleTask && !launchSingleInstance
1644 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001645
1646 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1647 // For whatever reason this activity is being launched into a new
1648 // task... yet the caller has requested a result back. Well, that
1649 // is pretty messed up, so instead immediately send back a cancel
1650 // and let the new task continue launched as normal without a
1651 // dependency on its originator.
1652 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1653 r.resultTo.task.stack.sendActivityResultLocked(-1,
1654 r.resultTo, r.resultWho, r.requestCode,
1655 Activity.RESULT_CANCELED, null);
1656 r.resultTo = null;
1657 }
1658
1659 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1660 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1661 }
1662
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001663 // If we are actually going to launch in to a new task, there are some cases where
1664 // we further want to do multiple task.
1665 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1666 if (launchTaskBehind
1667 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1668 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1669 }
1670 }
1671
Craig Mautner8849a5e2013-04-02 16:41:03 -07001672 // We'll invoke onUserLeaving before onPause only if the launching
1673 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001674 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001675 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001676
1677 // If the caller has asked not to resume at this point, we make note
1678 // of this in the record so that we can skip it when trying to find
1679 // the top running activity.
1680 if (!doResume) {
1681 r.delayedResume = true;
1682 }
1683
Craig Mautnera254cd72014-05-25 16:47:39 -07001684 ActivityRecord notTop =
1685 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001686
1687 // If the onlyIfNeeded flag is set, then we can do this if the activity
1688 // being launched is the same as the one making the call... or, as
1689 // a special case, if we do not know the caller then we count the
1690 // current top activity as the caller.
1691 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1692 ActivityRecord checkedCaller = sourceRecord;
1693 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001694 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001695 }
1696 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1697 // Caller is not the same as launcher, so always needed.
1698 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1699 }
1700 }
1701
Craig Mautner8849a5e2013-04-02 16:41:03 -07001702 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001703 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001704
1705 // If the caller is not coming from another activity, but has given us an
1706 // explicit task into which they would like us to launch the new activity,
1707 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001708 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1709 final Intent baseIntent = inTask.getBaseIntent();
1710 final ActivityRecord root = inTask.getRootActivity();
1711 if (baseIntent == null) {
1712 ActivityOptions.abort(options);
1713 throw new IllegalArgumentException("Launching into task without base intent: "
1714 + inTask);
1715 }
1716
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001717 // If this task is empty, then we are adding the first activity -- it
1718 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001719 if (launchSingleInstance || launchSingleTask) {
1720 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1721 ActivityOptions.abort(options);
1722 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1723 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001724 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001725 if (root != null) {
1726 ActivityOptions.abort(options);
1727 throw new IllegalArgumentException("Caller with inTask " + inTask
1728 + " has root " + root + " but target is singleInstance/Task");
1729 }
1730 }
1731
1732 // If task is empty, then adopt the interesting intent launch flags in to the
1733 // activity being started.
1734 if (root == null) {
1735 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1736 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1737 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1738 launchFlags = (launchFlags&~flagsOfInterest)
1739 | (baseIntent.getFlags()&flagsOfInterest);
1740 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001741 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001742 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001743
Dianne Hackborn962d5352014-08-29 16:27:40 -07001744 // If the task is not empty and the caller is asking to start it as the root
1745 // of a new task, then we don't actually want to start this on the task. We
1746 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001747 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001748 addingToTask = false;
1749
1750 } else {
1751 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001752 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001753
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001754 reuseTask = inTask;
1755 } else {
1756 inTask = null;
1757 }
1758
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001759 if (inTask == null) {
1760 if (sourceRecord == null) {
1761 // This activity is not being started from another... in this
1762 // case we -always- start a new task.
1763 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1764 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1765 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1766 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1767 }
1768 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1769 // The original activity who is starting us is running as a single
1770 // instance... this new activity it is starting must go on its
1771 // own task.
1772 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1773 } else if (launchSingleInstance || launchSingleTask) {
1774 // The activity being started is a single instance... it always
1775 // gets launched into its own task.
1776 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1777 }
1778 }
1779
1780 ActivityInfo newTaskInfo = null;
1781 Intent newTaskIntent = null;
1782 ActivityStack sourceStack;
1783 if (sourceRecord != null) {
1784 if (sourceRecord.finishing) {
1785 // If the source is finishing, we can't further count it as our source. This
1786 // is because the task it is associated with may now be empty and on its way out,
1787 // so we don't want to blindly throw it in to that task. Instead we will take
1788 // the NEW_TASK flow and try to find a task for it. But save the task information
1789 // so it can be used when creating the new task.
1790 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1791 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1792 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1793 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1794 newTaskInfo = sourceRecord.info;
1795 newTaskIntent = sourceRecord.task.intent;
1796 }
1797 sourceRecord = null;
1798 sourceStack = null;
1799 } else {
1800 sourceStack = sourceRecord.task.stack;
1801 }
1802 } else {
1803 sourceStack = null;
1804 }
1805
1806 boolean movedHome = false;
1807 ActivityStack targetStack;
1808
1809 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001810 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001811
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001812 // We may want to try to place the new activity in to an existing task. We always
1813 // do this if the target activity is singleTask or singleInstance; we will also do
1814 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1815 // us to still place it in a new task: multi task, always doc mode, or being asked to
1816 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001817 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1818 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001819 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001820 // If bring to front is requested, and no result is requested and we have not
1821 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001822 // we can find a task that was started with this same
1823 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001824 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001825 // See if there is a task to bring to the front. If this is
1826 // a SINGLE_INSTANCE activity, there can be one and only one
1827 // instance of it in the history, and it is always in its own
1828 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001829 ActivityRecord intentActivity = !launchSingleInstance ?
1830 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001831 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001832 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001833 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001834 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001835 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1836 }
Craig Mautner29219d92013-04-16 20:19:12 -07001837 if (r.task == null) {
1838 r.task = intentActivity.task;
1839 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001840 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001841 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001842 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1843 + " from " + intentActivity);
Craig Mautner299f9602015-01-26 09:47:33 -08001844 targetStack.moveToFront("intentActivityFound");
Craig Mautner8849a5e2013-04-02 16:41:03 -07001845 if (intentActivity.task.intent == null) {
1846 // This task was started because of movement of
1847 // the activity based on affinity... now that we
1848 // are actually launching it, we can assign the
1849 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001850 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001851 }
1852 // If the target task is not in the front, then we need
1853 // to bring it to the front... except... well, with
1854 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1855 // to have the same behavior as if a new instance was
1856 // being started, which means not bringing it to the front
1857 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001858 final ActivityStack lastStack = getLastStack();
1859 ActivityRecord curTop = lastStack == null?
1860 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001861 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001862 if (curTop != null && (curTop.task != intentActivity.task ||
1863 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001864 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001865 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1866 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001867 // We really do want to push this one into the
1868 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001869 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001870 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1871 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001872 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001873 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1874 options, "bringingFoundTaskToFront");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001875 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001876 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1877 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001878 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001879 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001880 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001881 options = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001882 movedToFront = true;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001883 }
1884 }
1885 // If the caller has requested that the target task be
1886 // reset, then do so.
1887 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1888 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1889 }
1890 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1891 // We don't need to start a new activity, and
1892 // the client said not to do anything if that
1893 // is the case, so this is it! And for paranoia, make
1894 // sure we have correctly resumed the top activity.
1895 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001896 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001897
1898 // Make sure to notify Keyguard as well if we are not running an app
1899 // transition later.
1900 if (!movedToFront) {
1901 notifyActivityDrawnForKeyguard();
1902 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001903 } else {
1904 ActivityOptions.abort(options);
1905 }
1906 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1907 }
1908 if ((launchFlags &
1909 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1910 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1911 // The caller has requested to completely replace any
1912 // existing task with its new activity. Well that should
1913 // not be too hard...
1914 reuseTask = intentActivity.task;
1915 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001916 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001917 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001918 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001919 // In this situation we want to remove all activities
1920 // from the task up to the one being started. In most
1921 // cases this means we are resetting the task to its
1922 // initial state.
1923 ActivityRecord top =
1924 intentActivity.task.performClearTaskLocked(r, launchFlags);
1925 if (top != null) {
1926 if (top.frontOfTask) {
1927 // Activity aliases may mean we use different
1928 // intents for the top activity, so make sure
1929 // the task now has the identity of the new
1930 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001931 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001932 }
1933 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1934 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001935 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001936 } else {
1937 // A special case: we need to
1938 // start the activity because it is not currently
1939 // running, and the caller has asked to clear the
1940 // current task to have this activity at the top.
1941 addingToTask = true;
1942 // Now pretend like this activity is being started
1943 // by the top of its task, so it is put in the
1944 // right place.
1945 sourceRecord = intentActivity;
1946 }
1947 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1948 // In this case the top activity on the task is the
1949 // same as the one being launched, so we take that
1950 // as a request to bring the task to the foreground.
1951 // If the top activity in the task is the root
1952 // activity, deliver this new intent to it if it
1953 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001954 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001955 && intentActivity.realActivity.equals(r.realActivity)) {
1956 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1957 intentActivity.task);
1958 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001959 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001960 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001961 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
1962 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001963 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1964 // In this case we are launching the root activity
1965 // of the task, but with a different intent. We
1966 // should start a new instance on top.
1967 addingToTask = true;
1968 sourceRecord = intentActivity;
1969 }
1970 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1971 // In this case an activity is being launched in to an
1972 // existing task, without resetting that task. This
1973 // is typically the situation of launching an activity
1974 // from a notification or shortcut. We want to place
1975 // the new activity on top of the current task.
1976 addingToTask = true;
1977 sourceRecord = intentActivity;
1978 } else if (!intentActivity.task.rootWasReset) {
1979 // In this case we are launching in to an existing task
1980 // that has not yet been started from its front door.
1981 // The current task has been brought to the front.
1982 // Ideally, we'd probably like to place this new task
1983 // at the bottom of its stack, but that's a little hard
1984 // to do with the current organization of the code so
1985 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07001986 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001987 }
1988 if (!addingToTask && reuseTask == null) {
1989 // We didn't do anything... but it was needed (a.k.a., client
1990 // don't use that intent!) And for paranoia, make
1991 // sure we have correctly resumed the top activity.
1992 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001993 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001994 if (!movedToFront) {
1995 // Make sure to notify Keyguard as well if we are not running an app
1996 // transition later.
1997 notifyActivityDrawnForKeyguard();
1998 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001999 } else {
2000 ActivityOptions.abort(options);
2001 }
2002 return ActivityManager.START_TASK_TO_FRONT;
2003 }
2004 }
2005 }
2006 }
2007
2008 //String uri = r.intent.toURI();
2009 //Intent intent2 = new Intent(uri);
2010 //Slog.i(TAG, "Given intent: " + r.intent);
2011 //Slog.i(TAG, "URI is: " + uri);
2012 //Slog.i(TAG, "To intent: " + intent2);
2013
2014 if (r.packageName != null) {
2015 // If the activity being launched is the same as the one currently
2016 // at the top, then we need to check if it should only be launched
2017 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002018 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002019 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002020 if (top != null && r.resultTo == null) {
2021 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2022 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002023 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002024 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002025 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2026 top.task);
2027 // For paranoia, make sure we have correctly
2028 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002029 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002030 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002031 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002032 }
2033 ActivityOptions.abort(options);
2034 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2035 // We don't need to start a new activity, and
2036 // the client said not to do anything if that
2037 // is the case, so this is it!
2038 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2039 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002040 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002041 return ActivityManager.START_DELIVERED_TO_TOP;
2042 }
2043 }
2044 }
2045 }
2046
2047 } else {
2048 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002049 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2050 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002051 }
2052 ActivityOptions.abort(options);
2053 return ActivityManager.START_CLASS_NOT_FOUND;
2054 }
2055
2056 boolean newTask = false;
2057 boolean keepCurTransition = false;
2058
Craig Mautnerbb742462014-07-07 15:28:55 -07002059 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002060 sourceRecord.task : null;
2061
Craig Mautner8849a5e2013-04-02 16:41:03 -07002062 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002063 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002064 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002065 if (isLockTaskModeViolation(reuseTask)) {
2066 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2067 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2068 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002069 newTask = true;
2070 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerbb742462014-07-07 15:28:55 -07002071 if (!launchTaskBehind) {
Craig Mautner299f9602015-01-26 09:47:33 -08002072 targetStack.moveToFront("startingNewTask");
Craig Mautnerbb742462014-07-07 15:28:55 -07002073 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002074 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002075 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2076 newTaskInfo != null ? newTaskInfo : r.info,
2077 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002078 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2079 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002080 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2081 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002082 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002083 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002084 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002085 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002086 if ((launchFlags &
2087 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2088 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2089 // Caller wants to appear on home activity, so before starting
2090 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002091 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002092 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002093 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002094 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002095 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002096 if (isLockTaskModeViolation(sourceTask)) {
2097 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2098 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2099 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002100 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002101 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002102 final TaskRecord topTask = targetStack.topTask();
2103 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002104 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2105 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002106 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002107 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002108 // In this case, we are adding the activity to an existing
2109 // task, but the caller has asked to clear that task if the
2110 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002111 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002112 keepCurTransition = true;
2113 if (top != null) {
2114 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002115 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002116 // For paranoia, make sure we have correctly
2117 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002118 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002119 if (doResume) {
2120 targetStack.resumeTopActivityLocked(null);
2121 }
2122 ActivityOptions.abort(options);
2123 return ActivityManager.START_DELIVERED_TO_TOP;
2124 }
2125 } else if (!addingToTask &&
2126 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2127 // In this case, we are launching an activity in our own task
2128 // that may already be running somewhere in the history, and
2129 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002130 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002131 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002132 final TaskRecord task = top.task;
2133 task.moveActivityToFrontLocked(top);
2134 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002135 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002136 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002137 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002138 if (doResume) {
2139 targetStack.resumeTopActivityLocked(null);
2140 }
2141 return ActivityManager.START_DELIVERED_TO_TOP;
2142 }
2143 }
2144 // An existing activity is starting this new activity, so we want
2145 // to keep the new one in the same task as the one that is starting
2146 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002147 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002148 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002149 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002150
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002151 } else if (inTask != null) {
2152 // The calling is asking that the new activity be started in an explicit
2153 // task it has provided to us.
2154 if (isLockTaskModeViolation(inTask)) {
2155 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2156 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2157 }
2158 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002159 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002160
2161 // Check whether we should actually launch the new activity in to the task,
2162 // or just reuse the current activity on top.
2163 ActivityRecord top = inTask.getTopActivity();
2164 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2165 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2166 || launchSingleTop || launchSingleTask) {
2167 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2168 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2169 // We don't need to start a new activity, and
2170 // the client said not to do anything if that
2171 // is the case, so this is it!
2172 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2173 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002174 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002175 return ActivityManager.START_DELIVERED_TO_TOP;
2176 }
2177 }
2178
Dianne Hackborn962d5352014-08-29 16:27:40 -07002179 if (!addingToTask) {
2180 // We don't actually want to have this activity added to the task, so just
2181 // stop here but still tell the caller that we consumed the intent.
2182 ActivityOptions.abort(options);
2183 return ActivityManager.START_TASK_TO_FRONT;
2184 }
2185
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002186 r.setTask(inTask, null);
2187 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2188 + " in explicit task " + r.task);
2189
Craig Mautner8849a5e2013-04-02 16:41:03 -07002190 } else {
2191 // This not being started from an existing activity, and not part
2192 // of a new task... just put it in the top task, though these days
2193 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002194 targetStack = adjustStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002195 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002196 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002197 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002198 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002199 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002200 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2201 + " in new guessed " + r.task);
2202 }
2203
2204 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002205 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002206
Craig Mautner76e2a762014-06-24 09:05:43 -07002207 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2208 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2209 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002210 if (newTask) {
2211 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2212 }
2213 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002214 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002215 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002216 if (!launchTaskBehind) {
2217 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002218 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002219 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002220 return ActivityManager.START_SUCCESS;
2221 }
2222
Craig Mautneree36c772014-07-16 14:56:05 -07002223 final void doPendingActivityLaunchesLocked(boolean doResume) {
2224 while (!mPendingActivityLaunches.isEmpty()) {
2225 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002226 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002227 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002228 }
2229 }
2230
Craig Mautner7f13ed32014-07-28 14:00:35 -07002231 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002232 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2233 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002234 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002235 mPendingActivityLaunches.remove(palNdx);
2236 }
2237 }
2238 }
2239
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002240 void acquireLaunchWakelock() {
2241 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2242 throw new IllegalStateException("Calling must be system uid");
2243 }
2244 mLaunchingActivity.acquire();
2245 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2246 // To be safe, don't allow the wake lock to be held for too long.
2247 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2248 }
2249 }
2250
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002251 /**
2252 * Called when the frontmost task is idle.
2253 * @return the state of mService.mBooting before this was called.
2254 */
2255 private boolean checkFinishBootingLocked() {
2256 final boolean booting = mService.mBooting;
2257 boolean enableScreen = false;
2258 mService.mBooting = false;
2259 if (!mService.mBooted) {
2260 mService.mBooted = true;
2261 enableScreen = true;
2262 }
2263 if (booting || enableScreen) {
2264 mService.postFinishBooting(booting, enableScreen);
2265 }
2266 return booting;
2267 }
2268
Craig Mautnerf3333272013-04-22 10:55:53 -07002269 // Checked.
2270 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2271 Configuration config) {
2272 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2273
Craig Mautnerf3333272013-04-22 10:55:53 -07002274 ArrayList<ActivityRecord> stops = null;
2275 ArrayList<ActivityRecord> finishes = null;
2276 ArrayList<UserStartedState> startingUsers = null;
2277 int NS = 0;
2278 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002279 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002280 boolean activityRemoved = false;
2281
2282 ActivityRecord r = ActivityRecord.forToken(token);
2283 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002284 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2285 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002286 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2287 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002288 if (fromTimeout) {
2289 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002290 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002291
2292 // This is a hack to semi-deal with a race condition
2293 // in the client where it can be constructed with a
2294 // newer configuration from when we asked it to launch.
2295 // We'll update with whatever configuration it now says
2296 // it used to launch.
2297 if (config != null) {
2298 r.configuration = config;
2299 }
2300
2301 // We are now idle. If someone is waiting for a thumbnail from
2302 // us, we can now deliver.
2303 r.idle = true;
2304
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002305 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002306 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002307 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002308 }
2309 }
2310
2311 if (allResumedActivitiesIdle()) {
2312 if (r != null) {
2313 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002314 }
2315
2316 if (mLaunchingActivity.isHeld()) {
2317 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2318 if (VALIDATE_WAKE_LOCK_CALLER &&
2319 Binder.getCallingUid() != Process.myUid()) {
2320 throw new IllegalStateException("Calling must be system uid");
2321 }
2322 mLaunchingActivity.release();
2323 }
2324 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002325 }
2326
2327 // Atomically retrieve all of the other things to do.
2328 stops = processStoppingActivitiesLocked(true);
2329 NS = stops != null ? stops.size() : 0;
2330 if ((NF=mFinishingActivities.size()) > 0) {
2331 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2332 mFinishingActivities.clear();
2333 }
2334
Craig Mautnerf3333272013-04-22 10:55:53 -07002335 if (mStartingUsers.size() > 0) {
2336 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2337 mStartingUsers.clear();
2338 }
2339
Craig Mautnerf3333272013-04-22 10:55:53 -07002340 // Stop any activities that are scheduled to do so but have been
2341 // waiting for the next one to start.
2342 for (int i = 0; i < NS; i++) {
2343 r = stops.get(i);
2344 final ActivityStack stack = r.task.stack;
2345 if (r.finishing) {
2346 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2347 } else {
2348 stack.stopActivityLocked(r);
2349 }
2350 }
2351
2352 // Finish any activities that are scheduled to do so but have been
2353 // waiting for the next one to start.
2354 for (int i = 0; i < NF; i++) {
2355 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002356 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002357 }
2358
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002359 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002360 // Complete user switch
2361 if (startingUsers != null) {
2362 for (int i = 0; i < startingUsers.size(); i++) {
2363 mService.finishUserSwitch(startingUsers.get(i));
2364 }
2365 }
2366 // Complete starting up of background users
2367 if (mStartingBackgroundUsers.size() > 0) {
2368 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2369 mStartingBackgroundUsers.clear();
2370 for (int i = 0; i < startingUsers.size(); i++) {
2371 mService.finishUserBoot(startingUsers.get(i));
2372 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002373 }
2374 }
2375
2376 mService.trimApplications();
2377 //dump();
2378 //mWindowManager.dump();
2379
Craig Mautnerf3333272013-04-22 10:55:53 -07002380 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002381 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002382 }
2383
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002384 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002385 }
2386
Craig Mautner8e569572013-10-11 17:36:59 -07002387 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002388 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002389 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2390 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002391 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2392 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2393 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002394 }
Craig Mautner19091252013-10-05 00:03:53 -07002395 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002396 }
2397
2398 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002399 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2400 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002401 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2402 stacks.get(stackNdx).closeSystemDialogsLocked();
2403 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002404 }
2405 }
2406
Craig Mautner93529a42013-10-04 15:03:13 -07002407 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002408 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002409 }
2410
Craig Mautner8d341ef2013-03-26 09:03:27 -07002411 /**
2412 * @return true if some activity was finished (or would have finished if doit were true).
2413 */
2414 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2415 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002416 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2417 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002418 final int numStacks = stacks.size();
2419 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2420 final ActivityStack stack = stacks.get(stackNdx);
2421 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2422 didSomething = true;
2423 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002424 }
2425 }
2426 return didSomething;
2427 }
2428
Dianne Hackborna413dc02013-07-12 12:02:55 -07002429 void updatePreviousProcessLocked(ActivityRecord r) {
2430 // Now that this process has stopped, we may want to consider
2431 // it to be the previous app to try to keep around in case
2432 // the user wants to return to it.
2433
2434 // First, found out what is currently the foreground app, so that
2435 // we don't blow away the previous app if this activity is being
2436 // hosted by the process that is actually still the foreground.
2437 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002438 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2439 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002440 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2441 final ActivityStack stack = stacks.get(stackNdx);
2442 if (isFrontStack(stack)) {
2443 if (stack.mResumedActivity != null) {
2444 fgApp = stack.mResumedActivity.app;
2445 } else if (stack.mPausingActivity != null) {
2446 fgApp = stack.mPausingActivity.app;
2447 }
2448 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002449 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002450 }
2451 }
2452
2453 // Now set this one as the previous process, only if that really
2454 // makes sense to.
2455 if (r.app != null && fgApp != null && r.app != fgApp
2456 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002457 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002458 mService.mPreviousProcess = r.app;
2459 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2460 }
2461 }
2462
Craig Mautner05d29032013-05-03 13:40:13 -07002463 boolean resumeTopActivitiesLocked() {
2464 return resumeTopActivitiesLocked(null, null, null);
2465 }
2466
2467 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2468 Bundle targetOptions) {
2469 if (targetStack == null) {
2470 targetStack = getFocusedStack();
2471 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002472 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002473 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002474 if (isFrontStack(targetStack)) {
2475 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2476 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002477 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2478 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002479 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2480 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002481 if (stack == targetStack) {
2482 // Already started above.
2483 continue;
2484 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002485 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002486 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002487 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002488 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002489 }
Craig Mautner05d29032013-05-03 13:40:13 -07002490 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002491 }
2492
Todd Kennedy539db512014-12-15 09:57:55 -08002493 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002494 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2495 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002496 final int numStacks = stacks.size();
2497 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2498 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002499 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002500 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002501 }
2502 }
2503
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002504 void finishVoiceTask(IVoiceInteractionSession session) {
2505 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2506 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2507 final int numStacks = stacks.size();
2508 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2509 final ActivityStack stack = stacks.get(stackNdx);
2510 stack.finishVoiceTask(session);
2511 }
2512 }
2513 }
2514
Craig Mautner299f9602015-01-26 09:47:33 -08002515 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002516 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2517 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002518 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002519 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2520 // Caller wants the home activity moved with it. To accomplish this,
2521 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002522 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002523 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002524 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08002525 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2526 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002527 }
2528
Craig Mautner967212c2013-04-13 21:10:58 -07002529 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002530 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2531 if (activityContainer != null) {
2532 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002533 }
2534 return null;
2535 }
2536
Craig Mautner967212c2013-04-13 21:10:58 -07002537 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002538 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002539 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2540 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002541 }
2542 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002543 }
2544
Craig Mautner4a1cb222013-12-04 16:14:06 -08002545 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002546 ActivityRecord homeActivity = getHomeActivity();
2547 if (homeActivity != null) {
2548 return homeActivity.appToken;
2549 }
2550 return null;
2551 }
2552
2553 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002554 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2555 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2556 final TaskRecord task = tasks.get(taskNdx);
2557 if (task.isHomeTask()) {
2558 final ArrayList<ActivityRecord> activities = task.mActivities;
2559 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2560 final ActivityRecord r = activities.get(activityNdx);
2561 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002562 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002563 }
2564 }
2565 }
2566 }
2567 return null;
2568 }
2569
Todd Kennedyca4d8422015-01-15 15:19:22 -08002570 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002571 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002572 ActivityContainer activityContainer =
2573 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002574 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002575 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002576 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002577 return activityContainer;
2578 }
2579
Craig Mautner34b73df2014-01-12 21:11:08 -08002580 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002581 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2582 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2583 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002584 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2585 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002586 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002587 }
2588 }
2589
Craig Mautner95da1082014-02-24 17:54:35 -08002590 void deleteActivityContainer(IActivityContainer container) {
2591 ActivityContainer activityContainer = (ActivityContainer)container;
2592 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002593 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2594 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002595 final int stackId = activityContainer.mStackId;
2596 mActivityContainers.remove(stackId);
2597 mWindowManager.removeStack(stackId);
2598 }
2599 }
2600
Todd Kennedy4900bf92015-01-16 16:05:14 -08002601 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002602 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2603 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002604 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002605 }
2606
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002607 ActivityContainer activityContainer = new ActivityContainer(stackId);
2608 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002609 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002610 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002611 }
2612
2613 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002614 while (true) {
2615 if (++mLastStackId <= HOME_STACK_ID) {
2616 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002617 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002618 if (getStack(mLastStackId) == null) {
2619 break;
2620 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002621 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002622 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002623 }
2624
Wale Ogunwale7de05352014-12-12 15:21:33 -08002625 private boolean restoreRecentTaskLocked(TaskRecord task) {
2626 ActivityStack stack = null;
2627 // Determine stack to restore task to.
2628 if (mLeanbackOnlyDevice) {
2629 // There is only one stack for lean back devices.
2630 stack = mHomeStack;
2631 } else {
2632 // Look for the top stack on the home display that isn't the home stack.
2633 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2634 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2635 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
2636 if (!tmpStack.isHomeStack()) {
2637 stack = tmpStack;
2638 break;
2639 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002640 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002641 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002642
2643 if (stack == null) {
2644 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2645 // before an application stack is created...Go ahead and create one on the default
2646 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002647 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002648 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002649 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002650 if (DEBUG_RECENTS)
2651 Slog.v(TAG, "Created stack=" + stack + " for recents restoration.");
2652 }
2653
2654 if (stack == null) {
2655 // What does this mean??? Not sure how we would get here...
2656 if (DEBUG_RECENTS)
2657 Slog.v(TAG, "Unable to find/create stack to restore recent task=" + task);
2658 return false;
2659 }
2660
2661 stack.addTask(task, false, false);
2662 if (DEBUG_RECENTS)
2663 Slog.v(TAG, "Added restored task=" + task + " to stack=" + stack);
2664 final ArrayList<ActivityRecord> activities = task.mActivities;
2665 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2666 final ActivityRecord r = activities.get(activityNdx);
2667 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2668 r.info.screenOrientation, r.fullscreen,
2669 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2670 r.userId, r.info.configChanges, task.voiceSession != null,
2671 r.mLaunchTaskBehind);
2672 }
2673 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002674 }
2675
Craig Mautner299f9602015-01-26 09:47:33 -08002676 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002677 final TaskRecord task = anyTaskForIdLocked(taskId);
2678 if (task == null) {
2679 return;
2680 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002681 final ActivityStack stack = getStack(stackId);
2682 if (stack == null) {
2683 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2684 return;
2685 }
Craig Mautner299f9602015-01-26 09:47:33 -08002686 task.stack.removeTask(task, "moveTaskToStack");
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002687 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002688 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002689 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002690 }
2691
Craig Mautnerac6f8432013-07-17 13:24:59 -07002692 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002693 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002694 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2695 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002696 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2697 final ActivityStack stack = stacks.get(stackNdx);
2698 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002699 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2700 continue;
2701 }
2702 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2703 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2704 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002705 continue;
2706 }
2707 final ActivityRecord ar = stack.findTaskLocked(r);
2708 if (ar != null) {
2709 return ar;
2710 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002711 }
2712 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002713 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002714 return null;
2715 }
2716
2717 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002718 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2719 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002720 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2721 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2722 if (ar != null) {
2723 return ar;
2724 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002725 }
2726 }
2727 return null;
2728 }
2729
Craig Mautner8d341ef2013-03-26 09:03:27 -07002730 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002731 scheduleSleepTimeout();
2732 if (!mGoingToSleep.isHeld()) {
2733 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002734 if (mLaunchingActivity.isHeld()) {
2735 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2736 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002737 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002738 mLaunchingActivity.release();
2739 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002740 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002741 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002742 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002743 }
2744
2745 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002746 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002747
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002748 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002749 final long endTime = System.currentTimeMillis() + timeout;
2750 while (true) {
2751 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002752 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2753 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002754 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2755 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2756 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002757 }
2758 if (cantShutdown) {
2759 long timeRemaining = endTime - System.currentTimeMillis();
2760 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002761 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002762 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002763 } catch (InterruptedException e) {
2764 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002765 } else {
2766 Slog.w(TAG, "Activity manager shutdown timed out");
2767 timedout = true;
2768 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002769 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002770 } else {
2771 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002772 }
2773 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002774
2775 // Force checkReadyForSleep to complete.
2776 mSleepTimeout = true;
2777 checkReadyForSleepLocked();
2778
Craig Mautner8d341ef2013-03-26 09:03:27 -07002779 return timedout;
2780 }
2781
2782 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002783 removeSleepTimeouts();
2784 if (mGoingToSleep.isHeld()) {
2785 mGoingToSleep.release();
2786 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002787 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2788 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002789 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2790 final ActivityStack stack = stacks.get(stackNdx);
2791 stack.awakeFromSleepingLocked();
2792 if (isFrontStack(stack)) {
2793 resumeTopActivitiesLocked();
2794 }
Craig Mautner5314a402013-09-26 12:40:16 -07002795 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002796 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002797 mGoingToSleepActivities.clear();
2798 }
2799
2800 void activitySleptLocked(ActivityRecord r) {
2801 mGoingToSleepActivities.remove(r);
2802 checkReadyForSleepLocked();
2803 }
2804
2805 void checkReadyForSleepLocked() {
2806 if (!mService.isSleepingOrShuttingDown()) {
2807 // Do not care.
2808 return;
2809 }
2810
2811 if (!mSleepTimeout) {
2812 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002813 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2814 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002815 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2816 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2817 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002818 }
2819
2820 if (mStoppingActivities.size() > 0) {
2821 // Still need to tell some activities to stop; can't sleep yet.
2822 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2823 + mStoppingActivities.size() + " activities");
2824 scheduleIdleLocked();
2825 dontSleep = true;
2826 }
2827
2828 if (mGoingToSleepActivities.size() > 0) {
2829 // Still need to tell some activities to sleep; can't sleep yet.
2830 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2831 + mGoingToSleepActivities.size() + " activities");
2832 dontSleep = true;
2833 }
2834
2835 if (dontSleep) {
2836 return;
2837 }
2838 }
2839
Craig Mautnere0a38842013-12-16 16:14:02 -08002840 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2841 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002842 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2843 stacks.get(stackNdx).goToSleep();
2844 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002845 }
2846
2847 removeSleepTimeouts();
2848
2849 if (mGoingToSleep.isHeld()) {
2850 mGoingToSleep.release();
2851 }
2852 if (mService.mShuttingDown) {
2853 mService.notifyAll();
2854 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002855 }
2856
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002857 boolean reportResumedActivityLocked(ActivityRecord r) {
2858 final ActivityStack stack = r.task.stack;
2859 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002860 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002861 }
2862 if (allResumedActivitiesComplete()) {
2863 ensureActivitiesVisibleLocked(null, 0);
2864 mWindowManager.executeAppTransition();
2865 return true;
2866 }
2867 return false;
2868 }
2869
Craig Mautner8d341ef2013-03-26 09:03:27 -07002870 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002871 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2872 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002873 final int numStacks = stacks.size();
2874 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2875 final ActivityStack stack = stacks.get(stackNdx);
2876 stack.handleAppCrashLocked(app);
2877 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002878 }
2879 }
2880
Jose Lima4b6c6692014-08-12 17:41:12 -07002881 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002882 final ActivityStack stack = r.task.stack;
2883 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002884 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2885 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002886 return false;
2887 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002888 final boolean isVisible = stack.hasVisibleBehindActivity();
2889 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2890 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002891
2892 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002893 if (top == null || top == r || (visible == isVisible)) {
2894 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2895 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002896 return true;
2897 }
2898
2899 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002900 if (visible && top.fullscreen) {
2901 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07002902 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07002903 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07002904 " top.app.thread=" + top.app.thread);
2905 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002906 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2907 // Only the activity set as currently visible behind should actively reset its
2908 // visible behind state.
2909 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
2910 + visible + " stack.getVisibleBehindActivity()=" +
2911 stack.getVisibleBehindActivity() + " r=" + r);
2912 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002913 }
2914
Jose Lima4b6c6692014-08-12 17:41:12 -07002915 stack.setVisibleBehindActivity(visible ? r : null);
2916 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002917 // Make the activity immediately above r opaque.
2918 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2919 if (next != null) {
2920 mService.convertFromTranslucent(next.appToken);
2921 }
2922 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002923 if (top.app != null && top.app.thread != null) {
2924 // Notify the top app of the change.
2925 try {
2926 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2927 } catch (RemoteException e) {
2928 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002929 }
2930 return true;
2931 }
2932
Craig Mautnerbb742462014-07-07 15:28:55 -07002933 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2934 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2935 r.mLaunchTaskBehind = false;
2936 final TaskRecord task = r.task;
2937 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002938 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002939 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002940 mWindowManager.setAppVisibility(r.appToken, false);
2941 }
2942
2943 void scheduleLaunchTaskBehindComplete(IBinder token) {
2944 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2945 }
2946
Craig Mautnerde4ef022013-04-07 19:01:33 -07002947 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002948 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002949 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2950 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002951 final int topStackNdx = stacks.size() - 1;
2952 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2953 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002954 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002955 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002956 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002957 }
2958
2959 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002960 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2961 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002962 final int numStacks = stacks.size();
2963 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2964 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002965 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002966 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002967 }
2968 }
2969
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002970 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2971 // Examine all activities currently running in the process.
2972 TaskRecord firstTask = null;
2973 // Tasks is non-null only if two or more tasks are found.
2974 ArraySet<TaskRecord> tasks = null;
2975 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
2976 for (int i=0; i<app.activities.size(); i++) {
2977 ActivityRecord r = app.activities.get(i);
2978 // First, if we find an activity that is in the process of being destroyed,
2979 // then we just aren't going to do anything for now; we want things to settle
2980 // down before we try to prune more activities.
2981 if (r.finishing || r.state == ActivityState.DESTROYING
2982 || r.state == ActivityState.DESTROYED) {
2983 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
2984 return;
2985 }
2986 // Don't consider any activies that are currently not in a state where they
2987 // can be destroyed.
2988 if (r.visible || !r.stopped || !r.haveState
2989 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
2990 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
2991 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
2992 continue;
2993 }
2994 if (r.task != null) {
2995 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
2996 + " from " + r);
2997 if (firstTask == null) {
2998 firstTask = r.task;
2999 } else if (firstTask != r.task) {
3000 if (tasks == null) {
3001 tasks = new ArraySet<>();
3002 tasks.add(firstTask);
3003 }
3004 tasks.add(r.task);
3005 }
3006 }
3007 }
3008 if (tasks == null) {
3009 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3010 return;
3011 }
3012 // If we have activities in multiple tasks that are in a position to be destroyed,
3013 // let's iterate through the tasks and release the oldest one.
3014 final int numDisplays = mActivityDisplays.size();
3015 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3016 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3017 // Step through all stacks starting from behind, to hit the oldest things first.
3018 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3019 final ActivityStack stack = stacks.get(stackNdx);
3020 // Try to release activities in this stack; if we manage to, we are done.
3021 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3022 return;
3023 }
3024 }
3025 }
3026 }
3027
Craig Mautner8d341ef2013-03-26 09:03:27 -07003028 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003029 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
3030 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003031 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003032
Craig Mautner858d8a62013-04-23 17:08:34 -07003033 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003034 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3035 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003036 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003037 final ActivityStack stack = stacks.get(stackNdx);
3038 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003039 TaskRecord task = stack.topTask();
3040 if (task != null) {
3041 mWindowManager.moveTaskToTop(task.taskId);
3042 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003043 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003044 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003045
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003046 ActivityStack stack = getStack(restoreStackId);
3047 if (stack == null) {
3048 stack = mHomeStack;
3049 }
3050 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003051 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003052 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003053 TaskRecord task = stack.topTask();
3054 if (task != null) {
3055 mWindowManager.moveTaskToTop(task.taskId);
3056 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003057 } else {
3058 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003059 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003060 }
Craig Mautner93529a42013-10-04 15:03:13 -07003061 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003062 }
3063
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003064 /**
3065 * Add background users to send boot completed events to.
3066 * @param userId The user being started in the background
3067 * @param uss The state object for the user.
3068 */
3069 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3070 mStartingBackgroundUsers.add(uss);
3071 }
3072
Craig Mautnerde4ef022013-04-07 19:01:33 -07003073 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003074 ArrayList<ActivityRecord> stops = null;
3075
3076 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003077 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3078 ActivityRecord s = mStoppingActivities.get(activityNdx);
3079 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
3080 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
3081 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3082 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003083 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003084 if (s.finishing) {
3085 // If this activity is finishing, it is sitting on top of
3086 // everyone else but we now know it is no longer needed...
3087 // so get rid of it. Otherwise, we need to go through the
3088 // normal flow and hide it once we determine that it is
3089 // hidden by the activities in front of it.
3090 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003091 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003092 }
3093 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003094 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003095 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3096 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003097 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003098 }
3099 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003100 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003101 }
3102 }
3103
3104 return stops;
3105 }
3106
Craig Mautnercf910b02013-04-23 11:23:27 -07003107 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003108 // FIXME
3109/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3110 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003111 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003112 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003113 if (isFrontStack(stack)) {
3114 if (r == null) {
3115 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3116 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003117 final ActivityRecord pausing = stack.mPausingActivity;
3118 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003119 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003120 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003121 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003122 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003123 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003124 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003125 }
3126 }
3127 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003128 final ActivityRecord resumed = stack.mResumedActivity;
3129 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003130 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003131 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003132 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003133 if (r != null && (state == ActivityState.INITIALIZING
3134 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003135 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003136 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003137 }
3138 }
3139 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003140*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003141 }
3142
Craig Mautner27084302013-03-25 08:05:25 -07003143 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003144 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003145 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003146 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3147 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3148 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003149 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003150 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003151
Craig Mautner20e72272013-04-01 13:45:53 -07003152 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003153 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003154 }
3155
Dianne Hackborn390517b2013-05-30 15:03:32 -07003156 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3157 boolean needSep, String prefix) {
3158 if (activity != null) {
3159 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3160 if (needSep) {
3161 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003162 }
3163 pw.print(prefix);
3164 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003165 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003166 }
3167 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003168 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003169 }
3170
Craig Mautner8d341ef2013-03-26 09:03:27 -07003171 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3172 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003173 boolean printed = false;
3174 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003175 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3176 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003177 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003178 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003179 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003180 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003181 final ActivityStack stack = stacks.get(stackNdx);
3182 StringBuilder stackHeader = new StringBuilder(128);
3183 stackHeader.append(" Stack #");
3184 stackHeader.append(stack.mStackId);
3185 stackHeader.append(":");
3186 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3187 needSep, stackHeader.toString());
3188 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3189 !dumpAll, false, dumpPackage, true,
3190 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003191
Craig Mautner4a1cb222013-12-04 16:14:06 -08003192 needSep = printed;
3193 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3194 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003195 if (pr) {
3196 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003197 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003198 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003199 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3200 " mResumedActivity: ");
3201 if (pr) {
3202 printed = true;
3203 needSep = false;
3204 }
3205 if (dumpAll) {
3206 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3207 " mLastPausedActivity: ");
3208 if (pr) {
3209 printed = true;
3210 needSep = true;
3211 }
3212 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3213 needSep, " mLastNoHistoryActivity: ");
3214 }
3215 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003216 }
3217 }
3218
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003219 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3220 false, dumpPackage, true, " Activities waiting to finish:", null);
3221 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3222 false, dumpPackage, true, " Activities waiting to stop:", null);
3223 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3224 false, dumpPackage, true, " Activities waiting for another to become visible:",
3225 null);
3226 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3227 false, dumpPackage, true, " Activities waiting to sleep:", null);
3228 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3229 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003230
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003231 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003232 }
3233
Dianne Hackborn390517b2013-05-30 15:03:32 -07003234 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003235 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003236 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003237 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003238 String innerPrefix = null;
3239 String[] args = null;
3240 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003241 for (int i=list.size()-1; i>=0; i--) {
3242 final ActivityRecord r = list.get(i);
3243 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3244 continue;
3245 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003246 if (innerPrefix == null) {
3247 innerPrefix = prefix + " ";
3248 args = new String[0];
3249 }
3250 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003251 final boolean full = !brief && (complete || !r.isInHistory());
3252 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003253 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003254 needNL = false;
3255 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003256 if (header1 != null) {
3257 pw.println(header1);
3258 header1 = null;
3259 }
3260 if (header2 != null) {
3261 pw.println(header2);
3262 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003263 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003264 if (lastTask != r.task) {
3265 lastTask = r.task;
3266 pw.print(prefix);
3267 pw.print(full ? "* " : " ");
3268 pw.println(lastTask);
3269 if (full) {
3270 lastTask.dump(pw, prefix + " ");
3271 } else if (complete) {
3272 // Complete + brief == give a summary. Isn't that obvious?!?
3273 if (lastTask.intent != null) {
3274 pw.print(prefix); pw.print(" ");
3275 pw.println(lastTask.intent.toInsecureStringWithClip());
3276 }
3277 }
3278 }
3279 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3280 pw.print(" #"); pw.print(i); pw.print(": ");
3281 pw.println(r);
3282 if (full) {
3283 r.dump(pw, innerPrefix);
3284 } else if (complete) {
3285 // Complete + brief == give a summary. Isn't that obvious?!?
3286 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3287 if (r.app != null) {
3288 pw.print(innerPrefix); pw.println(r.app);
3289 }
3290 }
3291 if (client && r.app != null && r.app.thread != null) {
3292 // flush anything that is already in the PrintWriter since the thread is going
3293 // to write to the file descriptor directly
3294 pw.flush();
3295 try {
3296 TransferPipe tp = new TransferPipe();
3297 try {
3298 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3299 r.appToken, innerPrefix, args);
3300 // Short timeout, since blocking here can
3301 // deadlock with the application.
3302 tp.go(fd, 2000);
3303 } finally {
3304 tp.kill();
3305 }
3306 } catch (IOException e) {
3307 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3308 } catch (RemoteException e) {
3309 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3310 }
3311 needNL = true;
3312 }
3313 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003314 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003315 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003316
Craig Mautnerf3333272013-04-22 10:55:53 -07003317 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003318 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003319 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3320 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003321 }
3322
3323 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003324 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003325 }
3326
3327 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003328 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003329 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3330 }
3331
Craig Mautner05d29032013-05-03 13:40:13 -07003332 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003333 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3334 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3335 }
Craig Mautner05d29032013-05-03 13:40:13 -07003336 }
3337
Craig Mautner0eea92c2013-05-16 13:35:39 -07003338 void removeSleepTimeouts() {
3339 mSleepTimeout = false;
3340 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3341 }
3342
3343 final void scheduleSleepTimeout() {
3344 removeSleepTimeouts();
3345 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3346 }
3347
Craig Mautner4a1cb222013-12-04 16:14:06 -08003348 @Override
3349 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003350 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003351 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3352 }
3353
3354 @Override
3355 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003356 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003357 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3358 }
3359
3360 @Override
3361 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003362 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003363 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3364 }
3365
3366 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003367 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003368 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003369 newDisplay = mActivityDisplays.get(displayId) == null;
3370 if (newDisplay) {
3371 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003372 if (activityDisplay.mDisplay == null) {
3373 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3374 return;
3375 }
Craig Mautner4504de52013-12-20 09:06:56 -08003376 mActivityDisplays.put(displayId, activityDisplay);
3377 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003378 }
Craig Mautner4504de52013-12-20 09:06:56 -08003379 if (newDisplay) {
3380 mWindowManager.onDisplayAdded(displayId);
3381 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003382 }
3383
3384 public void handleDisplayRemovedLocked(int displayId) {
3385 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003386 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3387 if (activityDisplay != null) {
3388 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003389 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003390 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003391 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003392 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003393 }
3394 }
3395 mWindowManager.onDisplayRemoved(displayId);
3396 }
3397
3398 public void handleDisplayChangedLocked(int displayId) {
3399 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003400 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3401 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003402 // TODO: Update the bounds.
3403 }
3404 }
3405 mWindowManager.onDisplayChanged(displayId);
3406 }
3407
3408 StackInfo getStackInfo(ActivityStack stack) {
3409 StackInfo info = new StackInfo();
3410 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3411 info.displayId = Display.DEFAULT_DISPLAY;
3412 info.stackId = stack.mStackId;
3413
3414 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3415 final int numTasks = tasks.size();
3416 int[] taskIds = new int[numTasks];
3417 String[] taskNames = new String[numTasks];
3418 for (int i = 0; i < numTasks; ++i) {
3419 final TaskRecord task = tasks.get(i);
3420 taskIds[i] = task.taskId;
3421 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3422 : task.realActivity != null ? task.realActivity.flattenToString()
3423 : task.getTopActivity() != null ? task.getTopActivity().packageName
3424 : "unknown";
3425 }
3426 info.taskIds = taskIds;
3427 info.taskNames = taskNames;
3428 return info;
3429 }
3430
3431 StackInfo getStackInfoLocked(int stackId) {
3432 ActivityStack stack = getStack(stackId);
3433 if (stack != null) {
3434 return getStackInfo(stack);
3435 }
3436 return null;
3437 }
3438
3439 ArrayList<StackInfo> getAllStackInfosLocked() {
3440 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003441 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3442 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003443 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3444 list.add(getStackInfo(stacks.get(ndx)));
3445 }
3446 }
3447 return list;
3448 }
3449
Jason Monka8f569c2014-07-07 12:15:21 -04003450 void showLockTaskToast() {
3451 mLockTaskNotify.showToast(mLockTaskIsLocked);
3452 }
3453
Craig Mautner299f9602015-01-26 09:47:33 -08003454 void setLockTaskModeLocked(TaskRecord task, boolean isLocked, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003455 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003456 // Take out of lock task mode if necessary
3457 if (mLockTaskModeTask != null) {
3458 final Message lockTaskMsg = Message.obtain();
3459 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3460 lockTaskMsg.what = LOCK_TASK_END_MSG;
3461 mLockTaskModeTask = null;
3462 mHandler.sendMessage(lockTaskMsg);
3463 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003464 return;
3465 }
3466 if (isLockTaskModeViolation(task)) {
3467 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3468 return;
3469 }
3470 mLockTaskModeTask = task;
Craig Mautner299f9602015-01-26 09:47:33 -08003471 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003472 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003473
3474 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003475 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3476 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003477 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003478 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003479 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003480 }
3481
3482 boolean isLockTaskModeViolation(TaskRecord task) {
3483 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3484 }
3485
3486 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3487 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003488 final Message lockTaskMsg = Message.obtain();
3489 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3490 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003491 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003492 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003493 }
3494 }
3495
3496 boolean isInLockTaskMode() {
3497 return mLockTaskModeTask != null;
3498 }
3499
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003500 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003501
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003502 public ActivityStackSupervisorHandler(Looper looper) {
3503 super(looper);
3504 }
3505
Craig Mautnerf3333272013-04-22 10:55:53 -07003506 void activityIdleInternal(ActivityRecord r) {
3507 synchronized (mService) {
3508 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3509 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003510 }
3511
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003512 @Override
3513 public void handleMessage(Message msg) {
3514 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003515 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003516 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003517 if (mService.mDidDexOpt) {
3518 mService.mDidDexOpt = false;
3519 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3520 nmsg.obj = msg.obj;
3521 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3522 return;
3523 }
3524 // We don't at this point know if the activity is fullscreen,
3525 // so we need to be conservative and assume it isn't.
3526 activityIdleInternal((ActivityRecord)msg.obj);
3527 } break;
3528 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003529 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003530 activityIdleInternal((ActivityRecord)msg.obj);
3531 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003532 case RESUME_TOP_ACTIVITY_MSG: {
3533 synchronized (mService) {
3534 resumeTopActivitiesLocked();
3535 }
3536 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003537 case SLEEP_TIMEOUT_MSG: {
3538 synchronized (mService) {
3539 if (mService.isSleepingOrShuttingDown()) {
3540 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3541 mSleepTimeout = true;
3542 checkReadyForSleepLocked();
3543 }
3544 }
3545 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003546 case LAUNCH_TIMEOUT_MSG: {
3547 if (mService.mDidDexOpt) {
3548 mService.mDidDexOpt = false;
3549 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3550 return;
3551 }
3552 synchronized (mService) {
3553 if (mLaunchingActivity.isHeld()) {
3554 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3555 if (VALIDATE_WAKE_LOCK_CALLER
3556 && Binder.getCallingUid() != Process.myUid()) {
3557 throw new IllegalStateException("Calling must be system uid");
3558 }
3559 mLaunchingActivity.release();
3560 }
3561 }
3562 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003563 case HANDLE_DISPLAY_ADDED: {
3564 handleDisplayAddedLocked(msg.arg1);
3565 } break;
3566 case HANDLE_DISPLAY_CHANGED: {
3567 handleDisplayChangedLocked(msg.arg1);
3568 } break;
3569 case HANDLE_DISPLAY_REMOVED: {
3570 handleDisplayRemovedLocked(msg.arg1);
3571 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003572 case CONTAINER_CALLBACK_VISIBILITY: {
3573 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003574 final IActivityContainerCallback callback = container.mCallback;
3575 if (callback != null) {
3576 try {
3577 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3578 } catch (RemoteException e) {
3579 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003580 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003581 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003582 case LOCK_TASK_START_MSG: {
3583 // When lock task starts, we disable the status bars.
3584 try {
Jason Monk62515be2014-05-21 16:06:19 -04003585 if (mLockTaskNotify == null) {
3586 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003587 }
Jason Monk62515be2014-05-21 16:06:19 -04003588 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003589 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003590 if (getStatusBarService() != null) {
3591 int flags =
3592 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003593 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003594 flags ^= StatusBarManager.DISABLE_HOME
3595 | StatusBarManager.DISABLE_RECENT;
3596 }
3597 getStatusBarService().disable(flags, mToken,
3598 mService.mContext.getPackageName());
3599 }
3600 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003601 if (getDevicePolicyManager() != null) {
3602 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003603 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003604 }
justinzhang5286d3f2014-05-12 17:06:01 -04003605 } catch (RemoteException ex) {
3606 throw new RuntimeException(ex);
3607 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003608 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003609 case LOCK_TASK_END_MSG: {
3610 // When lock task ends, we enable the status bars.
3611 try {
Jason Monk62515be2014-05-21 16:06:19 -04003612 if (getStatusBarService() != null) {
3613 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3614 mService.mContext.getPackageName());
3615 }
3616 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003617 if (getDevicePolicyManager() != null) {
3618 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3619 msg.arg1);
3620 }
Jason Monk62515be2014-05-21 16:06:19 -04003621 if (mLockTaskNotify == null) {
3622 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3623 }
3624 mLockTaskNotify.show(false);
3625 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003626 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003627 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003628 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003629 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003630 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003631 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003632 new LockPatternUtils(mService.mContext)
3633 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003634 }
3635 } catch (SettingNotFoundException e) {
3636 // No setting, don't lock.
3637 }
justinzhang5286d3f2014-05-12 17:06:01 -04003638 } catch (RemoteException ex) {
3639 throw new RuntimeException(ex);
3640 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003641 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003642 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3643 final ActivityContainer container = (ActivityContainer) msg.obj;
3644 final IActivityContainerCallback callback = container.mCallback;
3645 if (callback != null) {
3646 try {
3647 callback.onAllActivitiesComplete(container.asBinder());
3648 } catch (RemoteException e) {
3649 }
3650 }
3651 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003652 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3653 synchronized (mService) {
3654 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3655 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003656 final ActivityContainer container = (ActivityContainer) msg.obj;
3657 container.mStack.finishAllActivitiesLocked(true);
3658 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003659 }
3660 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003661 case LAUNCH_TASK_BEHIND_COMPLETE: {
3662 synchronized (mService) {
3663 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3664 if (r != null) {
3665 handleLaunchTaskBehindCompleteLocked(r);
3666 }
3667 }
3668 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003669 }
3670 }
3671 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003672
Ying Wangb081a592014-04-22 15:20:16 -07003673 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003674 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003675 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003676 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003677 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003678 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003679 ActivityRecord mParentActivity = null;
3680 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003681
Craig Mautnere3a00d72014-04-16 08:31:19 -07003682 boolean mVisible = true;
3683
Craig Mautner4a1cb222013-12-04 16:14:06 -08003684 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003685 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003686
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003687 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3688 final static int CONTAINER_STATE_NO_SURFACE = 1;
3689 final static int CONTAINER_STATE_FINISHING = 2;
3690 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3691
3692 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003693 synchronized (mService) {
3694 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003695 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003696 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003697 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003698 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003699 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003700
Craig Mautnere0a38842013-12-16 16:14:02 -08003701 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003702 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3703 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003704 mActivityDisplay = activityDisplay;
3705 mStack.mDisplayId = activityDisplay.mDisplayId;
3706 mStack.mStacks = activityDisplay.mStacks;
3707
3708 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003709 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003710 }
3711
3712 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003713 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003714 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003715 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3716 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003717 return;
3718 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003719 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003720 }
3721 }
3722
3723 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003724 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003725 synchronized (mService) {
3726 if (mActivityDisplay != null) {
3727 return mActivityDisplay.mDisplayId;
3728 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003729 }
3730 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003731 }
3732
Jeff Brownca9bc702014-02-11 14:32:56 -08003733 @Override
3734 public boolean injectEvent(InputEvent event) {
3735 final long origId = Binder.clearCallingIdentity();
3736 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003737 synchronized (mService) {
3738 if (mActivityDisplay != null) {
3739 return mInputManagerInternal.injectInputEvent(event,
3740 mActivityDisplay.mDisplayId,
3741 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3742 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003743 }
3744 return false;
3745 } finally {
3746 Binder.restoreCallingIdentity(origId);
3747 }
3748 }
3749
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003750 @Override
3751 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003752 synchronized (mService) {
3753 if (mContainerState == CONTAINER_STATE_FINISHING) {
3754 return;
3755 }
3756 mContainerState = CONTAINER_STATE_FINISHING;
3757
3758 final Message msg =
3759 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003760 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003761
3762 long origId = Binder.clearCallingIdentity();
3763 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003764 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003765 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003766 } finally {
3767 Binder.restoreCallingIdentity(origId);
3768 }
3769 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003770 }
3771
Craig Mautner60257702014-09-17 15:02:33 -07003772 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003773 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3774 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003775 if (mActivityDisplay != null) {
3776 mActivityDisplay.detachActivitiesLocked(mStack);
3777 mActivityDisplay = null;
3778 mStack.mDisplayId = -1;
3779 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003780 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003781 }
3782 }
3783
3784 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003785 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003786 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003787 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003788 Binder.getCallingUid(), mCurrentUser, false,
3789 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003790 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003791 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003792 String mimeType = intent.getType();
3793 if (mimeType == null && intent.getData() != null
3794 && "content".equals(intent.getData().getScheme())) {
3795 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3796 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003797 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0,
3798 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003799 }
3800
3801 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003802 public final int startActivityIntentSender(IIntentSender intentSender) {
3803 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3804
3805 if (!(intentSender instanceof PendingIntentRecord)) {
3806 throw new IllegalArgumentException("Bad PendingIntent object");
3807 }
3808
3809 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003810 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003811 }
3812
Craig Mautner247ab652014-04-25 10:09:00 -07003813 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3814 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003815 Binder.getCallingUid(), mCurrentUser, false,
3816 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003817 if (resolvedType == null) {
3818 resolvedType = intent.getType();
3819 if (resolvedType == null && intent.getData() != null
3820 && "content".equals(intent.getData().getScheme())) {
3821 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3822 }
3823 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003824 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003825 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003826 throw new SecurityException(
3827 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3828 }
3829 }
3830
3831 /** Throw a SecurityException if allowEmbedded is not true */
3832 @Override
3833 public final void checkEmbeddedAllowed(Intent intent) {
3834 checkEmbeddedAllowedInner(intent, null);
3835 }
3836
3837 /** Throw a SecurityException if allowEmbedded is not true */
3838 @Override
3839 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3840 if (!(intentSender instanceof PendingIntentRecord)) {
3841 throw new IllegalArgumentException("Bad PendingIntent object");
3842 }
3843 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3844 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3845 pendingIntent.key.requestResolvedType);
3846 }
3847
Craig Mautnerdf88d732014-01-27 09:21:32 -08003848 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003849 public IBinder asBinder() {
3850 return this;
3851 }
3852
Craig Mautner4504de52013-12-20 09:06:56 -08003853 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003854 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003855 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003856 }
3857
Craig Mautner4a1cb222013-12-04 16:14:06 -08003858 ActivityStackSupervisor getOuter() {
3859 return ActivityStackSupervisor.this;
3860 }
3861
Craig Mautnerd163e752014-06-13 17:18:47 -07003862 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003863 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003864 }
3865
3866 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003867 synchronized (mService) {
3868 if (mActivityDisplay != null) {
3869 mActivityDisplay.getBounds(outBounds);
3870 } else {
3871 outBounds.set(0, 0);
3872 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003873 }
3874 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003875
Craig Mautner6985bad2014-04-21 15:22:06 -07003876 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003877 void setVisible(boolean visible) {
3878 if (mVisible != visible) {
3879 mVisible = visible;
3880 if (mCallback != null) {
3881 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3882 0 /* unused */, this).sendToTarget();
3883 }
3884 }
3885 }
3886
Craig Mautner6985bad2014-04-21 15:22:06 -07003887 void setDrawn() {
3888 }
3889
Craig Mautner1b4bf852014-05-26 15:06:32 -07003890 // You can always start a new task on a regular ActivityStack.
3891 boolean isEligibleForNewTasks() {
3892 return true;
3893 }
3894
Craig Mautnerd163e752014-06-13 17:18:47 -07003895 void onTaskListEmptyLocked() {
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003896 }
3897
Craig Mautner34b73df2014-01-12 21:11:08 -08003898 @Override
3899 public String toString() {
3900 return mIdString + (mActivityDisplay == null ? "N" : "A");
3901 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003902 }
3903
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003904 private class VirtualActivityContainer extends ActivityContainer {
3905 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003906 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003907
3908 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3909 super(getNextStackId());
3910 mParentActivity = parent;
3911 mCallback = callback;
3912 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003913 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003914 }
3915
3916 @Override
3917 public void setSurface(Surface surface, int width, int height, int density) {
3918 super.setSurface(surface, width, height, density);
3919
3920 synchronized (mService) {
3921 final long origId = Binder.clearCallingIdentity();
3922 try {
3923 setSurfaceLocked(surface, width, height, density);
3924 } finally {
3925 Binder.restoreCallingIdentity(origId);
3926 }
3927 }
3928 }
3929
3930 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3931 if (mContainerState == CONTAINER_STATE_FINISHING) {
3932 return;
3933 }
3934 VirtualActivityDisplay virtualActivityDisplay =
3935 (VirtualActivityDisplay) mActivityDisplay;
3936 if (virtualActivityDisplay == null) {
3937 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003938 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003939 mActivityDisplay = virtualActivityDisplay;
3940 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3941 attachToDisplayLocked(virtualActivityDisplay);
3942 }
3943
3944 if (mSurface != null) {
3945 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003946 }
3947
Craig Mautner6985bad2014-04-21 15:22:06 -07003948 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003949 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003950 mStack.resumeTopActivityLocked(null);
3951 } else {
3952 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003953 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003954 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003955 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003956 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003957 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003958
Craig Mautnerd163e752014-06-13 17:18:47 -07003959 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003960
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003961 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3962 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003963 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003964
Craig Mautner6985bad2014-04-21 15:22:06 -07003965 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003966 boolean isAttachedLocked() {
3967 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003968 }
3969
3970 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003971 void setDrawn() {
3972 synchronized (mService) {
3973 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003974 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003975 }
3976 }
3977
Craig Mautner1b4bf852014-05-26 15:06:32 -07003978 // Never start a new task on an ActivityView if it isn't explicitly specified.
3979 @Override
3980 boolean isEligibleForNewTasks() {
3981 return false;
3982 }
3983
Craig Mautner60257702014-09-17 15:02:33 -07003984 void onTaskListEmptyLocked() {
3985 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3986 detachLocked();
3987 deleteActivityContainer(this);
3988 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3989 }
3990
Craig Mautnerd163e752014-06-13 17:18:47 -07003991 private void setSurfaceIfReadyLocked() {
3992 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003993 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3994 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3995 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3996 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3997 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003998 }
3999 }
4000
Craig Mautner4a1cb222013-12-04 16:14:06 -08004001 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4002 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004003 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004004 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004005 int mDisplayId;
4006 Display mDisplay;
4007 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004008
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4010 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004011 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012
Jose Lima4b6c6692014-08-12 17:41:12 -07004013 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004014
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004015 ActivityDisplay() {
4016 }
Craig Mautner4504de52013-12-20 09:06:56 -08004017
Craig Mautner1a70a162014-09-13 12:09:31 -07004018 // After instantiation, check that mDisplay is not null before using this. The alternative
4019 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004020 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004021 final Display display = mDisplayManager.getDisplay(displayId);
4022 if (display == null) {
4023 return;
4024 }
4025 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004026 }
4027
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004028 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004029 mDisplay = display;
4030 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004031 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004032 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004033
4034 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004035 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4036 + mDisplayId);
4037 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004038 }
4039
Craig Mautnere0a38842013-12-16 16:14:02 -08004040 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004041 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004042 + " from displayId=" + mDisplayId);
4043 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004044 }
4045
4046 void getBounds(Point bounds) {
4047 mDisplay.getDisplayInfo(mDisplayInfo);
4048 bounds.x = mDisplayInfo.appWidth;
4049 bounds.y = mDisplayInfo.appHeight;
4050 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004051
Jose Lima4b6c6692014-08-12 17:41:12 -07004052 void setVisibleBehindActivity(ActivityRecord r) {
4053 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004054 }
4055
Jose Lima4b6c6692014-08-12 17:41:12 -07004056 boolean hasVisibleBehindActivity() {
4057 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004058 }
4059
Craig Mautner34b73df2014-01-12 21:11:08 -08004060 @Override
4061 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004062 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4063 }
4064 }
4065
4066 class VirtualActivityDisplay extends ActivityDisplay {
4067 VirtualDisplay mVirtualDisplay;
4068
Craig Mautner6985bad2014-04-21 15:22:06 -07004069 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004070 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004071 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4072 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4073 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4074 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004075
4076 init(mVirtualDisplay.getDisplay());
4077
4078 mWindowManager.handleDisplayAdded(mDisplayId);
4079 }
4080
4081 void setSurface(Surface surface) {
4082 if (mVirtualDisplay != null) {
4083 mVirtualDisplay.setSurface(surface);
4084 }
4085 }
4086
4087 @Override
4088 void detachActivitiesLocked(ActivityStack stack) {
4089 super.detachActivitiesLocked(stack);
4090 if (mVirtualDisplay != null) {
4091 mVirtualDisplay.release();
4092 mVirtualDisplay = null;
4093 }
4094 }
4095
4096 @Override
4097 public String toString() {
4098 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004099 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004100 }
Jose Lima58e66d62014-05-27 20:00:27 -07004101
4102 private boolean isLeanbackOnlyDevice() {
4103 boolean onLeanbackOnly = false;
4104 try {
4105 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4106 PackageManager.FEATURE_LEANBACK_ONLY);
4107 } catch (RemoteException e) {
4108 // noop
4109 }
4110
4111 return onLeanbackOnly;
4112 }
Craig Mautner27084302013-03-25 08:05:25 -07004113}