blob: b3d3d6a4ac9a775896f56a03096b521e9a0ab784 [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;
Wale Ogunwale60454db2015-01-23 16:05:07 -080066import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080067import android.hardware.display.DisplayManager;
68import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080069import android.hardware.display.DisplayManagerGlobal;
70import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080071import android.hardware.input.InputManager;
72import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080073import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070074import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070075import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070076import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070077import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070078import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070079import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070080import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070082import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070083import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070084import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040085import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070086import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070087import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040088import android.provider.Settings;
89import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070090import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070091import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070092import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070093import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080094import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070095
Craig Mautner4a1cb222013-12-04 16:14:06 -080096import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080097import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080098import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080099import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800100import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700101import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700102import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800103import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700104import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400105import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400106import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800107import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700108import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700109import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110
justinzhang5286d3f2014-05-12 17:06:01 -0400111
Craig Mautner8d341ef2013-03-26 09:03:27 -0700112import java.io.FileDescriptor;
113import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700114import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700115import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700116import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700117
Craig Mautner4a1cb222013-12-04 16:14:06 -0800118public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700119 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
120 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
121 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700122 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700123 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700124 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700125 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
126 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner30bd3bb2014-11-13 00:55:53 +0000127 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700128 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700129
Craig Mautner2219a1b2013-03-25 09:44:30 -0700130 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700131
Craig Mautnerf3333272013-04-22 10:55:53 -0700132 /** How long we wait until giving up on the last activity telling us it is idle. */
133 static final int IDLE_TIMEOUT = 10*1000;
134
Craig Mautner0eea92c2013-05-16 13:35:39 -0700135 /** How long we can hold the sleep wake lock before giving up. */
136 static final int SLEEP_TIMEOUT = 5*1000;
137
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700138 // How long we can hold the launch wake lock before giving up.
139 static final int LAUNCH_TIMEOUT = 10*1000;
140
Craig Mautner05d29032013-05-03 13:40:13 -0700141 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
142 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
143 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700144 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700145 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800146 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
147 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
148 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700149 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400150 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
151 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700152 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700153 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700154 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800155
Craig Mautner4504de52013-12-20 09:06:56 -0800156 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700157
Jason Monk62515be2014-05-21 16:06:19 -0400158 private static final String LOCK_TASK_TAG = "Lock-to-App";
159
justinzhang5286d3f2014-05-12 17:06:01 -0400160 /** Status Bar Service **/
161 private IBinder mToken = new Binder();
162 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400163 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400164
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700165 // For debugging to make sure the caller when acquiring/releasing our
166 // wake lock is the system process.
167 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700168
Craig Mautner27084302013-03-25 08:05:25 -0700169 final ActivityManagerService mService;
170
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800171 private final RecentTasks mRecentTasks;
172
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700173 final ActivityStackSupervisorHandler mHandler;
174
175 /** Short cut */
176 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800177 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700178
Craig Mautner8d341ef2013-03-26 09:03:27 -0700179 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700180 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700181
182 /** Task identifier that activities are currently being started in. Incremented each time a
183 * new task is created. */
184 private int mCurTaskId = 0;
185
Craig Mautner2420ead2013-04-01 17:13:20 -0700186 /** The current user */
187 private int mCurrentUser;
188
Craig Mautnere0a38842013-12-16 16:14:02 -0800189 /** The stack containing the launcher app. Assumed to always be attached to
190 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700191 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700192
Craig Mautnere0a38842013-12-16 16:14:02 -0800193 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700194 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700195
Craig Mautner4a1cb222013-12-04 16:14:06 -0800196 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
197 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800198 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800199 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700200
201 /** List of activities that are waiting for a new activity to become visible before completing
202 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800203 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700204
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700205 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800206 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700207
208 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800209 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700210
Craig Mautnerde4ef022013-04-07 19:01:33 -0700211 /** List of activities that are ready to be stopped, but waiting for the next activity to
212 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800213 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700214
Craig Mautnerf3333272013-04-22 10:55:53 -0700215 /** List of activities that are ready to be finished, but waiting for the previous activity to
216 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800217 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700218
Craig Mautner0eea92c2013-05-16 13:35:39 -0700219 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800220 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700221
Craig Mautnerf3333272013-04-22 10:55:53 -0700222 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800223 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700224
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700225 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800226 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700227
Craig Mautnerde4ef022013-04-07 19:01:33 -0700228 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
229 * is being brought in front of us. */
230 boolean mUserLeaving = false;
231
Craig Mautner0eea92c2013-05-16 13:35:39 -0700232 /** Set when we have taken too long waiting to go to sleep. */
233 boolean mSleepTimeout = false;
234
Jose Lima58e66d62014-05-27 20:00:27 -0700235 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
236 * setWindowManager is called. **/
237 private boolean mLeanbackOnlyDevice;
238
Craig Mautner0eea92c2013-05-16 13:35:39 -0700239 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700240 * We don't want to allow the device to go to sleep while in the process
241 * of launching an activity. This is primarily to allow alarm intent
242 * receivers to launch an activity and get that to run before the device
243 * goes back to sleep.
244 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700245 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700246
247 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700248 * Set when the system is going to sleep, until we have
249 * successfully paused the current activity and released our wake lock.
250 * At that point the system is allowed to actually sleep.
251 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700252 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700253
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700254 /** Stack id of the front stack when user switched, indexed by userId. */
255 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700256
Craig Mautner4504de52013-12-20 09:06:56 -0800257 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800258 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700259 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800260
261 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700262 private final SparseArray<ActivityDisplay> mActivityDisplays =
263 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800264
Jeff Brownca9bc702014-02-11 14:32:56 -0800265 InputManagerInternal mInputManagerInternal;
266
Craig Mautneraea74a52014-03-08 14:23:10 -0800267 /** If non-null then the task specified remains in front and no other tasks may be started
268 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400269 TaskRecord mLockTaskModeTask;
Benjamin Franz43261142015-02-11 15:59:44 +0000270 /** Store the current lock task mode. Possible values:
271 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActicityManager#LOCK_TASK_MODE_LOCKED},
272 * {@link ActicityManager#LOCK_TASK_MODE_PINNED}
273 */
274 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400275 /**
276 * Notifies the user when entering/exiting lock-task.
277 */
278 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800279
Craig Mautneree36c772014-07-16 14:56:05 -0700280 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
281 = new ArrayList<PendingActivityLaunch>();
282
Craig Mautner42d04db2014-11-06 12:13:23 -0800283 /** Used to keep resumeTopActivityLocked() from being entered recursively */
284 boolean inResumeTopActivity;
285
Craig Mautneree36c772014-07-16 14:56:05 -0700286 /**
287 * Description of a request to start a new activity, which has been held
288 * due to app switches being disabled.
289 */
290 static class PendingActivityLaunch {
291 final ActivityRecord r;
292 final ActivityRecord sourceRecord;
293 final int startFlags;
294 final ActivityStack stack;
295
296 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
297 int _startFlags, ActivityStack _stack) {
298 r = _r;
299 sourceRecord = _sourceRecord;
300 startFlags = _startFlags;
301 stack = _stack;
302 }
303 }
304
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800305 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700306 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800307 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700308 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
309 }
310
311 /**
312 * At the time when the constructor runs, the power manager has not yet been
313 * initialized. So we initialize our wakelocks afterwards.
314 */
315 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800316 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700317 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700318 mLaunchingActivity =
319 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
320 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700321 }
322
justinzhang5286d3f2014-05-12 17:06:01 -0400323 // This function returns a IStatusBarService. The value is from ServiceManager.
324 // getService and is cached.
325 private IStatusBarService getStatusBarService() {
326 synchronized (mService) {
327 if (mStatusBarService == null) {
328 mStatusBarService = IStatusBarService.Stub.asInterface(
329 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
330 if (mStatusBarService == null) {
331 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
332 }
333 }
334 return mStatusBarService;
335 }
336 }
337
Jason Monk35c62a42014-06-17 10:24:47 -0400338 private IDevicePolicyManager getDevicePolicyManager() {
339 synchronized (mService) {
340 if (mDevicePolicyManager == null) {
341 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
342 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
343 if (mDevicePolicyManager == null) {
344 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
345 }
346 }
347 return mDevicePolicyManager;
348 }
349 }
350
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700351 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800352 synchronized (mService) {
353 mWindowManager = wm;
354
355 mDisplayManager =
356 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
357 mDisplayManager.registerDisplayListener(this, null);
358
359 Display[] displays = mDisplayManager.getDisplays();
360 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
361 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800362 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700363 if (activityDisplay.mDisplay == null) {
364 throw new IllegalStateException("Default Display does not exist");
365 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800366 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800367 }
368
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700369 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800370 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800371
372 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700373
374 // Initialize this here, now that we can get a valid reference to PackageManager.
375 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800376 }
Craig Mautner27084302013-03-25 08:05:25 -0700377 }
378
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200379 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700380 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200381 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700382 }
383
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700384 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800385 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700386 }
387
Craig Mautnerde4ef022013-04-07 19:01:33 -0700388 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800389 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700390 }
391
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800392 /** Top of all visible stacks is/should always be equal to the focused stack.
393 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
394 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700395 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800396 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
397 if (parent != null) {
398 stack = parent.task.stack;
399 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800400 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700401 }
402
Craig Mautner299f9602015-01-26 09:47:33 -0800403 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800404 moveHomeStack(toFront, reason, null);
405 }
406
407 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800408 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800409 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800410 if (topNdx <= 0) {
411 return;
412 }
413 ActivityStack topStack = stacks.get(topNdx);
414 final boolean homeInFront = topStack == mHomeStack;
415 if (homeInFront != toFront) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416 stacks.remove(mHomeStack);
417 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800418 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
419 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700420 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800421
422 if (lastFocusedStack != null) {
423 mLastFocusedStack = lastFocusedStack;
424 }
425 mFocusedStack = stacks.get(topNdx);
426
Craig Mautnerde313752015-01-22 14:28:03 -0800427 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
428 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800429 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800430
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800431 if (mService.mBooting || !mService.mBooted) {
432 final ActivityRecord r = topRunningActivityLocked();
433 if (r != null && r.idle) {
434 checkFinishBootingLocked();
435 }
436 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700437 }
438
Craig Mautner299f9602015-01-26 09:47:33 -0800439 void moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700440 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
441 mWindowManager.showRecentApps();
442 return;
443 }
Craig Mautner299f9602015-01-26 09:47:33 -0800444 moveHomeStack(true, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700445 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700446 }
447
Craig Mautner299f9602015-01-26 09:47:33 -0800448 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700449 if (!mService.mBooting && !mService.mBooted) {
450 // Not ready yet!
451 return false;
452 }
453
Craig Mautner84984fa2014-06-19 11:19:20 -0700454 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
455 mWindowManager.showRecentApps();
456 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700457 }
Craig Mautner299f9602015-01-26 09:47:33 -0800458 moveHomeStackTaskToTop(homeStackTaskType, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700459 if (prev != null) {
460 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
461 }
462
Craig Mautnera8a90e02013-06-28 15:24:50 -0700463 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700464 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000465 if (r != null && r.isHomeActivity()) {
Craig Mautner299f9602015-01-26 09:47:33 -0800466 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700467 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700468 }
Craig Mautner299f9602015-01-26 09:47:33 -0800469 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700470 }
471
Craig Mautner8d341ef2013-03-26 09:03:27 -0700472 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800473 return anyTaskForIdLocked(id, true);
474 }
475
476 /**
477 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
478 * @param id Id of the task we would like returned.
479 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
480 * restore the task from recents to the active list.
481 */
482 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800483 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800484 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800485 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800486 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
487 ActivityStack stack = stacks.get(stackNdx);
488 TaskRecord task = stack.taskForIdLocked(id);
489 if (task != null) {
490 return task;
491 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700492 }
493 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800494
495 // Don't give up! Look in recents.
496 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800497 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800498 if (task == null) {
499 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
500 return null;
501 }
502
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800503 if (!restoreFromRecents) {
504 return task;
505 }
506
Wale Ogunwale7de05352014-12-12 15:21:33 -0800507 if (!restoreRecentTaskLocked(task)) {
508 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
509 return null;
510 }
511 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
512 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700513 }
514
Craig Mautner6170f732013-04-02 13:05:23 -0700515 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800516 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800517 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800518 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800519 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
520 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
521 if (r != null) {
522 return r;
523 }
Craig Mautner6170f732013-04-02 13:05:23 -0700524 }
525 }
526 return null;
527 }
528
Craig Mautneref73ee12014-04-23 11:45:37 -0700529 void setNextTaskId(int taskId) {
530 if (taskId > mCurTaskId) {
531 mCurTaskId = taskId;
532 }
533 }
534
Craig Mautner8d341ef2013-03-26 09:03:27 -0700535 int getNextTaskId() {
536 do {
537 mCurTaskId++;
538 if (mCurTaskId <= 0) {
539 mCurTaskId = 1;
540 }
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800541 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700542 return mCurTaskId;
543 }
544
Craig Mautnerde4ef022013-04-07 19:01:33 -0700545 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800546 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700547 if (stack == null) {
548 return null;
549 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700550 ActivityRecord resumedActivity = stack.mResumedActivity;
551 if (resumedActivity == null || resumedActivity.app == null) {
552 resumedActivity = stack.mPausingActivity;
553 if (resumedActivity == null || resumedActivity.app == null) {
554 resumedActivity = stack.topRunningActivityLocked(null);
555 }
556 }
557 return resumedActivity;
558 }
559
Dianne Hackbornff072722014-09-24 10:56:28 -0700560 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700561 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800562 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800563 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
564 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800565 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
566 final ActivityStack stack = stacks.get(stackNdx);
567 if (!isFrontStack(stack)) {
568 continue;
569 }
570 ActivityRecord hr = stack.topRunningActivityLocked(null);
571 if (hr != null) {
572 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
573 && processName.equals(hr.processName)) {
574 try {
George Mount2c92c972014-03-20 09:38:23 -0700575 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800576 didSomething = true;
577 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700578 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800579 Slog.w(TAG, "Exception in new application when starting activity "
580 + hr.intent.getComponent().flattenToShortString(), e);
581 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700582 }
Craig Mautner20e72272013-04-01 13:45:53 -0700583 }
Craig Mautner20e72272013-04-01 13:45:53 -0700584 }
585 }
586 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700587 if (!didSomething) {
588 ensureActivitiesVisibleLocked(null, 0);
589 }
Craig Mautner20e72272013-04-01 13:45:53 -0700590 return didSomething;
591 }
592
593 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800594 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
595 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800596 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
597 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800598 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800599 continue;
600 }
601 final ActivityRecord resumedActivity = stack.mResumedActivity;
602 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800603 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
604 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605 return false;
606 }
Craig Mautner20e72272013-04-01 13:45:53 -0700607 }
608 }
609 return true;
610 }
611
Craig Mautnerde4ef022013-04-07 19:01:33 -0700612 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
614 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800615 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
616 final ActivityStack stack = stacks.get(stackNdx);
617 if (isFrontStack(stack)) {
618 final ActivityRecord r = stack.mResumedActivity;
619 if (r != null && r.state != ActivityState.RESUMED) {
620 return false;
621 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700622 }
623 }
624 }
625 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 if (DEBUG_STACK) Slog.d(TAG,
627 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
628 mLastFocusedStack + " to=" + mFocusedStack);
629 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700630 return true;
631 }
632
633 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800634 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
635 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800636 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
637 final ActivityStack stack = stacks.get(stackNdx);
638 final ActivityRecord r = stack.mResumedActivity;
Craig Mautner8c14c152015-01-15 17:32:07 -0800639 if (r != null && (!r.nowVisible || mWaitingVisibleActivities.contains(r))) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800640 return false;
641 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700642 }
643 }
644 return true;
645 }
646
Craig Mautner2acc3892013-09-23 10:28:14 -0700647 /**
648 * Pause all activities in either all of the stacks or just the back stacks.
649 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700650 * @return true if any activity was paused as a result of this call.
651 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700652 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700653 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800654 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
655 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800656 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
657 final ActivityStack stack = stacks.get(stackNdx);
658 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
659 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
660 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700661 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
662 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700664 }
665 }
666 return someActivityPaused;
667 }
668
Craig Mautnerde4ef022013-04-07 19:01:33 -0700669 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700670 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800671 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
672 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800673 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
674 final ActivityStack stack = stacks.get(stackNdx);
675 final ActivityRecord r = stack.mPausingActivity;
676 if (r != null && r.state != ActivityState.PAUSED
677 && r.state != ActivityState.STOPPED
678 && r.state != ActivityState.STOPPING) {
679 if (DEBUG_STATES) {
680 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
681 pausing = false;
682 } else {
683 return false;
684 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700685 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700686 }
687 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700688 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700689 }
690
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700691 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
692 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500693 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800694 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
695 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
696 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
697 final ActivityStack stack = stacks.get(stackNdx);
698 if (stack.mResumedActivity != null &&
699 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700700 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800701 }
702 }
703 }
704 }
705
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700706 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700707 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700708 }
709
710 void sendWaitingVisibleReportLocked(ActivityRecord r) {
711 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700712 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700713 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700714 if (w.who == null) {
715 changed = true;
716 w.timeout = false;
717 if (r != null) {
718 w.who = new ComponentName(r.info.packageName, r.info.name);
719 }
720 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
721 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700722 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700723 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700724 if (changed) {
725 mService.notifyAll();
726 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700727 }
728
729 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
730 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700731 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700732 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700733 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700734 if (w.who == null) {
735 changed = true;
736 w.timeout = timeout;
737 if (r != null) {
738 w.who = new ComponentName(r.info.packageName, r.info.name);
739 }
740 w.thisTime = thisTime;
741 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700742 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700743 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700744 if (changed) {
745 mService.notifyAll();
746 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700747 }
748
Craig Mautner29219d92013-04-16 20:19:12 -0700749 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800750 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700751 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
752 if (r != null) {
753 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700754 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700755
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800757 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800758 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
759 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700760 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700761 r = stack.topRunningActivityLocked(null);
762 if (r != null) {
763 return r;
764 }
765 }
766 }
767 return null;
768 }
769
Dianne Hackborn09233282014-04-30 11:33:59 -0700770 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700771 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800772 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
773 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800774 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800775 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800776 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800777 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
778 final ActivityStack stack = stacks.get(stackNdx);
779 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
780 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700781 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700782 }
783 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700784
785 // The lists are already sorted from most recent to oldest. Just pull the most recent off
786 // each list and add it to list. Stop when all lists are empty or maxNum reached.
787 while (maxNum > 0) {
788 long mostRecentActiveTime = Long.MIN_VALUE;
789 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 final int numTaskLists = runningTaskLists.size();
791 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
792 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700793 if (!stackTaskList.isEmpty()) {
794 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
795 if (lastActiveTime > mostRecentActiveTime) {
796 mostRecentActiveTime = lastActiveTime;
797 selectedStackList = stackTaskList;
798 }
799 }
800 }
801 if (selectedStackList != null) {
802 list.add(selectedStackList.remove(0));
803 --maxNum;
804 } else {
805 break;
806 }
807 }
Craig Mautner20e72272013-04-01 13:45:53 -0700808 }
809
Craig Mautner23ac33b2013-04-01 16:26:35 -0700810 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700811 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700812 // Collect information about the target of the Intent.
813 ActivityInfo aInfo;
814 try {
815 ResolveInfo rInfo =
816 AppGlobals.getPackageManager().resolveIntent(
817 intent, resolvedType,
818 PackageManager.MATCH_DEFAULT_ONLY
819 | ActivityManagerService.STOCK_PM_FLAGS, userId);
820 aInfo = rInfo != null ? rInfo.activityInfo : null;
821 } catch (RemoteException e) {
822 aInfo = null;
823 }
824
825 if (aInfo != null) {
826 // Store the found target back into the intent, because now that
827 // we have it we never want to do this again. For example, if the
828 // user navigates back to this point in the history, we should
829 // always restart the exact same activity.
830 intent.setComponent(new ComponentName(
831 aInfo.applicationInfo.packageName, aInfo.name));
832
833 // Don't debug things in the system process
834 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
835 if (!aInfo.processName.equals("system")) {
836 mService.setDebugApp(aInfo.processName, true, false);
837 }
838 }
839
840 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
841 if (!aInfo.processName.equals("system")) {
842 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
843 }
844 }
845
Jeff Hao1b012d32014-08-20 10:35:34 -0700846 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700847 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700848 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700849 }
850 }
851 }
852 return aInfo;
853 }
854
Craig Mautner299f9602015-01-26 09:47:33 -0800855 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
856 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700857 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
858 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700859 }
860
Craig Mautner23ac33b2013-04-01 16:26:35 -0700861 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700862 String callingPackage, Intent intent, String resolvedType,
863 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700864 IBinder resultTo, String resultWho, int requestCode, int startFlags,
865 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700866 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700867 // Refuse possible leaked file descriptors
868 if (intent != null && intent.hasFileDescriptors()) {
869 throw new IllegalArgumentException("File descriptors passed in Intent");
870 }
871 boolean componentSpecified = intent.getComponent() != null;
872
873 // Don't modify the client's object!
874 intent = new Intent(intent);
875
876 // Collect information about the target of the Intent.
877 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700878 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700879
Craig Mautnere0a38842013-12-16 16:14:02 -0800880 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700881 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800882 if (container != null && container.mParentActivity != null &&
883 container.mParentActivity.state != ActivityState.RESUMED) {
884 // Cannot start a child activity if the parent is not resumed.
885 return ActivityManager.START_CANCELED;
886 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700887 final int realCallingPid = Binder.getCallingPid();
888 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700889 int callingPid;
890 if (callingUid >= 0) {
891 callingPid = -1;
892 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700893 callingPid = realCallingPid;
894 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700895 } else {
896 callingPid = callingUid = -1;
897 }
898
Craig Mautnere0a38842013-12-16 16:14:02 -0800899 final ActivityStack stack;
900 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800901 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800902 } else {
903 stack = container.mStack;
904 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800905 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700906 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700907 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700908
909 final long origId = Binder.clearCallingIdentity();
910
911 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800912 (aInfo.applicationInfo.privateFlags
913 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700914 // This may be a heavy-weight process! Check to see if we already
915 // have another, different heavy-weight process running.
916 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
917 if (mService.mHeavyWeightProcess != null &&
918 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
919 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700920 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700921 if (caller != null) {
922 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
923 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700924 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700925 } else {
926 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700927 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700928 + intent.toString());
929 ActivityOptions.abort(options);
930 return ActivityManager.START_PERMISSION_DENIED;
931 }
932 }
933
934 IIntentSender target = mService.getIntentSenderLocked(
935 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700936 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700937 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
938 | PendingIntent.FLAG_ONE_SHOT, null);
939
940 Intent newIntent = new Intent();
941 if (requestCode >= 0) {
942 // Caller is requesting a result.
943 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
944 }
945 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
946 new IntentSender(target));
947 if (mService.mHeavyWeightProcess.activities.size() > 0) {
948 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
949 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
950 hist.packageName);
951 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
952 hist.task.taskId);
953 }
954 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
955 aInfo.packageName);
956 newIntent.setFlags(intent.getFlags());
957 newIntent.setClassName("android",
958 HeavyWeightSwitcherActivity.class.getName());
959 intent = newIntent;
960 resolvedType = null;
961 caller = null;
962 callingUid = Binder.getCallingUid();
963 callingPid = Binder.getCallingPid();
964 componentSpecified = true;
965 try {
966 ResolveInfo rInfo =
967 AppGlobals.getPackageManager().resolveIntent(
968 intent, null,
969 PackageManager.MATCH_DEFAULT_ONLY
970 | ActivityManagerService.STOCK_PM_FLAGS, userId);
971 aInfo = rInfo != null ? rInfo.activityInfo : null;
972 aInfo = mService.getActivityInfoForUser(aInfo, userId);
973 } catch (RemoteException e) {
974 aInfo = null;
975 }
976 }
977 }
978 }
979
Dianne Hackborn91097de2014-04-04 18:02:06 -0700980 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
981 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700982 requestCode, callingPid, callingUid, callingPackage,
983 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700984 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985
Craig Mautner85c11a82014-07-17 12:38:18 -0700986 Binder.restoreCallingIdentity(origId);
987
Craig Mautnerde4ef022013-04-07 19:01:33 -0700988 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700989 // If the caller also wants to switch to a new configuration,
990 // do so now. This allows a clean switch, as we are waiting
991 // for the current activity to pause (so we will not destroy
992 // it), and have not yet started the next activity.
993 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
994 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700995 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700996 if (DEBUG_CONFIGURATION) Slog.v(TAG,
997 "Updating to new configuration after starting activity.");
998 mService.updateConfigurationLocked(config, null, false, false);
999 }
1000
Craig Mautner23ac33b2013-04-01 16:26:35 -07001001 if (outResult != null) {
1002 outResult.result = res;
1003 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001004 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001005 do {
1006 try {
1007 mService.wait();
1008 } catch (InterruptedException e) {
1009 }
1010 } while (!outResult.timeout && outResult.who == null);
1011 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001012 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001013 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001014 outResult.timeout = false;
1015 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1016 outResult.totalTime = 0;
1017 outResult.thisTime = 0;
1018 } else {
1019 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001020 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001021 do {
1022 try {
1023 mService.wait();
1024 } catch (InterruptedException e) {
1025 }
1026 } while (!outResult.timeout && outResult.who == null);
1027 }
1028 }
1029 }
1030
1031 return res;
1032 }
1033 }
1034
1035 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1036 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1037 Bundle options, int userId) {
1038 if (intents == null) {
1039 throw new NullPointerException("intents is null");
1040 }
1041 if (resolvedTypes == null) {
1042 throw new NullPointerException("resolvedTypes is null");
1043 }
1044 if (intents.length != resolvedTypes.length) {
1045 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1046 }
1047
Craig Mautner23ac33b2013-04-01 16:26:35 -07001048
1049 int callingPid;
1050 if (callingUid >= 0) {
1051 callingPid = -1;
1052 } else if (caller == null) {
1053 callingPid = Binder.getCallingPid();
1054 callingUid = Binder.getCallingUid();
1055 } else {
1056 callingPid = callingUid = -1;
1057 }
1058 final long origId = Binder.clearCallingIdentity();
1059 try {
1060 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001061 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001062 for (int i=0; i<intents.length; i++) {
1063 Intent intent = intents[i];
1064 if (intent == null) {
1065 continue;
1066 }
1067
1068 // Refuse possible leaked file descriptors
1069 if (intent != null && intent.hasFileDescriptors()) {
1070 throw new IllegalArgumentException("File descriptors passed in Intent");
1071 }
1072
1073 boolean componentSpecified = intent.getComponent() != null;
1074
1075 // Don't modify the client's object!
1076 intent = new Intent(intent);
1077
1078 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001079 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001080 // TODO: New, check if this is correct
1081 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1082
1083 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001084 (aInfo.applicationInfo.privateFlags
1085 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001086 throw new IllegalArgumentException(
1087 "FLAG_CANT_SAVE_STATE not supported here");
1088 }
1089
1090 Bundle theseOptions;
1091 if (options != null && i == intents.length-1) {
1092 theseOptions = options;
1093 } else {
1094 theseOptions = null;
1095 }
Craig Mautner6170f732013-04-02 13:05:23 -07001096 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001097 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1098 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001099 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001100 if (res < 0) {
1101 return res;
1102 }
1103
1104 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1105 }
1106 }
1107 } finally {
1108 Binder.restoreCallingIdentity(origId);
1109 }
1110
1111 return ActivityManager.START_SUCCESS;
1112 }
1113
Craig Mautner2420ead2013-04-01 17:13:20 -07001114 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001115 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001116 throws RemoteException {
1117
1118 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001119 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001120 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001121
1122 // schedule launch ticks to collect information about slow apps.
1123 r.startLaunchTickingLocked();
1124
1125 // Have the window manager re-evaluate the orientation of
1126 // the screen based on the new activity order. Note that
1127 // as a result of this, it can call back into the activity
1128 // manager with a new orientation. We don't care about that,
1129 // because the activity is not currently running so we are
1130 // just restarting it anyway.
1131 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001132 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001133 mService.mConfiguration,
1134 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1135 mService.updateConfigurationLocked(config, r, false, false);
1136 }
1137
1138 r.app = app;
1139 app.waitingToKill = null;
1140 r.launchCount++;
1141 r.lastLaunchTime = SystemClock.uptimeMillis();
1142
1143 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1144
1145 int idx = app.activities.indexOf(r);
1146 if (idx < 0) {
1147 app.activities.add(r);
1148 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001149 mService.updateLruProcessLocked(app, true, null);
1150 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001151
1152 final ActivityStack stack = r.task.stack;
1153 try {
1154 if (app.thread == null) {
1155 throw new RemoteException();
1156 }
1157 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001158 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001159 if (andResume) {
1160 results = r.results;
1161 newIntents = r.newIntents;
1162 }
1163 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1164 + " icicle=" + r.icicle
1165 + " with results=" + results + " newIntents=" + newIntents
1166 + " andResume=" + andResume);
1167 if (andResume) {
1168 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1169 r.userId, System.identityHashCode(r),
1170 r.task.taskId, r.shortComponentName);
1171 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001172 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001173 // Home process is the root process of the task.
1174 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001175 }
1176 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1177 r.sleeping = false;
1178 r.forceNewConfig = false;
1179 mService.showAskCompatModeDialogLocked(r);
1180 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1181 String profileFile = null;
1182 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001183 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1184 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1185 mService.mProfileProc = app;
1186 profileFile = mService.mProfileFile;
1187 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001188 }
1189 }
1190 app.hasShownUi = true;
1191 app.pendingUiClean = true;
1192 if (profileFd != null) {
1193 try {
1194 profileFd = profileFd.dup();
1195 } catch (IOException e) {
1196 if (profileFd != null) {
1197 try {
1198 profileFd.close();
1199 } catch (IOException o) {
1200 }
1201 profileFd = null;
1202 }
1203 }
1204 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001205
Jeff Hao1b012d32014-08-20 10:35:34 -07001206 ProfilerInfo profilerInfo = profileFile != null
1207 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1208 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001209 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001210 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001211 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001212 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1213 r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1214 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001215
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001216 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001217 // This may be a heavy-weight process! Note that the package
1218 // manager will ensure that only activity can run in the main
1219 // process of the .apk, which is the only thing that will be
1220 // considered heavy-weight.
1221 if (app.processName.equals(app.info.packageName)) {
1222 if (mService.mHeavyWeightProcess != null
1223 && mService.mHeavyWeightProcess != app) {
1224 Slog.w(TAG, "Starting new heavy weight process " + app
1225 + " when already running "
1226 + mService.mHeavyWeightProcess);
1227 }
1228 mService.mHeavyWeightProcess = app;
1229 Message msg = mService.mHandler.obtainMessage(
1230 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1231 msg.obj = r;
1232 mService.mHandler.sendMessage(msg);
1233 }
1234 }
1235
1236 } catch (RemoteException e) {
1237 if (r.launchFailed) {
1238 // This is the second time we failed -- finish activity
1239 // and give up.
1240 Slog.e(TAG, "Second failure launching "
1241 + r.intent.getComponent().flattenToShortString()
1242 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001243 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001244 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1245 "2nd-crash", false);
1246 return false;
1247 }
1248
1249 // This is the first time we failed -- restart process and
1250 // retry.
1251 app.activities.remove(r);
1252 throw e;
1253 }
1254
1255 r.launchFailed = false;
1256 if (stack.updateLRUListLocked(r)) {
1257 Slog.w(TAG, "Activity " + r
1258 + " being launched, but already in LRU list");
1259 }
1260
1261 if (andResume) {
1262 // As part of the process of launching, ActivityThread also performs
1263 // a resume.
1264 stack.minimalResumeActivityLocked(r);
1265 } else {
1266 // This activity is not starting in the resumed state... which
1267 // should look like we asked it to pause+stop (but remain visible),
1268 // and it has done so and reported back the current icicle and
1269 // other state.
1270 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1271 + " (starting in stopped state)");
1272 r.state = ActivityState.STOPPED;
1273 r.stopped = true;
1274 }
1275
1276 // Launch the new version setup screen if needed. We do this -after-
1277 // launching the initial activity (that is, home), so that it can have
1278 // a chance to initialize itself while in the background, making the
1279 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001280 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001281 mService.startSetupActivityLocked();
1282 }
1283
Dianne Hackborn465fa392014-09-14 14:21:18 -07001284 // Update any services we are bound to that might care about whether
1285 // their client may have activities.
1286 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1287
Craig Mautner2420ead2013-04-01 17:13:20 -07001288 return true;
1289 }
1290
Craig Mautnere79d42682013-04-01 19:01:53 -07001291 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001292 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001293 // Is this activity's application already running?
1294 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001295 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001296
1297 r.task.stack.setLaunchTime(r);
1298
1299 if (app != null && app.thread != null) {
1300 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001301 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1302 || !"android".equals(r.info.packageName)) {
1303 // Don't add this if it is a platform component that is marked
1304 // to run in multiple processes, because this is actually
1305 // part of the framework so doesn't make sense to track as a
1306 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001307 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1308 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001309 }
George Mount2c92c972014-03-20 09:38:23 -07001310 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001311 return;
1312 } catch (RemoteException e) {
1313 Slog.w(TAG, "Exception when starting activity "
1314 + r.intent.getComponent().flattenToShortString(), e);
1315 }
1316
1317 // If a dead object exception was thrown -- fall through to
1318 // restart the application.
1319 }
1320
1321 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001322 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001323 }
1324
Craig Mautner6170f732013-04-02 13:05:23 -07001325 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001326 Intent intent, String resolvedType, ActivityInfo aInfo,
1327 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1328 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001329 int callingPid, int callingUid, String callingPackage,
1330 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001331 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1332 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001333 int err = ActivityManager.START_SUCCESS;
1334
1335 ProcessRecord callerApp = null;
1336 if (caller != null) {
1337 callerApp = mService.getRecordForAppLocked(caller);
1338 if (callerApp != null) {
1339 callingPid = callerApp.pid;
1340 callingUid = callerApp.info.uid;
1341 } else {
1342 Slog.w(TAG, "Unable to find app for caller " + caller
1343 + " (pid=" + callingPid + ") when starting: "
1344 + intent.toString());
1345 err = ActivityManager.START_PERMISSION_DENIED;
1346 }
1347 }
1348
1349 if (err == ActivityManager.START_SUCCESS) {
1350 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1351 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001352 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001353 + " on display " + (container == null ? (mFocusedStack == null ?
1354 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1355 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1356 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001357 }
1358
1359 ActivityRecord sourceRecord = null;
1360 ActivityRecord resultRecord = null;
1361 if (resultTo != null) {
1362 sourceRecord = isInAnyStackLocked(resultTo);
1363 if (DEBUG_RESULTS) Slog.v(
1364 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1365 if (sourceRecord != null) {
1366 if (requestCode >= 0 && !sourceRecord.finishing) {
1367 resultRecord = sourceRecord;
1368 }
1369 }
1370 }
Craig Mautner6170f732013-04-02 13:05:23 -07001371
Dianne Hackborn91097de2014-04-04 18:02:06 -07001372 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001373
Dianne Hackborn95465202014-09-15 16:21:55 -07001374 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001375 // Transfer the result target from the source activity to the new
1376 // one being started, including any failures.
1377 if (requestCode >= 0) {
1378 ActivityOptions.abort(options);
1379 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1380 }
1381 resultRecord = sourceRecord.resultTo;
1382 resultWho = sourceRecord.resultWho;
1383 requestCode = sourceRecord.requestCode;
1384 sourceRecord.resultTo = null;
1385 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001386 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001387 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001388 if (sourceRecord.launchedFromUid == callingUid) {
1389 // The new activity is being launched from the same uid as the previous
1390 // activity in the flow, and asking to forward its result back to the
1391 // previous. In this case the activity is serving as a trampoline between
1392 // the two, so we also want to update its launchedFromPackage to be the
1393 // same as the previous activity. Note that this is safe, since we know
1394 // these two packages come from the same uid; the caller could just as
1395 // well have supplied that same package name itself. This specifially
1396 // deals with the case of an intent picker/chooser being launched in the app
1397 // flow to redirect to an activity picked by the user, where we want the final
1398 // activity to consider it to have been launched by the previous app activity.
1399 callingPackage = sourceRecord.launchedFromPackage;
1400 }
Craig Mautner6170f732013-04-02 13:05:23 -07001401 }
1402
1403 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1404 // We couldn't find a class that can handle the given Intent.
1405 // That's the end of that!
1406 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1407 }
1408
1409 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1410 // We couldn't find the specific class specified in the Intent.
1411 // Also the end of the line.
1412 err = ActivityManager.START_CLASS_NOT_FOUND;
1413 }
1414
Dianne Hackborn91097de2014-04-04 18:02:06 -07001415 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1416 && sourceRecord.task.voiceSession != null) {
1417 // If this activity is being launched as part of a voice session, we need
1418 // to ensure that it is safe to do so. If the upcoming activity will also
1419 // be part of the voice session, we can only launch it if it has explicitly
1420 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001421 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001422 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1423 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001424 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1425 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001426 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1427 }
1428 } catch (RemoteException e) {
1429 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1430 }
1431 }
1432 }
1433
1434 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1435 // If the caller is starting a new voice session, just make sure the target
1436 // is actually allowing it to run this way.
1437 try {
1438 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1439 intent, resolvedType)) {
1440 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1441 }
1442 } catch (RemoteException e) {
1443 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1444 }
1445 }
1446
louis_changcd5d1982014-08-01 10:09:08 +08001447 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1448
Craig Mautner6170f732013-04-02 13:05:23 -07001449 if (err != ActivityManager.START_SUCCESS) {
1450 if (resultRecord != null) {
1451 resultStack.sendActivityResultLocked(-1,
1452 resultRecord, resultWho, requestCode,
1453 Activity.RESULT_CANCELED, null);
1454 }
Craig Mautner6170f732013-04-02 13:05:23 -07001455 ActivityOptions.abort(options);
1456 return err;
1457 }
1458
1459 final int startAnyPerm = mService.checkPermission(
1460 START_ANY_ACTIVITY, callingPid, callingUid);
1461 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1462 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1463 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1464 if (resultRecord != null) {
1465 resultStack.sendActivityResultLocked(-1,
1466 resultRecord, resultWho, requestCode,
1467 Activity.RESULT_CANCELED, null);
1468 }
Craig Mautner6170f732013-04-02 13:05:23 -07001469 String msg;
1470 if (!aInfo.exported) {
1471 msg = "Permission Denial: starting " + intent.toString()
1472 + " from " + callerApp + " (pid=" + callingPid
1473 + ", uid=" + callingUid + ")"
1474 + " not exported from uid " + aInfo.applicationInfo.uid;
1475 } else {
1476 msg = "Permission Denial: starting " + intent.toString()
1477 + " from " + callerApp + " (pid=" + callingPid
1478 + ", uid=" + callingUid + ")"
1479 + " requires " + aInfo.permission;
1480 }
1481 Slog.w(TAG, msg);
1482 throw new SecurityException(msg);
1483 }
1484
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001485 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001486 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001487
Craig Mautner6170f732013-04-02 13:05:23 -07001488 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001489 try {
1490 // The Intent we give to the watcher has the extra data
1491 // stripped off, since it can contain private information.
1492 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001493 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001494 aInfo.applicationInfo.packageName);
1495 } catch (RemoteException e) {
1496 mService.mController = null;
1497 }
Ben Gruver5e207332013-04-03 17:41:37 -07001498 }
Craig Mautner6170f732013-04-02 13:05:23 -07001499
Ben Gruver5e207332013-04-03 17:41:37 -07001500 if (abort) {
1501 if (resultRecord != null) {
1502 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001503 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001504 }
Ben Gruver5e207332013-04-03 17:41:37 -07001505 // We pretend to the caller that it was really started, but
1506 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001507 ActivityOptions.abort(options);
1508 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001509 }
1510
1511 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001512 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001513 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001514 if (outActivity != null) {
1515 outActivity[0] = r;
1516 }
1517
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001518 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001519 if (voiceSession == null && (stack.mResumedActivity == null
1520 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001521 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1522 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001523 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001524 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001525 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001526 ActivityOptions.abort(options);
1527 return ActivityManager.START_SWITCHES_CANCELED;
1528 }
1529 }
1530
1531 if (mService.mDidAppSwitch) {
1532 // This is the second allowed switch since we stopped switches,
1533 // so now just generally allow switches. Use case: user presses
1534 // home (switches disabled, switch to home, mDidAppSwitch now true);
1535 // user taps a home icon (coming from home so allowed, we hit here
1536 // and now allow anyone to switch again).
1537 mService.mAppSwitchesAllowedTime = 0;
1538 } else {
1539 mService.mDidAppSwitch = true;
1540 }
1541
Craig Mautneree36c772014-07-16 14:56:05 -07001542 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001543
Dianne Hackborn91097de2014-04-04 18:02:06 -07001544 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001545 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001546
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001547 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001548 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001549 // activity start, but we are not actually doing an activity
1550 // switch... just dismiss the keyguard now, because we
1551 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001552 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001553 }
1554 return err;
1555 }
1556
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001557 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001558 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001559
1560 // On leanback only devices we should keep all activities in the same stack.
1561 if (!mLeanbackOnlyDevice &&
1562 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001563
1564 ActivityStack stack;
1565
Craig Mautnerac6f8432013-07-17 13:24:59 -07001566 if (task != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001567 stack = task.stack;
1568 if (stack.isOnHomeDisplay()) {
1569 if (mFocusedStack != stack) {
1570 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "computeStackFocus: Setting " +
Craig Mautnere0a38842013-12-16 16:14:02 -08001571 "focused stack to r=" + r + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001572 } else {
1573 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001574 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001575 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001576 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001577 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001578 }
1579
Craig Mautnere0a38842013-12-16 16:14:02 -08001580 final ActivityContainer container = r.mInitialActivityContainer;
1581 if (container != null) {
1582 // The first time put it on the desired stack, after this put on task stack.
1583 r.mInitialActivityContainer = null;
1584 return container.mStack;
1585 }
1586
Craig Mautner1b4bf852014-05-26 15:06:32 -07001587 if (mFocusedStack != mHomeStack && (!newTask ||
1588 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001589 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001590 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001591 return mFocusedStack;
1592 }
1593
Craig Mautnere0a38842013-12-16 16:14:02 -08001594 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1595 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001596 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001597 if (!stack.isHomeStack()) {
1598 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001599 "computeStackFocus: Setting focused stack=" + stack);
1600 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001601 }
1602 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001603
Craig Mautner4a1cb222013-12-04 16:14:06 -08001604 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001605 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
1606 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "computeStackFocus: New stack r=" + r +
1607 " stackId=" + stack.mStackId);
1608 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001609 }
1610 return mHomeStack;
1611 }
1612
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001613 boolean setFocusedStack(ActivityRecord r, String reason) {
1614 if (r == null) {
1615 // Not sure what you are trying to do, but it is not going to work...
1616 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001617 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001618 final TaskRecord task = r.task;
1619 if (task == null || task.stack == null) {
1620 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1621 return false;
1622 }
1623 task.stack.moveToFront(reason);
1624 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001625 }
1626
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001627 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001628 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001629 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001630 final Intent intent = r.intent;
1631 final int callingUid = r.launchedFromUid;
1632
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001633 // In some flows in to this function, we retrieve the task record and hold on to it
1634 // without a lock before calling back in to here... so the task at this point may
1635 // not actually be in recents. Check for that, and if it isn't in recents just
1636 // consider it invalid.
1637 if (inTask != null && !inTask.inRecents) {
1638 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1639 inTask = null;
1640 }
1641
Craig Mautnerad400af2014-08-13 16:41:36 -07001642 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001643 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1644 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001645
Craig Mautnera228ae92014-07-09 05:44:55 -07001646 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001647 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001648 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001649 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1650 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1651 "\"singleInstance\" or \"singleTask\"");
1652 launchFlags &=
1653 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1654 } else {
1655 switch (r.info.documentLaunchMode) {
1656 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1657 break;
1658 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1659 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1660 break;
1661 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1662 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1663 break;
1664 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1665 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1666 break;
1667 }
1668 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001669
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001670 final boolean launchTaskBehind = r.mLaunchTaskBehind
1671 && !launchSingleTask && !launchSingleInstance
1672 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001673
1674 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1675 // For whatever reason this activity is being launched into a new
1676 // task... yet the caller has requested a result back. Well, that
1677 // is pretty messed up, so instead immediately send back a cancel
1678 // and let the new task continue launched as normal without a
1679 // dependency on its originator.
1680 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1681 r.resultTo.task.stack.sendActivityResultLocked(-1,
1682 r.resultTo, r.resultWho, r.requestCode,
1683 Activity.RESULT_CANCELED, null);
1684 r.resultTo = null;
1685 }
1686
1687 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1688 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1689 }
1690
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001691 // If we are actually going to launch in to a new task, there are some cases where
1692 // we further want to do multiple task.
1693 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1694 if (launchTaskBehind
1695 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1696 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1697 }
1698 }
1699
Craig Mautner8849a5e2013-04-02 16:41:03 -07001700 // We'll invoke onUserLeaving before onPause only if the launching
1701 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001702 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001703 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001704
1705 // If the caller has asked not to resume at this point, we make note
1706 // of this in the record so that we can skip it when trying to find
1707 // the top running activity.
1708 if (!doResume) {
1709 r.delayedResume = true;
1710 }
1711
Craig Mautnera254cd72014-05-25 16:47:39 -07001712 ActivityRecord notTop =
1713 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001714
1715 // If the onlyIfNeeded flag is set, then we can do this if the activity
1716 // being launched is the same as the one making the call... or, as
1717 // a special case, if we do not know the caller then we count the
1718 // current top activity as the caller.
1719 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1720 ActivityRecord checkedCaller = sourceRecord;
1721 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001722 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001723 }
1724 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1725 // Caller is not the same as launcher, so always needed.
1726 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1727 }
1728 }
1729
Craig Mautner8849a5e2013-04-02 16:41:03 -07001730 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001731 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001732
1733 // If the caller is not coming from another activity, but has given us an
1734 // explicit task into which they would like us to launch the new activity,
1735 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001736 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1737 final Intent baseIntent = inTask.getBaseIntent();
1738 final ActivityRecord root = inTask.getRootActivity();
1739 if (baseIntent == null) {
1740 ActivityOptions.abort(options);
1741 throw new IllegalArgumentException("Launching into task without base intent: "
1742 + inTask);
1743 }
1744
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001745 // If this task is empty, then we are adding the first activity -- it
1746 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001747 if (launchSingleInstance || launchSingleTask) {
1748 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1749 ActivityOptions.abort(options);
1750 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1751 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001752 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001753 if (root != null) {
1754 ActivityOptions.abort(options);
1755 throw new IllegalArgumentException("Caller with inTask " + inTask
1756 + " has root " + root + " but target is singleInstance/Task");
1757 }
1758 }
1759
1760 // If task is empty, then adopt the interesting intent launch flags in to the
1761 // activity being started.
1762 if (root == null) {
1763 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1764 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1765 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1766 launchFlags = (launchFlags&~flagsOfInterest)
1767 | (baseIntent.getFlags()&flagsOfInterest);
1768 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001769 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001770 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001771
Dianne Hackborn962d5352014-08-29 16:27:40 -07001772 // If the task is not empty and the caller is asking to start it as the root
1773 // of a new task, then we don't actually want to start this on the task. We
1774 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001775 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001776 addingToTask = false;
1777
1778 } else {
1779 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001780 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001781
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001782 reuseTask = inTask;
1783 } else {
1784 inTask = null;
1785 }
1786
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001787 if (inTask == null) {
1788 if (sourceRecord == null) {
1789 // This activity is not being started from another... in this
1790 // case we -always- start a new task.
1791 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1792 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1793 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1794 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1795 }
1796 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1797 // The original activity who is starting us is running as a single
1798 // instance... this new activity it is starting must go on its
1799 // own task.
1800 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1801 } else if (launchSingleInstance || launchSingleTask) {
1802 // The activity being started is a single instance... it always
1803 // gets launched into its own task.
1804 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1805 }
1806 }
1807
1808 ActivityInfo newTaskInfo = null;
1809 Intent newTaskIntent = null;
1810 ActivityStack sourceStack;
1811 if (sourceRecord != null) {
1812 if (sourceRecord.finishing) {
1813 // If the source is finishing, we can't further count it as our source. This
1814 // is because the task it is associated with may now be empty and on its way out,
1815 // so we don't want to blindly throw it in to that task. Instead we will take
1816 // the NEW_TASK flow and try to find a task for it. But save the task information
1817 // so it can be used when creating the new task.
1818 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1819 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1820 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1821 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1822 newTaskInfo = sourceRecord.info;
1823 newTaskIntent = sourceRecord.task.intent;
1824 }
1825 sourceRecord = null;
1826 sourceStack = null;
1827 } else {
1828 sourceStack = sourceRecord.task.stack;
1829 }
1830 } else {
1831 sourceStack = null;
1832 }
1833
1834 boolean movedHome = false;
1835 ActivityStack targetStack;
1836
1837 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001838 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001839
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001840 // We may want to try to place the new activity in to an existing task. We always
1841 // do this if the target activity is singleTask or singleInstance; we will also do
1842 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1843 // us to still place it in a new task: multi task, always doc mode, or being asked to
1844 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001845 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1846 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001847 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001848 // If bring to front is requested, and no result is requested and we have not
1849 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001850 // we can find a task that was started with this same
1851 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001852 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001853 // See if there is a task to bring to the front. If this is
1854 // a SINGLE_INSTANCE activity, there can be one and only one
1855 // instance of it in the history, and it is always in its own
1856 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001857 ActivityRecord intentActivity = !launchSingleInstance ?
1858 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001859 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001860 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001861 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001862 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001863 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1864 }
Craig Mautner29219d92013-04-16 20:19:12 -07001865 if (r.task == null) {
1866 r.task = intentActivity.task;
1867 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001868 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001869 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001870 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1871 + " from " + intentActivity);
Craig Mautner299f9602015-01-26 09:47:33 -08001872 targetStack.moveToFront("intentActivityFound");
Craig Mautner8849a5e2013-04-02 16:41:03 -07001873 if (intentActivity.task.intent == null) {
1874 // This task was started because of movement of
1875 // the activity based on affinity... now that we
1876 // are actually launching it, we can assign the
1877 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001878 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001879 }
1880 // If the target task is not in the front, then we need
1881 // to bring it to the front... except... well, with
1882 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1883 // to have the same behavior as if a new instance was
1884 // being started, which means not bringing it to the front
1885 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001886 final ActivityStack lastStack = getLastStack();
1887 ActivityRecord curTop = lastStack == null?
1888 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001889 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001890 if (curTop != null && (curTop.task != intentActivity.task ||
1891 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001892 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001893 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1894 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001895 // We really do want to push this one into the
1896 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001897 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001898 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1899 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001900 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001901 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1902 options, "bringingFoundTaskToFront");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001903 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001904 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1905 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001906 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001907 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001908 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001909 options = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001910 movedToFront = true;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001911 }
1912 }
1913 // If the caller has requested that the target task be
1914 // reset, then do so.
1915 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1916 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1917 }
1918 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1919 // We don't need to start a new activity, and
1920 // the client said not to do anything if that
1921 // is the case, so this is it! And for paranoia, make
1922 // sure we have correctly resumed the top activity.
1923 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001924 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001925
1926 // Make sure to notify Keyguard as well if we are not running an app
1927 // transition later.
1928 if (!movedToFront) {
1929 notifyActivityDrawnForKeyguard();
1930 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001931 } else {
1932 ActivityOptions.abort(options);
1933 }
1934 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1935 }
1936 if ((launchFlags &
1937 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1938 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1939 // The caller has requested to completely replace any
1940 // existing task with its new activity. Well that should
1941 // not be too hard...
1942 reuseTask = intentActivity.task;
1943 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001944 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001945 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001946 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001947 // In this situation we want to remove all activities
1948 // from the task up to the one being started. In most
1949 // cases this means we are resetting the task to its
1950 // initial state.
1951 ActivityRecord top =
1952 intentActivity.task.performClearTaskLocked(r, launchFlags);
1953 if (top != null) {
1954 if (top.frontOfTask) {
1955 // Activity aliases may mean we use different
1956 // intents for the top activity, so make sure
1957 // the task now has the identity of the new
1958 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001959 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001960 }
1961 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1962 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001963 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001964 } else {
1965 // A special case: we need to
1966 // start the activity because it is not currently
1967 // running, and the caller has asked to clear the
1968 // current task to have this activity at the top.
1969 addingToTask = true;
1970 // Now pretend like this activity is being started
1971 // by the top of its task, so it is put in the
1972 // right place.
1973 sourceRecord = intentActivity;
1974 }
1975 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1976 // In this case the top activity on the task is the
1977 // same as the one being launched, so we take that
1978 // as a request to bring the task to the foreground.
1979 // If the top activity in the task is the root
1980 // activity, deliver this new intent to it if it
1981 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001982 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001983 && intentActivity.realActivity.equals(r.realActivity)) {
1984 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1985 intentActivity.task);
1986 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001987 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001988 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001989 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
1990 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001991 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1992 // In this case we are launching the root activity
1993 // of the task, but with a different intent. We
1994 // should start a new instance on top.
1995 addingToTask = true;
1996 sourceRecord = intentActivity;
1997 }
1998 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1999 // In this case an activity is being launched in to an
2000 // existing task, without resetting that task. This
2001 // is typically the situation of launching an activity
2002 // from a notification or shortcut. We want to place
2003 // the new activity on top of the current task.
2004 addingToTask = true;
2005 sourceRecord = intentActivity;
2006 } else if (!intentActivity.task.rootWasReset) {
2007 // In this case we are launching in to an existing task
2008 // that has not yet been started from its front door.
2009 // The current task has been brought to the front.
2010 // Ideally, we'd probably like to place this new task
2011 // at the bottom of its stack, but that's a little hard
2012 // to do with the current organization of the code so
2013 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002014 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002015 }
2016 if (!addingToTask && reuseTask == null) {
2017 // We didn't do anything... but it was needed (a.k.a., client
2018 // don't use that intent!) And for paranoia, make
2019 // sure we have correctly resumed the top activity.
2020 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002021 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002022 if (!movedToFront) {
2023 // Make sure to notify Keyguard as well if we are not running an app
2024 // transition later.
2025 notifyActivityDrawnForKeyguard();
2026 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002027 } else {
2028 ActivityOptions.abort(options);
2029 }
2030 return ActivityManager.START_TASK_TO_FRONT;
2031 }
2032 }
2033 }
2034 }
2035
2036 //String uri = r.intent.toURI();
2037 //Intent intent2 = new Intent(uri);
2038 //Slog.i(TAG, "Given intent: " + r.intent);
2039 //Slog.i(TAG, "URI is: " + uri);
2040 //Slog.i(TAG, "To intent: " + intent2);
2041
2042 if (r.packageName != null) {
2043 // If the activity being launched is the same as the one currently
2044 // at the top, then we need to check if it should only be launched
2045 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002046 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002047 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002048 if (top != null && r.resultTo == null) {
2049 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2050 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002051 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002052 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002053 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2054 top.task);
2055 // For paranoia, make sure we have correctly
2056 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002057 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002058 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002059 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002060 }
2061 ActivityOptions.abort(options);
2062 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2063 // We don't need to start a new activity, and
2064 // the client said not to do anything if that
2065 // is the case, so this is it!
2066 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2067 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002068 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002069 return ActivityManager.START_DELIVERED_TO_TOP;
2070 }
2071 }
2072 }
2073 }
2074
2075 } else {
2076 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002077 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2078 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002079 }
2080 ActivityOptions.abort(options);
2081 return ActivityManager.START_CLASS_NOT_FOUND;
2082 }
2083
2084 boolean newTask = false;
2085 boolean keepCurTransition = false;
2086
Craig Mautnerbb742462014-07-07 15:28:55 -07002087 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002088 sourceRecord.task : null;
2089
Craig Mautner8849a5e2013-04-02 16:41:03 -07002090 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002091 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002092 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002093 if (isLockTaskModeViolation(reuseTask)) {
2094 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2095 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2096 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002097 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002098 targetStack = computeStackFocus(r, newTask);
2099 targetStack.moveToFront("startingNewTask");
2100
Craig Mautner8849a5e2013-04-02 16:41:03 -07002101 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002102 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2103 newTaskInfo != null ? newTaskInfo : r.info,
2104 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002105 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2106 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002107 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2108 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002109 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002110 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002111 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002112 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002113 if ((launchFlags &
2114 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2115 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2116 // Caller wants to appear on home activity, so before starting
2117 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002118 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002119 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002120 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002122 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002123 if (isLockTaskModeViolation(sourceTask)) {
2124 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2125 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2126 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002127 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002128 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002129 final TaskRecord topTask = targetStack.topTask();
2130 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002131 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2132 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002133 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002134 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002135 // In this case, we are adding the activity to an existing
2136 // task, but the caller has asked to clear that task if the
2137 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002138 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002139 keepCurTransition = true;
2140 if (top != null) {
2141 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002142 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002143 // For paranoia, make sure we have correctly
2144 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002145 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002146 if (doResume) {
2147 targetStack.resumeTopActivityLocked(null);
2148 }
2149 ActivityOptions.abort(options);
2150 return ActivityManager.START_DELIVERED_TO_TOP;
2151 }
2152 } else if (!addingToTask &&
2153 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2154 // In this case, we are launching an activity in our own task
2155 // that may already be running somewhere in the history, and
2156 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002157 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002158 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002159 final TaskRecord task = top.task;
2160 task.moveActivityToFrontLocked(top);
2161 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002162 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002163 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002164 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002165 if (doResume) {
2166 targetStack.resumeTopActivityLocked(null);
2167 }
2168 return ActivityManager.START_DELIVERED_TO_TOP;
2169 }
2170 }
2171 // An existing activity is starting this new activity, so we want
2172 // to keep the new one in the same task as the one that is starting
2173 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002174 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002175 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002176 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002177
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002178 } else if (inTask != null) {
2179 // The calling is asking that the new activity be started in an explicit
2180 // task it has provided to us.
2181 if (isLockTaskModeViolation(inTask)) {
2182 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2183 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2184 }
2185 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002186 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002187
2188 // Check whether we should actually launch the new activity in to the task,
2189 // or just reuse the current activity on top.
2190 ActivityRecord top = inTask.getTopActivity();
2191 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2192 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2193 || launchSingleTop || launchSingleTask) {
2194 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2195 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2196 // We don't need to start a new activity, and
2197 // the client said not to do anything if that
2198 // is the case, so this is it!
2199 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2200 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002201 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002202 return ActivityManager.START_DELIVERED_TO_TOP;
2203 }
2204 }
2205
Dianne Hackborn962d5352014-08-29 16:27:40 -07002206 if (!addingToTask) {
2207 // We don't actually want to have this activity added to the task, so just
2208 // stop here but still tell the caller that we consumed the intent.
2209 ActivityOptions.abort(options);
2210 return ActivityManager.START_TASK_TO_FRONT;
2211 }
2212
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002213 r.setTask(inTask, null);
2214 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2215 + " in explicit task " + r.task);
2216
Craig Mautner8849a5e2013-04-02 16:41:03 -07002217 } else {
2218 // This not being started from an existing activity, and not part
2219 // of a new task... just put it in the top task, though these days
2220 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002221 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002222 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002223 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002224 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002225 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002226 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002227 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2228 + " in new guessed " + r.task);
2229 }
2230
2231 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002232 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002233
Craig Mautner76e2a762014-06-24 09:05:43 -07002234 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2235 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2236 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002237 if (newTask) {
2238 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2239 }
2240 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002241 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002242 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002243 if (!launchTaskBehind) {
2244 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002245 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002246 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002247 return ActivityManager.START_SUCCESS;
2248 }
2249
Craig Mautneree36c772014-07-16 14:56:05 -07002250 final void doPendingActivityLaunchesLocked(boolean doResume) {
2251 while (!mPendingActivityLaunches.isEmpty()) {
2252 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002253 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002254 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002255 }
2256 }
2257
Craig Mautner7f13ed32014-07-28 14:00:35 -07002258 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002259 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2260 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002261 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002262 mPendingActivityLaunches.remove(palNdx);
2263 }
2264 }
2265 }
2266
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002267 void acquireLaunchWakelock() {
2268 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2269 throw new IllegalStateException("Calling must be system uid");
2270 }
2271 mLaunchingActivity.acquire();
2272 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2273 // To be safe, don't allow the wake lock to be held for too long.
2274 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2275 }
2276 }
2277
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002278 /**
2279 * Called when the frontmost task is idle.
2280 * @return the state of mService.mBooting before this was called.
2281 */
2282 private boolean checkFinishBootingLocked() {
2283 final boolean booting = mService.mBooting;
2284 boolean enableScreen = false;
2285 mService.mBooting = false;
2286 if (!mService.mBooted) {
2287 mService.mBooted = true;
2288 enableScreen = true;
2289 }
2290 if (booting || enableScreen) {
2291 mService.postFinishBooting(booting, enableScreen);
2292 }
2293 return booting;
2294 }
2295
Craig Mautnerf3333272013-04-22 10:55:53 -07002296 // Checked.
2297 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2298 Configuration config) {
2299 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2300
Craig Mautnerf3333272013-04-22 10:55:53 -07002301 ArrayList<ActivityRecord> stops = null;
2302 ArrayList<ActivityRecord> finishes = null;
2303 ArrayList<UserStartedState> startingUsers = null;
2304 int NS = 0;
2305 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002306 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002307 boolean activityRemoved = false;
2308
2309 ActivityRecord r = ActivityRecord.forToken(token);
2310 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002311 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2312 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002313 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2314 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002315 if (fromTimeout) {
2316 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002317 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002318
2319 // This is a hack to semi-deal with a race condition
2320 // in the client where it can be constructed with a
2321 // newer configuration from when we asked it to launch.
2322 // We'll update with whatever configuration it now says
2323 // it used to launch.
2324 if (config != null) {
2325 r.configuration = config;
2326 }
2327
2328 // We are now idle. If someone is waiting for a thumbnail from
2329 // us, we can now deliver.
2330 r.idle = true;
2331
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002332 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002333 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002334 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002335 }
2336 }
2337
2338 if (allResumedActivitiesIdle()) {
2339 if (r != null) {
2340 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002341 }
2342
2343 if (mLaunchingActivity.isHeld()) {
2344 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2345 if (VALIDATE_WAKE_LOCK_CALLER &&
2346 Binder.getCallingUid() != Process.myUid()) {
2347 throw new IllegalStateException("Calling must be system uid");
2348 }
2349 mLaunchingActivity.release();
2350 }
2351 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002352 }
2353
2354 // Atomically retrieve all of the other things to do.
2355 stops = processStoppingActivitiesLocked(true);
2356 NS = stops != null ? stops.size() : 0;
2357 if ((NF=mFinishingActivities.size()) > 0) {
2358 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2359 mFinishingActivities.clear();
2360 }
2361
Craig Mautnerf3333272013-04-22 10:55:53 -07002362 if (mStartingUsers.size() > 0) {
2363 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2364 mStartingUsers.clear();
2365 }
2366
Craig Mautnerf3333272013-04-22 10:55:53 -07002367 // Stop any activities that are scheduled to do so but have been
2368 // waiting for the next one to start.
2369 for (int i = 0; i < NS; i++) {
2370 r = stops.get(i);
2371 final ActivityStack stack = r.task.stack;
2372 if (r.finishing) {
2373 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2374 } else {
2375 stack.stopActivityLocked(r);
2376 }
2377 }
2378
2379 // Finish any activities that are scheduled to do so but have been
2380 // waiting for the next one to start.
2381 for (int i = 0; i < NF; i++) {
2382 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002383 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002384 }
2385
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002386 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002387 // Complete user switch
2388 if (startingUsers != null) {
2389 for (int i = 0; i < startingUsers.size(); i++) {
2390 mService.finishUserSwitch(startingUsers.get(i));
2391 }
2392 }
2393 // Complete starting up of background users
2394 if (mStartingBackgroundUsers.size() > 0) {
2395 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2396 mStartingBackgroundUsers.clear();
2397 for (int i = 0; i < startingUsers.size(); i++) {
2398 mService.finishUserBoot(startingUsers.get(i));
2399 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002400 }
2401 }
2402
2403 mService.trimApplications();
2404 //dump();
2405 //mWindowManager.dump();
2406
Craig Mautnerf3333272013-04-22 10:55:53 -07002407 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002408 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002409 }
2410
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002411 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002412 }
2413
Craig Mautner8e569572013-10-11 17:36:59 -07002414 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002415 boolean hasVisibleActivities = 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 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2419 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2420 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002421 }
Craig Mautner19091252013-10-05 00:03:53 -07002422 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002423 }
2424
2425 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002426 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2427 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002428 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2429 stacks.get(stackNdx).closeSystemDialogsLocked();
2430 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002431 }
2432 }
2433
Craig Mautner93529a42013-10-04 15:03:13 -07002434 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002435 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002436 }
2437
Craig Mautner8d341ef2013-03-26 09:03:27 -07002438 /**
2439 * @return true if some activity was finished (or would have finished if doit were true).
2440 */
2441 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2442 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002443 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2444 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002445 final int numStacks = stacks.size();
2446 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2447 final ActivityStack stack = stacks.get(stackNdx);
2448 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2449 didSomething = true;
2450 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002451 }
2452 }
2453 return didSomething;
2454 }
2455
Dianne Hackborna413dc02013-07-12 12:02:55 -07002456 void updatePreviousProcessLocked(ActivityRecord r) {
2457 // Now that this process has stopped, we may want to consider
2458 // it to be the previous app to try to keep around in case
2459 // the user wants to return to it.
2460
2461 // First, found out what is currently the foreground app, so that
2462 // we don't blow away the previous app if this activity is being
2463 // hosted by the process that is actually still the foreground.
2464 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002465 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2466 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002467 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2468 final ActivityStack stack = stacks.get(stackNdx);
2469 if (isFrontStack(stack)) {
2470 if (stack.mResumedActivity != null) {
2471 fgApp = stack.mResumedActivity.app;
2472 } else if (stack.mPausingActivity != null) {
2473 fgApp = stack.mPausingActivity.app;
2474 }
2475 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002476 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002477 }
2478 }
2479
2480 // Now set this one as the previous process, only if that really
2481 // makes sense to.
2482 if (r.app != null && fgApp != null && r.app != fgApp
2483 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002484 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002485 mService.mPreviousProcess = r.app;
2486 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2487 }
2488 }
2489
Craig Mautner05d29032013-05-03 13:40:13 -07002490 boolean resumeTopActivitiesLocked() {
2491 return resumeTopActivitiesLocked(null, null, null);
2492 }
2493
2494 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2495 Bundle targetOptions) {
2496 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002497 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002498 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002499 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002500 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002501 if (isFrontStack(targetStack)) {
2502 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2503 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002504
Craig Mautnere0a38842013-12-16 16:14:02 -08002505 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2506 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002507 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2508 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002509 if (stack == targetStack) {
2510 // Already started above.
2511 continue;
2512 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002513 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002514 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002515 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002516 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002517 }
Craig Mautner05d29032013-05-03 13:40:13 -07002518 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002519 }
2520
Todd Kennedy539db512014-12-15 09:57:55 -08002521 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002522 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2523 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002524 final int numStacks = stacks.size();
2525 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2526 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002527 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002528 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002529 }
2530 }
2531
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002532 void finishVoiceTask(IVoiceInteractionSession session) {
2533 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2534 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2535 final int numStacks = stacks.size();
2536 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2537 final ActivityStack stack = stacks.get(stackNdx);
2538 stack.finishVoiceTask(session);
2539 }
2540 }
2541 }
2542
Craig Mautner299f9602015-01-26 09:47:33 -08002543 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002544 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2545 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002546 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002547 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2548 // Caller wants the home activity moved with it. To accomplish this,
2549 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002550 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002551 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002552 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08002553 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2554 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002555 }
2556
Craig Mautner967212c2013-04-13 21:10:58 -07002557 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002558 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2559 if (activityContainer != null) {
2560 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002561 }
2562 return null;
2563 }
2564
Craig Mautner967212c2013-04-13 21:10:58 -07002565 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002566 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002567 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2568 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002569 }
2570 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002571 }
2572
Craig Mautner4a1cb222013-12-04 16:14:06 -08002573 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002574 ActivityRecord homeActivity = getHomeActivity();
2575 if (homeActivity != null) {
2576 return homeActivity.appToken;
2577 }
2578 return null;
2579 }
2580
2581 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002582 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2583 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2584 final TaskRecord task = tasks.get(taskNdx);
2585 if (task.isHomeTask()) {
2586 final ArrayList<ActivityRecord> activities = task.mActivities;
2587 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2588 final ActivityRecord r = activities.get(activityNdx);
2589 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002590 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002591 }
2592 }
2593 }
2594 }
2595 return null;
2596 }
2597
Todd Kennedyca4d8422015-01-15 15:19:22 -08002598 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002599 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002600 ActivityContainer activityContainer =
2601 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002602 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002603 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002604 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002605 return activityContainer;
2606 }
2607
Craig Mautner34b73df2014-01-12 21:11:08 -08002608 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002609 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2610 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2611 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002612 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2613 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002614 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002615 }
2616 }
2617
Craig Mautner95da1082014-02-24 17:54:35 -08002618 void deleteActivityContainer(IActivityContainer container) {
2619 ActivityContainer activityContainer = (ActivityContainer)container;
2620 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002621 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2622 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002623 final int stackId = activityContainer.mStackId;
2624 mActivityContainers.remove(stackId);
2625 mWindowManager.removeStack(stackId);
2626 }
2627 }
2628
Wale Ogunwale60454db2015-01-23 16:05:07 -08002629 void resizeStackLocked(int stackId, Rect bounds) {
2630 final ActivityStack stack = getStack(stackId);
2631 if (stack == null) {
2632 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2633 return;
2634 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002635
2636 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002637 if (r != null && !r.task.mResizeable) {
2638 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002639 return;
2640 }
2641
Wale Ogunwale60454db2015-01-23 16:05:07 -08002642 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2643 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002644 if (r != null) {
2645 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2646 // And we need to make sure at this point that all other activities
2647 // are made visible with the correct configuration.
2648 ensureActivitiesVisibleLocked(r, 0);
2649 if (!updated) {
2650 resumeTopActivitiesLocked(stack, null, null);
2651 }
2652 }
2653 }
2654 }
2655
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002656 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2657 * current task stack if it only has one entry, moving the task to a stack that matches the
2658 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2659 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2660 task.mResizeable = true;
2661 final ActivityStack currentStack = task.stack;
2662 if (currentStack.isHomeStack()) {
2663 // Can't move task off the home stack. Sorry!
2664 return;
2665 }
2666
2667 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2668 if (matchingStackId != -1) {
2669 // There is already a stack with the right bounds!
2670 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2671 // Nothing to do here. Already in the right stack...
2672 return;
2673 }
2674 // Move task to stack with matching bounds.
2675 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2676 return;
2677 }
2678
2679 if (currentStack != null && currentStack.numTasks() == 1) {
2680 // Just resize the current stack since this is the task in it.
2681 resizeStackLocked(currentStack.mStackId, bounds);
2682 return;
2683 }
2684
2685 // Create new stack and move the task to it.
2686 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2687 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2688 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2689
2690 if (newStack == null) {
2691 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2692 return;
2693 }
2694 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2695 resizeStackLocked(newStack.mStackId, bounds);
2696 }
2697
Todd Kennedy4900bf92015-01-16 16:05:14 -08002698 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002699 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2700 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002701 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002702 }
2703
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002704 ActivityContainer activityContainer = new ActivityContainer(stackId);
2705 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002706 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002707 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002708 }
2709
2710 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002711 while (true) {
2712 if (++mLastStackId <= HOME_STACK_ID) {
2713 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002714 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002715 if (getStack(mLastStackId) == null) {
2716 break;
2717 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002718 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002719 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002720 }
2721
Wale Ogunwale7de05352014-12-12 15:21:33 -08002722 private boolean restoreRecentTaskLocked(TaskRecord task) {
2723 ActivityStack stack = null;
2724 // Determine stack to restore task to.
2725 if (mLeanbackOnlyDevice) {
2726 // There is only one stack for lean back devices.
2727 stack = mHomeStack;
2728 } else {
2729 // Look for the top stack on the home display that isn't the home stack.
2730 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2731 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2732 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -08002733 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002734 stack = tmpStack;
2735 break;
2736 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002737 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002738 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002739
2740 if (stack == null) {
2741 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2742 // before an application stack is created...Go ahead and create one on the default
2743 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002744 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002745 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002746 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002747 if (DEBUG_RECENTS)
2748 Slog.v(TAG, "Created stack=" + stack + " for recents restoration.");
2749 }
2750
2751 if (stack == null) {
2752 // What does this mean??? Not sure how we would get here...
2753 if (DEBUG_RECENTS)
2754 Slog.v(TAG, "Unable to find/create stack to restore recent task=" + task);
2755 return false;
2756 }
2757
2758 stack.addTask(task, false, false);
2759 if (DEBUG_RECENTS)
2760 Slog.v(TAG, "Added restored task=" + task + " to stack=" + stack);
2761 final ArrayList<ActivityRecord> activities = task.mActivities;
2762 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2763 final ActivityRecord r = activities.get(activityNdx);
2764 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2765 r.info.screenOrientation, r.fullscreen,
2766 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2767 r.userId, r.info.configChanges, task.voiceSession != null,
2768 r.mLaunchTaskBehind);
2769 }
2770 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002771 }
2772
Craig Mautner299f9602015-01-26 09:47:33 -08002773 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002774 final TaskRecord task = anyTaskForIdLocked(taskId);
2775 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002776 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002777 return;
2778 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002779 final ActivityStack stack = getStack(stackId);
2780 if (stack == null) {
2781 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2782 return;
2783 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002784 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2785 if (task.stack != null) {
2786 task.stack.removeTask(task, "moveTaskToStack", false);
2787 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002788 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002789 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002790 }
2791
Craig Mautnerac6f8432013-07-17 13:24:59 -07002792 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002793 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002794 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2795 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002796 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2797 final ActivityStack stack = stacks.get(stackNdx);
2798 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002799 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2800 continue;
2801 }
2802 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2803 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2804 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002805 continue;
2806 }
2807 final ActivityRecord ar = stack.findTaskLocked(r);
2808 if (ar != null) {
2809 return ar;
2810 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002811 }
2812 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002813 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002814 return null;
2815 }
2816
2817 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002818 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2819 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002820 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2821 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2822 if (ar != null) {
2823 return ar;
2824 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002825 }
2826 }
2827 return null;
2828 }
2829
Craig Mautner8d341ef2013-03-26 09:03:27 -07002830 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002831 scheduleSleepTimeout();
2832 if (!mGoingToSleep.isHeld()) {
2833 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002834 if (mLaunchingActivity.isHeld()) {
2835 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2836 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002837 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002838 mLaunchingActivity.release();
2839 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002840 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002841 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002842 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002843 }
2844
2845 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002846 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002847
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002848 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002849 final long endTime = System.currentTimeMillis() + timeout;
2850 while (true) {
2851 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002852 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2853 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002854 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2855 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2856 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002857 }
2858 if (cantShutdown) {
2859 long timeRemaining = endTime - System.currentTimeMillis();
2860 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002861 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002862 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002863 } catch (InterruptedException e) {
2864 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002865 } else {
2866 Slog.w(TAG, "Activity manager shutdown timed out");
2867 timedout = true;
2868 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002869 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002870 } else {
2871 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002872 }
2873 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002874
2875 // Force checkReadyForSleep to complete.
2876 mSleepTimeout = true;
2877 checkReadyForSleepLocked();
2878
Craig Mautner8d341ef2013-03-26 09:03:27 -07002879 return timedout;
2880 }
2881
2882 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002883 removeSleepTimeouts();
2884 if (mGoingToSleep.isHeld()) {
2885 mGoingToSleep.release();
2886 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002887 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2888 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002889 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2890 final ActivityStack stack = stacks.get(stackNdx);
2891 stack.awakeFromSleepingLocked();
2892 if (isFrontStack(stack)) {
2893 resumeTopActivitiesLocked();
2894 }
Craig Mautner5314a402013-09-26 12:40:16 -07002895 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002896 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002897 mGoingToSleepActivities.clear();
2898 }
2899
2900 void activitySleptLocked(ActivityRecord r) {
2901 mGoingToSleepActivities.remove(r);
2902 checkReadyForSleepLocked();
2903 }
2904
2905 void checkReadyForSleepLocked() {
2906 if (!mService.isSleepingOrShuttingDown()) {
2907 // Do not care.
2908 return;
2909 }
2910
2911 if (!mSleepTimeout) {
2912 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002913 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2914 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002915 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2916 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2917 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002918 }
2919
2920 if (mStoppingActivities.size() > 0) {
2921 // Still need to tell some activities to stop; can't sleep yet.
2922 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2923 + mStoppingActivities.size() + " activities");
2924 scheduleIdleLocked();
2925 dontSleep = true;
2926 }
2927
2928 if (mGoingToSleepActivities.size() > 0) {
2929 // Still need to tell some activities to sleep; can't sleep yet.
2930 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2931 + mGoingToSleepActivities.size() + " activities");
2932 dontSleep = true;
2933 }
2934
2935 if (dontSleep) {
2936 return;
2937 }
2938 }
2939
Craig Mautnere0a38842013-12-16 16:14:02 -08002940 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2941 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002942 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2943 stacks.get(stackNdx).goToSleep();
2944 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002945 }
2946
2947 removeSleepTimeouts();
2948
2949 if (mGoingToSleep.isHeld()) {
2950 mGoingToSleep.release();
2951 }
2952 if (mService.mShuttingDown) {
2953 mService.notifyAll();
2954 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002955 }
2956
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002957 boolean reportResumedActivityLocked(ActivityRecord r) {
2958 final ActivityStack stack = r.task.stack;
2959 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002960 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002961 }
2962 if (allResumedActivitiesComplete()) {
2963 ensureActivitiesVisibleLocked(null, 0);
2964 mWindowManager.executeAppTransition();
2965 return true;
2966 }
2967 return false;
2968 }
2969
Craig Mautner8d341ef2013-03-26 09:03:27 -07002970 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002971 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2972 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002973 final int numStacks = stacks.size();
2974 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2975 final ActivityStack stack = stacks.get(stackNdx);
2976 stack.handleAppCrashLocked(app);
2977 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002978 }
2979 }
2980
Jose Lima4b6c6692014-08-12 17:41:12 -07002981 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002982 final ActivityStack stack = r.task.stack;
2983 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002984 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2985 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002986 return false;
2987 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002988 final boolean isVisible = stack.hasVisibleBehindActivity();
2989 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2990 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002991
2992 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002993 if (top == null || top == r || (visible == isVisible)) {
2994 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2995 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002996 return true;
2997 }
2998
2999 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003000 if (visible && top.fullscreen) {
3001 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003002 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003003 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003004 " top.app.thread=" + top.app.thread);
3005 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003006 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3007 // Only the activity set as currently visible behind should actively reset its
3008 // visible behind state.
3009 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3010 + visible + " stack.getVisibleBehindActivity()=" +
3011 stack.getVisibleBehindActivity() + " r=" + r);
3012 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003013 }
3014
Jose Lima4b6c6692014-08-12 17:41:12 -07003015 stack.setVisibleBehindActivity(visible ? r : null);
3016 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003017 // Make the activity immediately above r opaque.
3018 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3019 if (next != null) {
3020 mService.convertFromTranslucent(next.appToken);
3021 }
3022 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003023 if (top.app != null && top.app.thread != null) {
3024 // Notify the top app of the change.
3025 try {
3026 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3027 } catch (RemoteException e) {
3028 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003029 }
3030 return true;
3031 }
3032
Craig Mautnerbb742462014-07-07 15:28:55 -07003033 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3034 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3035 r.mLaunchTaskBehind = false;
3036 final TaskRecord task = r.task;
3037 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003038 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003039 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003040 mWindowManager.setAppVisibility(r.appToken, false);
3041 }
3042
3043 void scheduleLaunchTaskBehindComplete(IBinder token) {
3044 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3045 }
3046
Craig Mautnerde4ef022013-04-07 19:01:33 -07003047 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003048 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003049 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3050 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003051 final int topStackNdx = stacks.size() - 1;
3052 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3053 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003054 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003055 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003056 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003057 }
3058
3059 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003060 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3061 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003062 final int numStacks = stacks.size();
3063 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3064 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003065 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003066 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003067 }
3068 }
3069
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003070 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3071 // Examine all activities currently running in the process.
3072 TaskRecord firstTask = null;
3073 // Tasks is non-null only if two or more tasks are found.
3074 ArraySet<TaskRecord> tasks = null;
3075 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3076 for (int i=0; i<app.activities.size(); i++) {
3077 ActivityRecord r = app.activities.get(i);
3078 // First, if we find an activity that is in the process of being destroyed,
3079 // then we just aren't going to do anything for now; we want things to settle
3080 // down before we try to prune more activities.
3081 if (r.finishing || r.state == ActivityState.DESTROYING
3082 || r.state == ActivityState.DESTROYED) {
3083 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3084 return;
3085 }
3086 // Don't consider any activies that are currently not in a state where they
3087 // can be destroyed.
3088 if (r.visible || !r.stopped || !r.haveState
3089 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
3090 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
3091 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3092 continue;
3093 }
3094 if (r.task != null) {
3095 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3096 + " from " + r);
3097 if (firstTask == null) {
3098 firstTask = r.task;
3099 } else if (firstTask != r.task) {
3100 if (tasks == null) {
3101 tasks = new ArraySet<>();
3102 tasks.add(firstTask);
3103 }
3104 tasks.add(r.task);
3105 }
3106 }
3107 }
3108 if (tasks == null) {
3109 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3110 return;
3111 }
3112 // If we have activities in multiple tasks that are in a position to be destroyed,
3113 // let's iterate through the tasks and release the oldest one.
3114 final int numDisplays = mActivityDisplays.size();
3115 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3116 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3117 // Step through all stacks starting from behind, to hit the oldest things first.
3118 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3119 final ActivityStack stack = stacks.get(stackNdx);
3120 // Try to release activities in this stack; if we manage to, we are done.
3121 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3122 return;
3123 }
3124 }
3125 }
3126 }
3127
Craig Mautner8d341ef2013-03-26 09:03:27 -07003128 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003129 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003130 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003131 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003132
Craig Mautner858d8a62013-04-23 17:08:34 -07003133 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003134 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3135 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003136 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003137 final ActivityStack stack = stacks.get(stackNdx);
3138 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003139 TaskRecord task = stack.topTask();
3140 if (task != null) {
3141 mWindowManager.moveTaskToTop(task.taskId);
3142 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003143 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003144 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003145
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003146 ActivityStack stack = getStack(restoreStackId);
3147 if (stack == null) {
3148 stack = mHomeStack;
3149 }
3150 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003151 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003152 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003153 TaskRecord task = stack.topTask();
3154 if (task != null) {
3155 mWindowManager.moveTaskToTop(task.taskId);
3156 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003157 } else {
3158 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003159 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003160 }
Craig Mautner93529a42013-10-04 15:03:13 -07003161 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003162 }
3163
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003164 /**
3165 * Add background users to send boot completed events to.
3166 * @param userId The user being started in the background
3167 * @param uss The state object for the user.
3168 */
3169 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3170 mStartingBackgroundUsers.add(uss);
3171 }
3172
Craig Mautnerde4ef022013-04-07 19:01:33 -07003173 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003174 ArrayList<ActivityRecord> stops = null;
3175
3176 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003177 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3178 ActivityRecord s = mStoppingActivities.get(activityNdx);
3179 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
3180 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
3181 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3182 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003183 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003184 if (s.finishing) {
3185 // If this activity is finishing, it is sitting on top of
3186 // everyone else but we now know it is no longer needed...
3187 // so get rid of it. Otherwise, we need to go through the
3188 // normal flow and hide it once we determine that it is
3189 // hidden by the activities in front of it.
3190 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003191 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003192 }
3193 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003194 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003195 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3196 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003197 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003198 }
3199 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003200 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003201 }
3202 }
3203
3204 return stops;
3205 }
3206
Craig Mautnercf910b02013-04-23 11:23:27 -07003207 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003208 // FIXME
3209/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3210 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003211 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003212 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003213 if (isFrontStack(stack)) {
3214 if (r == null) {
3215 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3216 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003217 final ActivityRecord pausing = stack.mPausingActivity;
3218 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003219 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003220 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003221 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003222 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003223 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003224 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003225 }
3226 }
3227 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003228 final ActivityRecord resumed = stack.mResumedActivity;
3229 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003230 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003231 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003232 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003233 if (r != null && (state == ActivityState.INITIALIZING
3234 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003235 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003236 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003237 }
3238 }
3239 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003240*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003241 }
3242
Craig Mautner27084302013-03-25 08:05:25 -07003243 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003244 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003245 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003246 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3247 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3248 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003249 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003250 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003251
Craig Mautner20e72272013-04-01 13:45:53 -07003252 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003253 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003254 }
3255
Dianne Hackborn390517b2013-05-30 15:03:32 -07003256 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3257 boolean needSep, String prefix) {
3258 if (activity != null) {
3259 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3260 if (needSep) {
3261 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003262 }
3263 pw.print(prefix);
3264 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003265 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003266 }
3267 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003268 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003269 }
3270
Craig Mautner8d341ef2013-03-26 09:03:27 -07003271 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3272 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003273 boolean printed = false;
3274 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003275 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3276 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003277 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003278 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003279 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003280 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003281 final ActivityStack stack = stacks.get(stackNdx);
3282 StringBuilder stackHeader = new StringBuilder(128);
3283 stackHeader.append(" Stack #");
3284 stackHeader.append(stack.mStackId);
3285 stackHeader.append(":");
3286 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3287 needSep, stackHeader.toString());
3288 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3289 !dumpAll, false, dumpPackage, true,
3290 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003291
Craig Mautner4a1cb222013-12-04 16:14:06 -08003292 needSep = printed;
3293 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3294 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003295 if (pr) {
3296 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003297 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003298 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003299 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3300 " mResumedActivity: ");
3301 if (pr) {
3302 printed = true;
3303 needSep = false;
3304 }
3305 if (dumpAll) {
3306 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3307 " mLastPausedActivity: ");
3308 if (pr) {
3309 printed = true;
3310 needSep = true;
3311 }
3312 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3313 needSep, " mLastNoHistoryActivity: ");
3314 }
3315 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003316 }
3317 }
3318
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003319 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3320 false, dumpPackage, true, " Activities waiting to finish:", null);
3321 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3322 false, dumpPackage, true, " Activities waiting to stop:", null);
3323 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3324 false, dumpPackage, true, " Activities waiting for another to become visible:",
3325 null);
3326 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3327 false, dumpPackage, true, " Activities waiting to sleep:", null);
3328 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3329 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003330
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003331 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003332 }
3333
Dianne Hackborn390517b2013-05-30 15:03:32 -07003334 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003335 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003336 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003337 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003338 String innerPrefix = null;
3339 String[] args = null;
3340 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003341 for (int i=list.size()-1; i>=0; i--) {
3342 final ActivityRecord r = list.get(i);
3343 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3344 continue;
3345 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003346 if (innerPrefix == null) {
3347 innerPrefix = prefix + " ";
3348 args = new String[0];
3349 }
3350 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003351 final boolean full = !brief && (complete || !r.isInHistory());
3352 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003353 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003354 needNL = false;
3355 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003356 if (header1 != null) {
3357 pw.println(header1);
3358 header1 = null;
3359 }
3360 if (header2 != null) {
3361 pw.println(header2);
3362 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003363 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003364 if (lastTask != r.task) {
3365 lastTask = r.task;
3366 pw.print(prefix);
3367 pw.print(full ? "* " : " ");
3368 pw.println(lastTask);
3369 if (full) {
3370 lastTask.dump(pw, prefix + " ");
3371 } else if (complete) {
3372 // Complete + brief == give a summary. Isn't that obvious?!?
3373 if (lastTask.intent != null) {
3374 pw.print(prefix); pw.print(" ");
3375 pw.println(lastTask.intent.toInsecureStringWithClip());
3376 }
3377 }
3378 }
3379 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3380 pw.print(" #"); pw.print(i); pw.print(": ");
3381 pw.println(r);
3382 if (full) {
3383 r.dump(pw, innerPrefix);
3384 } else if (complete) {
3385 // Complete + brief == give a summary. Isn't that obvious?!?
3386 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3387 if (r.app != null) {
3388 pw.print(innerPrefix); pw.println(r.app);
3389 }
3390 }
3391 if (client && r.app != null && r.app.thread != null) {
3392 // flush anything that is already in the PrintWriter since the thread is going
3393 // to write to the file descriptor directly
3394 pw.flush();
3395 try {
3396 TransferPipe tp = new TransferPipe();
3397 try {
3398 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3399 r.appToken, innerPrefix, args);
3400 // Short timeout, since blocking here can
3401 // deadlock with the application.
3402 tp.go(fd, 2000);
3403 } finally {
3404 tp.kill();
3405 }
3406 } catch (IOException e) {
3407 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3408 } catch (RemoteException e) {
3409 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3410 }
3411 needNL = true;
3412 }
3413 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003414 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003415 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003416
Craig Mautnerf3333272013-04-22 10:55:53 -07003417 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003418 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003419 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3420 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003421 }
3422
3423 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003424 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003425 }
3426
3427 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003428 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003429 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3430 }
3431
Craig Mautner05d29032013-05-03 13:40:13 -07003432 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003433 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3434 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3435 }
Craig Mautner05d29032013-05-03 13:40:13 -07003436 }
3437
Craig Mautner0eea92c2013-05-16 13:35:39 -07003438 void removeSleepTimeouts() {
3439 mSleepTimeout = false;
3440 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3441 }
3442
3443 final void scheduleSleepTimeout() {
3444 removeSleepTimeouts();
3445 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3446 }
3447
Craig Mautner4a1cb222013-12-04 16:14:06 -08003448 @Override
3449 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003450 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003451 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3452 }
3453
3454 @Override
3455 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003456 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003457 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3458 }
3459
3460 @Override
3461 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003462 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003463 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3464 }
3465
3466 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003467 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003468 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003469 newDisplay = mActivityDisplays.get(displayId) == null;
3470 if (newDisplay) {
3471 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003472 if (activityDisplay.mDisplay == null) {
3473 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3474 return;
3475 }
Craig Mautner4504de52013-12-20 09:06:56 -08003476 mActivityDisplays.put(displayId, activityDisplay);
3477 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003478 }
Craig Mautner4504de52013-12-20 09:06:56 -08003479 if (newDisplay) {
3480 mWindowManager.onDisplayAdded(displayId);
3481 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003482 }
3483
3484 public void handleDisplayRemovedLocked(int displayId) {
3485 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003486 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3487 if (activityDisplay != null) {
3488 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003489 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003490 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003491 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003492 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003493 }
3494 }
3495 mWindowManager.onDisplayRemoved(displayId);
3496 }
3497
3498 public void handleDisplayChangedLocked(int displayId) {
3499 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003500 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3501 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003502 // TODO: Update the bounds.
3503 }
3504 }
3505 mWindowManager.onDisplayChanged(displayId);
3506 }
3507
3508 StackInfo getStackInfo(ActivityStack stack) {
3509 StackInfo info = new StackInfo();
3510 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3511 info.displayId = Display.DEFAULT_DISPLAY;
3512 info.stackId = stack.mStackId;
3513
3514 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3515 final int numTasks = tasks.size();
3516 int[] taskIds = new int[numTasks];
3517 String[] taskNames = new String[numTasks];
3518 for (int i = 0; i < numTasks; ++i) {
3519 final TaskRecord task = tasks.get(i);
3520 taskIds[i] = task.taskId;
3521 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3522 : task.realActivity != null ? task.realActivity.flattenToString()
3523 : task.getTopActivity() != null ? task.getTopActivity().packageName
3524 : "unknown";
3525 }
3526 info.taskIds = taskIds;
3527 info.taskNames = taskNames;
3528 return info;
3529 }
3530
3531 StackInfo getStackInfoLocked(int stackId) {
3532 ActivityStack stack = getStack(stackId);
3533 if (stack != null) {
3534 return getStackInfo(stack);
3535 }
3536 return null;
3537 }
3538
3539 ArrayList<StackInfo> getAllStackInfosLocked() {
3540 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003541 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3542 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003543 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3544 list.add(getStackInfo(stacks.get(ndx)));
3545 }
3546 }
3547 return list;
3548 }
3549
Jason Monka8f569c2014-07-07 12:15:21 -04003550 void showLockTaskToast() {
Benjamin Franz43261142015-02-11 15:59:44 +00003551 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003552 }
3553
Benjamin Franz43261142015-02-11 15:59:44 +00003554 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003555 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003556 // Take out of lock task mode if necessary
3557 if (mLockTaskModeTask != null) {
3558 final Message lockTaskMsg = Message.obtain();
3559 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3560 lockTaskMsg.what = LOCK_TASK_END_MSG;
3561 mLockTaskModeTask = null;
3562 mHandler.sendMessage(lockTaskMsg);
3563 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003564 return;
3565 }
3566 if (isLockTaskModeViolation(task)) {
3567 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3568 return;
3569 }
3570 mLockTaskModeTask = task;
Craig Mautner299f9602015-01-26 09:47:33 -08003571 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003572 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003573
3574 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003575 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3576 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003577 lockTaskMsg.what = LOCK_TASK_START_MSG;
Benjamin Franz43261142015-02-11 15:59:44 +00003578 lockTaskMsg.arg2 = lockTaskModeState;
justinzhang5286d3f2014-05-12 17:06:01 -04003579 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003580 }
3581
3582 boolean isLockTaskModeViolation(TaskRecord task) {
3583 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3584 }
3585
3586 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3587 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003588 final Message lockTaskMsg = Message.obtain();
3589 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3590 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003591 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003592 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003593 }
3594 }
3595
Benjamin Franz43261142015-02-11 15:59:44 +00003596 int getLockTaskModeState() {
3597 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003598 }
3599
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003600 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003601
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003602 public ActivityStackSupervisorHandler(Looper looper) {
3603 super(looper);
3604 }
3605
Craig Mautnerf3333272013-04-22 10:55:53 -07003606 void activityIdleInternal(ActivityRecord r) {
3607 synchronized (mService) {
3608 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3609 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003610 }
3611
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003612 @Override
3613 public void handleMessage(Message msg) {
3614 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003615 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003616 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003617 if (mService.mDidDexOpt) {
3618 mService.mDidDexOpt = false;
3619 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3620 nmsg.obj = msg.obj;
3621 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3622 return;
3623 }
3624 // We don't at this point know if the activity is fullscreen,
3625 // so we need to be conservative and assume it isn't.
3626 activityIdleInternal((ActivityRecord)msg.obj);
3627 } break;
3628 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003629 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003630 activityIdleInternal((ActivityRecord)msg.obj);
3631 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003632 case RESUME_TOP_ACTIVITY_MSG: {
3633 synchronized (mService) {
3634 resumeTopActivitiesLocked();
3635 }
3636 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003637 case SLEEP_TIMEOUT_MSG: {
3638 synchronized (mService) {
3639 if (mService.isSleepingOrShuttingDown()) {
3640 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3641 mSleepTimeout = true;
3642 checkReadyForSleepLocked();
3643 }
3644 }
3645 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003646 case LAUNCH_TIMEOUT_MSG: {
3647 if (mService.mDidDexOpt) {
3648 mService.mDidDexOpt = false;
3649 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3650 return;
3651 }
3652 synchronized (mService) {
3653 if (mLaunchingActivity.isHeld()) {
3654 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3655 if (VALIDATE_WAKE_LOCK_CALLER
3656 && Binder.getCallingUid() != Process.myUid()) {
3657 throw new IllegalStateException("Calling must be system uid");
3658 }
3659 mLaunchingActivity.release();
3660 }
3661 }
3662 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003663 case HANDLE_DISPLAY_ADDED: {
3664 handleDisplayAddedLocked(msg.arg1);
3665 } break;
3666 case HANDLE_DISPLAY_CHANGED: {
3667 handleDisplayChangedLocked(msg.arg1);
3668 } break;
3669 case HANDLE_DISPLAY_REMOVED: {
3670 handleDisplayRemovedLocked(msg.arg1);
3671 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003672 case CONTAINER_CALLBACK_VISIBILITY: {
3673 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003674 final IActivityContainerCallback callback = container.mCallback;
3675 if (callback != null) {
3676 try {
3677 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3678 } catch (RemoteException e) {
3679 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003680 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003681 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003682 case LOCK_TASK_START_MSG: {
3683 // When lock task starts, we disable the status bars.
3684 try {
Jason Monk62515be2014-05-21 16:06:19 -04003685 if (mLockTaskNotify == null) {
3686 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003687 }
Jason Monk62515be2014-05-21 16:06:19 -04003688 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003689 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003690 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003691 int flags = 0;
3692 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_LOCKED) {
3693 flags = StatusBarManager.DISABLE_MASK
3694 & (~StatusBarManager.DISABLE_BACK);
3695 } else if (mLockTaskModeState ==
3696 ActivityManager.LOCK_TASK_MODE_PINNED) {
3697 flags = StatusBarManager.DISABLE_MASK
3698 & (~StatusBarManager.DISABLE_BACK)
3699 & (~StatusBarManager.DISABLE_HOME)
3700 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003701 }
3702 getStatusBarService().disable(flags, mToken,
3703 mService.mContext.getPackageName());
3704 }
3705 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003706 if (getDevicePolicyManager() != null) {
3707 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003708 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003709 }
justinzhang5286d3f2014-05-12 17:06:01 -04003710 } catch (RemoteException ex) {
3711 throw new RuntimeException(ex);
3712 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003713 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003714 case LOCK_TASK_END_MSG: {
3715 // When lock task ends, we enable the status bars.
3716 try {
Jason Monk62515be2014-05-21 16:06:19 -04003717 if (getStatusBarService() != null) {
3718 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3719 mService.mContext.getPackageName());
3720 }
3721 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003722 if (getDevicePolicyManager() != null) {
3723 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3724 msg.arg1);
3725 }
Jason Monk62515be2014-05-21 16:06:19 -04003726 if (mLockTaskNotify == null) {
3727 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3728 }
3729 mLockTaskNotify.show(false);
3730 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003731 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003732 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003733 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Benjamin Franz43261142015-02-11 15:59:44 +00003734 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_PINNED &&
3735 shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003736 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003737 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003738 new LockPatternUtils(mService.mContext)
3739 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003740 }
3741 } catch (SettingNotFoundException e) {
3742 // No setting, don't lock.
3743 }
justinzhang5286d3f2014-05-12 17:06:01 -04003744 } catch (RemoteException ex) {
3745 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003746 } finally {
3747 mLockTaskModeState = ActivityManager.LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003748 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003749 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003750 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3751 final ActivityContainer container = (ActivityContainer) msg.obj;
3752 final IActivityContainerCallback callback = container.mCallback;
3753 if (callback != null) {
3754 try {
3755 callback.onAllActivitiesComplete(container.asBinder());
3756 } catch (RemoteException e) {
3757 }
3758 }
3759 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003760 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3761 synchronized (mService) {
3762 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3763 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003764 final ActivityContainer container = (ActivityContainer) msg.obj;
3765 container.mStack.finishAllActivitiesLocked(true);
3766 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003767 }
3768 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003769 case LAUNCH_TASK_BEHIND_COMPLETE: {
3770 synchronized (mService) {
3771 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3772 if (r != null) {
3773 handleLaunchTaskBehindCompleteLocked(r);
3774 }
3775 }
3776 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003777 }
3778 }
3779 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003780
Ying Wangb081a592014-04-22 15:20:16 -07003781 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003782 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003783 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003784 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003785 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003786 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003787 ActivityRecord mParentActivity = null;
3788 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003789
Craig Mautnere3a00d72014-04-16 08:31:19 -07003790 boolean mVisible = true;
3791
Craig Mautner4a1cb222013-12-04 16:14:06 -08003792 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003793 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003795 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3796 final static int CONTAINER_STATE_NO_SURFACE = 1;
3797 final static int CONTAINER_STATE_FINISHING = 2;
3798 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3799
3800 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003801 synchronized (mService) {
3802 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003803 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003804 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003805 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003807 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003808
Craig Mautnere0a38842013-12-16 16:14:02 -08003809 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003810 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3811 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003812 mActivityDisplay = activityDisplay;
3813 mStack.mDisplayId = activityDisplay.mDisplayId;
3814 mStack.mStacks = activityDisplay.mStacks;
3815
3816 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003817 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003818 }
3819
3820 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003821 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003822 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003823 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3824 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003825 return;
3826 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003827 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 }
3829 }
3830
3831 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003832 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003833 synchronized (mService) {
3834 if (mActivityDisplay != null) {
3835 return mActivityDisplay.mDisplayId;
3836 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003837 }
3838 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003839 }
3840
Jeff Brownca9bc702014-02-11 14:32:56 -08003841 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003842 public int getStackId() {
3843 synchronized (mService) {
3844 return mStackId;
3845 }
3846 }
3847
3848 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003849 public boolean injectEvent(InputEvent event) {
3850 final long origId = Binder.clearCallingIdentity();
3851 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003852 synchronized (mService) {
3853 if (mActivityDisplay != null) {
3854 return mInputManagerInternal.injectInputEvent(event,
3855 mActivityDisplay.mDisplayId,
3856 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3857 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003858 }
3859 return false;
3860 } finally {
3861 Binder.restoreCallingIdentity(origId);
3862 }
3863 }
3864
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003865 @Override
3866 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003867 synchronized (mService) {
3868 if (mContainerState == CONTAINER_STATE_FINISHING) {
3869 return;
3870 }
3871 mContainerState = CONTAINER_STATE_FINISHING;
3872
3873 final Message msg =
3874 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003875 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003876
3877 long origId = Binder.clearCallingIdentity();
3878 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003879 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003880 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003881 } finally {
3882 Binder.restoreCallingIdentity(origId);
3883 }
3884 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003885 }
3886
Craig Mautner60257702014-09-17 15:02:33 -07003887 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003888 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3889 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003890 if (mActivityDisplay != null) {
3891 mActivityDisplay.detachActivitiesLocked(mStack);
3892 mActivityDisplay = null;
3893 mStack.mDisplayId = -1;
3894 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003895 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003896 }
3897 }
3898
3899 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003900 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003901 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08003902 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003903 Binder.getCallingUid(), mCurrentUser, false,
3904 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003905
Craig Mautnere0a38842013-12-16 16:14:02 -08003906 // TODO: Switch to user app stacks here.
3907 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08003908 final Uri data = intent.getData();
3909 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
3910 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08003911 }
Craig Mautnerb9168362015-02-26 20:40:19 -08003912 checkEmbeddedAllowedInner(userId, intent, mimeType);
3913
3914 intent.addFlags(FORCE_NEW_TASK_FLAGS);
3915 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
3916 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003917 }
3918
3919 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003920 public final int startActivityIntentSender(IIntentSender intentSender) {
3921 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3922
3923 if (!(intentSender instanceof PendingIntentRecord)) {
3924 throw new IllegalArgumentException("Bad PendingIntent object");
3925 }
3926
Craig Mautnerb9168362015-02-26 20:40:19 -08003927 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003928 Binder.getCallingUid(), mCurrentUser, false,
3929 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003930
3931 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3932 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3933 pendingIntent.key.requestResolvedType);
3934
3935 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3936 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3937 }
3938
3939 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003940 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003941 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003942 throw new SecurityException(
3943 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3944 }
3945 }
3946
Craig Mautnerdf88d732014-01-27 09:21:32 -08003947 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003948 public IBinder asBinder() {
3949 return this;
3950 }
3951
Craig Mautner4504de52013-12-20 09:06:56 -08003952 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003953 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003954 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003955 }
3956
Craig Mautner4a1cb222013-12-04 16:14:06 -08003957 ActivityStackSupervisor getOuter() {
3958 return ActivityStackSupervisor.this;
3959 }
3960
Craig Mautnerd163e752014-06-13 17:18:47 -07003961 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003962 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003963 }
3964
3965 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003966 synchronized (mService) {
3967 if (mActivityDisplay != null) {
3968 mActivityDisplay.getBounds(outBounds);
3969 } else {
3970 outBounds.set(0, 0);
3971 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003972 }
3973 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003974
Craig Mautner6985bad2014-04-21 15:22:06 -07003975 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003976 void setVisible(boolean visible) {
3977 if (mVisible != visible) {
3978 mVisible = visible;
3979 if (mCallback != null) {
3980 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3981 0 /* unused */, this).sendToTarget();
3982 }
3983 }
3984 }
3985
Craig Mautner6985bad2014-04-21 15:22:06 -07003986 void setDrawn() {
3987 }
3988
Craig Mautner1b4bf852014-05-26 15:06:32 -07003989 // You can always start a new task on a regular ActivityStack.
3990 boolean isEligibleForNewTasks() {
3991 return true;
3992 }
3993
Craig Mautnerd163e752014-06-13 17:18:47 -07003994 void onTaskListEmptyLocked() {
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -08003995 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3996 detachLocked();
3997 deleteActivityContainer(this);
3998 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003999 }
4000
Craig Mautner34b73df2014-01-12 21:11:08 -08004001 @Override
4002 public String toString() {
4003 return mIdString + (mActivityDisplay == null ? "N" : "A");
4004 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004005 }
4006
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004007 private class VirtualActivityContainer extends ActivityContainer {
4008 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004009 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004010
4011 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4012 super(getNextStackId());
4013 mParentActivity = parent;
4014 mCallback = callback;
4015 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004016 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004017 }
4018
4019 @Override
4020 public void setSurface(Surface surface, int width, int height, int density) {
4021 super.setSurface(surface, width, height, density);
4022
4023 synchronized (mService) {
4024 final long origId = Binder.clearCallingIdentity();
4025 try {
4026 setSurfaceLocked(surface, width, height, density);
4027 } finally {
4028 Binder.restoreCallingIdentity(origId);
4029 }
4030 }
4031 }
4032
4033 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4034 if (mContainerState == CONTAINER_STATE_FINISHING) {
4035 return;
4036 }
4037 VirtualActivityDisplay virtualActivityDisplay =
4038 (VirtualActivityDisplay) mActivityDisplay;
4039 if (virtualActivityDisplay == null) {
4040 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004041 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004042 mActivityDisplay = virtualActivityDisplay;
4043 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4044 attachToDisplayLocked(virtualActivityDisplay);
4045 }
4046
4047 if (mSurface != null) {
4048 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004049 }
4050
Craig Mautner6985bad2014-04-21 15:22:06 -07004051 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004052 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004053 mStack.resumeTopActivityLocked(null);
4054 } else {
4055 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004056 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004057 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004058 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004059 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004060 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004061
Craig Mautnerd163e752014-06-13 17:18:47 -07004062 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004063
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004064 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
4065 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004066 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004067
Craig Mautner6985bad2014-04-21 15:22:06 -07004068 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004069 boolean isAttachedLocked() {
4070 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004071 }
4072
4073 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004074 void setDrawn() {
4075 synchronized (mService) {
4076 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004077 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004078 }
4079 }
4080
Craig Mautner1b4bf852014-05-26 15:06:32 -07004081 // Never start a new task on an ActivityView if it isn't explicitly specified.
4082 @Override
4083 boolean isEligibleForNewTasks() {
4084 return false;
4085 }
4086
Craig Mautnerd163e752014-06-13 17:18:47 -07004087 private void setSurfaceIfReadyLocked() {
4088 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004089 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4090 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4091 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4092 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4093 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004094 }
4095 }
4096
Craig Mautner4a1cb222013-12-04 16:14:06 -08004097 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4098 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004099 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004101 int mDisplayId;
4102 Display mDisplay;
4103 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004104
Craig Mautner4a1cb222013-12-04 16:14:06 -08004105 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4106 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004107 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004108
Jose Lima4b6c6692014-08-12 17:41:12 -07004109 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004110
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004111 ActivityDisplay() {
4112 }
Craig Mautner4504de52013-12-20 09:06:56 -08004113
Craig Mautner1a70a162014-09-13 12:09:31 -07004114 // After instantiation, check that mDisplay is not null before using this. The alternative
4115 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004116 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004117 final Display display = mDisplayManager.getDisplay(displayId);
4118 if (display == null) {
4119 return;
4120 }
4121 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004122 }
4123
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004124 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004125 mDisplay = display;
4126 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004127 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004128 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004129
4130 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004131 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4132 + mDisplayId);
4133 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004134 }
4135
Craig Mautnere0a38842013-12-16 16:14:02 -08004136 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004137 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004138 + " from displayId=" + mDisplayId);
4139 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004140 }
4141
4142 void getBounds(Point bounds) {
4143 mDisplay.getDisplayInfo(mDisplayInfo);
4144 bounds.x = mDisplayInfo.appWidth;
4145 bounds.y = mDisplayInfo.appHeight;
4146 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004147
Jose Lima4b6c6692014-08-12 17:41:12 -07004148 void setVisibleBehindActivity(ActivityRecord r) {
4149 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004150 }
4151
Jose Lima4b6c6692014-08-12 17:41:12 -07004152 boolean hasVisibleBehindActivity() {
4153 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004154 }
4155
Craig Mautner34b73df2014-01-12 21:11:08 -08004156 @Override
4157 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004158 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4159 }
4160 }
4161
4162 class VirtualActivityDisplay extends ActivityDisplay {
4163 VirtualDisplay mVirtualDisplay;
4164
Craig Mautner6985bad2014-04-21 15:22:06 -07004165 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004166 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004167 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4168 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4169 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4170 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004171
4172 init(mVirtualDisplay.getDisplay());
4173
4174 mWindowManager.handleDisplayAdded(mDisplayId);
4175 }
4176
4177 void setSurface(Surface surface) {
4178 if (mVirtualDisplay != null) {
4179 mVirtualDisplay.setSurface(surface);
4180 }
4181 }
4182
4183 @Override
4184 void detachActivitiesLocked(ActivityStack stack) {
4185 super.detachActivitiesLocked(stack);
4186 if (mVirtualDisplay != null) {
4187 mVirtualDisplay.release();
4188 mVirtualDisplay = null;
4189 }
4190 }
4191
4192 @Override
4193 public String toString() {
4194 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004195 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004196 }
Jose Lima58e66d62014-05-27 20:00:27 -07004197
4198 private boolean isLeanbackOnlyDevice() {
4199 boolean onLeanbackOnly = false;
4200 try {
4201 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4202 PackageManager.FEATURE_LEANBACK_ONLY);
4203 } catch (RemoteException e) {
4204 // noop
4205 }
4206
4207 return onLeanbackOnly;
4208 }
Craig Mautner27084302013-03-25 08:05:25 -07004209}