blob: fb7a56ceaa570c4f6da0c379764833bfea897c66 [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 Mautner23ac33b2013-04-01 16:26:35 -070073import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070074import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070075import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070076import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070078import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070079import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070081import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070082import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070083import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040084import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070085import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070086import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040087import android.provider.Settings;
88import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070089import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070090import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070091import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070092import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080093import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070094
Craig Mautner4a1cb222013-12-04 16:14:06 -080095import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080096import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080097import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080098import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080099import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700100import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700101import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800102import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700103import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400104import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400105import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800106import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700107import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700108import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109
justinzhang5286d3f2014-05-12 17:06:01 -0400110
Craig Mautner8d341ef2013-03-26 09:03:27 -0700111import java.io.FileDescriptor;
112import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700113import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700114import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700115import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700116
Craig Mautner4a1cb222013-12-04 16:14:06 -0800117public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700118 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
119 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
120 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700121 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700122 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700123 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700124 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
125 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner30bd3bb2014-11-13 00:55:53 +0000126 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700127 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700128
Craig Mautner2219a1b2013-03-25 09:44:30 -0700129 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700130
Craig Mautnerf3333272013-04-22 10:55:53 -0700131 /** How long we wait until giving up on the last activity telling us it is idle. */
132 static final int IDLE_TIMEOUT = 10*1000;
133
Craig Mautner0eea92c2013-05-16 13:35:39 -0700134 /** How long we can hold the sleep wake lock before giving up. */
135 static final int SLEEP_TIMEOUT = 5*1000;
136
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700137 // How long we can hold the launch wake lock before giving up.
138 static final int LAUNCH_TIMEOUT = 10*1000;
139
Craig Mautner05d29032013-05-03 13:40:13 -0700140 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
141 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
142 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700143 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700144 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800145 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
146 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
147 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700148 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400149 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
150 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700151 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700152 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700153 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800154
Craig Mautner4504de52013-12-20 09:06:56 -0800155 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700156
Jason Monk62515be2014-05-21 16:06:19 -0400157 private static final String LOCK_TASK_TAG = "Lock-to-App";
158
justinzhang5286d3f2014-05-12 17:06:01 -0400159 /** Status Bar Service **/
160 private IBinder mToken = new Binder();
161 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400162 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400163
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700164 // For debugging to make sure the caller when acquiring/releasing our
165 // wake lock is the system process.
166 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700167
Craig Mautner27084302013-03-25 08:05:25 -0700168 final ActivityManagerService mService;
169
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800170 private final RecentTasks mRecentTasks;
171
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700172 final ActivityStackSupervisorHandler mHandler;
173
174 /** Short cut */
175 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800176 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700177
Craig Mautner8d341ef2013-03-26 09:03:27 -0700178 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700179 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700180
181 /** Task identifier that activities are currently being started in. Incremented each time a
182 * new task is created. */
183 private int mCurTaskId = 0;
184
Craig Mautner2420ead2013-04-01 17:13:20 -0700185 /** The current user */
186 private int mCurrentUser;
187
Craig Mautnere0a38842013-12-16 16:14:02 -0800188 /** The stack containing the launcher app. Assumed to always be attached to
189 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700190 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700191
Craig Mautnere0a38842013-12-16 16:14:02 -0800192 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700193 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700194
Craig Mautner4a1cb222013-12-04 16:14:06 -0800195 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
196 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800197 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800198 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700199
200 /** List of activities that are waiting for a new activity to become visible before completing
201 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800202 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700203
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700204 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800205 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700206
207 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800208 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700209
Craig Mautnerde4ef022013-04-07 19:01:33 -0700210 /** List of activities that are ready to be stopped, but waiting for the next activity to
211 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800212 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700213
Craig Mautnerf3333272013-04-22 10:55:53 -0700214 /** List of activities that are ready to be finished, but waiting for the previous activity to
215 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800216 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700217
Craig Mautner0eea92c2013-05-16 13:35:39 -0700218 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800219 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700220
Craig Mautnerf3333272013-04-22 10:55:53 -0700221 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800222 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700223
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700224 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800225 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700226
Craig Mautnerde4ef022013-04-07 19:01:33 -0700227 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
228 * is being brought in front of us. */
229 boolean mUserLeaving = false;
230
Craig Mautner0eea92c2013-05-16 13:35:39 -0700231 /** Set when we have taken too long waiting to go to sleep. */
232 boolean mSleepTimeout = false;
233
Jose Lima58e66d62014-05-27 20:00:27 -0700234 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
235 * setWindowManager is called. **/
236 private boolean mLeanbackOnlyDevice;
237
Craig Mautner0eea92c2013-05-16 13:35:39 -0700238 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700239 * We don't want to allow the device to go to sleep while in the process
240 * of launching an activity. This is primarily to allow alarm intent
241 * receivers to launch an activity and get that to run before the device
242 * goes back to sleep.
243 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700244 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700245
246 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700247 * Set when the system is going to sleep, until we have
248 * successfully paused the current activity and released our wake lock.
249 * At that point the system is allowed to actually sleep.
250 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700251 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700252
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700253 /** Stack id of the front stack when user switched, indexed by userId. */
254 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700255
Craig Mautner4504de52013-12-20 09:06:56 -0800256 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800257 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700258 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800259
260 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700261 private final SparseArray<ActivityDisplay> mActivityDisplays =
262 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800263
Jeff Brownca9bc702014-02-11 14:32:56 -0800264 InputManagerInternal mInputManagerInternal;
265
Craig Mautneraea74a52014-03-08 14:23:10 -0800266 /** If non-null then the task specified remains in front and no other tasks may be started
267 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400268 TaskRecord mLockTaskModeTask;
Jason Monka8f569c2014-07-07 12:15:21 -0400269 /** Whether lock task has been entered by an authorized app and cannot
270 * be exited. */
271 private boolean mLockTaskIsLocked;
Jason Monk62515be2014-05-21 16:06:19 -0400272 /**
273 * Notifies the user when entering/exiting lock-task.
274 */
275 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800276
Craig Mautneree36c772014-07-16 14:56:05 -0700277 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
278 = new ArrayList<PendingActivityLaunch>();
279
Craig Mautner42d04db2014-11-06 12:13:23 -0800280 /** Used to keep resumeTopActivityLocked() from being entered recursively */
281 boolean inResumeTopActivity;
282
Craig Mautneree36c772014-07-16 14:56:05 -0700283 /**
284 * Description of a request to start a new activity, which has been held
285 * due to app switches being disabled.
286 */
287 static class PendingActivityLaunch {
288 final ActivityRecord r;
289 final ActivityRecord sourceRecord;
290 final int startFlags;
291 final ActivityStack stack;
292
293 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
294 int _startFlags, ActivityStack _stack) {
295 r = _r;
296 sourceRecord = _sourceRecord;
297 startFlags = _startFlags;
298 stack = _stack;
299 }
300 }
301
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800302 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700303 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800304 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700305 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
306 }
307
308 /**
309 * At the time when the constructor runs, the power manager has not yet been
310 * initialized. So we initialize our wakelocks afterwards.
311 */
312 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800313 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700314 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700315 mLaunchingActivity =
316 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
317 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700318 }
319
justinzhang5286d3f2014-05-12 17:06:01 -0400320 // This function returns a IStatusBarService. The value is from ServiceManager.
321 // getService and is cached.
322 private IStatusBarService getStatusBarService() {
323 synchronized (mService) {
324 if (mStatusBarService == null) {
325 mStatusBarService = IStatusBarService.Stub.asInterface(
326 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
327 if (mStatusBarService == null) {
328 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
329 }
330 }
331 return mStatusBarService;
332 }
333 }
334
Jason Monk35c62a42014-06-17 10:24:47 -0400335 private IDevicePolicyManager getDevicePolicyManager() {
336 synchronized (mService) {
337 if (mDevicePolicyManager == null) {
338 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
339 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
340 if (mDevicePolicyManager == null) {
341 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
342 }
343 }
344 return mDevicePolicyManager;
345 }
346 }
347
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700348 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800349 synchronized (mService) {
350 mWindowManager = wm;
351
352 mDisplayManager =
353 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
354 mDisplayManager.registerDisplayListener(this, null);
355
356 Display[] displays = mDisplayManager.getDisplays();
357 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
358 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800359 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700360 if (activityDisplay.mDisplay == null) {
361 throw new IllegalStateException("Default Display does not exist");
362 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800363 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800364 }
365
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700366 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800367 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800368
369 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700370
371 // Initialize this here, now that we can get a valid reference to PackageManager.
372 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800373 }
Craig Mautner27084302013-03-25 08:05:25 -0700374 }
375
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200376 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700377 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200378 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700379 }
380
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700381 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800382 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700383 }
384
Craig Mautnerde4ef022013-04-07 19:01:33 -0700385 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800386 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700387 }
388
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800389 /** Top of all visible stacks. Use {@link ActivityStack#isStackVisibleLocked} to determine if a
390 * specific stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700391 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800392 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
393 if (parent != null) {
394 stack = parent.task.stack;
395 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800396 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800397 if (stacks != null && !stacks.isEmpty()) {
398 return stack == stacks.get(stacks.size() - 1);
399 }
400 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700401 }
402
Craig Mautner299f9602015-01-26 09:47:33 -0800403 void moveHomeStack(boolean toFront, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800404 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800405 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800406 if (topNdx <= 0) {
407 return;
408 }
409 ActivityStack topStack = stacks.get(topNdx);
410 final boolean homeInFront = topStack == mHomeStack;
411 if (homeInFront != toFront) {
412 mLastFocusedStack = topStack;
413 stacks.remove(mHomeStack);
414 stacks.add(toFront ? topNdx : 0, mHomeStack);
415 mFocusedStack = stacks.get(topNdx);
416 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
417 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700418 }
Craig Mautnerde313752015-01-22 14:28:03 -0800419 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
420 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800421 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800422
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800423 if (mService.mBooting || !mService.mBooted) {
424 final ActivityRecord r = topRunningActivityLocked();
425 if (r != null && r.idle) {
426 checkFinishBootingLocked();
427 }
428 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700429 }
430
Craig Mautner299f9602015-01-26 09:47:33 -0800431 void moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700432 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
433 mWindowManager.showRecentApps();
434 return;
435 }
Craig Mautner299f9602015-01-26 09:47:33 -0800436 moveHomeStack(true, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700437 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700438 }
439
Craig Mautner299f9602015-01-26 09:47:33 -0800440 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700441 if (!mService.mBooting && !mService.mBooted) {
442 // Not ready yet!
443 return false;
444 }
445
Craig Mautner84984fa2014-06-19 11:19:20 -0700446 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
447 mWindowManager.showRecentApps();
448 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700449 }
Craig Mautner299f9602015-01-26 09:47:33 -0800450 moveHomeStackTaskToTop(homeStackTaskType, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700451 if (prev != null) {
452 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
453 }
454
Craig Mautnera8a90e02013-06-28 15:24:50 -0700455 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700456 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000457 if (r != null && r.isHomeActivity()) {
Craig Mautner299f9602015-01-26 09:47:33 -0800458 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700459 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700460 }
Craig Mautner299f9602015-01-26 09:47:33 -0800461 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700462 }
463
Craig Mautner8d341ef2013-03-26 09:03:27 -0700464 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800465 return anyTaskForIdLocked(id, true);
466 }
467
468 /**
469 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
470 * @param id Id of the task we would like returned.
471 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
472 * restore the task from recents to the active list.
473 */
474 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800475 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800476 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800477 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800478 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
479 ActivityStack stack = stacks.get(stackNdx);
480 TaskRecord task = stack.taskForIdLocked(id);
481 if (task != null) {
482 return task;
483 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700484 }
485 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800486
487 // Don't give up! Look in recents.
488 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800489 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800490 if (task == null) {
491 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
492 return null;
493 }
494
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800495 if (!restoreFromRecents) {
496 return task;
497 }
498
Wale Ogunwale7de05352014-12-12 15:21:33 -0800499 if (!restoreRecentTaskLocked(task)) {
500 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
501 return null;
502 }
503 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
504 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700505 }
506
Craig Mautner6170f732013-04-02 13:05:23 -0700507 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800508 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800509 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800510 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800511 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
512 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
513 if (r != null) {
514 return r;
515 }
Craig Mautner6170f732013-04-02 13:05:23 -0700516 }
517 }
518 return null;
519 }
520
Craig Mautneref73ee12014-04-23 11:45:37 -0700521 void setNextTaskId(int taskId) {
522 if (taskId > mCurTaskId) {
523 mCurTaskId = taskId;
524 }
525 }
526
Craig Mautner8d341ef2013-03-26 09:03:27 -0700527 int getNextTaskId() {
528 do {
529 mCurTaskId++;
530 if (mCurTaskId <= 0) {
531 mCurTaskId = 1;
532 }
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800533 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700534 return mCurTaskId;
535 }
536
Craig Mautnerde4ef022013-04-07 19:01:33 -0700537 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800538 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700539 if (stack == null) {
540 return null;
541 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700542 ActivityRecord resumedActivity = stack.mResumedActivity;
543 if (resumedActivity == null || resumedActivity.app == null) {
544 resumedActivity = stack.mPausingActivity;
545 if (resumedActivity == null || resumedActivity.app == null) {
546 resumedActivity = stack.topRunningActivityLocked(null);
547 }
548 }
549 return resumedActivity;
550 }
551
Dianne Hackbornff072722014-09-24 10:56:28 -0700552 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700553 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800554 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800555 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
556 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800557 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
558 final ActivityStack stack = stacks.get(stackNdx);
559 if (!isFrontStack(stack)) {
560 continue;
561 }
562 ActivityRecord hr = stack.topRunningActivityLocked(null);
563 if (hr != null) {
564 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
565 && processName.equals(hr.processName)) {
566 try {
George Mount2c92c972014-03-20 09:38:23 -0700567 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 didSomething = true;
569 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700570 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800571 Slog.w(TAG, "Exception in new application when starting activity "
572 + hr.intent.getComponent().flattenToShortString(), e);
573 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700574 }
Craig Mautner20e72272013-04-01 13:45:53 -0700575 }
Craig Mautner20e72272013-04-01 13:45:53 -0700576 }
577 }
578 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700579 if (!didSomething) {
580 ensureActivitiesVisibleLocked(null, 0);
581 }
Craig Mautner20e72272013-04-01 13:45:53 -0700582 return didSomething;
583 }
584
585 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800586 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
587 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800588 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
589 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800590 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 continue;
592 }
593 final ActivityRecord resumedActivity = stack.mResumedActivity;
594 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800595 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
596 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597 return false;
598 }
Craig Mautner20e72272013-04-01 13:45:53 -0700599 }
600 }
601 return true;
602 }
603
Craig Mautnerde4ef022013-04-07 19:01:33 -0700604 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800605 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
606 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
608 final ActivityStack stack = stacks.get(stackNdx);
609 if (isFrontStack(stack)) {
610 final ActivityRecord r = stack.mResumedActivity;
611 if (r != null && r.state != ActivityState.RESUMED) {
612 return false;
613 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700614 }
615 }
616 }
617 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800618 if (DEBUG_STACK) Slog.d(TAG,
619 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
620 mLastFocusedStack + " to=" + mFocusedStack);
621 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700622 return true;
623 }
624
625 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800626 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
627 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800628 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
629 final ActivityStack stack = stacks.get(stackNdx);
630 final ActivityRecord r = stack.mResumedActivity;
Craig Mautner8c14c152015-01-15 17:32:07 -0800631 if (r != null && (!r.nowVisible || mWaitingVisibleActivities.contains(r))) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800632 return false;
633 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700634 }
635 }
636 return true;
637 }
638
Craig Mautner2acc3892013-09-23 10:28:14 -0700639 /**
640 * Pause all activities in either all of the stacks or just the back stacks.
641 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700642 * @return true if any activity was paused as a result of this call.
643 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700644 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700645 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800646 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
647 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800648 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
649 final ActivityStack stack = stacks.get(stackNdx);
650 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
651 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
652 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700653 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
654 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700656 }
657 }
658 return someActivityPaused;
659 }
660
Craig Mautnerde4ef022013-04-07 19:01:33 -0700661 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700662 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800663 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
664 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
666 final ActivityStack stack = stacks.get(stackNdx);
667 final ActivityRecord r = stack.mPausingActivity;
668 if (r != null && r.state != ActivityState.PAUSED
669 && r.state != ActivityState.STOPPED
670 && r.state != ActivityState.STOPPING) {
671 if (DEBUG_STATES) {
672 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
673 pausing = false;
674 } else {
675 return false;
676 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700677 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700678 }
679 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700680 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700681 }
682
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700683 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
684 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500685 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800686 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
687 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
688 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
689 final ActivityStack stack = stacks.get(stackNdx);
690 if (stack.mResumedActivity != null &&
691 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700692 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800693 }
694 }
695 }
696 }
697
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700698 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700699 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700700 }
701
702 void sendWaitingVisibleReportLocked(ActivityRecord r) {
703 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700704 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700705 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700706 if (w.who == null) {
707 changed = true;
708 w.timeout = false;
709 if (r != null) {
710 w.who = new ComponentName(r.info.packageName, r.info.name);
711 }
712 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
713 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700714 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700715 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700716 if (changed) {
717 mService.notifyAll();
718 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700719 }
720
721 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
722 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700723 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700724 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700725 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700726 if (w.who == null) {
727 changed = true;
728 w.timeout = timeout;
729 if (r != null) {
730 w.who = new ComponentName(r.info.packageName, r.info.name);
731 }
732 w.thisTime = thisTime;
733 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700734 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700735 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700736 if (changed) {
737 mService.notifyAll();
738 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700739 }
740
Craig Mautner29219d92013-04-16 20:19:12 -0700741 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800742 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700743 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
744 if (r != null) {
745 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700746 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700747
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800749 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800750 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
751 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700752 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700753 r = stack.topRunningActivityLocked(null);
754 if (r != null) {
755 return r;
756 }
757 }
758 }
759 return null;
760 }
761
Dianne Hackborn09233282014-04-30 11:33:59 -0700762 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700763 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800764 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
765 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800766 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800768 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
770 final ActivityStack stack = stacks.get(stackNdx);
771 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
772 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700773 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700774 }
775 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700776
777 // The lists are already sorted from most recent to oldest. Just pull the most recent off
778 // each list and add it to list. Stop when all lists are empty or maxNum reached.
779 while (maxNum > 0) {
780 long mostRecentActiveTime = Long.MIN_VALUE;
781 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800782 final int numTaskLists = runningTaskLists.size();
783 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
784 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700785 if (!stackTaskList.isEmpty()) {
786 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
787 if (lastActiveTime > mostRecentActiveTime) {
788 mostRecentActiveTime = lastActiveTime;
789 selectedStackList = stackTaskList;
790 }
791 }
792 }
793 if (selectedStackList != null) {
794 list.add(selectedStackList.remove(0));
795 --maxNum;
796 } else {
797 break;
798 }
799 }
Craig Mautner20e72272013-04-01 13:45:53 -0700800 }
801
Craig Mautner23ac33b2013-04-01 16:26:35 -0700802 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700803 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700804 // Collect information about the target of the Intent.
805 ActivityInfo aInfo;
806 try {
807 ResolveInfo rInfo =
808 AppGlobals.getPackageManager().resolveIntent(
809 intent, resolvedType,
810 PackageManager.MATCH_DEFAULT_ONLY
811 | ActivityManagerService.STOCK_PM_FLAGS, userId);
812 aInfo = rInfo != null ? rInfo.activityInfo : null;
813 } catch (RemoteException e) {
814 aInfo = null;
815 }
816
817 if (aInfo != null) {
818 // Store the found target back into the intent, because now that
819 // we have it we never want to do this again. For example, if the
820 // user navigates back to this point in the history, we should
821 // always restart the exact same activity.
822 intent.setComponent(new ComponentName(
823 aInfo.applicationInfo.packageName, aInfo.name));
824
825 // Don't debug things in the system process
826 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
827 if (!aInfo.processName.equals("system")) {
828 mService.setDebugApp(aInfo.processName, true, false);
829 }
830 }
831
832 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
833 if (!aInfo.processName.equals("system")) {
834 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
835 }
836 }
837
Jeff Hao1b012d32014-08-20 10:35:34 -0700838 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700839 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700840 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700841 }
842 }
843 }
844 return aInfo;
845 }
846
Craig Mautner299f9602015-01-26 09:47:33 -0800847 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
848 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700849 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
850 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700851 }
852
Craig Mautner23ac33b2013-04-01 16:26:35 -0700853 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700854 String callingPackage, Intent intent, String resolvedType,
855 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700856 IBinder resultTo, String resultWho, int requestCode, int startFlags,
857 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700858 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700859 // Refuse possible leaked file descriptors
860 if (intent != null && intent.hasFileDescriptors()) {
861 throw new IllegalArgumentException("File descriptors passed in Intent");
862 }
863 boolean componentSpecified = intent.getComponent() != null;
864
865 // Don't modify the client's object!
866 intent = new Intent(intent);
867
868 // Collect information about the target of the Intent.
869 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700870 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700871
Craig Mautnere0a38842013-12-16 16:14:02 -0800872 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700873 synchronized (mService) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700874 final int realCallingPid = Binder.getCallingPid();
875 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700876 int callingPid;
877 if (callingUid >= 0) {
878 callingPid = -1;
879 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700880 callingPid = realCallingPid;
881 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700882 } else {
883 callingPid = callingUid = -1;
884 }
885
Craig Mautnere0a38842013-12-16 16:14:02 -0800886 final ActivityStack stack;
887 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800888 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800889 } else {
890 stack = container.mStack;
891 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800892 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700893 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700894 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700895
896 final long origId = Binder.clearCallingIdentity();
897
898 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800899 (aInfo.applicationInfo.privateFlags
900 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700901 // This may be a heavy-weight process! Check to see if we already
902 // have another, different heavy-weight process running.
903 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
904 if (mService.mHeavyWeightProcess != null &&
905 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
906 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700907 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700908 if (caller != null) {
909 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
910 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700911 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700912 } else {
913 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700914 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700915 + intent.toString());
916 ActivityOptions.abort(options);
917 return ActivityManager.START_PERMISSION_DENIED;
918 }
919 }
920
921 IIntentSender target = mService.getIntentSenderLocked(
922 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700923 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
925 | PendingIntent.FLAG_ONE_SHOT, null);
926
927 Intent newIntent = new Intent();
928 if (requestCode >= 0) {
929 // Caller is requesting a result.
930 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
931 }
932 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
933 new IntentSender(target));
934 if (mService.mHeavyWeightProcess.activities.size() > 0) {
935 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
936 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
937 hist.packageName);
938 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
939 hist.task.taskId);
940 }
941 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
942 aInfo.packageName);
943 newIntent.setFlags(intent.getFlags());
944 newIntent.setClassName("android",
945 HeavyWeightSwitcherActivity.class.getName());
946 intent = newIntent;
947 resolvedType = null;
948 caller = null;
949 callingUid = Binder.getCallingUid();
950 callingPid = Binder.getCallingPid();
951 componentSpecified = true;
952 try {
953 ResolveInfo rInfo =
954 AppGlobals.getPackageManager().resolveIntent(
955 intent, null,
956 PackageManager.MATCH_DEFAULT_ONLY
957 | ActivityManagerService.STOCK_PM_FLAGS, userId);
958 aInfo = rInfo != null ? rInfo.activityInfo : null;
959 aInfo = mService.getActivityInfoForUser(aInfo, userId);
960 } catch (RemoteException e) {
961 aInfo = null;
962 }
963 }
964 }
965 }
966
Dianne Hackborn91097de2014-04-04 18:02:06 -0700967 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
968 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700969 requestCode, callingPid, callingUid, callingPackage,
970 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700971 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700972
Craig Mautner85c11a82014-07-17 12:38:18 -0700973 Binder.restoreCallingIdentity(origId);
974
Craig Mautnerde4ef022013-04-07 19:01:33 -0700975 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700976 // If the caller also wants to switch to a new configuration,
977 // do so now. This allows a clean switch, as we are waiting
978 // for the current activity to pause (so we will not destroy
979 // it), and have not yet started the next activity.
980 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
981 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700982 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700983 if (DEBUG_CONFIGURATION) Slog.v(TAG,
984 "Updating to new configuration after starting activity.");
985 mService.updateConfigurationLocked(config, null, false, false);
986 }
987
Craig Mautner23ac33b2013-04-01 16:26:35 -0700988 if (outResult != null) {
989 outResult.result = res;
990 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700991 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700992 do {
993 try {
994 mService.wait();
995 } catch (InterruptedException e) {
996 }
997 } while (!outResult.timeout && outResult.who == null);
998 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700999 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001000 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001001 outResult.timeout = false;
1002 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1003 outResult.totalTime = 0;
1004 outResult.thisTime = 0;
1005 } else {
1006 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001007 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001008 do {
1009 try {
1010 mService.wait();
1011 } catch (InterruptedException e) {
1012 }
1013 } while (!outResult.timeout && outResult.who == null);
1014 }
1015 }
1016 }
1017
1018 return res;
1019 }
1020 }
1021
1022 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1023 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1024 Bundle options, int userId) {
1025 if (intents == null) {
1026 throw new NullPointerException("intents is null");
1027 }
1028 if (resolvedTypes == null) {
1029 throw new NullPointerException("resolvedTypes is null");
1030 }
1031 if (intents.length != resolvedTypes.length) {
1032 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1033 }
1034
Craig Mautner23ac33b2013-04-01 16:26:35 -07001035
1036 int callingPid;
1037 if (callingUid >= 0) {
1038 callingPid = -1;
1039 } else if (caller == null) {
1040 callingPid = Binder.getCallingPid();
1041 callingUid = Binder.getCallingUid();
1042 } else {
1043 callingPid = callingUid = -1;
1044 }
1045 final long origId = Binder.clearCallingIdentity();
1046 try {
1047 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001048 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001049 for (int i=0; i<intents.length; i++) {
1050 Intent intent = intents[i];
1051 if (intent == null) {
1052 continue;
1053 }
1054
1055 // Refuse possible leaked file descriptors
1056 if (intent != null && intent.hasFileDescriptors()) {
1057 throw new IllegalArgumentException("File descriptors passed in Intent");
1058 }
1059
1060 boolean componentSpecified = intent.getComponent() != null;
1061
1062 // Don't modify the client's object!
1063 intent = new Intent(intent);
1064
1065 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001066 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001067 // TODO: New, check if this is correct
1068 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1069
1070 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001071 (aInfo.applicationInfo.privateFlags
1072 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001073 throw new IllegalArgumentException(
1074 "FLAG_CANT_SAVE_STATE not supported here");
1075 }
1076
1077 Bundle theseOptions;
1078 if (options != null && i == intents.length-1) {
1079 theseOptions = options;
1080 } else {
1081 theseOptions = null;
1082 }
Craig Mautner6170f732013-04-02 13:05:23 -07001083 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001084 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1085 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001086 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001087 if (res < 0) {
1088 return res;
1089 }
1090
1091 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1092 }
1093 }
1094 } finally {
1095 Binder.restoreCallingIdentity(origId);
1096 }
1097
1098 return ActivityManager.START_SUCCESS;
1099 }
1100
Craig Mautner2420ead2013-04-01 17:13:20 -07001101 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001102 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001103 throws RemoteException {
1104
1105 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001106 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001107 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001108
1109 // schedule launch ticks to collect information about slow apps.
1110 r.startLaunchTickingLocked();
1111
1112 // Have the window manager re-evaluate the orientation of
1113 // the screen based on the new activity order. Note that
1114 // as a result of this, it can call back into the activity
1115 // manager with a new orientation. We don't care about that,
1116 // because the activity is not currently running so we are
1117 // just restarting it anyway.
1118 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001119 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001120 mService.mConfiguration,
1121 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1122 mService.updateConfigurationLocked(config, r, false, false);
1123 }
1124
1125 r.app = app;
1126 app.waitingToKill = null;
1127 r.launchCount++;
1128 r.lastLaunchTime = SystemClock.uptimeMillis();
1129
1130 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1131
1132 int idx = app.activities.indexOf(r);
1133 if (idx < 0) {
1134 app.activities.add(r);
1135 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001136 mService.updateLruProcessLocked(app, true, null);
1137 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001138
1139 final ActivityStack stack = r.task.stack;
1140 try {
1141 if (app.thread == null) {
1142 throw new RemoteException();
1143 }
1144 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001145 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001146 if (andResume) {
1147 results = r.results;
1148 newIntents = r.newIntents;
1149 }
1150 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1151 + " icicle=" + r.icicle
1152 + " with results=" + results + " newIntents=" + newIntents
1153 + " andResume=" + andResume);
1154 if (andResume) {
1155 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1156 r.userId, System.identityHashCode(r),
1157 r.task.taskId, r.shortComponentName);
1158 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001159 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001160 // Home process is the root process of the task.
1161 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001162 }
1163 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1164 r.sleeping = false;
1165 r.forceNewConfig = false;
1166 mService.showAskCompatModeDialogLocked(r);
1167 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1168 String profileFile = null;
1169 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001170 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1171 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1172 mService.mProfileProc = app;
1173 profileFile = mService.mProfileFile;
1174 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001175 }
1176 }
1177 app.hasShownUi = true;
1178 app.pendingUiClean = true;
1179 if (profileFd != null) {
1180 try {
1181 profileFd = profileFd.dup();
1182 } catch (IOException e) {
1183 if (profileFd != null) {
1184 try {
1185 profileFd.close();
1186 } catch (IOException o) {
1187 }
1188 profileFd = null;
1189 }
1190 }
1191 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001192
Jeff Hao1b012d32014-08-20 10:35:34 -07001193 ProfilerInfo profilerInfo = profileFile != null
1194 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1195 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001196 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001197 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001198 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001199 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1200 r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1201 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001202
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001203 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001204 // This may be a heavy-weight process! Note that the package
1205 // manager will ensure that only activity can run in the main
1206 // process of the .apk, which is the only thing that will be
1207 // considered heavy-weight.
1208 if (app.processName.equals(app.info.packageName)) {
1209 if (mService.mHeavyWeightProcess != null
1210 && mService.mHeavyWeightProcess != app) {
1211 Slog.w(TAG, "Starting new heavy weight process " + app
1212 + " when already running "
1213 + mService.mHeavyWeightProcess);
1214 }
1215 mService.mHeavyWeightProcess = app;
1216 Message msg = mService.mHandler.obtainMessage(
1217 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1218 msg.obj = r;
1219 mService.mHandler.sendMessage(msg);
1220 }
1221 }
1222
1223 } catch (RemoteException e) {
1224 if (r.launchFailed) {
1225 // This is the second time we failed -- finish activity
1226 // and give up.
1227 Slog.e(TAG, "Second failure launching "
1228 + r.intent.getComponent().flattenToShortString()
1229 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001230 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001231 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1232 "2nd-crash", false);
1233 return false;
1234 }
1235
1236 // This is the first time we failed -- restart process and
1237 // retry.
1238 app.activities.remove(r);
1239 throw e;
1240 }
1241
1242 r.launchFailed = false;
1243 if (stack.updateLRUListLocked(r)) {
1244 Slog.w(TAG, "Activity " + r
1245 + " being launched, but already in LRU list");
1246 }
1247
1248 if (andResume) {
1249 // As part of the process of launching, ActivityThread also performs
1250 // a resume.
1251 stack.minimalResumeActivityLocked(r);
1252 } else {
1253 // This activity is not starting in the resumed state... which
1254 // should look like we asked it to pause+stop (but remain visible),
1255 // and it has done so and reported back the current icicle and
1256 // other state.
1257 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1258 + " (starting in stopped state)");
1259 r.state = ActivityState.STOPPED;
1260 r.stopped = true;
1261 }
1262
1263 // Launch the new version setup screen if needed. We do this -after-
1264 // launching the initial activity (that is, home), so that it can have
1265 // a chance to initialize itself while in the background, making the
1266 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001267 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 mService.startSetupActivityLocked();
1269 }
1270
Dianne Hackborn465fa392014-09-14 14:21:18 -07001271 // Update any services we are bound to that might care about whether
1272 // their client may have activities.
1273 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1274
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 return true;
1276 }
1277
Craig Mautnere79d42682013-04-01 19:01:53 -07001278 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001279 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001280 // Is this activity's application already running?
1281 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001282 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001283
1284 r.task.stack.setLaunchTime(r);
1285
1286 if (app != null && app.thread != null) {
1287 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001288 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1289 || !"android".equals(r.info.packageName)) {
1290 // Don't add this if it is a platform component that is marked
1291 // to run in multiple processes, because this is actually
1292 // part of the framework so doesn't make sense to track as a
1293 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001294 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1295 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001296 }
George Mount2c92c972014-03-20 09:38:23 -07001297 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001298 return;
1299 } catch (RemoteException e) {
1300 Slog.w(TAG, "Exception when starting activity "
1301 + r.intent.getComponent().flattenToShortString(), e);
1302 }
1303
1304 // If a dead object exception was thrown -- fall through to
1305 // restart the application.
1306 }
1307
1308 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001309 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001310 }
1311
Craig Mautner6170f732013-04-02 13:05:23 -07001312 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001313 Intent intent, String resolvedType, ActivityInfo aInfo,
1314 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1315 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001316 int callingPid, int callingUid, String callingPackage,
1317 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001318 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1319 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001320 int err = ActivityManager.START_SUCCESS;
1321
1322 ProcessRecord callerApp = null;
1323 if (caller != null) {
1324 callerApp = mService.getRecordForAppLocked(caller);
1325 if (callerApp != null) {
1326 callingPid = callerApp.pid;
1327 callingUid = callerApp.info.uid;
1328 } else {
1329 Slog.w(TAG, "Unable to find app for caller " + caller
1330 + " (pid=" + callingPid + ") when starting: "
1331 + intent.toString());
1332 err = ActivityManager.START_PERMISSION_DENIED;
1333 }
1334 }
1335
1336 if (err == ActivityManager.START_SUCCESS) {
1337 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1338 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001339 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001340 + " on display " + (container == null ? (mFocusedStack == null ?
1341 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1342 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1343 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001344 }
1345
1346 ActivityRecord sourceRecord = null;
1347 ActivityRecord resultRecord = null;
1348 if (resultTo != null) {
1349 sourceRecord = isInAnyStackLocked(resultTo);
1350 if (DEBUG_RESULTS) Slog.v(
1351 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1352 if (sourceRecord != null) {
1353 if (requestCode >= 0 && !sourceRecord.finishing) {
1354 resultRecord = sourceRecord;
1355 }
1356 }
1357 }
Craig Mautner6170f732013-04-02 13:05:23 -07001358
Dianne Hackborn91097de2014-04-04 18:02:06 -07001359 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001360
Dianne Hackborn95465202014-09-15 16:21:55 -07001361 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001362 // Transfer the result target from the source activity to the new
1363 // one being started, including any failures.
1364 if (requestCode >= 0) {
1365 ActivityOptions.abort(options);
1366 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1367 }
1368 resultRecord = sourceRecord.resultTo;
1369 resultWho = sourceRecord.resultWho;
1370 requestCode = sourceRecord.requestCode;
1371 sourceRecord.resultTo = null;
1372 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001373 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001374 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001375 if (sourceRecord.launchedFromUid == callingUid) {
1376 // The new activity is being launched from the same uid as the previous
1377 // activity in the flow, and asking to forward its result back to the
1378 // previous. In this case the activity is serving as a trampoline between
1379 // the two, so we also want to update its launchedFromPackage to be the
1380 // same as the previous activity. Note that this is safe, since we know
1381 // these two packages come from the same uid; the caller could just as
1382 // well have supplied that same package name itself. This specifially
1383 // deals with the case of an intent picker/chooser being launched in the app
1384 // flow to redirect to an activity picked by the user, where we want the final
1385 // activity to consider it to have been launched by the previous app activity.
1386 callingPackage = sourceRecord.launchedFromPackage;
1387 }
Craig Mautner6170f732013-04-02 13:05:23 -07001388 }
1389
1390 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1391 // We couldn't find a class that can handle the given Intent.
1392 // That's the end of that!
1393 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1394 }
1395
1396 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1397 // We couldn't find the specific class specified in the Intent.
1398 // Also the end of the line.
1399 err = ActivityManager.START_CLASS_NOT_FOUND;
1400 }
1401
Dianne Hackborn91097de2014-04-04 18:02:06 -07001402 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1403 && sourceRecord.task.voiceSession != null) {
1404 // If this activity is being launched as part of a voice session, we need
1405 // to ensure that it is safe to do so. If the upcoming activity will also
1406 // be part of the voice session, we can only launch it if it has explicitly
1407 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001408 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001409 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1410 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001411 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1412 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001413 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1414 }
1415 } catch (RemoteException e) {
1416 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1417 }
1418 }
1419 }
1420
1421 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1422 // If the caller is starting a new voice session, just make sure the target
1423 // is actually allowing it to run this way.
1424 try {
1425 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1426 intent, resolvedType)) {
1427 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1428 }
1429 } catch (RemoteException e) {
1430 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1431 }
1432 }
1433
louis_changcd5d1982014-08-01 10:09:08 +08001434 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1435
Craig Mautner6170f732013-04-02 13:05:23 -07001436 if (err != ActivityManager.START_SUCCESS) {
1437 if (resultRecord != null) {
1438 resultStack.sendActivityResultLocked(-1,
1439 resultRecord, resultWho, requestCode,
1440 Activity.RESULT_CANCELED, null);
1441 }
Craig Mautner6170f732013-04-02 13:05:23 -07001442 ActivityOptions.abort(options);
1443 return err;
1444 }
1445
1446 final int startAnyPerm = mService.checkPermission(
1447 START_ANY_ACTIVITY, callingPid, callingUid);
1448 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1449 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1450 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1451 if (resultRecord != null) {
1452 resultStack.sendActivityResultLocked(-1,
1453 resultRecord, resultWho, requestCode,
1454 Activity.RESULT_CANCELED, null);
1455 }
Craig Mautner6170f732013-04-02 13:05:23 -07001456 String msg;
1457 if (!aInfo.exported) {
1458 msg = "Permission Denial: starting " + intent.toString()
1459 + " from " + callerApp + " (pid=" + callingPid
1460 + ", uid=" + callingUid + ")"
1461 + " not exported from uid " + aInfo.applicationInfo.uid;
1462 } else {
1463 msg = "Permission Denial: starting " + intent.toString()
1464 + " from " + callerApp + " (pid=" + callingPid
1465 + ", uid=" + callingUid + ")"
1466 + " requires " + aInfo.permission;
1467 }
1468 Slog.w(TAG, msg);
1469 throw new SecurityException(msg);
1470 }
1471
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001472 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001473 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001474
Craig Mautner6170f732013-04-02 13:05:23 -07001475 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001476 try {
1477 // The Intent we give to the watcher has the extra data
1478 // stripped off, since it can contain private information.
1479 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001480 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001481 aInfo.applicationInfo.packageName);
1482 } catch (RemoteException e) {
1483 mService.mController = null;
1484 }
Ben Gruver5e207332013-04-03 17:41:37 -07001485 }
Craig Mautner6170f732013-04-02 13:05:23 -07001486
Ben Gruver5e207332013-04-03 17:41:37 -07001487 if (abort) {
1488 if (resultRecord != null) {
1489 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001490 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001491 }
Ben Gruver5e207332013-04-03 17:41:37 -07001492 // We pretend to the caller that it was really started, but
1493 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001494 ActivityOptions.abort(options);
1495 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001496 }
1497
1498 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001499 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001500 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001501 if (outActivity != null) {
1502 outActivity[0] = r;
1503 }
1504
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001505 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001506 if (voiceSession == null && (stack.mResumedActivity == null
1507 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001508 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1509 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001510 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001511 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001512 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001513 ActivityOptions.abort(options);
1514 return ActivityManager.START_SWITCHES_CANCELED;
1515 }
1516 }
1517
1518 if (mService.mDidAppSwitch) {
1519 // This is the second allowed switch since we stopped switches,
1520 // so now just generally allow switches. Use case: user presses
1521 // home (switches disabled, switch to home, mDidAppSwitch now true);
1522 // user taps a home icon (coming from home so allowed, we hit here
1523 // and now allow anyone to switch again).
1524 mService.mAppSwitchesAllowedTime = 0;
1525 } else {
1526 mService.mDidAppSwitch = true;
1527 }
1528
Craig Mautneree36c772014-07-16 14:56:05 -07001529 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001530
Dianne Hackborn91097de2014-04-04 18:02:06 -07001531 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001532 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001533
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001534 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001535 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001536 // activity start, but we are not actually doing an activity
1537 // switch... just dismiss the keyguard now, because we
1538 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001539 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001540 }
1541 return err;
1542 }
1543
Craig Mautner1b4bf852014-05-26 15:06:32 -07001544 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001545 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001546
1547 // On leanback only devices we should keep all activities in the same stack.
1548 if (!mLeanbackOnlyDevice &&
1549 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001550 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001551 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001552 if (taskStack.isOnHomeDisplay()) {
1553 if (mFocusedStack != taskStack) {
1554 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1555 "focused stack to r=" + r + " task=" + task);
1556 mFocusedStack = taskStack;
1557 } else {
1558 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1559 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1560 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001561 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001562 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001563 }
1564
Craig Mautnere0a38842013-12-16 16:14:02 -08001565 final ActivityContainer container = r.mInitialActivityContainer;
1566 if (container != null) {
1567 // The first time put it on the desired stack, after this put on task stack.
1568 r.mInitialActivityContainer = null;
1569 return container.mStack;
1570 }
1571
Craig Mautner1b4bf852014-05-26 15:06:32 -07001572 if (mFocusedStack != mHomeStack && (!newTask ||
1573 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001574 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1575 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1576 return mFocusedStack;
1577 }
1578
Craig Mautnere0a38842013-12-16 16:14:02 -08001579 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1580 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1581 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1582 if (!stack.isHomeStack()) {
1583 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1584 "adjustStackFocus: Setting focused stack=" + stack);
1585 mFocusedStack = stack;
1586 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001587 }
1588 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001589
Craig Mautner4a1cb222013-12-04 16:14:06 -08001590 // Need to create an app stack for this user.
Todd Kennedy4900bf92015-01-16 16:05:14 -08001591 mFocusedStack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001592 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
Todd Kennedy4900bf92015-01-16 16:05:14 -08001593 " stackId=" + mFocusedStack.mStackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001594 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001595 }
1596 return mHomeStack;
1597 }
1598
Craig Mautner299f9602015-01-26 09:47:33 -08001599 void setFocusedStack(ActivityRecord r, String reason) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001600 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001601 final TaskRecord task = r.task;
1602 boolean isHomeActivity = !r.isApplicationActivity();
1603 if (!isHomeActivity && task != null) {
1604 isHomeActivity = !task.isApplicationTask();
1605 }
1606 if (!isHomeActivity && task != null) {
1607 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1608 isHomeActivity = parent != null && parent.isHomeActivity();
1609 }
Craig Mautner299f9602015-01-26 09:47:33 -08001610 moveHomeStack(isHomeActivity, reason);
Craig Mautner29219d92013-04-16 20:19:12 -07001611 }
1612 }
1613
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001614 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001615 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001616 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001617 final Intent intent = r.intent;
1618 final int callingUid = r.launchedFromUid;
1619
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001620 // In some flows in to this function, we retrieve the task record and hold on to it
1621 // without a lock before calling back in to here... so the task at this point may
1622 // not actually be in recents. Check for that, and if it isn't in recents just
1623 // consider it invalid.
1624 if (inTask != null && !inTask.inRecents) {
1625 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1626 inTask = null;
1627 }
1628
Craig Mautnerad400af2014-08-13 16:41:36 -07001629 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001630 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1631 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001632
Craig Mautnera228ae92014-07-09 05:44:55 -07001633 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001634 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001635 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001636 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1637 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1638 "\"singleInstance\" or \"singleTask\"");
1639 launchFlags &=
1640 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1641 } else {
1642 switch (r.info.documentLaunchMode) {
1643 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1644 break;
1645 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1646 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1647 break;
1648 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1649 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1650 break;
1651 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1652 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1653 break;
1654 }
1655 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001656
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001657 final boolean launchTaskBehind = r.mLaunchTaskBehind
1658 && !launchSingleTask && !launchSingleInstance
1659 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001660
1661 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1662 // For whatever reason this activity is being launched into a new
1663 // task... yet the caller has requested a result back. Well, that
1664 // is pretty messed up, so instead immediately send back a cancel
1665 // and let the new task continue launched as normal without a
1666 // dependency on its originator.
1667 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1668 r.resultTo.task.stack.sendActivityResultLocked(-1,
1669 r.resultTo, r.resultWho, r.requestCode,
1670 Activity.RESULT_CANCELED, null);
1671 r.resultTo = null;
1672 }
1673
1674 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1675 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1676 }
1677
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001678 // If we are actually going to launch in to a new task, there are some cases where
1679 // we further want to do multiple task.
1680 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1681 if (launchTaskBehind
1682 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1683 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1684 }
1685 }
1686
Craig Mautner8849a5e2013-04-02 16:41:03 -07001687 // We'll invoke onUserLeaving before onPause only if the launching
1688 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001689 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001690 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001691
1692 // If the caller has asked not to resume at this point, we make note
1693 // of this in the record so that we can skip it when trying to find
1694 // the top running activity.
1695 if (!doResume) {
1696 r.delayedResume = true;
1697 }
1698
Craig Mautnera254cd72014-05-25 16:47:39 -07001699 ActivityRecord notTop =
1700 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001701
1702 // If the onlyIfNeeded flag is set, then we can do this if the activity
1703 // being launched is the same as the one making the call... or, as
1704 // a special case, if we do not know the caller then we count the
1705 // current top activity as the caller.
1706 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1707 ActivityRecord checkedCaller = sourceRecord;
1708 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001709 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001710 }
1711 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1712 // Caller is not the same as launcher, so always needed.
1713 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1714 }
1715 }
1716
Craig Mautner8849a5e2013-04-02 16:41:03 -07001717 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001718 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001719
1720 // If the caller is not coming from another activity, but has given us an
1721 // explicit task into which they would like us to launch the new activity,
1722 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001723 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1724 final Intent baseIntent = inTask.getBaseIntent();
1725 final ActivityRecord root = inTask.getRootActivity();
1726 if (baseIntent == null) {
1727 ActivityOptions.abort(options);
1728 throw new IllegalArgumentException("Launching into task without base intent: "
1729 + inTask);
1730 }
1731
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001732 // If this task is empty, then we are adding the first activity -- it
1733 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001734 if (launchSingleInstance || launchSingleTask) {
1735 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1736 ActivityOptions.abort(options);
1737 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1738 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001739 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001740 if (root != null) {
1741 ActivityOptions.abort(options);
1742 throw new IllegalArgumentException("Caller with inTask " + inTask
1743 + " has root " + root + " but target is singleInstance/Task");
1744 }
1745 }
1746
1747 // If task is empty, then adopt the interesting intent launch flags in to the
1748 // activity being started.
1749 if (root == null) {
1750 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1751 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1752 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1753 launchFlags = (launchFlags&~flagsOfInterest)
1754 | (baseIntent.getFlags()&flagsOfInterest);
1755 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001756 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001757 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001758
Dianne Hackborn962d5352014-08-29 16:27:40 -07001759 // If the task is not empty and the caller is asking to start it as the root
1760 // of a new task, then we don't actually want to start this on the task. We
1761 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001762 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001763 addingToTask = false;
1764
1765 } else {
1766 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001767 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001768
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001769 reuseTask = inTask;
1770 } else {
1771 inTask = null;
1772 }
1773
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001774 if (inTask == null) {
1775 if (sourceRecord == null) {
1776 // This activity is not being started from another... in this
1777 // case we -always- start a new task.
1778 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1779 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1780 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1781 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1782 }
1783 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1784 // The original activity who is starting us is running as a single
1785 // instance... this new activity it is starting must go on its
1786 // own task.
1787 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1788 } else if (launchSingleInstance || launchSingleTask) {
1789 // The activity being started is a single instance... it always
1790 // gets launched into its own task.
1791 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1792 }
1793 }
1794
1795 ActivityInfo newTaskInfo = null;
1796 Intent newTaskIntent = null;
1797 ActivityStack sourceStack;
1798 if (sourceRecord != null) {
1799 if (sourceRecord.finishing) {
1800 // If the source is finishing, we can't further count it as our source. This
1801 // is because the task it is associated with may now be empty and on its way out,
1802 // so we don't want to blindly throw it in to that task. Instead we will take
1803 // the NEW_TASK flow and try to find a task for it. But save the task information
1804 // so it can be used when creating the new task.
1805 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1806 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1807 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1808 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1809 newTaskInfo = sourceRecord.info;
1810 newTaskIntent = sourceRecord.task.intent;
1811 }
1812 sourceRecord = null;
1813 sourceStack = null;
1814 } else {
1815 sourceStack = sourceRecord.task.stack;
1816 }
1817 } else {
1818 sourceStack = null;
1819 }
1820
1821 boolean movedHome = false;
1822 ActivityStack targetStack;
1823
1824 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001825 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001826
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001827 // We may want to try to place the new activity in to an existing task. We always
1828 // do this if the target activity is singleTask or singleInstance; we will also do
1829 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1830 // us to still place it in a new task: multi task, always doc mode, or being asked to
1831 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001832 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1833 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001834 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001835 // If bring to front is requested, and no result is requested and we have not
1836 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001837 // we can find a task that was started with this same
1838 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001839 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001840 // See if there is a task to bring to the front. If this is
1841 // a SINGLE_INSTANCE activity, there can be one and only one
1842 // instance of it in the history, and it is always in its own
1843 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001844 ActivityRecord intentActivity = !launchSingleInstance ?
1845 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001846 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001847 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001848 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001849 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001850 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1851 }
Craig Mautner29219d92013-04-16 20:19:12 -07001852 if (r.task == null) {
1853 r.task = intentActivity.task;
1854 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001855 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001856 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001857 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1858 + " from " + intentActivity);
Craig Mautner299f9602015-01-26 09:47:33 -08001859 targetStack.moveToFront("intentActivityFound");
Craig Mautner8849a5e2013-04-02 16:41:03 -07001860 if (intentActivity.task.intent == null) {
1861 // This task was started because of movement of
1862 // the activity based on affinity... now that we
1863 // are actually launching it, we can assign the
1864 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001865 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001866 }
1867 // If the target task is not in the front, then we need
1868 // to bring it to the front... except... well, with
1869 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1870 // to have the same behavior as if a new instance was
1871 // being started, which means not bringing it to the front
1872 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001873 final ActivityStack lastStack = getLastStack();
1874 ActivityRecord curTop = lastStack == null?
1875 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001876 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001877 if (curTop != null && (curTop.task != intentActivity.task ||
1878 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001879 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001880 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1881 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001882 // We really do want to push this one into the
1883 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001884 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001885 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1886 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001887 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001888 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1889 options, "bringingFoundTaskToFront");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001890 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001891 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1892 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001893 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001894 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001895 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001896 options = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001897 movedToFront = true;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001898 }
1899 }
1900 // If the caller has requested that the target task be
1901 // reset, then do so.
1902 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1903 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1904 }
1905 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1906 // We don't need to start a new activity, and
1907 // the client said not to do anything if that
1908 // is the case, so this is it! And for paranoia, make
1909 // sure we have correctly resumed the top activity.
1910 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001911 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001912
1913 // Make sure to notify Keyguard as well if we are not running an app
1914 // transition later.
1915 if (!movedToFront) {
1916 notifyActivityDrawnForKeyguard();
1917 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001918 } else {
1919 ActivityOptions.abort(options);
1920 }
1921 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1922 }
1923 if ((launchFlags &
1924 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1925 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1926 // The caller has requested to completely replace any
1927 // existing task with its new activity. Well that should
1928 // not be too hard...
1929 reuseTask = intentActivity.task;
1930 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001931 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001932 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001933 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001934 // In this situation we want to remove all activities
1935 // from the task up to the one being started. In most
1936 // cases this means we are resetting the task to its
1937 // initial state.
1938 ActivityRecord top =
1939 intentActivity.task.performClearTaskLocked(r, launchFlags);
1940 if (top != null) {
1941 if (top.frontOfTask) {
1942 // Activity aliases may mean we use different
1943 // intents for the top activity, so make sure
1944 // the task now has the identity of the new
1945 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001946 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001947 }
1948 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1949 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001950 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001951 } else {
1952 // A special case: we need to
1953 // start the activity because it is not currently
1954 // running, and the caller has asked to clear the
1955 // current task to have this activity at the top.
1956 addingToTask = true;
1957 // Now pretend like this activity is being started
1958 // by the top of its task, so it is put in the
1959 // right place.
1960 sourceRecord = intentActivity;
1961 }
1962 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1963 // In this case the top activity on the task is the
1964 // same as the one being launched, so we take that
1965 // as a request to bring the task to the foreground.
1966 // If the top activity in the task is the root
1967 // activity, deliver this new intent to it if it
1968 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001969 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001970 && intentActivity.realActivity.equals(r.realActivity)) {
1971 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1972 intentActivity.task);
1973 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001974 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001975 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001976 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
1977 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001978 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1979 // In this case we are launching the root activity
1980 // of the task, but with a different intent. We
1981 // should start a new instance on top.
1982 addingToTask = true;
1983 sourceRecord = intentActivity;
1984 }
1985 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1986 // In this case an activity is being launched in to an
1987 // existing task, without resetting that task. This
1988 // is typically the situation of launching an activity
1989 // from a notification or shortcut. We want to place
1990 // the new activity on top of the current task.
1991 addingToTask = true;
1992 sourceRecord = intentActivity;
1993 } else if (!intentActivity.task.rootWasReset) {
1994 // In this case we are launching in to an existing task
1995 // that has not yet been started from its front door.
1996 // The current task has been brought to the front.
1997 // Ideally, we'd probably like to place this new task
1998 // at the bottom of its stack, but that's a little hard
1999 // to do with the current organization of the code so
2000 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002001 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002002 }
2003 if (!addingToTask && reuseTask == null) {
2004 // We didn't do anything... but it was needed (a.k.a., client
2005 // don't use that intent!) And for paranoia, make
2006 // sure we have correctly resumed the top activity.
2007 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002008 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002009 if (!movedToFront) {
2010 // Make sure to notify Keyguard as well if we are not running an app
2011 // transition later.
2012 notifyActivityDrawnForKeyguard();
2013 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002014 } else {
2015 ActivityOptions.abort(options);
2016 }
2017 return ActivityManager.START_TASK_TO_FRONT;
2018 }
2019 }
2020 }
2021 }
2022
2023 //String uri = r.intent.toURI();
2024 //Intent intent2 = new Intent(uri);
2025 //Slog.i(TAG, "Given intent: " + r.intent);
2026 //Slog.i(TAG, "URI is: " + uri);
2027 //Slog.i(TAG, "To intent: " + intent2);
2028
2029 if (r.packageName != null) {
2030 // If the activity being launched is the same as the one currently
2031 // at the top, then we need to check if it should only be launched
2032 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002033 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002034 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002035 if (top != null && r.resultTo == null) {
2036 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2037 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002038 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002039 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002040 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2041 top.task);
2042 // For paranoia, make sure we have correctly
2043 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002044 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002045 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002046 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002047 }
2048 ActivityOptions.abort(options);
2049 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2050 // We don't need to start a new activity, and
2051 // the client said not to do anything if that
2052 // is the case, so this is it!
2053 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2054 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002055 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002056 return ActivityManager.START_DELIVERED_TO_TOP;
2057 }
2058 }
2059 }
2060 }
2061
2062 } else {
2063 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002064 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2065 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002066 }
2067 ActivityOptions.abort(options);
2068 return ActivityManager.START_CLASS_NOT_FOUND;
2069 }
2070
2071 boolean newTask = false;
2072 boolean keepCurTransition = false;
2073
Craig Mautnerbb742462014-07-07 15:28:55 -07002074 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002075 sourceRecord.task : null;
2076
Craig Mautner8849a5e2013-04-02 16:41:03 -07002077 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002078 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002079 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002080 if (isLockTaskModeViolation(reuseTask)) {
2081 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2082 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2083 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002084 newTask = true;
2085 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerbb742462014-07-07 15:28:55 -07002086 if (!launchTaskBehind) {
Craig Mautner299f9602015-01-26 09:47:33 -08002087 targetStack.moveToFront("startingNewTask");
Craig Mautnerbb742462014-07-07 15:28:55 -07002088 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002089 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002090 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2091 newTaskInfo != null ? newTaskInfo : r.info,
2092 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002093 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2094 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002095 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2096 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002097 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002098 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002099 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002100 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002101 if ((launchFlags &
2102 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2103 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2104 // Caller wants to appear on home activity, so before starting
2105 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002106 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002107 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002108 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002109 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002110 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002111 if (isLockTaskModeViolation(sourceTask)) {
2112 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2113 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2114 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002115 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002116 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002117 final TaskRecord topTask = targetStack.topTask();
2118 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002119 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2120 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002121 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002122 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002123 // In this case, we are adding the activity to an existing
2124 // task, but the caller has asked to clear that task if the
2125 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002126 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002127 keepCurTransition = true;
2128 if (top != null) {
2129 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002130 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002131 // For paranoia, make sure we have correctly
2132 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002133 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002134 if (doResume) {
2135 targetStack.resumeTopActivityLocked(null);
2136 }
2137 ActivityOptions.abort(options);
2138 return ActivityManager.START_DELIVERED_TO_TOP;
2139 }
2140 } else if (!addingToTask &&
2141 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2142 // In this case, we are launching an activity in our own task
2143 // that may already be running somewhere in the history, and
2144 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002145 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002146 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002147 final TaskRecord task = top.task;
2148 task.moveActivityToFrontLocked(top);
2149 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002150 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002151 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002152 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002153 if (doResume) {
2154 targetStack.resumeTopActivityLocked(null);
2155 }
2156 return ActivityManager.START_DELIVERED_TO_TOP;
2157 }
2158 }
2159 // An existing activity is starting this new activity, so we want
2160 // to keep the new one in the same task as the one that is starting
2161 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002162 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002163 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002164 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002165
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002166 } else if (inTask != null) {
2167 // The calling is asking that the new activity be started in an explicit
2168 // task it has provided to us.
2169 if (isLockTaskModeViolation(inTask)) {
2170 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2171 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2172 }
2173 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002174 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002175
2176 // Check whether we should actually launch the new activity in to the task,
2177 // or just reuse the current activity on top.
2178 ActivityRecord top = inTask.getTopActivity();
2179 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2180 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2181 || launchSingleTop || launchSingleTask) {
2182 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2183 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2184 // We don't need to start a new activity, and
2185 // the client said not to do anything if that
2186 // is the case, so this is it!
2187 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2188 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002189 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002190 return ActivityManager.START_DELIVERED_TO_TOP;
2191 }
2192 }
2193
Dianne Hackborn962d5352014-08-29 16:27:40 -07002194 if (!addingToTask) {
2195 // We don't actually want to have this activity added to the task, so just
2196 // stop here but still tell the caller that we consumed the intent.
2197 ActivityOptions.abort(options);
2198 return ActivityManager.START_TASK_TO_FRONT;
2199 }
2200
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002201 r.setTask(inTask, null);
2202 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2203 + " in explicit task " + r.task);
2204
Craig Mautner8849a5e2013-04-02 16:41:03 -07002205 } else {
2206 // This not being started from an existing activity, and not part
2207 // of a new task... just put it in the top task, though these days
2208 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002209 targetStack = adjustStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002210 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002211 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002212 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002213 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002214 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002215 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2216 + " in new guessed " + r.task);
2217 }
2218
2219 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002220 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002221
Craig Mautner76e2a762014-06-24 09:05:43 -07002222 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2223 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2224 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002225 if (newTask) {
2226 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2227 }
2228 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002229 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002230 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002231 if (!launchTaskBehind) {
2232 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002233 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002234 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002235 return ActivityManager.START_SUCCESS;
2236 }
2237
Craig Mautneree36c772014-07-16 14:56:05 -07002238 final void doPendingActivityLaunchesLocked(boolean doResume) {
2239 while (!mPendingActivityLaunches.isEmpty()) {
2240 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002241 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002242 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002243 }
2244 }
2245
Craig Mautner7f13ed32014-07-28 14:00:35 -07002246 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002247 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2248 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002249 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002250 mPendingActivityLaunches.remove(palNdx);
2251 }
2252 }
2253 }
2254
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002255 void acquireLaunchWakelock() {
2256 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2257 throw new IllegalStateException("Calling must be system uid");
2258 }
2259 mLaunchingActivity.acquire();
2260 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2261 // To be safe, don't allow the wake lock to be held for too long.
2262 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2263 }
2264 }
2265
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002266 /**
2267 * Called when the frontmost task is idle.
2268 * @return the state of mService.mBooting before this was called.
2269 */
2270 private boolean checkFinishBootingLocked() {
2271 final boolean booting = mService.mBooting;
2272 boolean enableScreen = false;
2273 mService.mBooting = false;
2274 if (!mService.mBooted) {
2275 mService.mBooted = true;
2276 enableScreen = true;
2277 }
2278 if (booting || enableScreen) {
2279 mService.postFinishBooting(booting, enableScreen);
2280 }
2281 return booting;
2282 }
2283
Craig Mautnerf3333272013-04-22 10:55:53 -07002284 // Checked.
2285 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2286 Configuration config) {
2287 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2288
Craig Mautnerf3333272013-04-22 10:55:53 -07002289 ArrayList<ActivityRecord> stops = null;
2290 ArrayList<ActivityRecord> finishes = null;
2291 ArrayList<UserStartedState> startingUsers = null;
2292 int NS = 0;
2293 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002294 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002295 boolean activityRemoved = false;
2296
2297 ActivityRecord r = ActivityRecord.forToken(token);
2298 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002299 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2300 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002301 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2302 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002303 if (fromTimeout) {
2304 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002305 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002306
2307 // This is a hack to semi-deal with a race condition
2308 // in the client where it can be constructed with a
2309 // newer configuration from when we asked it to launch.
2310 // We'll update with whatever configuration it now says
2311 // it used to launch.
2312 if (config != null) {
2313 r.configuration = config;
2314 }
2315
2316 // We are now idle. If someone is waiting for a thumbnail from
2317 // us, we can now deliver.
2318 r.idle = true;
2319
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002320 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002321 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002322 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002323 }
2324 }
2325
2326 if (allResumedActivitiesIdle()) {
2327 if (r != null) {
2328 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002329 }
2330
2331 if (mLaunchingActivity.isHeld()) {
2332 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2333 if (VALIDATE_WAKE_LOCK_CALLER &&
2334 Binder.getCallingUid() != Process.myUid()) {
2335 throw new IllegalStateException("Calling must be system uid");
2336 }
2337 mLaunchingActivity.release();
2338 }
2339 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002340 }
2341
2342 // Atomically retrieve all of the other things to do.
2343 stops = processStoppingActivitiesLocked(true);
2344 NS = stops != null ? stops.size() : 0;
2345 if ((NF=mFinishingActivities.size()) > 0) {
2346 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2347 mFinishingActivities.clear();
2348 }
2349
Craig Mautnerf3333272013-04-22 10:55:53 -07002350 if (mStartingUsers.size() > 0) {
2351 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2352 mStartingUsers.clear();
2353 }
2354
Craig Mautnerf3333272013-04-22 10:55:53 -07002355 // Stop any activities that are scheduled to do so but have been
2356 // waiting for the next one to start.
2357 for (int i = 0; i < NS; i++) {
2358 r = stops.get(i);
2359 final ActivityStack stack = r.task.stack;
2360 if (r.finishing) {
2361 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2362 } else {
2363 stack.stopActivityLocked(r);
2364 }
2365 }
2366
2367 // Finish 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 < NF; i++) {
2370 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002371 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002372 }
2373
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002374 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002375 // Complete user switch
2376 if (startingUsers != null) {
2377 for (int i = 0; i < startingUsers.size(); i++) {
2378 mService.finishUserSwitch(startingUsers.get(i));
2379 }
2380 }
2381 // Complete starting up of background users
2382 if (mStartingBackgroundUsers.size() > 0) {
2383 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2384 mStartingBackgroundUsers.clear();
2385 for (int i = 0; i < startingUsers.size(); i++) {
2386 mService.finishUserBoot(startingUsers.get(i));
2387 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002388 }
2389 }
2390
2391 mService.trimApplications();
2392 //dump();
2393 //mWindowManager.dump();
2394
Craig Mautnerf3333272013-04-22 10:55:53 -07002395 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002396 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002397 }
2398
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002399 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002400 }
2401
Craig Mautner8e569572013-10-11 17:36:59 -07002402 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002403 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002404 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2405 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002406 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2407 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2408 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002409 }
Craig Mautner19091252013-10-05 00:03:53 -07002410 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002411 }
2412
2413 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002414 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2415 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002416 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2417 stacks.get(stackNdx).closeSystemDialogsLocked();
2418 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002419 }
2420 }
2421
Craig Mautner93529a42013-10-04 15:03:13 -07002422 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002423 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002424 }
2425
Craig Mautner8d341ef2013-03-26 09:03:27 -07002426 /**
2427 * @return true if some activity was finished (or would have finished if doit were true).
2428 */
2429 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2430 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002431 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2432 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002433 final int numStacks = stacks.size();
2434 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2435 final ActivityStack stack = stacks.get(stackNdx);
2436 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2437 didSomething = true;
2438 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002439 }
2440 }
2441 return didSomething;
2442 }
2443
Dianne Hackborna413dc02013-07-12 12:02:55 -07002444 void updatePreviousProcessLocked(ActivityRecord r) {
2445 // Now that this process has stopped, we may want to consider
2446 // it to be the previous app to try to keep around in case
2447 // the user wants to return to it.
2448
2449 // First, found out what is currently the foreground app, so that
2450 // we don't blow away the previous app if this activity is being
2451 // hosted by the process that is actually still the foreground.
2452 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002453 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2454 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002455 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2456 final ActivityStack stack = stacks.get(stackNdx);
2457 if (isFrontStack(stack)) {
2458 if (stack.mResumedActivity != null) {
2459 fgApp = stack.mResumedActivity.app;
2460 } else if (stack.mPausingActivity != null) {
2461 fgApp = stack.mPausingActivity.app;
2462 }
2463 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002464 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002465 }
2466 }
2467
2468 // Now set this one as the previous process, only if that really
2469 // makes sense to.
2470 if (r.app != null && fgApp != null && r.app != fgApp
2471 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002472 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002473 mService.mPreviousProcess = r.app;
2474 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2475 }
2476 }
2477
Craig Mautner05d29032013-05-03 13:40:13 -07002478 boolean resumeTopActivitiesLocked() {
2479 return resumeTopActivitiesLocked(null, null, null);
2480 }
2481
2482 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2483 Bundle targetOptions) {
2484 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002485 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002486 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002487 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002488 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002489 if (isFrontStack(targetStack)) {
2490 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2491 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002492
Craig Mautnere0a38842013-12-16 16:14:02 -08002493 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2494 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002495 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2496 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002497 if (stack == targetStack) {
2498 // Already started above.
2499 continue;
2500 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002501 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002502 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002503 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002504 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002505 }
Craig Mautner05d29032013-05-03 13:40:13 -07002506 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002507 }
2508
Todd Kennedy539db512014-12-15 09:57:55 -08002509 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002510 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2511 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002512 final int numStacks = stacks.size();
2513 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2514 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002515 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002516 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002517 }
2518 }
2519
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002520 void finishVoiceTask(IVoiceInteractionSession session) {
2521 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2522 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2523 final int numStacks = stacks.size();
2524 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2525 final ActivityStack stack = stacks.get(stackNdx);
2526 stack.finishVoiceTask(session);
2527 }
2528 }
2529 }
2530
Craig Mautner299f9602015-01-26 09:47:33 -08002531 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002532 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2533 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002534 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002535 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2536 // Caller wants the home activity moved with it. To accomplish this,
2537 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002538 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002539 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002540 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08002541 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2542 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002543 }
2544
Craig Mautner967212c2013-04-13 21:10:58 -07002545 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002546 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2547 if (activityContainer != null) {
2548 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002549 }
2550 return null;
2551 }
2552
Craig Mautner967212c2013-04-13 21:10:58 -07002553 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002554 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002555 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2556 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002557 }
2558 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002559 }
2560
Craig Mautner4a1cb222013-12-04 16:14:06 -08002561 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002562 ActivityRecord homeActivity = getHomeActivity();
2563 if (homeActivity != null) {
2564 return homeActivity.appToken;
2565 }
2566 return null;
2567 }
2568
2569 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002570 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2571 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2572 final TaskRecord task = tasks.get(taskNdx);
2573 if (task.isHomeTask()) {
2574 final ArrayList<ActivityRecord> activities = task.mActivities;
2575 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2576 final ActivityRecord r = activities.get(activityNdx);
2577 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002578 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002579 }
2580 }
2581 }
2582 }
2583 return null;
2584 }
2585
Todd Kennedyca4d8422015-01-15 15:19:22 -08002586 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002587 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002588 ActivityContainer activityContainer =
2589 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002590 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002591 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002592 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002593 return activityContainer;
2594 }
2595
Craig Mautner34b73df2014-01-12 21:11:08 -08002596 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002597 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2598 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2599 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002600 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2601 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002602 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002603 }
2604 }
2605
Craig Mautner95da1082014-02-24 17:54:35 -08002606 void deleteActivityContainer(IActivityContainer container) {
2607 ActivityContainer activityContainer = (ActivityContainer)container;
2608 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002609 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2610 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002611 final int stackId = activityContainer.mStackId;
2612 mActivityContainers.remove(stackId);
2613 mWindowManager.removeStack(stackId);
2614 }
2615 }
2616
Wale Ogunwale60454db2015-01-23 16:05:07 -08002617 void resizeStackLocked(int stackId, Rect bounds) {
2618 final ActivityStack stack = getStack(stackId);
2619 if (stack == null) {
2620 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2621 return;
2622 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002623
2624 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002625 if (r != null && !r.task.mResizeable) {
2626 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002627 return;
2628 }
2629
Wale Ogunwale60454db2015-01-23 16:05:07 -08002630 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2631 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002632 if (r != null) {
2633 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2634 // And we need to make sure at this point that all other activities
2635 // are made visible with the correct configuration.
2636 ensureActivitiesVisibleLocked(r, 0);
2637 if (!updated) {
2638 resumeTopActivitiesLocked(stack, null, null);
2639 }
2640 }
2641 }
2642 }
2643
Todd Kennedy4900bf92015-01-16 16:05:14 -08002644 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002645 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2646 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002647 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002648 }
2649
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002650 ActivityContainer activityContainer = new ActivityContainer(stackId);
2651 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002652 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002653 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002654 }
2655
2656 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002657 while (true) {
2658 if (++mLastStackId <= HOME_STACK_ID) {
2659 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002660 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002661 if (getStack(mLastStackId) == null) {
2662 break;
2663 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002664 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002665 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002666 }
2667
Wale Ogunwale7de05352014-12-12 15:21:33 -08002668 private boolean restoreRecentTaskLocked(TaskRecord task) {
2669 ActivityStack stack = null;
2670 // Determine stack to restore task to.
2671 if (mLeanbackOnlyDevice) {
2672 // There is only one stack for lean back devices.
2673 stack = mHomeStack;
2674 } else {
2675 // Look for the top stack on the home display that isn't the home stack.
2676 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2677 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2678 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -08002679 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002680 stack = tmpStack;
2681 break;
2682 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002683 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002684 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002685
2686 if (stack == null) {
2687 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2688 // before an application stack is created...Go ahead and create one on the default
2689 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002690 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002691 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002692 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002693 if (DEBUG_RECENTS)
2694 Slog.v(TAG, "Created stack=" + stack + " for recents restoration.");
2695 }
2696
2697 if (stack == null) {
2698 // What does this mean??? Not sure how we would get here...
2699 if (DEBUG_RECENTS)
2700 Slog.v(TAG, "Unable to find/create stack to restore recent task=" + task);
2701 return false;
2702 }
2703
2704 stack.addTask(task, false, false);
2705 if (DEBUG_RECENTS)
2706 Slog.v(TAG, "Added restored task=" + task + " to stack=" + stack);
2707 final ArrayList<ActivityRecord> activities = task.mActivities;
2708 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2709 final ActivityRecord r = activities.get(activityNdx);
2710 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2711 r.info.screenOrientation, r.fullscreen,
2712 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2713 r.userId, r.info.configChanges, task.voiceSession != null,
2714 r.mLaunchTaskBehind);
2715 }
2716 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002717 }
2718
Craig Mautner299f9602015-01-26 09:47:33 -08002719 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002720 final TaskRecord task = anyTaskForIdLocked(taskId);
2721 if (task == null) {
2722 return;
2723 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002724 final ActivityStack stack = getStack(stackId);
2725 if (stack == null) {
2726 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2727 return;
2728 }
Craig Mautner299f9602015-01-26 09:47:33 -08002729 task.stack.removeTask(task, "moveTaskToStack");
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002730 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002731 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002732 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002733 }
2734
Craig Mautnerac6f8432013-07-17 13:24:59 -07002735 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002736 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2738 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002739 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2740 final ActivityStack stack = stacks.get(stackNdx);
2741 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002742 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2743 continue;
2744 }
2745 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2746 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2747 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002748 continue;
2749 }
2750 final ActivityRecord ar = stack.findTaskLocked(r);
2751 if (ar != null) {
2752 return ar;
2753 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002754 }
2755 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002756 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002757 return null;
2758 }
2759
2760 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002761 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2762 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002763 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2764 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2765 if (ar != null) {
2766 return ar;
2767 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002768 }
2769 }
2770 return null;
2771 }
2772
Craig Mautner8d341ef2013-03-26 09:03:27 -07002773 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002774 scheduleSleepTimeout();
2775 if (!mGoingToSleep.isHeld()) {
2776 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002777 if (mLaunchingActivity.isHeld()) {
2778 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2779 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002780 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002781 mLaunchingActivity.release();
2782 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002783 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002784 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002785 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002786 }
2787
2788 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002789 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002790
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002791 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002792 final long endTime = System.currentTimeMillis() + timeout;
2793 while (true) {
2794 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002795 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2796 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002797 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2798 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2799 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002800 }
2801 if (cantShutdown) {
2802 long timeRemaining = endTime - System.currentTimeMillis();
2803 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002804 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002805 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002806 } catch (InterruptedException e) {
2807 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002808 } else {
2809 Slog.w(TAG, "Activity manager shutdown timed out");
2810 timedout = true;
2811 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002812 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002813 } else {
2814 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002815 }
2816 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002817
2818 // Force checkReadyForSleep to complete.
2819 mSleepTimeout = true;
2820 checkReadyForSleepLocked();
2821
Craig Mautner8d341ef2013-03-26 09:03:27 -07002822 return timedout;
2823 }
2824
2825 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002826 removeSleepTimeouts();
2827 if (mGoingToSleep.isHeld()) {
2828 mGoingToSleep.release();
2829 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002830 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2831 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002832 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2833 final ActivityStack stack = stacks.get(stackNdx);
2834 stack.awakeFromSleepingLocked();
2835 if (isFrontStack(stack)) {
2836 resumeTopActivitiesLocked();
2837 }
Craig Mautner5314a402013-09-26 12:40:16 -07002838 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002839 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002840 mGoingToSleepActivities.clear();
2841 }
2842
2843 void activitySleptLocked(ActivityRecord r) {
2844 mGoingToSleepActivities.remove(r);
2845 checkReadyForSleepLocked();
2846 }
2847
2848 void checkReadyForSleepLocked() {
2849 if (!mService.isSleepingOrShuttingDown()) {
2850 // Do not care.
2851 return;
2852 }
2853
2854 if (!mSleepTimeout) {
2855 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002856 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2857 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002858 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2859 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2860 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002861 }
2862
2863 if (mStoppingActivities.size() > 0) {
2864 // Still need to tell some activities to stop; can't sleep yet.
2865 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2866 + mStoppingActivities.size() + " activities");
2867 scheduleIdleLocked();
2868 dontSleep = true;
2869 }
2870
2871 if (mGoingToSleepActivities.size() > 0) {
2872 // Still need to tell some activities to sleep; can't sleep yet.
2873 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2874 + mGoingToSleepActivities.size() + " activities");
2875 dontSleep = true;
2876 }
2877
2878 if (dontSleep) {
2879 return;
2880 }
2881 }
2882
Craig Mautnere0a38842013-12-16 16:14:02 -08002883 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2884 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002885 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2886 stacks.get(stackNdx).goToSleep();
2887 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002888 }
2889
2890 removeSleepTimeouts();
2891
2892 if (mGoingToSleep.isHeld()) {
2893 mGoingToSleep.release();
2894 }
2895 if (mService.mShuttingDown) {
2896 mService.notifyAll();
2897 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002898 }
2899
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002900 boolean reportResumedActivityLocked(ActivityRecord r) {
2901 final ActivityStack stack = r.task.stack;
2902 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002903 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002904 }
2905 if (allResumedActivitiesComplete()) {
2906 ensureActivitiesVisibleLocked(null, 0);
2907 mWindowManager.executeAppTransition();
2908 return true;
2909 }
2910 return false;
2911 }
2912
Craig Mautner8d341ef2013-03-26 09:03:27 -07002913 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002914 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2915 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002916 final int numStacks = stacks.size();
2917 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2918 final ActivityStack stack = stacks.get(stackNdx);
2919 stack.handleAppCrashLocked(app);
2920 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002921 }
2922 }
2923
Jose Lima4b6c6692014-08-12 17:41:12 -07002924 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002925 final ActivityStack stack = r.task.stack;
2926 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002927 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2928 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002929 return false;
2930 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002931 final boolean isVisible = stack.hasVisibleBehindActivity();
2932 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2933 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002934
2935 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002936 if (top == null || top == r || (visible == isVisible)) {
2937 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2938 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002939 return true;
2940 }
2941
2942 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002943 if (visible && top.fullscreen) {
2944 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07002945 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07002946 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07002947 " top.app.thread=" + top.app.thread);
2948 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002949 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2950 // Only the activity set as currently visible behind should actively reset its
2951 // visible behind state.
2952 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
2953 + visible + " stack.getVisibleBehindActivity()=" +
2954 stack.getVisibleBehindActivity() + " r=" + r);
2955 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002956 }
2957
Jose Lima4b6c6692014-08-12 17:41:12 -07002958 stack.setVisibleBehindActivity(visible ? r : null);
2959 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002960 // Make the activity immediately above r opaque.
2961 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2962 if (next != null) {
2963 mService.convertFromTranslucent(next.appToken);
2964 }
2965 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002966 if (top.app != null && top.app.thread != null) {
2967 // Notify the top app of the change.
2968 try {
2969 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2970 } catch (RemoteException e) {
2971 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002972 }
2973 return true;
2974 }
2975
Craig Mautnerbb742462014-07-07 15:28:55 -07002976 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2977 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2978 r.mLaunchTaskBehind = false;
2979 final TaskRecord task = r.task;
2980 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002981 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002982 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002983 mWindowManager.setAppVisibility(r.appToken, false);
2984 }
2985
2986 void scheduleLaunchTaskBehindComplete(IBinder token) {
2987 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2988 }
2989
Craig Mautnerde4ef022013-04-07 19:01:33 -07002990 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002991 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002992 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2993 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002994 final int topStackNdx = stacks.size() - 1;
2995 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2996 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002997 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002998 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002999 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003000 }
3001
3002 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003003 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3004 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003005 final int numStacks = stacks.size();
3006 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3007 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003008 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003009 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003010 }
3011 }
3012
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003013 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3014 // Examine all activities currently running in the process.
3015 TaskRecord firstTask = null;
3016 // Tasks is non-null only if two or more tasks are found.
3017 ArraySet<TaskRecord> tasks = null;
3018 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3019 for (int i=0; i<app.activities.size(); i++) {
3020 ActivityRecord r = app.activities.get(i);
3021 // First, if we find an activity that is in the process of being destroyed,
3022 // then we just aren't going to do anything for now; we want things to settle
3023 // down before we try to prune more activities.
3024 if (r.finishing || r.state == ActivityState.DESTROYING
3025 || r.state == ActivityState.DESTROYED) {
3026 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3027 return;
3028 }
3029 // Don't consider any activies that are currently not in a state where they
3030 // can be destroyed.
3031 if (r.visible || !r.stopped || !r.haveState
3032 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
3033 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
3034 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3035 continue;
3036 }
3037 if (r.task != null) {
3038 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3039 + " from " + r);
3040 if (firstTask == null) {
3041 firstTask = r.task;
3042 } else if (firstTask != r.task) {
3043 if (tasks == null) {
3044 tasks = new ArraySet<>();
3045 tasks.add(firstTask);
3046 }
3047 tasks.add(r.task);
3048 }
3049 }
3050 }
3051 if (tasks == null) {
3052 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3053 return;
3054 }
3055 // If we have activities in multiple tasks that are in a position to be destroyed,
3056 // let's iterate through the tasks and release the oldest one.
3057 final int numDisplays = mActivityDisplays.size();
3058 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3059 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3060 // Step through all stacks starting from behind, to hit the oldest things first.
3061 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3062 final ActivityStack stack = stacks.get(stackNdx);
3063 // Try to release activities in this stack; if we manage to, we are done.
3064 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3065 return;
3066 }
3067 }
3068 }
3069 }
3070
Craig Mautner8d341ef2013-03-26 09:03:27 -07003071 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003072 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003073 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003074 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003075
Craig Mautner858d8a62013-04-23 17:08:34 -07003076 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003077 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3078 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003079 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003080 final ActivityStack stack = stacks.get(stackNdx);
3081 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003082 TaskRecord task = stack.topTask();
3083 if (task != null) {
3084 mWindowManager.moveTaskToTop(task.taskId);
3085 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003086 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003087 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003088
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003089 ActivityStack stack = getStack(restoreStackId);
3090 if (stack == null) {
3091 stack = mHomeStack;
3092 }
3093 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003094 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003095 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003096 TaskRecord task = stack.topTask();
3097 if (task != null) {
3098 mWindowManager.moveTaskToTop(task.taskId);
3099 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003100 } else {
3101 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003102 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003103 }
Craig Mautner93529a42013-10-04 15:03:13 -07003104 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003105 }
3106
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003107 /**
3108 * Add background users to send boot completed events to.
3109 * @param userId The user being started in the background
3110 * @param uss The state object for the user.
3111 */
3112 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3113 mStartingBackgroundUsers.add(uss);
3114 }
3115
Craig Mautnerde4ef022013-04-07 19:01:33 -07003116 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003117 ArrayList<ActivityRecord> stops = null;
3118
3119 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003120 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3121 ActivityRecord s = mStoppingActivities.get(activityNdx);
3122 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
3123 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
3124 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3125 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003126 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003127 if (s.finishing) {
3128 // If this activity is finishing, it is sitting on top of
3129 // everyone else but we now know it is no longer needed...
3130 // so get rid of it. Otherwise, we need to go through the
3131 // normal flow and hide it once we determine that it is
3132 // hidden by the activities in front of it.
3133 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003134 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003135 }
3136 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003137 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003138 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3139 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003140 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003141 }
3142 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003143 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003144 }
3145 }
3146
3147 return stops;
3148 }
3149
Craig Mautnercf910b02013-04-23 11:23:27 -07003150 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003151 // FIXME
3152/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3153 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003154 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003155 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003156 if (isFrontStack(stack)) {
3157 if (r == null) {
3158 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3159 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003160 final ActivityRecord pausing = stack.mPausingActivity;
3161 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003162 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003163 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003164 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003165 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003166 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003167 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003168 }
3169 }
3170 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003171 final ActivityRecord resumed = stack.mResumedActivity;
3172 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003173 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003174 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003175 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003176 if (r != null && (state == ActivityState.INITIALIZING
3177 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003178 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003179 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003180 }
3181 }
3182 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003183*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003184 }
3185
Craig Mautner27084302013-03-25 08:05:25 -07003186 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003187 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003188 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003189 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3190 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3191 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003192 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003193 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003194
Craig Mautner20e72272013-04-01 13:45:53 -07003195 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003196 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003197 }
3198
Dianne Hackborn390517b2013-05-30 15:03:32 -07003199 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3200 boolean needSep, String prefix) {
3201 if (activity != null) {
3202 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3203 if (needSep) {
3204 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003205 }
3206 pw.print(prefix);
3207 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003208 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003209 }
3210 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003211 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003212 }
3213
Craig Mautner8d341ef2013-03-26 09:03:27 -07003214 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3215 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003216 boolean printed = false;
3217 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003218 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3219 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003220 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003221 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003222 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003223 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003224 final ActivityStack stack = stacks.get(stackNdx);
3225 StringBuilder stackHeader = new StringBuilder(128);
3226 stackHeader.append(" Stack #");
3227 stackHeader.append(stack.mStackId);
3228 stackHeader.append(":");
3229 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3230 needSep, stackHeader.toString());
3231 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3232 !dumpAll, false, dumpPackage, true,
3233 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003234
Craig Mautner4a1cb222013-12-04 16:14:06 -08003235 needSep = printed;
3236 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3237 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003238 if (pr) {
3239 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003240 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003241 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003242 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3243 " mResumedActivity: ");
3244 if (pr) {
3245 printed = true;
3246 needSep = false;
3247 }
3248 if (dumpAll) {
3249 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3250 " mLastPausedActivity: ");
3251 if (pr) {
3252 printed = true;
3253 needSep = true;
3254 }
3255 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3256 needSep, " mLastNoHistoryActivity: ");
3257 }
3258 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003259 }
3260 }
3261
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003262 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3263 false, dumpPackage, true, " Activities waiting to finish:", null);
3264 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3265 false, dumpPackage, true, " Activities waiting to stop:", null);
3266 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3267 false, dumpPackage, true, " Activities waiting for another to become visible:",
3268 null);
3269 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3270 false, dumpPackage, true, " Activities waiting to sleep:", null);
3271 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3272 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003273
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003274 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003275 }
3276
Dianne Hackborn390517b2013-05-30 15:03:32 -07003277 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003278 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003279 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003280 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003281 String innerPrefix = null;
3282 String[] args = null;
3283 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003284 for (int i=list.size()-1; i>=0; i--) {
3285 final ActivityRecord r = list.get(i);
3286 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3287 continue;
3288 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003289 if (innerPrefix == null) {
3290 innerPrefix = prefix + " ";
3291 args = new String[0];
3292 }
3293 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003294 final boolean full = !brief && (complete || !r.isInHistory());
3295 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003296 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003297 needNL = false;
3298 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003299 if (header1 != null) {
3300 pw.println(header1);
3301 header1 = null;
3302 }
3303 if (header2 != null) {
3304 pw.println(header2);
3305 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003306 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003307 if (lastTask != r.task) {
3308 lastTask = r.task;
3309 pw.print(prefix);
3310 pw.print(full ? "* " : " ");
3311 pw.println(lastTask);
3312 if (full) {
3313 lastTask.dump(pw, prefix + " ");
3314 } else if (complete) {
3315 // Complete + brief == give a summary. Isn't that obvious?!?
3316 if (lastTask.intent != null) {
3317 pw.print(prefix); pw.print(" ");
3318 pw.println(lastTask.intent.toInsecureStringWithClip());
3319 }
3320 }
3321 }
3322 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3323 pw.print(" #"); pw.print(i); pw.print(": ");
3324 pw.println(r);
3325 if (full) {
3326 r.dump(pw, innerPrefix);
3327 } else if (complete) {
3328 // Complete + brief == give a summary. Isn't that obvious?!?
3329 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3330 if (r.app != null) {
3331 pw.print(innerPrefix); pw.println(r.app);
3332 }
3333 }
3334 if (client && r.app != null && r.app.thread != null) {
3335 // flush anything that is already in the PrintWriter since the thread is going
3336 // to write to the file descriptor directly
3337 pw.flush();
3338 try {
3339 TransferPipe tp = new TransferPipe();
3340 try {
3341 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3342 r.appToken, innerPrefix, args);
3343 // Short timeout, since blocking here can
3344 // deadlock with the application.
3345 tp.go(fd, 2000);
3346 } finally {
3347 tp.kill();
3348 }
3349 } catch (IOException e) {
3350 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3351 } catch (RemoteException e) {
3352 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3353 }
3354 needNL = true;
3355 }
3356 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003357 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003358 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003359
Craig Mautnerf3333272013-04-22 10:55:53 -07003360 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003361 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003362 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3363 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003364 }
3365
3366 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003367 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003368 }
3369
3370 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003371 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003372 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3373 }
3374
Craig Mautner05d29032013-05-03 13:40:13 -07003375 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003376 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3377 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3378 }
Craig Mautner05d29032013-05-03 13:40:13 -07003379 }
3380
Craig Mautner0eea92c2013-05-16 13:35:39 -07003381 void removeSleepTimeouts() {
3382 mSleepTimeout = false;
3383 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3384 }
3385
3386 final void scheduleSleepTimeout() {
3387 removeSleepTimeouts();
3388 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3389 }
3390
Craig Mautner4a1cb222013-12-04 16:14:06 -08003391 @Override
3392 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003393 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003394 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3395 }
3396
3397 @Override
3398 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003399 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003400 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3401 }
3402
3403 @Override
3404 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003405 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003406 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3407 }
3408
3409 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003410 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003411 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003412 newDisplay = mActivityDisplays.get(displayId) == null;
3413 if (newDisplay) {
3414 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003415 if (activityDisplay.mDisplay == null) {
3416 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3417 return;
3418 }
Craig Mautner4504de52013-12-20 09:06:56 -08003419 mActivityDisplays.put(displayId, activityDisplay);
3420 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003421 }
Craig Mautner4504de52013-12-20 09:06:56 -08003422 if (newDisplay) {
3423 mWindowManager.onDisplayAdded(displayId);
3424 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003425 }
3426
3427 public void handleDisplayRemovedLocked(int displayId) {
3428 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003429 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3430 if (activityDisplay != null) {
3431 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003432 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003433 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003434 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003435 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003436 }
3437 }
3438 mWindowManager.onDisplayRemoved(displayId);
3439 }
3440
3441 public void handleDisplayChangedLocked(int displayId) {
3442 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003443 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3444 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003445 // TODO: Update the bounds.
3446 }
3447 }
3448 mWindowManager.onDisplayChanged(displayId);
3449 }
3450
3451 StackInfo getStackInfo(ActivityStack stack) {
3452 StackInfo info = new StackInfo();
3453 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3454 info.displayId = Display.DEFAULT_DISPLAY;
3455 info.stackId = stack.mStackId;
3456
3457 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3458 final int numTasks = tasks.size();
3459 int[] taskIds = new int[numTasks];
3460 String[] taskNames = new String[numTasks];
3461 for (int i = 0; i < numTasks; ++i) {
3462 final TaskRecord task = tasks.get(i);
3463 taskIds[i] = task.taskId;
3464 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3465 : task.realActivity != null ? task.realActivity.flattenToString()
3466 : task.getTopActivity() != null ? task.getTopActivity().packageName
3467 : "unknown";
3468 }
3469 info.taskIds = taskIds;
3470 info.taskNames = taskNames;
3471 return info;
3472 }
3473
3474 StackInfo getStackInfoLocked(int stackId) {
3475 ActivityStack stack = getStack(stackId);
3476 if (stack != null) {
3477 return getStackInfo(stack);
3478 }
3479 return null;
3480 }
3481
3482 ArrayList<StackInfo> getAllStackInfosLocked() {
3483 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003484 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3485 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003486 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3487 list.add(getStackInfo(stacks.get(ndx)));
3488 }
3489 }
3490 return list;
3491 }
3492
Jason Monka8f569c2014-07-07 12:15:21 -04003493 void showLockTaskToast() {
3494 mLockTaskNotify.showToast(mLockTaskIsLocked);
3495 }
3496
Craig Mautner299f9602015-01-26 09:47:33 -08003497 void setLockTaskModeLocked(TaskRecord task, boolean isLocked, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003498 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003499 // Take out of lock task mode if necessary
3500 if (mLockTaskModeTask != null) {
3501 final Message lockTaskMsg = Message.obtain();
3502 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3503 lockTaskMsg.what = LOCK_TASK_END_MSG;
3504 mLockTaskModeTask = null;
3505 mHandler.sendMessage(lockTaskMsg);
3506 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003507 return;
3508 }
3509 if (isLockTaskModeViolation(task)) {
3510 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3511 return;
3512 }
3513 mLockTaskModeTask = task;
Craig Mautner299f9602015-01-26 09:47:33 -08003514 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003515 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003516
3517 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003518 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3519 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003520 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003521 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003522 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003523 }
3524
3525 boolean isLockTaskModeViolation(TaskRecord task) {
3526 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3527 }
3528
3529 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3530 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003531 final Message lockTaskMsg = Message.obtain();
3532 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3533 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003534 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003535 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003536 }
3537 }
3538
3539 boolean isInLockTaskMode() {
3540 return mLockTaskModeTask != null;
3541 }
3542
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003543 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003544
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003545 public ActivityStackSupervisorHandler(Looper looper) {
3546 super(looper);
3547 }
3548
Craig Mautnerf3333272013-04-22 10:55:53 -07003549 void activityIdleInternal(ActivityRecord r) {
3550 synchronized (mService) {
3551 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3552 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003553 }
3554
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003555 @Override
3556 public void handleMessage(Message msg) {
3557 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003558 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003559 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003560 if (mService.mDidDexOpt) {
3561 mService.mDidDexOpt = false;
3562 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3563 nmsg.obj = msg.obj;
3564 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3565 return;
3566 }
3567 // We don't at this point know if the activity is fullscreen,
3568 // so we need to be conservative and assume it isn't.
3569 activityIdleInternal((ActivityRecord)msg.obj);
3570 } break;
3571 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003572 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003573 activityIdleInternal((ActivityRecord)msg.obj);
3574 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003575 case RESUME_TOP_ACTIVITY_MSG: {
3576 synchronized (mService) {
3577 resumeTopActivitiesLocked();
3578 }
3579 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003580 case SLEEP_TIMEOUT_MSG: {
3581 synchronized (mService) {
3582 if (mService.isSleepingOrShuttingDown()) {
3583 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3584 mSleepTimeout = true;
3585 checkReadyForSleepLocked();
3586 }
3587 }
3588 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003589 case LAUNCH_TIMEOUT_MSG: {
3590 if (mService.mDidDexOpt) {
3591 mService.mDidDexOpt = false;
3592 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3593 return;
3594 }
3595 synchronized (mService) {
3596 if (mLaunchingActivity.isHeld()) {
3597 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3598 if (VALIDATE_WAKE_LOCK_CALLER
3599 && Binder.getCallingUid() != Process.myUid()) {
3600 throw new IllegalStateException("Calling must be system uid");
3601 }
3602 mLaunchingActivity.release();
3603 }
3604 }
3605 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003606 case HANDLE_DISPLAY_ADDED: {
3607 handleDisplayAddedLocked(msg.arg1);
3608 } break;
3609 case HANDLE_DISPLAY_CHANGED: {
3610 handleDisplayChangedLocked(msg.arg1);
3611 } break;
3612 case HANDLE_DISPLAY_REMOVED: {
3613 handleDisplayRemovedLocked(msg.arg1);
3614 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003615 case CONTAINER_CALLBACK_VISIBILITY: {
3616 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003617 final IActivityContainerCallback callback = container.mCallback;
3618 if (callback != null) {
3619 try {
3620 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3621 } catch (RemoteException e) {
3622 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003623 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003624 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003625 case LOCK_TASK_START_MSG: {
3626 // When lock task starts, we disable the status bars.
3627 try {
Jason Monk62515be2014-05-21 16:06:19 -04003628 if (mLockTaskNotify == null) {
3629 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003630 }
Jason Monk62515be2014-05-21 16:06:19 -04003631 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003632 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003633 if (getStatusBarService() != null) {
3634 int flags =
3635 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003636 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003637 flags ^= StatusBarManager.DISABLE_HOME
3638 | StatusBarManager.DISABLE_RECENT;
3639 }
3640 getStatusBarService().disable(flags, mToken,
3641 mService.mContext.getPackageName());
3642 }
3643 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003644 if (getDevicePolicyManager() != null) {
3645 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003646 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003647 }
justinzhang5286d3f2014-05-12 17:06:01 -04003648 } catch (RemoteException ex) {
3649 throw new RuntimeException(ex);
3650 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003651 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003652 case LOCK_TASK_END_MSG: {
3653 // When lock task ends, we enable the status bars.
3654 try {
Jason Monk62515be2014-05-21 16:06:19 -04003655 if (getStatusBarService() != null) {
3656 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3657 mService.mContext.getPackageName());
3658 }
3659 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003660 if (getDevicePolicyManager() != null) {
3661 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3662 msg.arg1);
3663 }
Jason Monk62515be2014-05-21 16:06:19 -04003664 if (mLockTaskNotify == null) {
3665 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3666 }
3667 mLockTaskNotify.show(false);
3668 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003669 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003670 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003671 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003672 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003673 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003674 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003675 new LockPatternUtils(mService.mContext)
3676 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003677 }
3678 } catch (SettingNotFoundException e) {
3679 // No setting, don't lock.
3680 }
justinzhang5286d3f2014-05-12 17:06:01 -04003681 } catch (RemoteException ex) {
3682 throw new RuntimeException(ex);
3683 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003684 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003685 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3686 final ActivityContainer container = (ActivityContainer) msg.obj;
3687 final IActivityContainerCallback callback = container.mCallback;
3688 if (callback != null) {
3689 try {
3690 callback.onAllActivitiesComplete(container.asBinder());
3691 } catch (RemoteException e) {
3692 }
3693 }
3694 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003695 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3696 synchronized (mService) {
3697 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3698 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003699 final ActivityContainer container = (ActivityContainer) msg.obj;
3700 container.mStack.finishAllActivitiesLocked(true);
3701 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003702 }
3703 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003704 case LAUNCH_TASK_BEHIND_COMPLETE: {
3705 synchronized (mService) {
3706 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3707 if (r != null) {
3708 handleLaunchTaskBehindCompleteLocked(r);
3709 }
3710 }
3711 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003712 }
3713 }
3714 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003715
Ying Wangb081a592014-04-22 15:20:16 -07003716 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003717 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003718 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003719 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003720 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003721 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003722 ActivityRecord mParentActivity = null;
3723 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003724
Craig Mautnere3a00d72014-04-16 08:31:19 -07003725 boolean mVisible = true;
3726
Craig Mautner4a1cb222013-12-04 16:14:06 -08003727 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003728 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003729
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003730 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3731 final static int CONTAINER_STATE_NO_SURFACE = 1;
3732 final static int CONTAINER_STATE_FINISHING = 2;
3733 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3734
3735 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003736 synchronized (mService) {
3737 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003738 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003739 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003740 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003741 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003742 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003743
Craig Mautnere0a38842013-12-16 16:14:02 -08003744 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003745 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3746 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003747 mActivityDisplay = activityDisplay;
3748 mStack.mDisplayId = activityDisplay.mDisplayId;
3749 mStack.mStacks = activityDisplay.mStacks;
3750
3751 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003752 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003753 }
3754
3755 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003756 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003757 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003758 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3759 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003760 return;
3761 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003762 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003763 }
3764 }
3765
3766 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003767 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003768 synchronized (mService) {
3769 if (mActivityDisplay != null) {
3770 return mActivityDisplay.mDisplayId;
3771 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003772 }
3773 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003774 }
3775
Jeff Brownca9bc702014-02-11 14:32:56 -08003776 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003777 public int getStackId() {
3778 synchronized (mService) {
3779 return mStackId;
3780 }
3781 }
3782
3783 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003784 public boolean injectEvent(InputEvent event) {
3785 final long origId = Binder.clearCallingIdentity();
3786 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003787 synchronized (mService) {
3788 if (mActivityDisplay != null) {
3789 return mInputManagerInternal.injectInputEvent(event,
3790 mActivityDisplay.mDisplayId,
3791 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3792 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003793 }
3794 return false;
3795 } finally {
3796 Binder.restoreCallingIdentity(origId);
3797 }
3798 }
3799
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003800 @Override
3801 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003802 synchronized (mService) {
3803 if (mContainerState == CONTAINER_STATE_FINISHING) {
3804 return;
3805 }
3806 mContainerState = CONTAINER_STATE_FINISHING;
3807
3808 final Message msg =
3809 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003810 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003811
3812 long origId = Binder.clearCallingIdentity();
3813 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003814 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003815 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003816 } finally {
3817 Binder.restoreCallingIdentity(origId);
3818 }
3819 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003820 }
3821
Craig Mautner60257702014-09-17 15:02:33 -07003822 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003823 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3824 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003825 if (mActivityDisplay != null) {
3826 mActivityDisplay.detachActivitiesLocked(mStack);
3827 mActivityDisplay = null;
3828 mStack.mDisplayId = -1;
3829 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003830 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 }
3832 }
3833
3834 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003835 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003836 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003837 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003838 Binder.getCallingUid(), mCurrentUser, false,
3839 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003840 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003841 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003842 String mimeType = intent.getType();
3843 if (mimeType == null && intent.getData() != null
3844 && "content".equals(intent.getData().getScheme())) {
3845 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3846 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003847 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0,
3848 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003849 }
3850
3851 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003852 public final int startActivityIntentSender(IIntentSender intentSender) {
3853 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3854
3855 if (!(intentSender instanceof PendingIntentRecord)) {
3856 throw new IllegalArgumentException("Bad PendingIntent object");
3857 }
3858
3859 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003860 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003861 }
3862
Craig Mautner247ab652014-04-25 10:09:00 -07003863 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3864 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003865 Binder.getCallingUid(), mCurrentUser, false,
3866 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003867 if (resolvedType == null) {
3868 resolvedType = intent.getType();
3869 if (resolvedType == null && intent.getData() != null
3870 && "content".equals(intent.getData().getScheme())) {
3871 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3872 }
3873 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003874 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003875 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003876 throw new SecurityException(
3877 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3878 }
3879 }
3880
3881 /** Throw a SecurityException if allowEmbedded is not true */
3882 @Override
3883 public final void checkEmbeddedAllowed(Intent intent) {
3884 checkEmbeddedAllowedInner(intent, null);
3885 }
3886
3887 /** Throw a SecurityException if allowEmbedded is not true */
3888 @Override
3889 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3890 if (!(intentSender instanceof PendingIntentRecord)) {
3891 throw new IllegalArgumentException("Bad PendingIntent object");
3892 }
3893 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3894 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3895 pendingIntent.key.requestResolvedType);
3896 }
3897
Craig Mautnerdf88d732014-01-27 09:21:32 -08003898 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003899 public IBinder asBinder() {
3900 return this;
3901 }
3902
Craig Mautner4504de52013-12-20 09:06:56 -08003903 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003904 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003905 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003906 }
3907
Craig Mautner4a1cb222013-12-04 16:14:06 -08003908 ActivityStackSupervisor getOuter() {
3909 return ActivityStackSupervisor.this;
3910 }
3911
Craig Mautnerd163e752014-06-13 17:18:47 -07003912 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003913 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003914 }
3915
3916 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003917 synchronized (mService) {
3918 if (mActivityDisplay != null) {
3919 mActivityDisplay.getBounds(outBounds);
3920 } else {
3921 outBounds.set(0, 0);
3922 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003923 }
3924 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003925
Craig Mautner6985bad2014-04-21 15:22:06 -07003926 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003927 void setVisible(boolean visible) {
3928 if (mVisible != visible) {
3929 mVisible = visible;
3930 if (mCallback != null) {
3931 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3932 0 /* unused */, this).sendToTarget();
3933 }
3934 }
3935 }
3936
Craig Mautner6985bad2014-04-21 15:22:06 -07003937 void setDrawn() {
3938 }
3939
Craig Mautner1b4bf852014-05-26 15:06:32 -07003940 // You can always start a new task on a regular ActivityStack.
3941 boolean isEligibleForNewTasks() {
3942 return true;
3943 }
3944
Craig Mautnerd163e752014-06-13 17:18:47 -07003945 void onTaskListEmptyLocked() {
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -08003946 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3947 detachLocked();
3948 deleteActivityContainer(this);
3949 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003950 }
3951
Craig Mautner34b73df2014-01-12 21:11:08 -08003952 @Override
3953 public String toString() {
3954 return mIdString + (mActivityDisplay == null ? "N" : "A");
3955 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003956 }
3957
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003958 private class VirtualActivityContainer extends ActivityContainer {
3959 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003960 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003961
3962 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3963 super(getNextStackId());
3964 mParentActivity = parent;
3965 mCallback = callback;
3966 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003967 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003968 }
3969
3970 @Override
3971 public void setSurface(Surface surface, int width, int height, int density) {
3972 super.setSurface(surface, width, height, density);
3973
3974 synchronized (mService) {
3975 final long origId = Binder.clearCallingIdentity();
3976 try {
3977 setSurfaceLocked(surface, width, height, density);
3978 } finally {
3979 Binder.restoreCallingIdentity(origId);
3980 }
3981 }
3982 }
3983
3984 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3985 if (mContainerState == CONTAINER_STATE_FINISHING) {
3986 return;
3987 }
3988 VirtualActivityDisplay virtualActivityDisplay =
3989 (VirtualActivityDisplay) mActivityDisplay;
3990 if (virtualActivityDisplay == null) {
3991 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003992 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003993 mActivityDisplay = virtualActivityDisplay;
3994 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3995 attachToDisplayLocked(virtualActivityDisplay);
3996 }
3997
3998 if (mSurface != null) {
3999 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004000 }
4001
Craig Mautner6985bad2014-04-21 15:22:06 -07004002 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004003 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004004 mStack.resumeTopActivityLocked(null);
4005 } else {
4006 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004007 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004008 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004009 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004010 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004011 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004012
Craig Mautnerd163e752014-06-13 17:18:47 -07004013 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004014
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004015 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
4016 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004017 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004018
Craig Mautner6985bad2014-04-21 15:22:06 -07004019 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004020 boolean isAttachedLocked() {
4021 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004022 }
4023
4024 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004025 void setDrawn() {
4026 synchronized (mService) {
4027 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004028 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004029 }
4030 }
4031
Craig Mautner1b4bf852014-05-26 15:06:32 -07004032 // Never start a new task on an ActivityView if it isn't explicitly specified.
4033 @Override
4034 boolean isEligibleForNewTasks() {
4035 return false;
4036 }
4037
Craig Mautnerd163e752014-06-13 17:18:47 -07004038 private void setSurfaceIfReadyLocked() {
4039 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004040 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4041 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4042 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4043 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4044 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004045 }
4046 }
4047
Craig Mautner4a1cb222013-12-04 16:14:06 -08004048 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4049 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004050 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004051 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004052 int mDisplayId;
4053 Display mDisplay;
4054 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004055
Craig Mautner4a1cb222013-12-04 16:14:06 -08004056 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4057 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004058 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004059
Jose Lima4b6c6692014-08-12 17:41:12 -07004060 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004061
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004062 ActivityDisplay() {
4063 }
Craig Mautner4504de52013-12-20 09:06:56 -08004064
Craig Mautner1a70a162014-09-13 12:09:31 -07004065 // After instantiation, check that mDisplay is not null before using this. The alternative
4066 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004067 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004068 final Display display = mDisplayManager.getDisplay(displayId);
4069 if (display == null) {
4070 return;
4071 }
4072 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004073 }
4074
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004075 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004076 mDisplay = display;
4077 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004078 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004079 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004080
4081 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004082 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4083 + mDisplayId);
4084 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004085 }
4086
Craig Mautnere0a38842013-12-16 16:14:02 -08004087 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004088 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004089 + " from displayId=" + mDisplayId);
4090 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004091 }
4092
4093 void getBounds(Point bounds) {
4094 mDisplay.getDisplayInfo(mDisplayInfo);
4095 bounds.x = mDisplayInfo.appWidth;
4096 bounds.y = mDisplayInfo.appHeight;
4097 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004098
Jose Lima4b6c6692014-08-12 17:41:12 -07004099 void setVisibleBehindActivity(ActivityRecord r) {
4100 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004101 }
4102
Jose Lima4b6c6692014-08-12 17:41:12 -07004103 boolean hasVisibleBehindActivity() {
4104 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004105 }
4106
Craig Mautner34b73df2014-01-12 21:11:08 -08004107 @Override
4108 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004109 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4110 }
4111 }
4112
4113 class VirtualActivityDisplay extends ActivityDisplay {
4114 VirtualDisplay mVirtualDisplay;
4115
Craig Mautner6985bad2014-04-21 15:22:06 -07004116 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004117 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004118 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4119 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4120 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4121 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004122
4123 init(mVirtualDisplay.getDisplay());
4124
4125 mWindowManager.handleDisplayAdded(mDisplayId);
4126 }
4127
4128 void setSurface(Surface surface) {
4129 if (mVirtualDisplay != null) {
4130 mVirtualDisplay.setSurface(surface);
4131 }
4132 }
4133
4134 @Override
4135 void detachActivitiesLocked(ActivityStack stack) {
4136 super.detachActivitiesLocked(stack);
4137 if (mVirtualDisplay != null) {
4138 mVirtualDisplay.release();
4139 mVirtualDisplay = null;
4140 }
4141 }
4142
4143 @Override
4144 public String toString() {
4145 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004146 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004147 }
Jose Lima58e66d62014-05-27 20:00:27 -07004148
4149 private boolean isLeanbackOnlyDevice() {
4150 boolean onLeanbackOnly = false;
4151 try {
4152 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4153 PackageManager.FEATURE_LEANBACK_ONLY);
4154 } catch (RemoteException e) {
4155 // noop
4156 }
4157
4158 return onLeanbackOnly;
4159 }
Craig Mautner27084302013-03-25 08:05:25 -07004160}