blob: c4234eb02568f17b3a64a1adcc2b3f357067f864 [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 Ogunwalecb82f302015-02-25 07:53:40 -0800389 /** Top of all visible stacks is/should always be equal to the focused stack.
390 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
391 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700392 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800393 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
394 if (parent != null) {
395 stack = parent.task.stack;
396 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800397 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700398 }
399
Craig Mautner299f9602015-01-26 09:47:33 -0800400 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800401 moveHomeStack(toFront, reason, null);
402 }
403
404 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800405 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800406 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800407 if (topNdx <= 0) {
408 return;
409 }
410 ActivityStack topStack = stacks.get(topNdx);
411 final boolean homeInFront = topStack == mHomeStack;
412 if (homeInFront != toFront) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800413 stacks.remove(mHomeStack);
414 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800415 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
416 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700417 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800418
419 if (lastFocusedStack != null) {
420 mLastFocusedStack = lastFocusedStack;
421 }
422 mFocusedStack = stacks.get(topNdx);
423
Craig Mautnerde313752015-01-22 14:28:03 -0800424 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
425 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800426 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800427
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800428 if (mService.mBooting || !mService.mBooted) {
429 final ActivityRecord r = topRunningActivityLocked();
430 if (r != null && r.idle) {
431 checkFinishBootingLocked();
432 }
433 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700434 }
435
Craig Mautner299f9602015-01-26 09:47:33 -0800436 void moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700437 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
438 mWindowManager.showRecentApps();
439 return;
440 }
Craig Mautner299f9602015-01-26 09:47:33 -0800441 moveHomeStack(true, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700442 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700443 }
444
Craig Mautner299f9602015-01-26 09:47:33 -0800445 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700446 if (!mService.mBooting && !mService.mBooted) {
447 // Not ready yet!
448 return false;
449 }
450
Craig Mautner84984fa2014-06-19 11:19:20 -0700451 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
452 mWindowManager.showRecentApps();
453 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700454 }
Craig Mautner299f9602015-01-26 09:47:33 -0800455 moveHomeStackTaskToTop(homeStackTaskType, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700456 if (prev != null) {
457 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
458 }
459
Craig Mautnera8a90e02013-06-28 15:24:50 -0700460 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700461 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000462 if (r != null && r.isHomeActivity()) {
Craig Mautner299f9602015-01-26 09:47:33 -0800463 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700464 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700465 }
Craig Mautner299f9602015-01-26 09:47:33 -0800466 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700467 }
468
Craig Mautner8d341ef2013-03-26 09:03:27 -0700469 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800470 return anyTaskForIdLocked(id, true);
471 }
472
473 /**
474 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
475 * @param id Id of the task we would like returned.
476 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
477 * restore the task from recents to the active list.
478 */
479 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800480 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800481 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800482 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800483 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
484 ActivityStack stack = stacks.get(stackNdx);
485 TaskRecord task = stack.taskForIdLocked(id);
486 if (task != null) {
487 return task;
488 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700489 }
490 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800491
492 // Don't give up! Look in recents.
493 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800494 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800495 if (task == null) {
496 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
497 return null;
498 }
499
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800500 if (!restoreFromRecents) {
501 return task;
502 }
503
Wale Ogunwale7de05352014-12-12 15:21:33 -0800504 if (!restoreRecentTaskLocked(task)) {
505 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
506 return null;
507 }
508 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
509 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700510 }
511
Craig Mautner6170f732013-04-02 13:05:23 -0700512 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800513 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800514 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800515 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800516 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
517 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
518 if (r != null) {
519 return r;
520 }
Craig Mautner6170f732013-04-02 13:05:23 -0700521 }
522 }
523 return null;
524 }
525
Craig Mautneref73ee12014-04-23 11:45:37 -0700526 void setNextTaskId(int taskId) {
527 if (taskId > mCurTaskId) {
528 mCurTaskId = taskId;
529 }
530 }
531
Craig Mautner8d341ef2013-03-26 09:03:27 -0700532 int getNextTaskId() {
533 do {
534 mCurTaskId++;
535 if (mCurTaskId <= 0) {
536 mCurTaskId = 1;
537 }
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800538 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700539 return mCurTaskId;
540 }
541
Craig Mautnerde4ef022013-04-07 19:01:33 -0700542 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800543 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700544 if (stack == null) {
545 return null;
546 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700547 ActivityRecord resumedActivity = stack.mResumedActivity;
548 if (resumedActivity == null || resumedActivity.app == null) {
549 resumedActivity = stack.mPausingActivity;
550 if (resumedActivity == null || resumedActivity.app == null) {
551 resumedActivity = stack.topRunningActivityLocked(null);
552 }
553 }
554 return resumedActivity;
555 }
556
Dianne Hackbornff072722014-09-24 10:56:28 -0700557 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700558 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800559 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800560 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
561 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800562 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
563 final ActivityStack stack = stacks.get(stackNdx);
564 if (!isFrontStack(stack)) {
565 continue;
566 }
567 ActivityRecord hr = stack.topRunningActivityLocked(null);
568 if (hr != null) {
569 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
570 && processName.equals(hr.processName)) {
571 try {
George Mount2c92c972014-03-20 09:38:23 -0700572 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800573 didSomething = true;
574 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700575 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800576 Slog.w(TAG, "Exception in new application when starting activity "
577 + hr.intent.getComponent().flattenToShortString(), e);
578 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700579 }
Craig Mautner20e72272013-04-01 13:45:53 -0700580 }
Craig Mautner20e72272013-04-01 13:45:53 -0700581 }
582 }
583 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700584 if (!didSomething) {
585 ensureActivitiesVisibleLocked(null, 0);
586 }
Craig Mautner20e72272013-04-01 13:45:53 -0700587 return didSomething;
588 }
589
590 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800591 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
592 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800593 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
594 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800595 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800596 continue;
597 }
598 final ActivityRecord resumedActivity = stack.mResumedActivity;
599 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800600 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
601 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 return false;
603 }
Craig Mautner20e72272013-04-01 13:45:53 -0700604 }
605 }
606 return true;
607 }
608
Craig Mautnerde4ef022013-04-07 19:01:33 -0700609 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800610 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
611 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800612 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
613 final ActivityStack stack = stacks.get(stackNdx);
614 if (isFrontStack(stack)) {
615 final ActivityRecord r = stack.mResumedActivity;
616 if (r != null && r.state != ActivityState.RESUMED) {
617 return false;
618 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700619 }
620 }
621 }
622 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800623 if (DEBUG_STACK) Slog.d(TAG,
624 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
625 mLastFocusedStack + " to=" + mFocusedStack);
626 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700627 return true;
628 }
629
630 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800631 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
632 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800633 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
634 final ActivityStack stack = stacks.get(stackNdx);
635 final ActivityRecord r = stack.mResumedActivity;
Craig Mautner8c14c152015-01-15 17:32:07 -0800636 if (r != null && (!r.nowVisible || mWaitingVisibleActivities.contains(r))) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800637 return false;
638 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700639 }
640 }
641 return true;
642 }
643
Craig Mautner2acc3892013-09-23 10:28:14 -0700644 /**
645 * Pause all activities in either all of the stacks or just the back stacks.
646 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700647 * @return true if any activity was paused as a result of this call.
648 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700649 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700650 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800651 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
652 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800653 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
654 final ActivityStack stack = stacks.get(stackNdx);
655 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
656 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
657 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700658 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
659 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800660 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700661 }
662 }
663 return someActivityPaused;
664 }
665
Craig Mautnerde4ef022013-04-07 19:01:33 -0700666 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700667 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800668 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
669 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800670 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
671 final ActivityStack stack = stacks.get(stackNdx);
672 final ActivityRecord r = stack.mPausingActivity;
673 if (r != null && r.state != ActivityState.PAUSED
674 && r.state != ActivityState.STOPPED
675 && r.state != ActivityState.STOPPING) {
676 if (DEBUG_STATES) {
677 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
678 pausing = false;
679 } else {
680 return false;
681 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700682 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700683 }
684 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700685 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700686 }
687
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700688 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
689 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500690 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800691 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
692 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
693 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
694 final ActivityStack stack = stacks.get(stackNdx);
695 if (stack.mResumedActivity != null &&
696 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700697 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800698 }
699 }
700 }
701 }
702
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700703 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700704 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700705 }
706
707 void sendWaitingVisibleReportLocked(ActivityRecord r) {
708 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700709 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700710 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700711 if (w.who == null) {
712 changed = true;
713 w.timeout = false;
714 if (r != null) {
715 w.who = new ComponentName(r.info.packageName, r.info.name);
716 }
717 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
718 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700719 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700720 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700721 if (changed) {
722 mService.notifyAll();
723 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700724 }
725
726 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
727 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700728 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700729 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700730 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700731 if (w.who == null) {
732 changed = true;
733 w.timeout = timeout;
734 if (r != null) {
735 w.who = new ComponentName(r.info.packageName, r.info.name);
736 }
737 w.thisTime = thisTime;
738 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700739 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700740 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700741 if (changed) {
742 mService.notifyAll();
743 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700744 }
745
Craig Mautner29219d92013-04-16 20:19:12 -0700746 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800747 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700748 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
749 if (r != null) {
750 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700751 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700752
Craig Mautner4a1cb222013-12-04 16:14:06 -0800753 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800754 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800755 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
756 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700757 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700758 r = stack.topRunningActivityLocked(null);
759 if (r != null) {
760 return r;
761 }
762 }
763 }
764 return null;
765 }
766
Dianne Hackborn09233282014-04-30 11:33:59 -0700767 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700768 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800769 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
770 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800771 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800772 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800773 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
775 final ActivityStack stack = stacks.get(stackNdx);
776 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
777 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700778 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700779 }
780 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700781
782 // The lists are already sorted from most recent to oldest. Just pull the most recent off
783 // each list and add it to list. Stop when all lists are empty or maxNum reached.
784 while (maxNum > 0) {
785 long mostRecentActiveTime = Long.MIN_VALUE;
786 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800787 final int numTaskLists = runningTaskLists.size();
788 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
789 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700790 if (!stackTaskList.isEmpty()) {
791 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
792 if (lastActiveTime > mostRecentActiveTime) {
793 mostRecentActiveTime = lastActiveTime;
794 selectedStackList = stackTaskList;
795 }
796 }
797 }
798 if (selectedStackList != null) {
799 list.add(selectedStackList.remove(0));
800 --maxNum;
801 } else {
802 break;
803 }
804 }
Craig Mautner20e72272013-04-01 13:45:53 -0700805 }
806
Craig Mautner23ac33b2013-04-01 16:26:35 -0700807 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700808 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700809 // Collect information about the target of the Intent.
810 ActivityInfo aInfo;
811 try {
812 ResolveInfo rInfo =
813 AppGlobals.getPackageManager().resolveIntent(
814 intent, resolvedType,
815 PackageManager.MATCH_DEFAULT_ONLY
816 | ActivityManagerService.STOCK_PM_FLAGS, userId);
817 aInfo = rInfo != null ? rInfo.activityInfo : null;
818 } catch (RemoteException e) {
819 aInfo = null;
820 }
821
822 if (aInfo != null) {
823 // Store the found target back into the intent, because now that
824 // we have it we never want to do this again. For example, if the
825 // user navigates back to this point in the history, we should
826 // always restart the exact same activity.
827 intent.setComponent(new ComponentName(
828 aInfo.applicationInfo.packageName, aInfo.name));
829
830 // Don't debug things in the system process
831 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
832 if (!aInfo.processName.equals("system")) {
833 mService.setDebugApp(aInfo.processName, true, false);
834 }
835 }
836
837 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
838 if (!aInfo.processName.equals("system")) {
839 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
840 }
841 }
842
Jeff Hao1b012d32014-08-20 10:35:34 -0700843 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700844 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700845 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700846 }
847 }
848 }
849 return aInfo;
850 }
851
Craig Mautner299f9602015-01-26 09:47:33 -0800852 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
853 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700854 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
855 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700856 }
857
Craig Mautner23ac33b2013-04-01 16:26:35 -0700858 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700859 String callingPackage, Intent intent, String resolvedType,
860 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700861 IBinder resultTo, String resultWho, int requestCode, int startFlags,
862 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700863 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700864 // Refuse possible leaked file descriptors
865 if (intent != null && intent.hasFileDescriptors()) {
866 throw new IllegalArgumentException("File descriptors passed in Intent");
867 }
868 boolean componentSpecified = intent.getComponent() != null;
869
870 // Don't modify the client's object!
871 intent = new Intent(intent);
872
873 // Collect information about the target of the Intent.
874 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700875 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700876
Craig Mautnere0a38842013-12-16 16:14:02 -0800877 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700878 synchronized (mService) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700879 final int realCallingPid = Binder.getCallingPid();
880 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700881 int callingPid;
882 if (callingUid >= 0) {
883 callingPid = -1;
884 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700885 callingPid = realCallingPid;
886 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700887 } else {
888 callingPid = callingUid = -1;
889 }
890
Craig Mautnere0a38842013-12-16 16:14:02 -0800891 final ActivityStack stack;
892 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800893 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800894 } else {
895 stack = container.mStack;
896 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800897 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700898 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700899 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700900
901 final long origId = Binder.clearCallingIdentity();
902
903 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800904 (aInfo.applicationInfo.privateFlags
905 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700906 // This may be a heavy-weight process! Check to see if we already
907 // have another, different heavy-weight process running.
908 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
909 if (mService.mHeavyWeightProcess != null &&
910 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
911 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700912 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700913 if (caller != null) {
914 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
915 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700916 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700917 } else {
918 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700919 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700920 + intent.toString());
921 ActivityOptions.abort(options);
922 return ActivityManager.START_PERMISSION_DENIED;
923 }
924 }
925
926 IIntentSender target = mService.getIntentSenderLocked(
927 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700928 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700929 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
930 | PendingIntent.FLAG_ONE_SHOT, null);
931
932 Intent newIntent = new Intent();
933 if (requestCode >= 0) {
934 // Caller is requesting a result.
935 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
936 }
937 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
938 new IntentSender(target));
939 if (mService.mHeavyWeightProcess.activities.size() > 0) {
940 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
941 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
942 hist.packageName);
943 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
944 hist.task.taskId);
945 }
946 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
947 aInfo.packageName);
948 newIntent.setFlags(intent.getFlags());
949 newIntent.setClassName("android",
950 HeavyWeightSwitcherActivity.class.getName());
951 intent = newIntent;
952 resolvedType = null;
953 caller = null;
954 callingUid = Binder.getCallingUid();
955 callingPid = Binder.getCallingPid();
956 componentSpecified = true;
957 try {
958 ResolveInfo rInfo =
959 AppGlobals.getPackageManager().resolveIntent(
960 intent, null,
961 PackageManager.MATCH_DEFAULT_ONLY
962 | ActivityManagerService.STOCK_PM_FLAGS, userId);
963 aInfo = rInfo != null ? rInfo.activityInfo : null;
964 aInfo = mService.getActivityInfoForUser(aInfo, userId);
965 } catch (RemoteException e) {
966 aInfo = null;
967 }
968 }
969 }
970 }
971
Dianne Hackborn91097de2014-04-04 18:02:06 -0700972 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
973 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700974 requestCode, callingPid, callingUid, callingPackage,
975 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700976 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700977
Craig Mautner85c11a82014-07-17 12:38:18 -0700978 Binder.restoreCallingIdentity(origId);
979
Craig Mautnerde4ef022013-04-07 19:01:33 -0700980 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700981 // If the caller also wants to switch to a new configuration,
982 // do so now. This allows a clean switch, as we are waiting
983 // for the current activity to pause (so we will not destroy
984 // it), and have not yet started the next activity.
985 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
986 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700988 if (DEBUG_CONFIGURATION) Slog.v(TAG,
989 "Updating to new configuration after starting activity.");
990 mService.updateConfigurationLocked(config, null, false, false);
991 }
992
Craig Mautner23ac33b2013-04-01 16:26:35 -0700993 if (outResult != null) {
994 outResult.result = res;
995 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700996 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700997 do {
998 try {
999 mService.wait();
1000 } catch (InterruptedException e) {
1001 }
1002 } while (!outResult.timeout && outResult.who == null);
1003 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001004 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001005 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001006 outResult.timeout = false;
1007 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1008 outResult.totalTime = 0;
1009 outResult.thisTime = 0;
1010 } else {
1011 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001012 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001013 do {
1014 try {
1015 mService.wait();
1016 } catch (InterruptedException e) {
1017 }
1018 } while (!outResult.timeout && outResult.who == null);
1019 }
1020 }
1021 }
1022
1023 return res;
1024 }
1025 }
1026
1027 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1028 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1029 Bundle options, int userId) {
1030 if (intents == null) {
1031 throw new NullPointerException("intents is null");
1032 }
1033 if (resolvedTypes == null) {
1034 throw new NullPointerException("resolvedTypes is null");
1035 }
1036 if (intents.length != resolvedTypes.length) {
1037 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1038 }
1039
Craig Mautner23ac33b2013-04-01 16:26:35 -07001040
1041 int callingPid;
1042 if (callingUid >= 0) {
1043 callingPid = -1;
1044 } else if (caller == null) {
1045 callingPid = Binder.getCallingPid();
1046 callingUid = Binder.getCallingUid();
1047 } else {
1048 callingPid = callingUid = -1;
1049 }
1050 final long origId = Binder.clearCallingIdentity();
1051 try {
1052 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001053 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001054 for (int i=0; i<intents.length; i++) {
1055 Intent intent = intents[i];
1056 if (intent == null) {
1057 continue;
1058 }
1059
1060 // Refuse possible leaked file descriptors
1061 if (intent != null && intent.hasFileDescriptors()) {
1062 throw new IllegalArgumentException("File descriptors passed in Intent");
1063 }
1064
1065 boolean componentSpecified = intent.getComponent() != null;
1066
1067 // Don't modify the client's object!
1068 intent = new Intent(intent);
1069
1070 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001071 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001072 // TODO: New, check if this is correct
1073 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1074
1075 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001076 (aInfo.applicationInfo.privateFlags
1077 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001078 throw new IllegalArgumentException(
1079 "FLAG_CANT_SAVE_STATE not supported here");
1080 }
1081
1082 Bundle theseOptions;
1083 if (options != null && i == intents.length-1) {
1084 theseOptions = options;
1085 } else {
1086 theseOptions = null;
1087 }
Craig Mautner6170f732013-04-02 13:05:23 -07001088 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001089 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1090 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001091 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001092 if (res < 0) {
1093 return res;
1094 }
1095
1096 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1097 }
1098 }
1099 } finally {
1100 Binder.restoreCallingIdentity(origId);
1101 }
1102
1103 return ActivityManager.START_SUCCESS;
1104 }
1105
Craig Mautner2420ead2013-04-01 17:13:20 -07001106 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001107 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001108 throws RemoteException {
1109
1110 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001111 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001112 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001113
1114 // schedule launch ticks to collect information about slow apps.
1115 r.startLaunchTickingLocked();
1116
1117 // Have the window manager re-evaluate the orientation of
1118 // the screen based on the new activity order. Note that
1119 // as a result of this, it can call back into the activity
1120 // manager with a new orientation. We don't care about that,
1121 // because the activity is not currently running so we are
1122 // just restarting it anyway.
1123 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001124 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001125 mService.mConfiguration,
1126 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1127 mService.updateConfigurationLocked(config, r, false, false);
1128 }
1129
1130 r.app = app;
1131 app.waitingToKill = null;
1132 r.launchCount++;
1133 r.lastLaunchTime = SystemClock.uptimeMillis();
1134
1135 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1136
1137 int idx = app.activities.indexOf(r);
1138 if (idx < 0) {
1139 app.activities.add(r);
1140 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001141 mService.updateLruProcessLocked(app, true, null);
1142 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001143
1144 final ActivityStack stack = r.task.stack;
1145 try {
1146 if (app.thread == null) {
1147 throw new RemoteException();
1148 }
1149 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001150 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001151 if (andResume) {
1152 results = r.results;
1153 newIntents = r.newIntents;
1154 }
1155 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1156 + " icicle=" + r.icicle
1157 + " with results=" + results + " newIntents=" + newIntents
1158 + " andResume=" + andResume);
1159 if (andResume) {
1160 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1161 r.userId, System.identityHashCode(r),
1162 r.task.taskId, r.shortComponentName);
1163 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001164 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001165 // Home process is the root process of the task.
1166 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001167 }
1168 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1169 r.sleeping = false;
1170 r.forceNewConfig = false;
1171 mService.showAskCompatModeDialogLocked(r);
1172 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1173 String profileFile = null;
1174 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001175 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1176 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1177 mService.mProfileProc = app;
1178 profileFile = mService.mProfileFile;
1179 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001180 }
1181 }
1182 app.hasShownUi = true;
1183 app.pendingUiClean = true;
1184 if (profileFd != null) {
1185 try {
1186 profileFd = profileFd.dup();
1187 } catch (IOException e) {
1188 if (profileFd != null) {
1189 try {
1190 profileFd.close();
1191 } catch (IOException o) {
1192 }
1193 profileFd = null;
1194 }
1195 }
1196 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001197
Jeff Hao1b012d32014-08-20 10:35:34 -07001198 ProfilerInfo profilerInfo = profileFile != null
1199 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1200 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001201 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001203 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001204 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1205 r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1206 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001207
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001208 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001209 // This may be a heavy-weight process! Note that the package
1210 // manager will ensure that only activity can run in the main
1211 // process of the .apk, which is the only thing that will be
1212 // considered heavy-weight.
1213 if (app.processName.equals(app.info.packageName)) {
1214 if (mService.mHeavyWeightProcess != null
1215 && mService.mHeavyWeightProcess != app) {
1216 Slog.w(TAG, "Starting new heavy weight process " + app
1217 + " when already running "
1218 + mService.mHeavyWeightProcess);
1219 }
1220 mService.mHeavyWeightProcess = app;
1221 Message msg = mService.mHandler.obtainMessage(
1222 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1223 msg.obj = r;
1224 mService.mHandler.sendMessage(msg);
1225 }
1226 }
1227
1228 } catch (RemoteException e) {
1229 if (r.launchFailed) {
1230 // This is the second time we failed -- finish activity
1231 // and give up.
1232 Slog.e(TAG, "Second failure launching "
1233 + r.intent.getComponent().flattenToShortString()
1234 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001235 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001236 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1237 "2nd-crash", false);
1238 return false;
1239 }
1240
1241 // This is the first time we failed -- restart process and
1242 // retry.
1243 app.activities.remove(r);
1244 throw e;
1245 }
1246
1247 r.launchFailed = false;
1248 if (stack.updateLRUListLocked(r)) {
1249 Slog.w(TAG, "Activity " + r
1250 + " being launched, but already in LRU list");
1251 }
1252
1253 if (andResume) {
1254 // As part of the process of launching, ActivityThread also performs
1255 // a resume.
1256 stack.minimalResumeActivityLocked(r);
1257 } else {
1258 // This activity is not starting in the resumed state... which
1259 // should look like we asked it to pause+stop (but remain visible),
1260 // and it has done so and reported back the current icicle and
1261 // other state.
1262 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1263 + " (starting in stopped state)");
1264 r.state = ActivityState.STOPPED;
1265 r.stopped = true;
1266 }
1267
1268 // Launch the new version setup screen if needed. We do this -after-
1269 // launching the initial activity (that is, home), so that it can have
1270 // a chance to initialize itself while in the background, making the
1271 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001272 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001273 mService.startSetupActivityLocked();
1274 }
1275
Dianne Hackborn465fa392014-09-14 14:21:18 -07001276 // Update any services we are bound to that might care about whether
1277 // their client may have activities.
1278 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1279
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 return true;
1281 }
1282
Craig Mautnere79d42682013-04-01 19:01:53 -07001283 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001284 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001285 // Is this activity's application already running?
1286 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001287 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001288
1289 r.task.stack.setLaunchTime(r);
1290
1291 if (app != null && app.thread != null) {
1292 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001293 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1294 || !"android".equals(r.info.packageName)) {
1295 // Don't add this if it is a platform component that is marked
1296 // to run in multiple processes, because this is actually
1297 // part of the framework so doesn't make sense to track as a
1298 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001299 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1300 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001301 }
George Mount2c92c972014-03-20 09:38:23 -07001302 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001303 return;
1304 } catch (RemoteException e) {
1305 Slog.w(TAG, "Exception when starting activity "
1306 + r.intent.getComponent().flattenToShortString(), e);
1307 }
1308
1309 // If a dead object exception was thrown -- fall through to
1310 // restart the application.
1311 }
1312
1313 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001314 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001315 }
1316
Craig Mautner6170f732013-04-02 13:05:23 -07001317 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001318 Intent intent, String resolvedType, ActivityInfo aInfo,
1319 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1320 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001321 int callingPid, int callingUid, String callingPackage,
1322 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001323 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1324 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001325 int err = ActivityManager.START_SUCCESS;
1326
1327 ProcessRecord callerApp = null;
1328 if (caller != null) {
1329 callerApp = mService.getRecordForAppLocked(caller);
1330 if (callerApp != null) {
1331 callingPid = callerApp.pid;
1332 callingUid = callerApp.info.uid;
1333 } else {
1334 Slog.w(TAG, "Unable to find app for caller " + caller
1335 + " (pid=" + callingPid + ") when starting: "
1336 + intent.toString());
1337 err = ActivityManager.START_PERMISSION_DENIED;
1338 }
1339 }
1340
1341 if (err == ActivityManager.START_SUCCESS) {
1342 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1343 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001344 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001345 + " on display " + (container == null ? (mFocusedStack == null ?
1346 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1347 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1348 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001349 }
1350
1351 ActivityRecord sourceRecord = null;
1352 ActivityRecord resultRecord = null;
1353 if (resultTo != null) {
1354 sourceRecord = isInAnyStackLocked(resultTo);
1355 if (DEBUG_RESULTS) Slog.v(
1356 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1357 if (sourceRecord != null) {
1358 if (requestCode >= 0 && !sourceRecord.finishing) {
1359 resultRecord = sourceRecord;
1360 }
1361 }
1362 }
Craig Mautner6170f732013-04-02 13:05:23 -07001363
Dianne Hackborn91097de2014-04-04 18:02:06 -07001364 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001365
Dianne Hackborn95465202014-09-15 16:21:55 -07001366 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001367 // Transfer the result target from the source activity to the new
1368 // one being started, including any failures.
1369 if (requestCode >= 0) {
1370 ActivityOptions.abort(options);
1371 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1372 }
1373 resultRecord = sourceRecord.resultTo;
1374 resultWho = sourceRecord.resultWho;
1375 requestCode = sourceRecord.requestCode;
1376 sourceRecord.resultTo = null;
1377 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001378 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001379 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001380 if (sourceRecord.launchedFromUid == callingUid) {
1381 // The new activity is being launched from the same uid as the previous
1382 // activity in the flow, and asking to forward its result back to the
1383 // previous. In this case the activity is serving as a trampoline between
1384 // the two, so we also want to update its launchedFromPackage to be the
1385 // same as the previous activity. Note that this is safe, since we know
1386 // these two packages come from the same uid; the caller could just as
1387 // well have supplied that same package name itself. This specifially
1388 // deals with the case of an intent picker/chooser being launched in the app
1389 // flow to redirect to an activity picked by the user, where we want the final
1390 // activity to consider it to have been launched by the previous app activity.
1391 callingPackage = sourceRecord.launchedFromPackage;
1392 }
Craig Mautner6170f732013-04-02 13:05:23 -07001393 }
1394
1395 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1396 // We couldn't find a class that can handle the given Intent.
1397 // That's the end of that!
1398 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1399 }
1400
1401 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1402 // We couldn't find the specific class specified in the Intent.
1403 // Also the end of the line.
1404 err = ActivityManager.START_CLASS_NOT_FOUND;
1405 }
1406
Dianne Hackborn91097de2014-04-04 18:02:06 -07001407 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1408 && sourceRecord.task.voiceSession != null) {
1409 // If this activity is being launched as part of a voice session, we need
1410 // to ensure that it is safe to do so. If the upcoming activity will also
1411 // be part of the voice session, we can only launch it if it has explicitly
1412 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001413 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001414 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1415 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001416 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1417 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001418 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1419 }
1420 } catch (RemoteException e) {
1421 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1422 }
1423 }
1424 }
1425
1426 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1427 // If the caller is starting a new voice session, just make sure the target
1428 // is actually allowing it to run this way.
1429 try {
1430 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1431 intent, resolvedType)) {
1432 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1433 }
1434 } catch (RemoteException e) {
1435 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1436 }
1437 }
1438
louis_changcd5d1982014-08-01 10:09:08 +08001439 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1440
Craig Mautner6170f732013-04-02 13:05:23 -07001441 if (err != ActivityManager.START_SUCCESS) {
1442 if (resultRecord != null) {
1443 resultStack.sendActivityResultLocked(-1,
1444 resultRecord, resultWho, requestCode,
1445 Activity.RESULT_CANCELED, null);
1446 }
Craig Mautner6170f732013-04-02 13:05:23 -07001447 ActivityOptions.abort(options);
1448 return err;
1449 }
1450
1451 final int startAnyPerm = mService.checkPermission(
1452 START_ANY_ACTIVITY, callingPid, callingUid);
1453 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1454 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1455 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1456 if (resultRecord != null) {
1457 resultStack.sendActivityResultLocked(-1,
1458 resultRecord, resultWho, requestCode,
1459 Activity.RESULT_CANCELED, null);
1460 }
Craig Mautner6170f732013-04-02 13:05:23 -07001461 String msg;
1462 if (!aInfo.exported) {
1463 msg = "Permission Denial: starting " + intent.toString()
1464 + " from " + callerApp + " (pid=" + callingPid
1465 + ", uid=" + callingUid + ")"
1466 + " not exported from uid " + aInfo.applicationInfo.uid;
1467 } else {
1468 msg = "Permission Denial: starting " + intent.toString()
1469 + " from " + callerApp + " (pid=" + callingPid
1470 + ", uid=" + callingUid + ")"
1471 + " requires " + aInfo.permission;
1472 }
1473 Slog.w(TAG, msg);
1474 throw new SecurityException(msg);
1475 }
1476
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001477 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001478 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001479
Craig Mautner6170f732013-04-02 13:05:23 -07001480 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001481 try {
1482 // The Intent we give to the watcher has the extra data
1483 // stripped off, since it can contain private information.
1484 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001485 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001486 aInfo.applicationInfo.packageName);
1487 } catch (RemoteException e) {
1488 mService.mController = null;
1489 }
Ben Gruver5e207332013-04-03 17:41:37 -07001490 }
Craig Mautner6170f732013-04-02 13:05:23 -07001491
Ben Gruver5e207332013-04-03 17:41:37 -07001492 if (abort) {
1493 if (resultRecord != null) {
1494 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001495 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001496 }
Ben Gruver5e207332013-04-03 17:41:37 -07001497 // We pretend to the caller that it was really started, but
1498 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001499 ActivityOptions.abort(options);
1500 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001501 }
1502
1503 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001504 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001505 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001506 if (outActivity != null) {
1507 outActivity[0] = r;
1508 }
1509
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001510 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001511 if (voiceSession == null && (stack.mResumedActivity == null
1512 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001513 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1514 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001515 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001516 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001517 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001518 ActivityOptions.abort(options);
1519 return ActivityManager.START_SWITCHES_CANCELED;
1520 }
1521 }
1522
1523 if (mService.mDidAppSwitch) {
1524 // This is the second allowed switch since we stopped switches,
1525 // so now just generally allow switches. Use case: user presses
1526 // home (switches disabled, switch to home, mDidAppSwitch now true);
1527 // user taps a home icon (coming from home so allowed, we hit here
1528 // and now allow anyone to switch again).
1529 mService.mAppSwitchesAllowedTime = 0;
1530 } else {
1531 mService.mDidAppSwitch = true;
1532 }
1533
Craig Mautneree36c772014-07-16 14:56:05 -07001534 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001535
Dianne Hackborn91097de2014-04-04 18:02:06 -07001536 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001537 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001538
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001539 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001540 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001541 // activity start, but we are not actually doing an activity
1542 // switch... just dismiss the keyguard now, because we
1543 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001544 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001545 }
1546 return err;
1547 }
1548
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001549 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001550 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001551
1552 // On leanback only devices we should keep all activities in the same stack.
1553 if (!mLeanbackOnlyDevice &&
1554 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001555
1556 ActivityStack stack;
1557
Craig Mautnerac6f8432013-07-17 13:24:59 -07001558 if (task != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001559 stack = task.stack;
1560 if (stack.isOnHomeDisplay()) {
1561 if (mFocusedStack != stack) {
1562 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "computeStackFocus: Setting " +
Craig Mautnere0a38842013-12-16 16:14:02 -08001563 "focused stack to r=" + r + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001564 } else {
1565 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001566 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001567 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001568 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001569 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001570 }
1571
Craig Mautnere0a38842013-12-16 16:14:02 -08001572 final ActivityContainer container = r.mInitialActivityContainer;
1573 if (container != null) {
1574 // The first time put it on the desired stack, after this put on task stack.
1575 r.mInitialActivityContainer = null;
1576 return container.mStack;
1577 }
1578
Craig Mautner1b4bf852014-05-26 15:06:32 -07001579 if (mFocusedStack != mHomeStack && (!newTask ||
1580 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001581 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001582 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001583 return mFocusedStack;
1584 }
1585
Craig Mautnere0a38842013-12-16 16:14:02 -08001586 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1587 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001588 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001589 if (!stack.isHomeStack()) {
1590 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001591 "computeStackFocus: Setting focused stack=" + stack);
1592 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001593 }
1594 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001595
Craig Mautner4a1cb222013-12-04 16:14:06 -08001596 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001597 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
1598 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "computeStackFocus: New stack r=" + r +
1599 " stackId=" + stack.mStackId);
1600 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001601 }
1602 return mHomeStack;
1603 }
1604
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001605 boolean setFocusedStack(ActivityRecord r, String reason) {
1606 if (r == null) {
1607 // Not sure what you are trying to do, but it is not going to work...
1608 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001609 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001610 final TaskRecord task = r.task;
1611 if (task == null || task.stack == null) {
1612 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1613 return false;
1614 }
1615 task.stack.moveToFront(reason);
1616 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001617 }
1618
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001619 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001620 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001621 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001622 final Intent intent = r.intent;
1623 final int callingUid = r.launchedFromUid;
1624
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001625 // In some flows in to this function, we retrieve the task record and hold on to it
1626 // without a lock before calling back in to here... so the task at this point may
1627 // not actually be in recents. Check for that, and if it isn't in recents just
1628 // consider it invalid.
1629 if (inTask != null && !inTask.inRecents) {
1630 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1631 inTask = null;
1632 }
1633
Craig Mautnerad400af2014-08-13 16:41:36 -07001634 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001635 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1636 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001637
Craig Mautnera228ae92014-07-09 05:44:55 -07001638 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001639 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001640 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001641 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1642 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1643 "\"singleInstance\" or \"singleTask\"");
1644 launchFlags &=
1645 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1646 } else {
1647 switch (r.info.documentLaunchMode) {
1648 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1649 break;
1650 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1651 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1652 break;
1653 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1654 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1655 break;
1656 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1657 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1658 break;
1659 }
1660 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001661
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001662 final boolean launchTaskBehind = r.mLaunchTaskBehind
1663 && !launchSingleTask && !launchSingleInstance
1664 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001665
1666 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1667 // For whatever reason this activity is being launched into a new
1668 // task... yet the caller has requested a result back. Well, that
1669 // is pretty messed up, so instead immediately send back a cancel
1670 // and let the new task continue launched as normal without a
1671 // dependency on its originator.
1672 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1673 r.resultTo.task.stack.sendActivityResultLocked(-1,
1674 r.resultTo, r.resultWho, r.requestCode,
1675 Activity.RESULT_CANCELED, null);
1676 r.resultTo = null;
1677 }
1678
1679 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1680 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1681 }
1682
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001683 // If we are actually going to launch in to a new task, there are some cases where
1684 // we further want to do multiple task.
1685 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1686 if (launchTaskBehind
1687 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1688 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1689 }
1690 }
1691
Craig Mautner8849a5e2013-04-02 16:41:03 -07001692 // We'll invoke onUserLeaving before onPause only if the launching
1693 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001694 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001695 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001696
1697 // If the caller has asked not to resume at this point, we make note
1698 // of this in the record so that we can skip it when trying to find
1699 // the top running activity.
1700 if (!doResume) {
1701 r.delayedResume = true;
1702 }
1703
Craig Mautnera254cd72014-05-25 16:47:39 -07001704 ActivityRecord notTop =
1705 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001706
1707 // If the onlyIfNeeded flag is set, then we can do this if the activity
1708 // being launched is the same as the one making the call... or, as
1709 // a special case, if we do not know the caller then we count the
1710 // current top activity as the caller.
1711 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1712 ActivityRecord checkedCaller = sourceRecord;
1713 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001714 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001715 }
1716 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1717 // Caller is not the same as launcher, so always needed.
1718 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1719 }
1720 }
1721
Craig Mautner8849a5e2013-04-02 16:41:03 -07001722 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001723 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001724
1725 // If the caller is not coming from another activity, but has given us an
1726 // explicit task into which they would like us to launch the new activity,
1727 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001728 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1729 final Intent baseIntent = inTask.getBaseIntent();
1730 final ActivityRecord root = inTask.getRootActivity();
1731 if (baseIntent == null) {
1732 ActivityOptions.abort(options);
1733 throw new IllegalArgumentException("Launching into task without base intent: "
1734 + inTask);
1735 }
1736
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001737 // If this task is empty, then we are adding the first activity -- it
1738 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001739 if (launchSingleInstance || launchSingleTask) {
1740 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1741 ActivityOptions.abort(options);
1742 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1743 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001744 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001745 if (root != null) {
1746 ActivityOptions.abort(options);
1747 throw new IllegalArgumentException("Caller with inTask " + inTask
1748 + " has root " + root + " but target is singleInstance/Task");
1749 }
1750 }
1751
1752 // If task is empty, then adopt the interesting intent launch flags in to the
1753 // activity being started.
1754 if (root == null) {
1755 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1756 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1757 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1758 launchFlags = (launchFlags&~flagsOfInterest)
1759 | (baseIntent.getFlags()&flagsOfInterest);
1760 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001761 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001762 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001763
Dianne Hackborn962d5352014-08-29 16:27:40 -07001764 // If the task is not empty and the caller is asking to start it as the root
1765 // of a new task, then we don't actually want to start this on the task. We
1766 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001767 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001768 addingToTask = false;
1769
1770 } else {
1771 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001772 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001773
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001774 reuseTask = inTask;
1775 } else {
1776 inTask = null;
1777 }
1778
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001779 if (inTask == null) {
1780 if (sourceRecord == null) {
1781 // This activity is not being started from another... in this
1782 // case we -always- start a new task.
1783 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1784 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1785 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1786 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1787 }
1788 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1789 // The original activity who is starting us is running as a single
1790 // instance... this new activity it is starting must go on its
1791 // own task.
1792 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1793 } else if (launchSingleInstance || launchSingleTask) {
1794 // The activity being started is a single instance... it always
1795 // gets launched into its own task.
1796 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1797 }
1798 }
1799
1800 ActivityInfo newTaskInfo = null;
1801 Intent newTaskIntent = null;
1802 ActivityStack sourceStack;
1803 if (sourceRecord != null) {
1804 if (sourceRecord.finishing) {
1805 // If the source is finishing, we can't further count it as our source. This
1806 // is because the task it is associated with may now be empty and on its way out,
1807 // so we don't want to blindly throw it in to that task. Instead we will take
1808 // the NEW_TASK flow and try to find a task for it. But save the task information
1809 // so it can be used when creating the new task.
1810 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1811 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1812 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1813 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1814 newTaskInfo = sourceRecord.info;
1815 newTaskIntent = sourceRecord.task.intent;
1816 }
1817 sourceRecord = null;
1818 sourceStack = null;
1819 } else {
1820 sourceStack = sourceRecord.task.stack;
1821 }
1822 } else {
1823 sourceStack = null;
1824 }
1825
1826 boolean movedHome = false;
1827 ActivityStack targetStack;
1828
1829 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001830 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001831
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001832 // We may want to try to place the new activity in to an existing task. We always
1833 // do this if the target activity is singleTask or singleInstance; we will also do
1834 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1835 // us to still place it in a new task: multi task, always doc mode, or being asked to
1836 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001837 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1838 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001839 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001840 // If bring to front is requested, and no result is requested and we have not
1841 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001842 // we can find a task that was started with this same
1843 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001844 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001845 // See if there is a task to bring to the front. If this is
1846 // a SINGLE_INSTANCE activity, there can be one and only one
1847 // instance of it in the history, and it is always in its own
1848 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001849 ActivityRecord intentActivity = !launchSingleInstance ?
1850 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001851 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001852 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001853 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001854 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001855 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1856 }
Craig Mautner29219d92013-04-16 20:19:12 -07001857 if (r.task == null) {
1858 r.task = intentActivity.task;
1859 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001860 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001861 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001862 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1863 + " from " + intentActivity);
Craig Mautner299f9602015-01-26 09:47:33 -08001864 targetStack.moveToFront("intentActivityFound");
Craig Mautner8849a5e2013-04-02 16:41:03 -07001865 if (intentActivity.task.intent == null) {
1866 // This task was started because of movement of
1867 // the activity based on affinity... now that we
1868 // are actually launching it, we can assign the
1869 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001870 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001871 }
1872 // If the target task is not in the front, then we need
1873 // to bring it to the front... except... well, with
1874 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1875 // to have the same behavior as if a new instance was
1876 // being started, which means not bringing it to the front
1877 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001878 final ActivityStack lastStack = getLastStack();
1879 ActivityRecord curTop = lastStack == null?
1880 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001881 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001882 if (curTop != null && (curTop.task != intentActivity.task ||
1883 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001884 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001885 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1886 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001887 // We really do want to push this one into the
1888 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001889 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001890 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1891 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001892 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001893 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1894 options, "bringingFoundTaskToFront");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001895 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001896 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1897 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001898 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001899 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001900 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001901 options = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001902 movedToFront = true;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001903 }
1904 }
1905 // If the caller has requested that the target task be
1906 // reset, then do so.
1907 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1908 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1909 }
1910 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1911 // We don't need to start a new activity, and
1912 // the client said not to do anything if that
1913 // is the case, so this is it! And for paranoia, make
1914 // sure we have correctly resumed the top activity.
1915 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001916 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001917
1918 // Make sure to notify Keyguard as well if we are not running an app
1919 // transition later.
1920 if (!movedToFront) {
1921 notifyActivityDrawnForKeyguard();
1922 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001923 } else {
1924 ActivityOptions.abort(options);
1925 }
1926 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1927 }
1928 if ((launchFlags &
1929 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1930 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1931 // The caller has requested to completely replace any
1932 // existing task with its new activity. Well that should
1933 // not be too hard...
1934 reuseTask = intentActivity.task;
1935 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001936 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001937 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001938 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001939 // In this situation we want to remove all activities
1940 // from the task up to the one being started. In most
1941 // cases this means we are resetting the task to its
1942 // initial state.
1943 ActivityRecord top =
1944 intentActivity.task.performClearTaskLocked(r, launchFlags);
1945 if (top != null) {
1946 if (top.frontOfTask) {
1947 // Activity aliases may mean we use different
1948 // intents for the top activity, so make sure
1949 // the task now has the identity of the new
1950 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001951 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001952 }
1953 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1954 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001955 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001956 } else {
1957 // A special case: we need to
1958 // start the activity because it is not currently
1959 // running, and the caller has asked to clear the
1960 // current task to have this activity at the top.
1961 addingToTask = true;
1962 // Now pretend like this activity is being started
1963 // by the top of its task, so it is put in the
1964 // right place.
1965 sourceRecord = intentActivity;
1966 }
1967 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1968 // In this case the top activity on the task is the
1969 // same as the one being launched, so we take that
1970 // as a request to bring the task to the foreground.
1971 // If the top activity in the task is the root
1972 // activity, deliver this new intent to it if it
1973 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001974 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001975 && intentActivity.realActivity.equals(r.realActivity)) {
1976 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1977 intentActivity.task);
1978 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001979 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001980 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001981 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
1982 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001983 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1984 // In this case we are launching the root activity
1985 // of the task, but with a different intent. We
1986 // should start a new instance on top.
1987 addingToTask = true;
1988 sourceRecord = intentActivity;
1989 }
1990 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1991 // In this case an activity is being launched in to an
1992 // existing task, without resetting that task. This
1993 // is typically the situation of launching an activity
1994 // from a notification or shortcut. We want to place
1995 // the new activity on top of the current task.
1996 addingToTask = true;
1997 sourceRecord = intentActivity;
1998 } else if (!intentActivity.task.rootWasReset) {
1999 // In this case we are launching in to an existing task
2000 // that has not yet been started from its front door.
2001 // The current task has been brought to the front.
2002 // Ideally, we'd probably like to place this new task
2003 // at the bottom of its stack, but that's a little hard
2004 // to do with the current organization of the code so
2005 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002006 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002007 }
2008 if (!addingToTask && reuseTask == null) {
2009 // We didn't do anything... but it was needed (a.k.a., client
2010 // don't use that intent!) And for paranoia, make
2011 // sure we have correctly resumed the top activity.
2012 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002013 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002014 if (!movedToFront) {
2015 // Make sure to notify Keyguard as well if we are not running an app
2016 // transition later.
2017 notifyActivityDrawnForKeyguard();
2018 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002019 } else {
2020 ActivityOptions.abort(options);
2021 }
2022 return ActivityManager.START_TASK_TO_FRONT;
2023 }
2024 }
2025 }
2026 }
2027
2028 //String uri = r.intent.toURI();
2029 //Intent intent2 = new Intent(uri);
2030 //Slog.i(TAG, "Given intent: " + r.intent);
2031 //Slog.i(TAG, "URI is: " + uri);
2032 //Slog.i(TAG, "To intent: " + intent2);
2033
2034 if (r.packageName != null) {
2035 // If the activity being launched is the same as the one currently
2036 // at the top, then we need to check if it should only be launched
2037 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002038 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002039 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002040 if (top != null && r.resultTo == null) {
2041 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2042 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002043 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002044 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002045 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2046 top.task);
2047 // For paranoia, make sure we have correctly
2048 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002049 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002050 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002051 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002052 }
2053 ActivityOptions.abort(options);
2054 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2055 // We don't need to start a new activity, and
2056 // the client said not to do anything if that
2057 // is the case, so this is it!
2058 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2059 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002060 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002061 return ActivityManager.START_DELIVERED_TO_TOP;
2062 }
2063 }
2064 }
2065 }
2066
2067 } else {
2068 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002069 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2070 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002071 }
2072 ActivityOptions.abort(options);
2073 return ActivityManager.START_CLASS_NOT_FOUND;
2074 }
2075
2076 boolean newTask = false;
2077 boolean keepCurTransition = false;
2078
Craig Mautnerbb742462014-07-07 15:28:55 -07002079 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002080 sourceRecord.task : null;
2081
Craig Mautner8849a5e2013-04-02 16:41:03 -07002082 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002083 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002084 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002085 if (isLockTaskModeViolation(reuseTask)) {
2086 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2087 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2088 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002089 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002090 targetStack = computeStackFocus(r, newTask);
2091 targetStack.moveToFront("startingNewTask");
2092
Craig Mautner8849a5e2013-04-02 16:41:03 -07002093 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002094 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2095 newTaskInfo != null ? newTaskInfo : r.info,
2096 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002097 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2098 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002099 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2100 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002101 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002102 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002103 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002104 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002105 if ((launchFlags &
2106 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2107 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2108 // Caller wants to appear on home activity, so before starting
2109 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002110 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002111 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002112 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002113 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002114 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002115 if (isLockTaskModeViolation(sourceTask)) {
2116 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2117 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2118 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002119 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002120 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002121 final TaskRecord topTask = targetStack.topTask();
2122 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002123 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2124 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002125 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002126 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002127 // In this case, we are adding the activity to an existing
2128 // task, but the caller has asked to clear that task if the
2129 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002130 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002131 keepCurTransition = true;
2132 if (top != null) {
2133 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002134 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002135 // For paranoia, make sure we have correctly
2136 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002137 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002138 if (doResume) {
2139 targetStack.resumeTopActivityLocked(null);
2140 }
2141 ActivityOptions.abort(options);
2142 return ActivityManager.START_DELIVERED_TO_TOP;
2143 }
2144 } else if (!addingToTask &&
2145 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2146 // In this case, we are launching an activity in our own task
2147 // that may already be running somewhere in the history, and
2148 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002149 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002150 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002151 final TaskRecord task = top.task;
2152 task.moveActivityToFrontLocked(top);
2153 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002154 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002155 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002156 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002157 if (doResume) {
2158 targetStack.resumeTopActivityLocked(null);
2159 }
2160 return ActivityManager.START_DELIVERED_TO_TOP;
2161 }
2162 }
2163 // An existing activity is starting this new activity, so we want
2164 // to keep the new one in the same task as the one that is starting
2165 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002166 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002167 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002168 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002169
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002170 } else if (inTask != null) {
2171 // The calling is asking that the new activity be started in an explicit
2172 // task it has provided to us.
2173 if (isLockTaskModeViolation(inTask)) {
2174 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2175 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2176 }
2177 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002178 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002179
2180 // Check whether we should actually launch the new activity in to the task,
2181 // or just reuse the current activity on top.
2182 ActivityRecord top = inTask.getTopActivity();
2183 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2184 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2185 || launchSingleTop || launchSingleTask) {
2186 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2187 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2188 // We don't need to start a new activity, and
2189 // the client said not to do anything if that
2190 // is the case, so this is it!
2191 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2192 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002193 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002194 return ActivityManager.START_DELIVERED_TO_TOP;
2195 }
2196 }
2197
Dianne Hackborn962d5352014-08-29 16:27:40 -07002198 if (!addingToTask) {
2199 // We don't actually want to have this activity added to the task, so just
2200 // stop here but still tell the caller that we consumed the intent.
2201 ActivityOptions.abort(options);
2202 return ActivityManager.START_TASK_TO_FRONT;
2203 }
2204
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002205 r.setTask(inTask, null);
2206 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2207 + " in explicit task " + r.task);
2208
Craig Mautner8849a5e2013-04-02 16:41:03 -07002209 } else {
2210 // This not being started from an existing activity, and not part
2211 // of a new task... just put it in the top task, though these days
2212 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002213 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002214 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002215 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002216 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002217 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002218 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002219 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2220 + " in new guessed " + r.task);
2221 }
2222
2223 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002224 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002225
Craig Mautner76e2a762014-06-24 09:05:43 -07002226 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2227 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2228 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002229 if (newTask) {
2230 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2231 }
2232 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002233 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002234 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002235 if (!launchTaskBehind) {
2236 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002237 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002238 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002239 return ActivityManager.START_SUCCESS;
2240 }
2241
Craig Mautneree36c772014-07-16 14:56:05 -07002242 final void doPendingActivityLaunchesLocked(boolean doResume) {
2243 while (!mPendingActivityLaunches.isEmpty()) {
2244 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002245 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002246 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002247 }
2248 }
2249
Craig Mautner7f13ed32014-07-28 14:00:35 -07002250 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002251 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2252 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002253 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002254 mPendingActivityLaunches.remove(palNdx);
2255 }
2256 }
2257 }
2258
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002259 void acquireLaunchWakelock() {
2260 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2261 throw new IllegalStateException("Calling must be system uid");
2262 }
2263 mLaunchingActivity.acquire();
2264 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2265 // To be safe, don't allow the wake lock to be held for too long.
2266 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2267 }
2268 }
2269
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002270 /**
2271 * Called when the frontmost task is idle.
2272 * @return the state of mService.mBooting before this was called.
2273 */
2274 private boolean checkFinishBootingLocked() {
2275 final boolean booting = mService.mBooting;
2276 boolean enableScreen = false;
2277 mService.mBooting = false;
2278 if (!mService.mBooted) {
2279 mService.mBooted = true;
2280 enableScreen = true;
2281 }
2282 if (booting || enableScreen) {
2283 mService.postFinishBooting(booting, enableScreen);
2284 }
2285 return booting;
2286 }
2287
Craig Mautnerf3333272013-04-22 10:55:53 -07002288 // Checked.
2289 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2290 Configuration config) {
2291 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2292
Craig Mautnerf3333272013-04-22 10:55:53 -07002293 ArrayList<ActivityRecord> stops = null;
2294 ArrayList<ActivityRecord> finishes = null;
2295 ArrayList<UserStartedState> startingUsers = null;
2296 int NS = 0;
2297 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002298 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002299 boolean activityRemoved = false;
2300
2301 ActivityRecord r = ActivityRecord.forToken(token);
2302 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002303 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2304 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002305 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2306 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002307 if (fromTimeout) {
2308 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002309 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002310
2311 // This is a hack to semi-deal with a race condition
2312 // in the client where it can be constructed with a
2313 // newer configuration from when we asked it to launch.
2314 // We'll update with whatever configuration it now says
2315 // it used to launch.
2316 if (config != null) {
2317 r.configuration = config;
2318 }
2319
2320 // We are now idle. If someone is waiting for a thumbnail from
2321 // us, we can now deliver.
2322 r.idle = true;
2323
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002324 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002325 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002326 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002327 }
2328 }
2329
2330 if (allResumedActivitiesIdle()) {
2331 if (r != null) {
2332 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002333 }
2334
2335 if (mLaunchingActivity.isHeld()) {
2336 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2337 if (VALIDATE_WAKE_LOCK_CALLER &&
2338 Binder.getCallingUid() != Process.myUid()) {
2339 throw new IllegalStateException("Calling must be system uid");
2340 }
2341 mLaunchingActivity.release();
2342 }
2343 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002344 }
2345
2346 // Atomically retrieve all of the other things to do.
2347 stops = processStoppingActivitiesLocked(true);
2348 NS = stops != null ? stops.size() : 0;
2349 if ((NF=mFinishingActivities.size()) > 0) {
2350 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2351 mFinishingActivities.clear();
2352 }
2353
Craig Mautnerf3333272013-04-22 10:55:53 -07002354 if (mStartingUsers.size() > 0) {
2355 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2356 mStartingUsers.clear();
2357 }
2358
Craig Mautnerf3333272013-04-22 10:55:53 -07002359 // Stop any activities that are scheduled to do so but have been
2360 // waiting for the next one to start.
2361 for (int i = 0; i < NS; i++) {
2362 r = stops.get(i);
2363 final ActivityStack stack = r.task.stack;
2364 if (r.finishing) {
2365 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2366 } else {
2367 stack.stopActivityLocked(r);
2368 }
2369 }
2370
2371 // Finish any activities that are scheduled to do so but have been
2372 // waiting for the next one to start.
2373 for (int i = 0; i < NF; i++) {
2374 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002375 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002376 }
2377
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002378 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002379 // Complete user switch
2380 if (startingUsers != null) {
2381 for (int i = 0; i < startingUsers.size(); i++) {
2382 mService.finishUserSwitch(startingUsers.get(i));
2383 }
2384 }
2385 // Complete starting up of background users
2386 if (mStartingBackgroundUsers.size() > 0) {
2387 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2388 mStartingBackgroundUsers.clear();
2389 for (int i = 0; i < startingUsers.size(); i++) {
2390 mService.finishUserBoot(startingUsers.get(i));
2391 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002392 }
2393 }
2394
2395 mService.trimApplications();
2396 //dump();
2397 //mWindowManager.dump();
2398
Craig Mautnerf3333272013-04-22 10:55:53 -07002399 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002400 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002401 }
2402
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002403 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002404 }
2405
Craig Mautner8e569572013-10-11 17:36:59 -07002406 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002407 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002408 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2409 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002410 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2411 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2412 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002413 }
Craig Mautner19091252013-10-05 00:03:53 -07002414 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002415 }
2416
2417 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002418 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2419 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002420 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2421 stacks.get(stackNdx).closeSystemDialogsLocked();
2422 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002423 }
2424 }
2425
Craig Mautner93529a42013-10-04 15:03:13 -07002426 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002427 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002428 }
2429
Craig Mautner8d341ef2013-03-26 09:03:27 -07002430 /**
2431 * @return true if some activity was finished (or would have finished if doit were true).
2432 */
2433 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2434 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002435 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2436 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002437 final int numStacks = stacks.size();
2438 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2439 final ActivityStack stack = stacks.get(stackNdx);
2440 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2441 didSomething = true;
2442 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002443 }
2444 }
2445 return didSomething;
2446 }
2447
Dianne Hackborna413dc02013-07-12 12:02:55 -07002448 void updatePreviousProcessLocked(ActivityRecord r) {
2449 // Now that this process has stopped, we may want to consider
2450 // it to be the previous app to try to keep around in case
2451 // the user wants to return to it.
2452
2453 // First, found out what is currently the foreground app, so that
2454 // we don't blow away the previous app if this activity is being
2455 // hosted by the process that is actually still the foreground.
2456 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002457 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2458 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002459 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2460 final ActivityStack stack = stacks.get(stackNdx);
2461 if (isFrontStack(stack)) {
2462 if (stack.mResumedActivity != null) {
2463 fgApp = stack.mResumedActivity.app;
2464 } else if (stack.mPausingActivity != null) {
2465 fgApp = stack.mPausingActivity.app;
2466 }
2467 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002468 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002469 }
2470 }
2471
2472 // Now set this one as the previous process, only if that really
2473 // makes sense to.
2474 if (r.app != null && fgApp != null && r.app != fgApp
2475 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002476 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002477 mService.mPreviousProcess = r.app;
2478 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2479 }
2480 }
2481
Craig Mautner05d29032013-05-03 13:40:13 -07002482 boolean resumeTopActivitiesLocked() {
2483 return resumeTopActivitiesLocked(null, null, null);
2484 }
2485
2486 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2487 Bundle targetOptions) {
2488 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002489 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002490 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002491 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002492 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002493 if (isFrontStack(targetStack)) {
2494 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2495 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002496
Craig Mautnere0a38842013-12-16 16:14:02 -08002497 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2498 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002499 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2500 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002501 if (stack == targetStack) {
2502 // Already started above.
2503 continue;
2504 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002505 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002506 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002507 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002508 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002509 }
Craig Mautner05d29032013-05-03 13:40:13 -07002510 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002511 }
2512
Todd Kennedy539db512014-12-15 09:57:55 -08002513 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002514 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2515 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002516 final int numStacks = stacks.size();
2517 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2518 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002519 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002520 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002521 }
2522 }
2523
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002524 void finishVoiceTask(IVoiceInteractionSession session) {
2525 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2526 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2527 final int numStacks = stacks.size();
2528 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2529 final ActivityStack stack = stacks.get(stackNdx);
2530 stack.finishVoiceTask(session);
2531 }
2532 }
2533 }
2534
Craig Mautner299f9602015-01-26 09:47:33 -08002535 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002536 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2537 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002538 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002539 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2540 // Caller wants the home activity moved with it. To accomplish this,
2541 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002542 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002543 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002544 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08002545 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2546 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002547 }
2548
Craig Mautner967212c2013-04-13 21:10:58 -07002549 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002550 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2551 if (activityContainer != null) {
2552 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002553 }
2554 return null;
2555 }
2556
Craig Mautner967212c2013-04-13 21:10:58 -07002557 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002558 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002559 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2560 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002561 }
2562 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002563 }
2564
Craig Mautner4a1cb222013-12-04 16:14:06 -08002565 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002566 ActivityRecord homeActivity = getHomeActivity();
2567 if (homeActivity != null) {
2568 return homeActivity.appToken;
2569 }
2570 return null;
2571 }
2572
2573 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002574 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2575 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2576 final TaskRecord task = tasks.get(taskNdx);
2577 if (task.isHomeTask()) {
2578 final ArrayList<ActivityRecord> activities = task.mActivities;
2579 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2580 final ActivityRecord r = activities.get(activityNdx);
2581 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002582 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002583 }
2584 }
2585 }
2586 }
2587 return null;
2588 }
2589
Todd Kennedyca4d8422015-01-15 15:19:22 -08002590 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002591 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002592 ActivityContainer activityContainer =
2593 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002594 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002595 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002596 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002597 return activityContainer;
2598 }
2599
Craig Mautner34b73df2014-01-12 21:11:08 -08002600 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002601 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2602 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2603 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002604 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2605 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002606 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002607 }
2608 }
2609
Craig Mautner95da1082014-02-24 17:54:35 -08002610 void deleteActivityContainer(IActivityContainer container) {
2611 ActivityContainer activityContainer = (ActivityContainer)container;
2612 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002613 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2614 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002615 final int stackId = activityContainer.mStackId;
2616 mActivityContainers.remove(stackId);
2617 mWindowManager.removeStack(stackId);
2618 }
2619 }
2620
Wale Ogunwale60454db2015-01-23 16:05:07 -08002621 void resizeStackLocked(int stackId, Rect bounds) {
2622 final ActivityStack stack = getStack(stackId);
2623 if (stack == null) {
2624 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2625 return;
2626 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002627
2628 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002629 if (r != null && !r.task.mResizeable) {
2630 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002631 return;
2632 }
2633
Wale Ogunwale60454db2015-01-23 16:05:07 -08002634 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2635 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002636 if (r != null) {
2637 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2638 // And we need to make sure at this point that all other activities
2639 // are made visible with the correct configuration.
2640 ensureActivitiesVisibleLocked(r, 0);
2641 if (!updated) {
2642 resumeTopActivitiesLocked(stack, null, null);
2643 }
2644 }
2645 }
2646 }
2647
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002648 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2649 * current task stack if it only has one entry, moving the task to a stack that matches the
2650 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2651 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2652 task.mResizeable = true;
2653 final ActivityStack currentStack = task.stack;
2654 if (currentStack.isHomeStack()) {
2655 // Can't move task off the home stack. Sorry!
2656 return;
2657 }
2658
2659 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2660 if (matchingStackId != -1) {
2661 // There is already a stack with the right bounds!
2662 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2663 // Nothing to do here. Already in the right stack...
2664 return;
2665 }
2666 // Move task to stack with matching bounds.
2667 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2668 return;
2669 }
2670
2671 if (currentStack != null && currentStack.numTasks() == 1) {
2672 // Just resize the current stack since this is the task in it.
2673 resizeStackLocked(currentStack.mStackId, bounds);
2674 return;
2675 }
2676
2677 // Create new stack and move the task to it.
2678 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2679 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2680 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2681
2682 if (newStack == null) {
2683 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2684 return;
2685 }
2686 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2687 resizeStackLocked(newStack.mStackId, bounds);
2688 }
2689
Todd Kennedy4900bf92015-01-16 16:05:14 -08002690 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002691 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2692 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002693 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002694 }
2695
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002696 ActivityContainer activityContainer = new ActivityContainer(stackId);
2697 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002698 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002699 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002700 }
2701
2702 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002703 while (true) {
2704 if (++mLastStackId <= HOME_STACK_ID) {
2705 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002706 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002707 if (getStack(mLastStackId) == null) {
2708 break;
2709 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002710 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002711 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002712 }
2713
Wale Ogunwale7de05352014-12-12 15:21:33 -08002714 private boolean restoreRecentTaskLocked(TaskRecord task) {
2715 ActivityStack stack = null;
2716 // Determine stack to restore task to.
2717 if (mLeanbackOnlyDevice) {
2718 // There is only one stack for lean back devices.
2719 stack = mHomeStack;
2720 } else {
2721 // Look for the top stack on the home display that isn't the home stack.
2722 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2723 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2724 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -08002725 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002726 stack = tmpStack;
2727 break;
2728 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002729 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002730 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002731
2732 if (stack == null) {
2733 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2734 // before an application stack is created...Go ahead and create one on the default
2735 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002736 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002737 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002738 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002739 if (DEBUG_RECENTS)
2740 Slog.v(TAG, "Created stack=" + stack + " for recents restoration.");
2741 }
2742
2743 if (stack == null) {
2744 // What does this mean??? Not sure how we would get here...
2745 if (DEBUG_RECENTS)
2746 Slog.v(TAG, "Unable to find/create stack to restore recent task=" + task);
2747 return false;
2748 }
2749
2750 stack.addTask(task, false, false);
2751 if (DEBUG_RECENTS)
2752 Slog.v(TAG, "Added restored task=" + task + " to stack=" + stack);
2753 final ArrayList<ActivityRecord> activities = task.mActivities;
2754 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2755 final ActivityRecord r = activities.get(activityNdx);
2756 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2757 r.info.screenOrientation, r.fullscreen,
2758 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2759 r.userId, r.info.configChanges, task.voiceSession != null,
2760 r.mLaunchTaskBehind);
2761 }
2762 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002763 }
2764
Craig Mautner299f9602015-01-26 09:47:33 -08002765 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002766 final TaskRecord task = anyTaskForIdLocked(taskId);
2767 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002768 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002769 return;
2770 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002771 final ActivityStack stack = getStack(stackId);
2772 if (stack == null) {
2773 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2774 return;
2775 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002776 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2777 if (task.stack != null) {
2778 task.stack.removeTask(task, "moveTaskToStack", false);
2779 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002780 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002781 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002782 }
2783
Craig Mautnerac6f8432013-07-17 13:24:59 -07002784 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002785 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002786 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2787 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002788 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2789 final ActivityStack stack = stacks.get(stackNdx);
2790 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002791 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2792 continue;
2793 }
2794 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2795 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2796 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002797 continue;
2798 }
2799 final ActivityRecord ar = stack.findTaskLocked(r);
2800 if (ar != null) {
2801 return ar;
2802 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002803 }
2804 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002805 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002806 return null;
2807 }
2808
2809 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002810 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2811 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002812 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2813 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2814 if (ar != null) {
2815 return ar;
2816 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002817 }
2818 }
2819 return null;
2820 }
2821
Craig Mautner8d341ef2013-03-26 09:03:27 -07002822 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002823 scheduleSleepTimeout();
2824 if (!mGoingToSleep.isHeld()) {
2825 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002826 if (mLaunchingActivity.isHeld()) {
2827 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2828 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002829 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002830 mLaunchingActivity.release();
2831 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002832 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002833 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002834 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002835 }
2836
2837 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002838 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002839
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002840 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002841 final long endTime = System.currentTimeMillis() + timeout;
2842 while (true) {
2843 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002844 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2845 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002846 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2847 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2848 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002849 }
2850 if (cantShutdown) {
2851 long timeRemaining = endTime - System.currentTimeMillis();
2852 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002853 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002854 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002855 } catch (InterruptedException e) {
2856 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002857 } else {
2858 Slog.w(TAG, "Activity manager shutdown timed out");
2859 timedout = true;
2860 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002861 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002862 } else {
2863 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002864 }
2865 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002866
2867 // Force checkReadyForSleep to complete.
2868 mSleepTimeout = true;
2869 checkReadyForSleepLocked();
2870
Craig Mautner8d341ef2013-03-26 09:03:27 -07002871 return timedout;
2872 }
2873
2874 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002875 removeSleepTimeouts();
2876 if (mGoingToSleep.isHeld()) {
2877 mGoingToSleep.release();
2878 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002879 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2880 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002881 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2882 final ActivityStack stack = stacks.get(stackNdx);
2883 stack.awakeFromSleepingLocked();
2884 if (isFrontStack(stack)) {
2885 resumeTopActivitiesLocked();
2886 }
Craig Mautner5314a402013-09-26 12:40:16 -07002887 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002888 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002889 mGoingToSleepActivities.clear();
2890 }
2891
2892 void activitySleptLocked(ActivityRecord r) {
2893 mGoingToSleepActivities.remove(r);
2894 checkReadyForSleepLocked();
2895 }
2896
2897 void checkReadyForSleepLocked() {
2898 if (!mService.isSleepingOrShuttingDown()) {
2899 // Do not care.
2900 return;
2901 }
2902
2903 if (!mSleepTimeout) {
2904 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002905 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2906 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002907 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2908 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2909 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002910 }
2911
2912 if (mStoppingActivities.size() > 0) {
2913 // Still need to tell some activities to stop; can't sleep yet.
2914 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2915 + mStoppingActivities.size() + " activities");
2916 scheduleIdleLocked();
2917 dontSleep = true;
2918 }
2919
2920 if (mGoingToSleepActivities.size() > 0) {
2921 // Still need to tell some activities to sleep; can't sleep yet.
2922 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2923 + mGoingToSleepActivities.size() + " activities");
2924 dontSleep = true;
2925 }
2926
2927 if (dontSleep) {
2928 return;
2929 }
2930 }
2931
Craig Mautnere0a38842013-12-16 16:14:02 -08002932 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2933 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002934 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2935 stacks.get(stackNdx).goToSleep();
2936 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002937 }
2938
2939 removeSleepTimeouts();
2940
2941 if (mGoingToSleep.isHeld()) {
2942 mGoingToSleep.release();
2943 }
2944 if (mService.mShuttingDown) {
2945 mService.notifyAll();
2946 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002947 }
2948
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002949 boolean reportResumedActivityLocked(ActivityRecord r) {
2950 final ActivityStack stack = r.task.stack;
2951 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002952 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002953 }
2954 if (allResumedActivitiesComplete()) {
2955 ensureActivitiesVisibleLocked(null, 0);
2956 mWindowManager.executeAppTransition();
2957 return true;
2958 }
2959 return false;
2960 }
2961
Craig Mautner8d341ef2013-03-26 09:03:27 -07002962 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002963 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2964 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002965 final int numStacks = stacks.size();
2966 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2967 final ActivityStack stack = stacks.get(stackNdx);
2968 stack.handleAppCrashLocked(app);
2969 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002970 }
2971 }
2972
Jose Lima4b6c6692014-08-12 17:41:12 -07002973 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002974 final ActivityStack stack = r.task.stack;
2975 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002976 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2977 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002978 return false;
2979 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002980 final boolean isVisible = stack.hasVisibleBehindActivity();
2981 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2982 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002983
2984 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002985 if (top == null || top == r || (visible == isVisible)) {
2986 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2987 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002988 return true;
2989 }
2990
2991 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002992 if (visible && top.fullscreen) {
2993 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07002994 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07002995 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07002996 " top.app.thread=" + top.app.thread);
2997 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002998 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2999 // Only the activity set as currently visible behind should actively reset its
3000 // visible behind state.
3001 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3002 + visible + " stack.getVisibleBehindActivity()=" +
3003 stack.getVisibleBehindActivity() + " r=" + r);
3004 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003005 }
3006
Jose Lima4b6c6692014-08-12 17:41:12 -07003007 stack.setVisibleBehindActivity(visible ? r : null);
3008 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003009 // Make the activity immediately above r opaque.
3010 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3011 if (next != null) {
3012 mService.convertFromTranslucent(next.appToken);
3013 }
3014 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003015 if (top.app != null && top.app.thread != null) {
3016 // Notify the top app of the change.
3017 try {
3018 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3019 } catch (RemoteException e) {
3020 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003021 }
3022 return true;
3023 }
3024
Craig Mautnerbb742462014-07-07 15:28:55 -07003025 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3026 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3027 r.mLaunchTaskBehind = false;
3028 final TaskRecord task = r.task;
3029 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003030 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003031 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003032 mWindowManager.setAppVisibility(r.appToken, false);
3033 }
3034
3035 void scheduleLaunchTaskBehindComplete(IBinder token) {
3036 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3037 }
3038
Craig Mautnerde4ef022013-04-07 19:01:33 -07003039 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003040 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003041 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3042 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003043 final int topStackNdx = stacks.size() - 1;
3044 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3045 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003046 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003047 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003048 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003049 }
3050
3051 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003052 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3053 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003054 final int numStacks = stacks.size();
3055 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3056 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003057 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003058 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003059 }
3060 }
3061
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003062 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3063 // Examine all activities currently running in the process.
3064 TaskRecord firstTask = null;
3065 // Tasks is non-null only if two or more tasks are found.
3066 ArraySet<TaskRecord> tasks = null;
3067 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3068 for (int i=0; i<app.activities.size(); i++) {
3069 ActivityRecord r = app.activities.get(i);
3070 // First, if we find an activity that is in the process of being destroyed,
3071 // then we just aren't going to do anything for now; we want things to settle
3072 // down before we try to prune more activities.
3073 if (r.finishing || r.state == ActivityState.DESTROYING
3074 || r.state == ActivityState.DESTROYED) {
3075 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3076 return;
3077 }
3078 // Don't consider any activies that are currently not in a state where they
3079 // can be destroyed.
3080 if (r.visible || !r.stopped || !r.haveState
3081 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
3082 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
3083 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3084 continue;
3085 }
3086 if (r.task != null) {
3087 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3088 + " from " + r);
3089 if (firstTask == null) {
3090 firstTask = r.task;
3091 } else if (firstTask != r.task) {
3092 if (tasks == null) {
3093 tasks = new ArraySet<>();
3094 tasks.add(firstTask);
3095 }
3096 tasks.add(r.task);
3097 }
3098 }
3099 }
3100 if (tasks == null) {
3101 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3102 return;
3103 }
3104 // If we have activities in multiple tasks that are in a position to be destroyed,
3105 // let's iterate through the tasks and release the oldest one.
3106 final int numDisplays = mActivityDisplays.size();
3107 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3108 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3109 // Step through all stacks starting from behind, to hit the oldest things first.
3110 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3111 final ActivityStack stack = stacks.get(stackNdx);
3112 // Try to release activities in this stack; if we manage to, we are done.
3113 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3114 return;
3115 }
3116 }
3117 }
3118 }
3119
Craig Mautner8d341ef2013-03-26 09:03:27 -07003120 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003121 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003122 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003123 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003124
Craig Mautner858d8a62013-04-23 17:08:34 -07003125 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003126 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3127 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003128 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003129 final ActivityStack stack = stacks.get(stackNdx);
3130 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003131 TaskRecord task = stack.topTask();
3132 if (task != null) {
3133 mWindowManager.moveTaskToTop(task.taskId);
3134 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003135 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003136 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003137
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003138 ActivityStack stack = getStack(restoreStackId);
3139 if (stack == null) {
3140 stack = mHomeStack;
3141 }
3142 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003143 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003144 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003145 TaskRecord task = stack.topTask();
3146 if (task != null) {
3147 mWindowManager.moveTaskToTop(task.taskId);
3148 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003149 } else {
3150 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003151 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003152 }
Craig Mautner93529a42013-10-04 15:03:13 -07003153 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003154 }
3155
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003156 /**
3157 * Add background users to send boot completed events to.
3158 * @param userId The user being started in the background
3159 * @param uss The state object for the user.
3160 */
3161 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3162 mStartingBackgroundUsers.add(uss);
3163 }
3164
Craig Mautnerde4ef022013-04-07 19:01:33 -07003165 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003166 ArrayList<ActivityRecord> stops = null;
3167
3168 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003169 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3170 ActivityRecord s = mStoppingActivities.get(activityNdx);
3171 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
3172 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
3173 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3174 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003175 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003176 if (s.finishing) {
3177 // If this activity is finishing, it is sitting on top of
3178 // everyone else but we now know it is no longer needed...
3179 // so get rid of it. Otherwise, we need to go through the
3180 // normal flow and hide it once we determine that it is
3181 // hidden by the activities in front of it.
3182 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003183 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003184 }
3185 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003186 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003187 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3188 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003189 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003190 }
3191 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003192 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003193 }
3194 }
3195
3196 return stops;
3197 }
3198
Craig Mautnercf910b02013-04-23 11:23:27 -07003199 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003200 // FIXME
3201/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3202 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003203 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003204 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003205 if (isFrontStack(stack)) {
3206 if (r == null) {
3207 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3208 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003209 final ActivityRecord pausing = stack.mPausingActivity;
3210 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003211 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003212 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003213 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003214 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003215 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003216 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003217 }
3218 }
3219 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003220 final ActivityRecord resumed = stack.mResumedActivity;
3221 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003222 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003223 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003224 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003225 if (r != null && (state == ActivityState.INITIALIZING
3226 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003227 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003228 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003229 }
3230 }
3231 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003232*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003233 }
3234
Craig Mautner27084302013-03-25 08:05:25 -07003235 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003236 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003237 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003238 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3239 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3240 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003241 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003242 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003243
Craig Mautner20e72272013-04-01 13:45:53 -07003244 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003245 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003246 }
3247
Dianne Hackborn390517b2013-05-30 15:03:32 -07003248 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3249 boolean needSep, String prefix) {
3250 if (activity != null) {
3251 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3252 if (needSep) {
3253 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003254 }
3255 pw.print(prefix);
3256 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003257 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003258 }
3259 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003260 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003261 }
3262
Craig Mautner8d341ef2013-03-26 09:03:27 -07003263 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3264 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003265 boolean printed = false;
3266 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003267 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3268 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003269 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003270 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003271 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003272 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003273 final ActivityStack stack = stacks.get(stackNdx);
3274 StringBuilder stackHeader = new StringBuilder(128);
3275 stackHeader.append(" Stack #");
3276 stackHeader.append(stack.mStackId);
3277 stackHeader.append(":");
3278 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3279 needSep, stackHeader.toString());
3280 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3281 !dumpAll, false, dumpPackage, true,
3282 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003283
Craig Mautner4a1cb222013-12-04 16:14:06 -08003284 needSep = printed;
3285 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3286 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003287 if (pr) {
3288 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003289 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003290 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003291 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3292 " mResumedActivity: ");
3293 if (pr) {
3294 printed = true;
3295 needSep = false;
3296 }
3297 if (dumpAll) {
3298 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3299 " mLastPausedActivity: ");
3300 if (pr) {
3301 printed = true;
3302 needSep = true;
3303 }
3304 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3305 needSep, " mLastNoHistoryActivity: ");
3306 }
3307 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003308 }
3309 }
3310
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003311 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3312 false, dumpPackage, true, " Activities waiting to finish:", null);
3313 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3314 false, dumpPackage, true, " Activities waiting to stop:", null);
3315 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3316 false, dumpPackage, true, " Activities waiting for another to become visible:",
3317 null);
3318 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3319 false, dumpPackage, true, " Activities waiting to sleep:", null);
3320 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3321 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003322
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003323 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003324 }
3325
Dianne Hackborn390517b2013-05-30 15:03:32 -07003326 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003327 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003328 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003329 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003330 String innerPrefix = null;
3331 String[] args = null;
3332 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003333 for (int i=list.size()-1; i>=0; i--) {
3334 final ActivityRecord r = list.get(i);
3335 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3336 continue;
3337 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003338 if (innerPrefix == null) {
3339 innerPrefix = prefix + " ";
3340 args = new String[0];
3341 }
3342 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003343 final boolean full = !brief && (complete || !r.isInHistory());
3344 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003345 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003346 needNL = false;
3347 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003348 if (header1 != null) {
3349 pw.println(header1);
3350 header1 = null;
3351 }
3352 if (header2 != null) {
3353 pw.println(header2);
3354 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003355 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003356 if (lastTask != r.task) {
3357 lastTask = r.task;
3358 pw.print(prefix);
3359 pw.print(full ? "* " : " ");
3360 pw.println(lastTask);
3361 if (full) {
3362 lastTask.dump(pw, prefix + " ");
3363 } else if (complete) {
3364 // Complete + brief == give a summary. Isn't that obvious?!?
3365 if (lastTask.intent != null) {
3366 pw.print(prefix); pw.print(" ");
3367 pw.println(lastTask.intent.toInsecureStringWithClip());
3368 }
3369 }
3370 }
3371 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3372 pw.print(" #"); pw.print(i); pw.print(": ");
3373 pw.println(r);
3374 if (full) {
3375 r.dump(pw, innerPrefix);
3376 } else if (complete) {
3377 // Complete + brief == give a summary. Isn't that obvious?!?
3378 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3379 if (r.app != null) {
3380 pw.print(innerPrefix); pw.println(r.app);
3381 }
3382 }
3383 if (client && r.app != null && r.app.thread != null) {
3384 // flush anything that is already in the PrintWriter since the thread is going
3385 // to write to the file descriptor directly
3386 pw.flush();
3387 try {
3388 TransferPipe tp = new TransferPipe();
3389 try {
3390 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3391 r.appToken, innerPrefix, args);
3392 // Short timeout, since blocking here can
3393 // deadlock with the application.
3394 tp.go(fd, 2000);
3395 } finally {
3396 tp.kill();
3397 }
3398 } catch (IOException e) {
3399 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3400 } catch (RemoteException e) {
3401 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3402 }
3403 needNL = true;
3404 }
3405 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003406 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003407 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003408
Craig Mautnerf3333272013-04-22 10:55:53 -07003409 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003410 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003411 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3412 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003413 }
3414
3415 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003416 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003417 }
3418
3419 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003420 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003421 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3422 }
3423
Craig Mautner05d29032013-05-03 13:40:13 -07003424 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003425 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3426 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3427 }
Craig Mautner05d29032013-05-03 13:40:13 -07003428 }
3429
Craig Mautner0eea92c2013-05-16 13:35:39 -07003430 void removeSleepTimeouts() {
3431 mSleepTimeout = false;
3432 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3433 }
3434
3435 final void scheduleSleepTimeout() {
3436 removeSleepTimeouts();
3437 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3438 }
3439
Craig Mautner4a1cb222013-12-04 16:14:06 -08003440 @Override
3441 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003442 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003443 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3444 }
3445
3446 @Override
3447 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003448 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003449 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3450 }
3451
3452 @Override
3453 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003454 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003455 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3456 }
3457
3458 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003459 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003460 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003461 newDisplay = mActivityDisplays.get(displayId) == null;
3462 if (newDisplay) {
3463 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003464 if (activityDisplay.mDisplay == null) {
3465 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3466 return;
3467 }
Craig Mautner4504de52013-12-20 09:06:56 -08003468 mActivityDisplays.put(displayId, activityDisplay);
3469 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003470 }
Craig Mautner4504de52013-12-20 09:06:56 -08003471 if (newDisplay) {
3472 mWindowManager.onDisplayAdded(displayId);
3473 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003474 }
3475
3476 public void handleDisplayRemovedLocked(int displayId) {
3477 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003478 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3479 if (activityDisplay != null) {
3480 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003481 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003482 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003483 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003484 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003485 }
3486 }
3487 mWindowManager.onDisplayRemoved(displayId);
3488 }
3489
3490 public void handleDisplayChangedLocked(int displayId) {
3491 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003492 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3493 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003494 // TODO: Update the bounds.
3495 }
3496 }
3497 mWindowManager.onDisplayChanged(displayId);
3498 }
3499
3500 StackInfo getStackInfo(ActivityStack stack) {
3501 StackInfo info = new StackInfo();
3502 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3503 info.displayId = Display.DEFAULT_DISPLAY;
3504 info.stackId = stack.mStackId;
3505
3506 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3507 final int numTasks = tasks.size();
3508 int[] taskIds = new int[numTasks];
3509 String[] taskNames = new String[numTasks];
3510 for (int i = 0; i < numTasks; ++i) {
3511 final TaskRecord task = tasks.get(i);
3512 taskIds[i] = task.taskId;
3513 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3514 : task.realActivity != null ? task.realActivity.flattenToString()
3515 : task.getTopActivity() != null ? task.getTopActivity().packageName
3516 : "unknown";
3517 }
3518 info.taskIds = taskIds;
3519 info.taskNames = taskNames;
3520 return info;
3521 }
3522
3523 StackInfo getStackInfoLocked(int stackId) {
3524 ActivityStack stack = getStack(stackId);
3525 if (stack != null) {
3526 return getStackInfo(stack);
3527 }
3528 return null;
3529 }
3530
3531 ArrayList<StackInfo> getAllStackInfosLocked() {
3532 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003533 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3534 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003535 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3536 list.add(getStackInfo(stacks.get(ndx)));
3537 }
3538 }
3539 return list;
3540 }
3541
Jason Monka8f569c2014-07-07 12:15:21 -04003542 void showLockTaskToast() {
3543 mLockTaskNotify.showToast(mLockTaskIsLocked);
3544 }
3545
Craig Mautner299f9602015-01-26 09:47:33 -08003546 void setLockTaskModeLocked(TaskRecord task, boolean isLocked, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003547 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003548 // Take out of lock task mode if necessary
3549 if (mLockTaskModeTask != null) {
3550 final Message lockTaskMsg = Message.obtain();
3551 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3552 lockTaskMsg.what = LOCK_TASK_END_MSG;
3553 mLockTaskModeTask = null;
3554 mHandler.sendMessage(lockTaskMsg);
3555 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003556 return;
3557 }
3558 if (isLockTaskModeViolation(task)) {
3559 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3560 return;
3561 }
3562 mLockTaskModeTask = task;
Craig Mautner299f9602015-01-26 09:47:33 -08003563 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003564 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003565
3566 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003567 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3568 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003569 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003570 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003571 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003572 }
3573
3574 boolean isLockTaskModeViolation(TaskRecord task) {
3575 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3576 }
3577
3578 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3579 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003580 final Message lockTaskMsg = Message.obtain();
3581 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3582 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003583 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003584 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003585 }
3586 }
3587
3588 boolean isInLockTaskMode() {
3589 return mLockTaskModeTask != null;
3590 }
3591
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003592 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003593
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003594 public ActivityStackSupervisorHandler(Looper looper) {
3595 super(looper);
3596 }
3597
Craig Mautnerf3333272013-04-22 10:55:53 -07003598 void activityIdleInternal(ActivityRecord r) {
3599 synchronized (mService) {
3600 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3601 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003602 }
3603
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003604 @Override
3605 public void handleMessage(Message msg) {
3606 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003607 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003608 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003609 if (mService.mDidDexOpt) {
3610 mService.mDidDexOpt = false;
3611 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3612 nmsg.obj = msg.obj;
3613 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3614 return;
3615 }
3616 // We don't at this point know if the activity is fullscreen,
3617 // so we need to be conservative and assume it isn't.
3618 activityIdleInternal((ActivityRecord)msg.obj);
3619 } break;
3620 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003621 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003622 activityIdleInternal((ActivityRecord)msg.obj);
3623 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003624 case RESUME_TOP_ACTIVITY_MSG: {
3625 synchronized (mService) {
3626 resumeTopActivitiesLocked();
3627 }
3628 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003629 case SLEEP_TIMEOUT_MSG: {
3630 synchronized (mService) {
3631 if (mService.isSleepingOrShuttingDown()) {
3632 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3633 mSleepTimeout = true;
3634 checkReadyForSleepLocked();
3635 }
3636 }
3637 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003638 case LAUNCH_TIMEOUT_MSG: {
3639 if (mService.mDidDexOpt) {
3640 mService.mDidDexOpt = false;
3641 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3642 return;
3643 }
3644 synchronized (mService) {
3645 if (mLaunchingActivity.isHeld()) {
3646 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3647 if (VALIDATE_WAKE_LOCK_CALLER
3648 && Binder.getCallingUid() != Process.myUid()) {
3649 throw new IllegalStateException("Calling must be system uid");
3650 }
3651 mLaunchingActivity.release();
3652 }
3653 }
3654 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003655 case HANDLE_DISPLAY_ADDED: {
3656 handleDisplayAddedLocked(msg.arg1);
3657 } break;
3658 case HANDLE_DISPLAY_CHANGED: {
3659 handleDisplayChangedLocked(msg.arg1);
3660 } break;
3661 case HANDLE_DISPLAY_REMOVED: {
3662 handleDisplayRemovedLocked(msg.arg1);
3663 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003664 case CONTAINER_CALLBACK_VISIBILITY: {
3665 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003666 final IActivityContainerCallback callback = container.mCallback;
3667 if (callback != null) {
3668 try {
3669 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3670 } catch (RemoteException e) {
3671 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003672 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003673 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003674 case LOCK_TASK_START_MSG: {
3675 // When lock task starts, we disable the status bars.
3676 try {
Jason Monk62515be2014-05-21 16:06:19 -04003677 if (mLockTaskNotify == null) {
3678 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003679 }
Jason Monk62515be2014-05-21 16:06:19 -04003680 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003681 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003682 if (getStatusBarService() != null) {
3683 int flags =
3684 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003685 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003686 flags ^= StatusBarManager.DISABLE_HOME
3687 | StatusBarManager.DISABLE_RECENT;
3688 }
3689 getStatusBarService().disable(flags, mToken,
3690 mService.mContext.getPackageName());
3691 }
3692 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003693 if (getDevicePolicyManager() != null) {
3694 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003695 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003696 }
justinzhang5286d3f2014-05-12 17:06:01 -04003697 } catch (RemoteException ex) {
3698 throw new RuntimeException(ex);
3699 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003700 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003701 case LOCK_TASK_END_MSG: {
3702 // When lock task ends, we enable the status bars.
3703 try {
Jason Monk62515be2014-05-21 16:06:19 -04003704 if (getStatusBarService() != null) {
3705 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3706 mService.mContext.getPackageName());
3707 }
3708 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003709 if (getDevicePolicyManager() != null) {
3710 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3711 msg.arg1);
3712 }
Jason Monk62515be2014-05-21 16:06:19 -04003713 if (mLockTaskNotify == null) {
3714 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3715 }
3716 mLockTaskNotify.show(false);
3717 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003718 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003719 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003720 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003721 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003722 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003723 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003724 new LockPatternUtils(mService.mContext)
3725 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003726 }
3727 } catch (SettingNotFoundException e) {
3728 // No setting, don't lock.
3729 }
justinzhang5286d3f2014-05-12 17:06:01 -04003730 } catch (RemoteException ex) {
3731 throw new RuntimeException(ex);
3732 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003733 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003734 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3735 final ActivityContainer container = (ActivityContainer) msg.obj;
3736 final IActivityContainerCallback callback = container.mCallback;
3737 if (callback != null) {
3738 try {
3739 callback.onAllActivitiesComplete(container.asBinder());
3740 } catch (RemoteException e) {
3741 }
3742 }
3743 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003744 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3745 synchronized (mService) {
3746 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3747 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003748 final ActivityContainer container = (ActivityContainer) msg.obj;
3749 container.mStack.finishAllActivitiesLocked(true);
3750 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003751 }
3752 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003753 case LAUNCH_TASK_BEHIND_COMPLETE: {
3754 synchronized (mService) {
3755 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3756 if (r != null) {
3757 handleLaunchTaskBehindCompleteLocked(r);
3758 }
3759 }
3760 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003761 }
3762 }
3763 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003764
Ying Wangb081a592014-04-22 15:20:16 -07003765 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003766 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003767 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003768 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003769 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003770 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003771 ActivityRecord mParentActivity = null;
3772 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003773
Craig Mautnere3a00d72014-04-16 08:31:19 -07003774 boolean mVisible = true;
3775
Craig Mautner4a1cb222013-12-04 16:14:06 -08003776 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003777 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003778
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003779 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3780 final static int CONTAINER_STATE_NO_SURFACE = 1;
3781 final static int CONTAINER_STATE_FINISHING = 2;
3782 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3783
3784 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003785 synchronized (mService) {
3786 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003787 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003788 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003789 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003791 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003792
Craig Mautnere0a38842013-12-16 16:14:02 -08003793 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003794 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3795 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003796 mActivityDisplay = activityDisplay;
3797 mStack.mDisplayId = activityDisplay.mDisplayId;
3798 mStack.mStacks = activityDisplay.mStacks;
3799
3800 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003801 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003802 }
3803
3804 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003805 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003807 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3808 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003809 return;
3810 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003811 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003812 }
3813 }
3814
3815 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003816 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003817 synchronized (mService) {
3818 if (mActivityDisplay != null) {
3819 return mActivityDisplay.mDisplayId;
3820 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003821 }
3822 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003823 }
3824
Jeff Brownca9bc702014-02-11 14:32:56 -08003825 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003826 public int getStackId() {
3827 synchronized (mService) {
3828 return mStackId;
3829 }
3830 }
3831
3832 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003833 public boolean injectEvent(InputEvent event) {
3834 final long origId = Binder.clearCallingIdentity();
3835 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003836 synchronized (mService) {
3837 if (mActivityDisplay != null) {
3838 return mInputManagerInternal.injectInputEvent(event,
3839 mActivityDisplay.mDisplayId,
3840 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3841 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003842 }
3843 return false;
3844 } finally {
3845 Binder.restoreCallingIdentity(origId);
3846 }
3847 }
3848
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003849 @Override
3850 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003851 synchronized (mService) {
3852 if (mContainerState == CONTAINER_STATE_FINISHING) {
3853 return;
3854 }
3855 mContainerState = CONTAINER_STATE_FINISHING;
3856
3857 final Message msg =
3858 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003859 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003860
3861 long origId = Binder.clearCallingIdentity();
3862 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003863 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003864 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003865 } finally {
3866 Binder.restoreCallingIdentity(origId);
3867 }
3868 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003869 }
3870
Craig Mautner60257702014-09-17 15:02:33 -07003871 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003872 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3873 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003874 if (mActivityDisplay != null) {
3875 mActivityDisplay.detachActivitiesLocked(mStack);
3876 mActivityDisplay = null;
3877 mStack.mDisplayId = -1;
3878 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003879 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003880 }
3881 }
3882
3883 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003884 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003885 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003886 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003887 Binder.getCallingUid(), mCurrentUser, false,
3888 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003889 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003890 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003891 String mimeType = intent.getType();
3892 if (mimeType == null && intent.getData() != null
3893 && "content".equals(intent.getData().getScheme())) {
3894 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3895 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003896 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0,
3897 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003898 }
3899
3900 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003901 public final int startActivityIntentSender(IIntentSender intentSender) {
3902 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3903
3904 if (!(intentSender instanceof PendingIntentRecord)) {
3905 throw new IllegalArgumentException("Bad PendingIntent object");
3906 }
3907
3908 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003909 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003910 }
3911
Craig Mautner247ab652014-04-25 10:09:00 -07003912 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3913 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003914 Binder.getCallingUid(), mCurrentUser, false,
3915 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003916 if (resolvedType == null) {
3917 resolvedType = intent.getType();
3918 if (resolvedType == null && intent.getData() != null
3919 && "content".equals(intent.getData().getScheme())) {
3920 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3921 }
3922 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003923 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003924 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003925 throw new SecurityException(
3926 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3927 }
3928 }
3929
3930 /** Throw a SecurityException if allowEmbedded is not true */
3931 @Override
3932 public final void checkEmbeddedAllowed(Intent intent) {
3933 checkEmbeddedAllowedInner(intent, null);
3934 }
3935
3936 /** Throw a SecurityException if allowEmbedded is not true */
3937 @Override
3938 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3939 if (!(intentSender instanceof PendingIntentRecord)) {
3940 throw new IllegalArgumentException("Bad PendingIntent object");
3941 }
3942 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3943 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3944 pendingIntent.key.requestResolvedType);
3945 }
3946
Craig Mautnerdf88d732014-01-27 09:21:32 -08003947 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003948 public IBinder asBinder() {
3949 return this;
3950 }
3951
Craig Mautner4504de52013-12-20 09:06:56 -08003952 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003953 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003954 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003955 }
3956
Craig Mautner4a1cb222013-12-04 16:14:06 -08003957 ActivityStackSupervisor getOuter() {
3958 return ActivityStackSupervisor.this;
3959 }
3960
Craig Mautnerd163e752014-06-13 17:18:47 -07003961 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003962 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003963 }
3964
3965 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003966 synchronized (mService) {
3967 if (mActivityDisplay != null) {
3968 mActivityDisplay.getBounds(outBounds);
3969 } else {
3970 outBounds.set(0, 0);
3971 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003972 }
3973 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003974
Craig Mautner6985bad2014-04-21 15:22:06 -07003975 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003976 void setVisible(boolean visible) {
3977 if (mVisible != visible) {
3978 mVisible = visible;
3979 if (mCallback != null) {
3980 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3981 0 /* unused */, this).sendToTarget();
3982 }
3983 }
3984 }
3985
Craig Mautner6985bad2014-04-21 15:22:06 -07003986 void setDrawn() {
3987 }
3988
Craig Mautner1b4bf852014-05-26 15:06:32 -07003989 // You can always start a new task on a regular ActivityStack.
3990 boolean isEligibleForNewTasks() {
3991 return true;
3992 }
3993
Craig Mautnerd163e752014-06-13 17:18:47 -07003994 void onTaskListEmptyLocked() {
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -08003995 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3996 detachLocked();
3997 deleteActivityContainer(this);
3998 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003999 }
4000
Craig Mautner34b73df2014-01-12 21:11:08 -08004001 @Override
4002 public String toString() {
4003 return mIdString + (mActivityDisplay == null ? "N" : "A");
4004 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004005 }
4006
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004007 private class VirtualActivityContainer extends ActivityContainer {
4008 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004009 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004010
4011 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4012 super(getNextStackId());
4013 mParentActivity = parent;
4014 mCallback = callback;
4015 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004016 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004017 }
4018
4019 @Override
4020 public void setSurface(Surface surface, int width, int height, int density) {
4021 super.setSurface(surface, width, height, density);
4022
4023 synchronized (mService) {
4024 final long origId = Binder.clearCallingIdentity();
4025 try {
4026 setSurfaceLocked(surface, width, height, density);
4027 } finally {
4028 Binder.restoreCallingIdentity(origId);
4029 }
4030 }
4031 }
4032
4033 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4034 if (mContainerState == CONTAINER_STATE_FINISHING) {
4035 return;
4036 }
4037 VirtualActivityDisplay virtualActivityDisplay =
4038 (VirtualActivityDisplay) mActivityDisplay;
4039 if (virtualActivityDisplay == null) {
4040 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004041 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004042 mActivityDisplay = virtualActivityDisplay;
4043 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4044 attachToDisplayLocked(virtualActivityDisplay);
4045 }
4046
4047 if (mSurface != null) {
4048 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004049 }
4050
Craig Mautner6985bad2014-04-21 15:22:06 -07004051 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004052 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004053 mStack.resumeTopActivityLocked(null);
4054 } else {
4055 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004056 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004057 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004058 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004059 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004060 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004061
Craig Mautnerd163e752014-06-13 17:18:47 -07004062 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004063
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004064 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
4065 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004066 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004067
Craig Mautner6985bad2014-04-21 15:22:06 -07004068 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004069 boolean isAttachedLocked() {
4070 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004071 }
4072
4073 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004074 void setDrawn() {
4075 synchronized (mService) {
4076 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004077 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004078 }
4079 }
4080
Craig Mautner1b4bf852014-05-26 15:06:32 -07004081 // Never start a new task on an ActivityView if it isn't explicitly specified.
4082 @Override
4083 boolean isEligibleForNewTasks() {
4084 return false;
4085 }
4086
Craig Mautnerd163e752014-06-13 17:18:47 -07004087 private void setSurfaceIfReadyLocked() {
4088 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004089 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4090 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4091 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4092 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4093 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004094 }
4095 }
4096
Craig Mautner4a1cb222013-12-04 16:14:06 -08004097 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4098 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004099 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004101 int mDisplayId;
4102 Display mDisplay;
4103 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004104
Craig Mautner4a1cb222013-12-04 16:14:06 -08004105 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4106 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004107 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004108
Jose Lima4b6c6692014-08-12 17:41:12 -07004109 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004110
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004111 ActivityDisplay() {
4112 }
Craig Mautner4504de52013-12-20 09:06:56 -08004113
Craig Mautner1a70a162014-09-13 12:09:31 -07004114 // After instantiation, check that mDisplay is not null before using this. The alternative
4115 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004116 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004117 final Display display = mDisplayManager.getDisplay(displayId);
4118 if (display == null) {
4119 return;
4120 }
4121 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004122 }
4123
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004124 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004125 mDisplay = display;
4126 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004127 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004128 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004129
4130 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004131 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4132 + mDisplayId);
4133 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004134 }
4135
Craig Mautnere0a38842013-12-16 16:14:02 -08004136 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004137 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004138 + " from displayId=" + mDisplayId);
4139 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004140 }
4141
4142 void getBounds(Point bounds) {
4143 mDisplay.getDisplayInfo(mDisplayInfo);
4144 bounds.x = mDisplayInfo.appWidth;
4145 bounds.y = mDisplayInfo.appHeight;
4146 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004147
Jose Lima4b6c6692014-08-12 17:41:12 -07004148 void setVisibleBehindActivity(ActivityRecord r) {
4149 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004150 }
4151
Jose Lima4b6c6692014-08-12 17:41:12 -07004152 boolean hasVisibleBehindActivity() {
4153 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004154 }
4155
Craig Mautner34b73df2014-01-12 21:11:08 -08004156 @Override
4157 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004158 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4159 }
4160 }
4161
4162 class VirtualActivityDisplay extends ActivityDisplay {
4163 VirtualDisplay mVirtualDisplay;
4164
Craig Mautner6985bad2014-04-21 15:22:06 -07004165 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004166 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004167 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4168 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4169 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4170 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004171
4172 init(mVirtualDisplay.getDisplay());
4173
4174 mWindowManager.handleDisplayAdded(mDisplayId);
4175 }
4176
4177 void setSurface(Surface surface) {
4178 if (mVirtualDisplay != null) {
4179 mVirtualDisplay.setSurface(surface);
4180 }
4181 }
4182
4183 @Override
4184 void detachActivitiesLocked(ActivityStack stack) {
4185 super.detachActivitiesLocked(stack);
4186 if (mVirtualDisplay != null) {
4187 mVirtualDisplay.release();
4188 mVirtualDisplay = null;
4189 }
4190 }
4191
4192 @Override
4193 public String toString() {
4194 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004195 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004196 }
Jose Lima58e66d62014-05-27 20:00:27 -07004197
4198 private boolean isLeanbackOnlyDevice() {
4199 boolean onLeanbackOnly = false;
4200 try {
4201 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4202 PackageManager.FEATURE_LEANBACK_ONLY);
4203 } catch (RemoteException e) {
4204 // noop
4205 }
4206
4207 return onLeanbackOnly;
4208 }
Craig Mautner27084302013-03-25 08:05:25 -07004209}