blob: cbbb11a8dfb34ffc08ed7e65b6f03d5c0e29ebe9 [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;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080023import static com.android.server.am.ActivityManagerDebugConfig.*;
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 Mautner84984fa2014-06-19 11:19:20 -070034import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
35import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070037
Craig Mautner2420ead2013-04-01 17:13:20 -070038import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080040import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityOptions;
42import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import android.app.IActivityContainer;
44import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070048import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070049import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070051import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040052import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040053import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070055import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070056import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070057import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.pm.ApplicationInfo;
61import android.content.pm.PackageManager;
62import android.content.pm.ResolveInfo;
63import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080064import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080065import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080066import android.hardware.display.DisplayManager;
67import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080068import android.hardware.display.DisplayManagerGlobal;
69import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080070import android.hardware.input.InputManager;
71import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080072import android.net.Uri;
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 {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800118 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
119
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800120 static final boolean DEBUG = DEBUG_ALL || false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700121 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
122 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700123 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700124 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700125 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700126 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
127 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner30bd3bb2014-11-13 00:55:53 +0000128 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700129 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700130
Craig Mautner2219a1b2013-03-25 09:44:30 -0700131 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700132
Craig Mautnerf3333272013-04-22 10:55:53 -0700133 /** How long we wait until giving up on the last activity telling us it is idle. */
134 static final int IDLE_TIMEOUT = 10*1000;
135
Craig Mautner0eea92c2013-05-16 13:35:39 -0700136 /** How long we can hold the sleep wake lock before giving up. */
137 static final int SLEEP_TIMEOUT = 5*1000;
138
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700139 // How long we can hold the launch wake lock before giving up.
140 static final int LAUNCH_TIMEOUT = 10*1000;
141
Craig Mautner05d29032013-05-03 13:40:13 -0700142 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
143 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
144 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700145 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700146 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800147 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
148 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
149 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700150 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400151 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
152 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700153 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700154 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700155 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800156
Craig Mautner4504de52013-12-20 09:06:56 -0800157 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700158
Jason Monk62515be2014-05-21 16:06:19 -0400159 private static final String LOCK_TASK_TAG = "Lock-to-App";
160
justinzhang5286d3f2014-05-12 17:06:01 -0400161 /** Status Bar Service **/
162 private IBinder mToken = new Binder();
163 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400164 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400165
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700166 // For debugging to make sure the caller when acquiring/releasing our
167 // wake lock is the system process.
168 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700169
Craig Mautner27084302013-03-25 08:05:25 -0700170 final ActivityManagerService mService;
171
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800172 private final RecentTasks mRecentTasks;
173
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700174 final ActivityStackSupervisorHandler mHandler;
175
176 /** Short cut */
177 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800178 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700179
Craig Mautner8d341ef2013-03-26 09:03:27 -0700180 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700181 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700182
183 /** Task identifier that activities are currently being started in. Incremented each time a
184 * new task is created. */
185 private int mCurTaskId = 0;
186
Craig Mautner2420ead2013-04-01 17:13:20 -0700187 /** The current user */
188 private int mCurrentUser;
189
Craig Mautnere0a38842013-12-16 16:14:02 -0800190 /** The stack containing the launcher app. Assumed to always be attached to
191 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700192 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700193
Craig Mautnere0a38842013-12-16 16:14:02 -0800194 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700195 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700196
Craig Mautner4a1cb222013-12-04 16:14:06 -0800197 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
198 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800199 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800200 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700201
202 /** List of activities that are waiting for a new activity to become visible before completing
203 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800204 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700205
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700206 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800207 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700208
209 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800210 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700211
Craig Mautnerde4ef022013-04-07 19:01:33 -0700212 /** List of activities that are ready to be stopped, but waiting for the next activity to
213 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800214 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700215
Craig Mautnerf3333272013-04-22 10:55:53 -0700216 /** List of activities that are ready to be finished, but waiting for the previous activity to
217 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800218 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700219
Craig Mautner0eea92c2013-05-16 13:35:39 -0700220 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800221 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700222
Craig Mautnerf3333272013-04-22 10:55:53 -0700223 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800224 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700225
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700226 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800227 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700228
Craig Mautnerde4ef022013-04-07 19:01:33 -0700229 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
230 * is being brought in front of us. */
231 boolean mUserLeaving = false;
232
Craig Mautner0eea92c2013-05-16 13:35:39 -0700233 /** Set when we have taken too long waiting to go to sleep. */
234 boolean mSleepTimeout = false;
235
Jose Lima58e66d62014-05-27 20:00:27 -0700236 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
237 * setWindowManager is called. **/
238 private boolean mLeanbackOnlyDevice;
239
Craig Mautner0eea92c2013-05-16 13:35:39 -0700240 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700241 * We don't want to allow the device to go to sleep while in the process
242 * of launching an activity. This is primarily to allow alarm intent
243 * receivers to launch an activity and get that to run before the device
244 * goes back to sleep.
245 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700246 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700247
248 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700249 * Set when the system is going to sleep, until we have
250 * successfully paused the current activity and released our wake lock.
251 * At that point the system is allowed to actually sleep.
252 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700253 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700254
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700255 /** Stack id of the front stack when user switched, indexed by userId. */
256 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700257
Craig Mautner4504de52013-12-20 09:06:56 -0800258 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800259 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700260 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800261
262 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700263 private final SparseArray<ActivityDisplay> mActivityDisplays =
264 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800265
Jeff Brownca9bc702014-02-11 14:32:56 -0800266 InputManagerInternal mInputManagerInternal;
267
Craig Mautneraea74a52014-03-08 14:23:10 -0800268 /** If non-null then the task specified remains in front and no other tasks may be started
269 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400270 TaskRecord mLockTaskModeTask;
Benjamin Franz43261142015-02-11 15:59:44 +0000271 /** Store the current lock task mode. Possible values:
272 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActicityManager#LOCK_TASK_MODE_LOCKED},
273 * {@link ActicityManager#LOCK_TASK_MODE_PINNED}
274 */
275 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400276 /**
277 * Notifies the user when entering/exiting lock-task.
278 */
279 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800280
Craig Mautneree36c772014-07-16 14:56:05 -0700281 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
282 = new ArrayList<PendingActivityLaunch>();
283
Craig Mautner42d04db2014-11-06 12:13:23 -0800284 /** Used to keep resumeTopActivityLocked() from being entered recursively */
285 boolean inResumeTopActivity;
286
Craig Mautneree36c772014-07-16 14:56:05 -0700287 /**
288 * Description of a request to start a new activity, which has been held
289 * due to app switches being disabled.
290 */
291 static class PendingActivityLaunch {
292 final ActivityRecord r;
293 final ActivityRecord sourceRecord;
294 final int startFlags;
295 final ActivityStack stack;
296
297 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
298 int _startFlags, ActivityStack _stack) {
299 r = _r;
300 sourceRecord = _sourceRecord;
301 startFlags = _startFlags;
302 stack = _stack;
303 }
304 }
305
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800306 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700307 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800308 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700309 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
310 }
311
312 /**
313 * At the time when the constructor runs, the power manager has not yet been
314 * initialized. So we initialize our wakelocks afterwards.
315 */
316 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700318 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700319 mLaunchingActivity =
320 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
321 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700322 }
323
justinzhang5286d3f2014-05-12 17:06:01 -0400324 // This function returns a IStatusBarService. The value is from ServiceManager.
325 // getService and is cached.
326 private IStatusBarService getStatusBarService() {
327 synchronized (mService) {
328 if (mStatusBarService == null) {
329 mStatusBarService = IStatusBarService.Stub.asInterface(
330 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
331 if (mStatusBarService == null) {
332 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
333 }
334 }
335 return mStatusBarService;
336 }
337 }
338
Jason Monk35c62a42014-06-17 10:24:47 -0400339 private IDevicePolicyManager getDevicePolicyManager() {
340 synchronized (mService) {
341 if (mDevicePolicyManager == null) {
342 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
343 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
344 if (mDevicePolicyManager == null) {
345 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
346 }
347 }
348 return mDevicePolicyManager;
349 }
350 }
351
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700352 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800353 synchronized (mService) {
354 mWindowManager = wm;
355
356 mDisplayManager =
357 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
358 mDisplayManager.registerDisplayListener(this, null);
359
360 Display[] displays = mDisplayManager.getDisplays();
361 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
362 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800363 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700364 if (activityDisplay.mDisplay == null) {
365 throw new IllegalStateException("Default Display does not exist");
366 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800367 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800368 }
369
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700370 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800371 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800372
373 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700374
375 // Initialize this here, now that we can get a valid reference to PackageManager.
376 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800377 }
Craig Mautner27084302013-03-25 08:05:25 -0700378 }
379
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200380 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700381 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200382 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700383 }
384
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700385 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800386 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700387 }
388
Craig Mautnerde4ef022013-04-07 19:01:33 -0700389 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800390 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700391 }
392
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800393 /** Top of all visible stacks is/should always be equal to the focused stack.
394 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
395 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700396 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700397 if (stack == null) {
398 return false;
399 }
400
Craig Mautnerdf88d732014-01-27 09:21:32 -0800401 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
402 if (parent != null) {
403 stack = parent.task.stack;
404 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800405 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700406 }
407
Craig Mautner299f9602015-01-26 09:47:33 -0800408 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800409 moveHomeStack(toFront, reason, null);
410 }
411
412 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800413 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800414 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800415 if (topNdx <= 0) {
416 return;
417 }
418 ActivityStack topStack = stacks.get(topNdx);
419 final boolean homeInFront = topStack == mHomeStack;
420 if (homeInFront != toFront) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800421 stacks.remove(mHomeStack);
422 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800423 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
424 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700425 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800426
427 if (lastFocusedStack != null) {
428 mLastFocusedStack = lastFocusedStack;
429 }
430 mFocusedStack = stacks.get(topNdx);
431
Craig Mautnerde313752015-01-22 14:28:03 -0800432 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
433 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800434 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800435
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800436 if (mService.mBooting || !mService.mBooted) {
437 final ActivityRecord r = topRunningActivityLocked();
438 if (r != null && r.idle) {
439 checkFinishBootingLocked();
440 }
441 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700442 }
443
Craig Mautner299f9602015-01-26 09:47:33 -0800444 void moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700445 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
446 mWindowManager.showRecentApps();
447 return;
448 }
Craig Mautner299f9602015-01-26 09:47:33 -0800449 moveHomeStack(true, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700450 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700451 }
452
Craig Mautner299f9602015-01-26 09:47:33 -0800453 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700454 if (!mService.mBooting && !mService.mBooted) {
455 // Not ready yet!
456 return false;
457 }
458
Craig Mautner84984fa2014-06-19 11:19:20 -0700459 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
460 mWindowManager.showRecentApps();
461 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700462 }
Craig Mautner299f9602015-01-26 09:47:33 -0800463 moveHomeStackTaskToTop(homeStackTaskType, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700464 if (prev != null) {
465 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
466 }
467
Craig Mautnera8a90e02013-06-28 15:24:50 -0700468 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700469 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000470 if (r != null && r.isHomeActivity()) {
Craig Mautner299f9602015-01-26 09:47:33 -0800471 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700472 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700473 }
Craig Mautner299f9602015-01-26 09:47:33 -0800474 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700475 }
476
Craig Mautner8d341ef2013-03-26 09:03:27 -0700477 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700478 return anyTaskForIdLocked(id, true);
479 }
480
481 /**
482 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
483 * @param id Id of the task we would like returned.
484 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
485 * restore the task from recents to the active list.
486 */
487 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800488 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800489 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800490 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800491 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
492 ActivityStack stack = stacks.get(stackNdx);
493 TaskRecord task = stack.taskForIdLocked(id);
494 if (task != null) {
495 return task;
496 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700497 }
498 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800499
500 // Don't give up! Look in recents.
501 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800502 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800503 if (task == null) {
504 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
505 return null;
506 }
507
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700508 if (!restoreFromRecents) {
509 return task;
510 }
511
Wale Ogunwale7de05352014-12-12 15:21:33 -0800512 if (!restoreRecentTaskLocked(task)) {
513 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
514 return null;
515 }
516 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
517 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700518 }
519
Craig Mautner6170f732013-04-02 13:05:23 -0700520 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800521 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800523 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800524 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
525 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
526 if (r != null) {
527 return r;
528 }
Craig Mautner6170f732013-04-02 13:05:23 -0700529 }
530 }
531 return null;
532 }
533
Craig Mautneref73ee12014-04-23 11:45:37 -0700534 void setNextTaskId(int taskId) {
535 if (taskId > mCurTaskId) {
536 mCurTaskId = taskId;
537 }
538 }
539
Craig Mautner8d341ef2013-03-26 09:03:27 -0700540 int getNextTaskId() {
541 do {
542 mCurTaskId++;
543 if (mCurTaskId <= 0) {
544 mCurTaskId = 1;
545 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700546 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700547 return mCurTaskId;
548 }
549
Craig Mautnerde4ef022013-04-07 19:01:33 -0700550 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800551 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700552 if (stack == null) {
553 return null;
554 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700555 ActivityRecord resumedActivity = stack.mResumedActivity;
556 if (resumedActivity == null || resumedActivity.app == null) {
557 resumedActivity = stack.mPausingActivity;
558 if (resumedActivity == null || resumedActivity.app == null) {
559 resumedActivity = stack.topRunningActivityLocked(null);
560 }
561 }
562 return resumedActivity;
563 }
564
Dianne Hackbornff072722014-09-24 10:56:28 -0700565 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700566 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800567 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800568 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
569 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800570 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
571 final ActivityStack stack = stacks.get(stackNdx);
572 if (!isFrontStack(stack)) {
573 continue;
574 }
575 ActivityRecord hr = stack.topRunningActivityLocked(null);
576 if (hr != null) {
577 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
578 && processName.equals(hr.processName)) {
579 try {
George Mount2c92c972014-03-20 09:38:23 -0700580 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800581 didSomething = true;
582 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700583 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800584 Slog.w(TAG, "Exception in new application when starting activity "
585 + hr.intent.getComponent().flattenToShortString(), e);
586 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700587 }
Craig Mautner20e72272013-04-01 13:45:53 -0700588 }
Craig Mautner20e72272013-04-01 13:45:53 -0700589 }
590 }
591 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700592 if (!didSomething) {
593 ensureActivitiesVisibleLocked(null, 0);
594 }
Craig Mautner20e72272013-04-01 13:45:53 -0700595 return didSomething;
596 }
597
598 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800599 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
600 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800601 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
602 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800603 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800604 continue;
605 }
606 final ActivityRecord resumedActivity = stack.mResumedActivity;
607 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800608 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
609 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610 return false;
611 }
Craig Mautner20e72272013-04-01 13:45:53 -0700612 }
613 }
614 return true;
615 }
616
Craig Mautnerde4ef022013-04-07 19:01:33 -0700617 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800618 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
619 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800620 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
621 final ActivityStack stack = stacks.get(stackNdx);
622 if (isFrontStack(stack)) {
623 final ActivityRecord r = stack.mResumedActivity;
624 if (r != null && r.state != ActivityState.RESUMED) {
625 return false;
626 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700627 }
628 }
629 }
630 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 if (DEBUG_STACK) Slog.d(TAG,
632 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
633 mLastFocusedStack + " to=" + mFocusedStack);
634 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700635 return true;
636 }
637
638 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
640 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
642 final ActivityStack stack = stacks.get(stackNdx);
643 final ActivityRecord r = stack.mResumedActivity;
Craig Mautner8c14c152015-01-15 17:32:07 -0800644 if (r != null && (!r.nowVisible || mWaitingVisibleActivities.contains(r))) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800645 return false;
646 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700647 }
648 }
649 return true;
650 }
651
Craig Mautner2acc3892013-09-23 10:28:14 -0700652 /**
653 * Pause all activities in either all of the stacks or just the back stacks.
654 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700655 * @return true if any activity was paused as a result of this call.
656 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700657 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700658 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800659 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
660 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800661 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
662 final ActivityStack stack = stacks.get(stackNdx);
663 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
664 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
665 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700666 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
667 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700669 }
670 }
671 return someActivityPaused;
672 }
673
Craig Mautnerde4ef022013-04-07 19:01:33 -0700674 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700675 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800676 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
677 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
679 final ActivityStack stack = stacks.get(stackNdx);
680 final ActivityRecord r = stack.mPausingActivity;
681 if (r != null && r.state != ActivityState.PAUSED
682 && r.state != ActivityState.STOPPED
683 && r.state != ActivityState.STOPPING) {
684 if (DEBUG_STATES) {
685 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
686 pausing = false;
687 } else {
688 return false;
689 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700690 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700691 }
692 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700693 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700694 }
695
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700696 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
697 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500698 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800699 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
700 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
701 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
702 final ActivityStack stack = stacks.get(stackNdx);
703 if (stack.mResumedActivity != null &&
704 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700705 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800706 }
707 }
708 }
709 }
710
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700711 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700712 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700713 }
714
715 void sendWaitingVisibleReportLocked(ActivityRecord r) {
716 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700717 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700718 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700719 if (w.who == null) {
720 changed = true;
721 w.timeout = false;
722 if (r != null) {
723 w.who = new ComponentName(r.info.packageName, r.info.name);
724 }
725 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
726 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700727 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700728 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700729 if (changed) {
730 mService.notifyAll();
731 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700732 }
733
734 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
735 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700736 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700737 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700738 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700739 if (w.who == null) {
740 changed = true;
741 w.timeout = timeout;
742 if (r != null) {
743 w.who = new ComponentName(r.info.packageName, r.info.name);
744 }
745 w.thisTime = thisTime;
746 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700747 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700748 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700749 if (changed) {
750 mService.notifyAll();
751 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700752 }
753
Craig Mautner29219d92013-04-16 20:19:12 -0700754 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800755 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700756 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
757 if (r != null) {
758 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700759 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700760
Craig Mautner4a1cb222013-12-04 16:14:06 -0800761 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800762 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800763 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
764 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700765 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700766 r = stack.topRunningActivityLocked(null);
767 if (r != null) {
768 return r;
769 }
770 }
771 }
772 return null;
773 }
774
Dianne Hackborn09233282014-04-30 11:33:59 -0700775 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700776 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800777 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
778 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800779 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800780 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800781 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800782 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
783 final ActivityStack stack = stacks.get(stackNdx);
784 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
785 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700786 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700787 }
788 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700789
790 // The lists are already sorted from most recent to oldest. Just pull the most recent off
791 // each list and add it to list. Stop when all lists are empty or maxNum reached.
792 while (maxNum > 0) {
793 long mostRecentActiveTime = Long.MIN_VALUE;
794 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800795 final int numTaskLists = runningTaskLists.size();
796 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
797 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700798 if (!stackTaskList.isEmpty()) {
799 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
800 if (lastActiveTime > mostRecentActiveTime) {
801 mostRecentActiveTime = lastActiveTime;
802 selectedStackList = stackTaskList;
803 }
804 }
805 }
806 if (selectedStackList != null) {
807 list.add(selectedStackList.remove(0));
808 --maxNum;
809 } else {
810 break;
811 }
812 }
Craig Mautner20e72272013-04-01 13:45:53 -0700813 }
814
Craig Mautner23ac33b2013-04-01 16:26:35 -0700815 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700816 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700817 // Collect information about the target of the Intent.
818 ActivityInfo aInfo;
819 try {
820 ResolveInfo rInfo =
821 AppGlobals.getPackageManager().resolveIntent(
822 intent, resolvedType,
823 PackageManager.MATCH_DEFAULT_ONLY
824 | ActivityManagerService.STOCK_PM_FLAGS, userId);
825 aInfo = rInfo != null ? rInfo.activityInfo : null;
826 } catch (RemoteException e) {
827 aInfo = null;
828 }
829
830 if (aInfo != null) {
831 // Store the found target back into the intent, because now that
832 // we have it we never want to do this again. For example, if the
833 // user navigates back to this point in the history, we should
834 // always restart the exact same activity.
835 intent.setComponent(new ComponentName(
836 aInfo.applicationInfo.packageName, aInfo.name));
837
838 // Don't debug things in the system process
839 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
840 if (!aInfo.processName.equals("system")) {
841 mService.setDebugApp(aInfo.processName, true, false);
842 }
843 }
844
845 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
846 if (!aInfo.processName.equals("system")) {
847 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
848 }
849 }
850
Jeff Hao1b012d32014-08-20 10:35:34 -0700851 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700852 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700853 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700854 }
855 }
856 }
857 return aInfo;
858 }
859
Craig Mautner299f9602015-01-26 09:47:33 -0800860 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
861 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700862 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
863 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700864 }
865
Craig Mautner23ac33b2013-04-01 16:26:35 -0700866 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700867 String callingPackage, Intent intent, String resolvedType,
868 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700869 IBinder resultTo, String resultWho, int requestCode, int startFlags,
870 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700871 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700872 // Refuse possible leaked file descriptors
873 if (intent != null && intent.hasFileDescriptors()) {
874 throw new IllegalArgumentException("File descriptors passed in Intent");
875 }
876 boolean componentSpecified = intent.getComponent() != null;
877
878 // Don't modify the client's object!
879 intent = new Intent(intent);
880
881 // Collect information about the target of the Intent.
882 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700883 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700884
Craig Mautnere0a38842013-12-16 16:14:02 -0800885 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700886 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800887 if (container != null && container.mParentActivity != null &&
888 container.mParentActivity.state != ActivityState.RESUMED) {
889 // Cannot start a child activity if the parent is not resumed.
890 return ActivityManager.START_CANCELED;
891 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700892 final int realCallingPid = Binder.getCallingPid();
893 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700894 int callingPid;
895 if (callingUid >= 0) {
896 callingPid = -1;
897 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700898 callingPid = realCallingPid;
899 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700900 } else {
901 callingPid = callingUid = -1;
902 }
903
Craig Mautnere0a38842013-12-16 16:14:02 -0800904 final ActivityStack stack;
905 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800906 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800907 } else {
908 stack = container.mStack;
909 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800910 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700911 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700912 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700913
914 final long origId = Binder.clearCallingIdentity();
915
916 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800917 (aInfo.applicationInfo.privateFlags
918 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700919 // This may be a heavy-weight process! Check to see if we already
920 // have another, different heavy-weight process running.
921 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
922 if (mService.mHeavyWeightProcess != null &&
923 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
924 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700925 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926 if (caller != null) {
927 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
928 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700929 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700930 } else {
931 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700932 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700933 + intent.toString());
934 ActivityOptions.abort(options);
935 return ActivityManager.START_PERMISSION_DENIED;
936 }
937 }
938
939 IIntentSender target = mService.getIntentSenderLocked(
940 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700941 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700942 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
943 | PendingIntent.FLAG_ONE_SHOT, null);
944
945 Intent newIntent = new Intent();
946 if (requestCode >= 0) {
947 // Caller is requesting a result.
948 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
949 }
950 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
951 new IntentSender(target));
952 if (mService.mHeavyWeightProcess.activities.size() > 0) {
953 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
954 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
955 hist.packageName);
956 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
957 hist.task.taskId);
958 }
959 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
960 aInfo.packageName);
961 newIntent.setFlags(intent.getFlags());
962 newIntent.setClassName("android",
963 HeavyWeightSwitcherActivity.class.getName());
964 intent = newIntent;
965 resolvedType = null;
966 caller = null;
967 callingUid = Binder.getCallingUid();
968 callingPid = Binder.getCallingPid();
969 componentSpecified = true;
970 try {
971 ResolveInfo rInfo =
972 AppGlobals.getPackageManager().resolveIntent(
973 intent, null,
974 PackageManager.MATCH_DEFAULT_ONLY
975 | ActivityManagerService.STOCK_PM_FLAGS, userId);
976 aInfo = rInfo != null ? rInfo.activityInfo : null;
977 aInfo = mService.getActivityInfoForUser(aInfo, userId);
978 } catch (RemoteException e) {
979 aInfo = null;
980 }
981 }
982 }
983 }
984
Dianne Hackborn91097de2014-04-04 18:02:06 -0700985 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
986 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700987 requestCode, callingPid, callingUid, callingPackage,
988 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700989 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700990
Craig Mautner85c11a82014-07-17 12:38:18 -0700991 Binder.restoreCallingIdentity(origId);
992
Craig Mautnerde4ef022013-04-07 19:01:33 -0700993 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700994 // If the caller also wants to switch to a new configuration,
995 // do so now. This allows a clean switch, as we are waiting
996 // for the current activity to pause (so we will not destroy
997 // it), and have not yet started the next activity.
998 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
999 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001000 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001001 if (DEBUG_CONFIGURATION) Slog.v(TAG,
1002 "Updating to new configuration after starting activity.");
1003 mService.updateConfigurationLocked(config, null, false, false);
1004 }
1005
Craig Mautner23ac33b2013-04-01 16:26:35 -07001006 if (outResult != null) {
1007 outResult.result = res;
1008 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001009 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001010 do {
1011 try {
1012 mService.wait();
1013 } catch (InterruptedException e) {
1014 }
1015 } while (!outResult.timeout && outResult.who == null);
1016 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001017 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001018 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001019 outResult.timeout = false;
1020 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1021 outResult.totalTime = 0;
1022 outResult.thisTime = 0;
1023 } else {
1024 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001025 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001026 do {
1027 try {
1028 mService.wait();
1029 } catch (InterruptedException e) {
1030 }
1031 } while (!outResult.timeout && outResult.who == null);
1032 }
1033 }
1034 }
1035
1036 return res;
1037 }
1038 }
1039
1040 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1041 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1042 Bundle options, int userId) {
1043 if (intents == null) {
1044 throw new NullPointerException("intents is null");
1045 }
1046 if (resolvedTypes == null) {
1047 throw new NullPointerException("resolvedTypes is null");
1048 }
1049 if (intents.length != resolvedTypes.length) {
1050 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1051 }
1052
Craig Mautner23ac33b2013-04-01 16:26:35 -07001053
1054 int callingPid;
1055 if (callingUid >= 0) {
1056 callingPid = -1;
1057 } else if (caller == null) {
1058 callingPid = Binder.getCallingPid();
1059 callingUid = Binder.getCallingUid();
1060 } else {
1061 callingPid = callingUid = -1;
1062 }
1063 final long origId = Binder.clearCallingIdentity();
1064 try {
1065 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001066 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001067 for (int i=0; i<intents.length; i++) {
1068 Intent intent = intents[i];
1069 if (intent == null) {
1070 continue;
1071 }
1072
1073 // Refuse possible leaked file descriptors
1074 if (intent != null && intent.hasFileDescriptors()) {
1075 throw new IllegalArgumentException("File descriptors passed in Intent");
1076 }
1077
1078 boolean componentSpecified = intent.getComponent() != null;
1079
1080 // Don't modify the client's object!
1081 intent = new Intent(intent);
1082
1083 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001084 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001085 // TODO: New, check if this is correct
1086 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1087
1088 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001089 (aInfo.applicationInfo.privateFlags
1090 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001091 throw new IllegalArgumentException(
1092 "FLAG_CANT_SAVE_STATE not supported here");
1093 }
1094
1095 Bundle theseOptions;
1096 if (options != null && i == intents.length-1) {
1097 theseOptions = options;
1098 } else {
1099 theseOptions = null;
1100 }
Craig Mautner6170f732013-04-02 13:05:23 -07001101 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001102 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1103 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001104 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001105 if (res < 0) {
1106 return res;
1107 }
1108
1109 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1110 }
1111 }
1112 } finally {
1113 Binder.restoreCallingIdentity(origId);
1114 }
1115
1116 return ActivityManager.START_SUCCESS;
1117 }
1118
Craig Mautner2420ead2013-04-01 17:13:20 -07001119 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001120 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001121 throws RemoteException {
1122
1123 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001124 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001125 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001126
1127 // schedule launch ticks to collect information about slow apps.
1128 r.startLaunchTickingLocked();
1129
1130 // Have the window manager re-evaluate the orientation of
1131 // the screen based on the new activity order. Note that
1132 // as a result of this, it can call back into the activity
1133 // manager with a new orientation. We don't care about that,
1134 // because the activity is not currently running so we are
1135 // just restarting it anyway.
1136 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001137 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001138 mService.mConfiguration,
1139 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1140 mService.updateConfigurationLocked(config, r, false, false);
1141 }
1142
1143 r.app = app;
1144 app.waitingToKill = null;
1145 r.launchCount++;
1146 r.lastLaunchTime = SystemClock.uptimeMillis();
1147
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001148 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001149
1150 int idx = app.activities.indexOf(r);
1151 if (idx < 0) {
1152 app.activities.add(r);
1153 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001154 mService.updateLruProcessLocked(app, true, null);
1155 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001156
1157 final ActivityStack stack = r.task.stack;
1158 try {
1159 if (app.thread == null) {
1160 throw new RemoteException();
1161 }
1162 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001163 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001164 if (andResume) {
1165 results = r.results;
1166 newIntents = r.newIntents;
1167 }
1168 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1169 + " icicle=" + r.icicle
1170 + " with results=" + results + " newIntents=" + newIntents
1171 + " andResume=" + andResume);
1172 if (andResume) {
1173 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1174 r.userId, System.identityHashCode(r),
1175 r.task.taskId, r.shortComponentName);
1176 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001177 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001178 // Home process is the root process of the task.
1179 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001180 }
1181 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1182 r.sleeping = false;
1183 r.forceNewConfig = false;
1184 mService.showAskCompatModeDialogLocked(r);
1185 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1186 String profileFile = null;
1187 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001188 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1189 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1190 mService.mProfileProc = app;
1191 profileFile = mService.mProfileFile;
1192 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001193 }
1194 }
1195 app.hasShownUi = true;
1196 app.pendingUiClean = true;
1197 if (profileFd != null) {
1198 try {
1199 profileFd = profileFd.dup();
1200 } catch (IOException e) {
1201 if (profileFd != null) {
1202 try {
1203 profileFd.close();
1204 } catch (IOException o) {
1205 }
1206 profileFd = null;
1207 }
1208 }
1209 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001210
Jeff Hao1b012d32014-08-20 10:35:34 -07001211 ProfilerInfo profilerInfo = profileFile != null
1212 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1213 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001214 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001216 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001217 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1218 r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1219 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001220
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001221 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001222 // This may be a heavy-weight process! Note that the package
1223 // manager will ensure that only activity can run in the main
1224 // process of the .apk, which is the only thing that will be
1225 // considered heavy-weight.
1226 if (app.processName.equals(app.info.packageName)) {
1227 if (mService.mHeavyWeightProcess != null
1228 && mService.mHeavyWeightProcess != app) {
1229 Slog.w(TAG, "Starting new heavy weight process " + app
1230 + " when already running "
1231 + mService.mHeavyWeightProcess);
1232 }
1233 mService.mHeavyWeightProcess = app;
1234 Message msg = mService.mHandler.obtainMessage(
1235 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1236 msg.obj = r;
1237 mService.mHandler.sendMessage(msg);
1238 }
1239 }
1240
1241 } catch (RemoteException e) {
1242 if (r.launchFailed) {
1243 // This is the second time we failed -- finish activity
1244 // and give up.
1245 Slog.e(TAG, "Second failure launching "
1246 + r.intent.getComponent().flattenToShortString()
1247 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001248 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1250 "2nd-crash", false);
1251 return false;
1252 }
1253
1254 // This is the first time we failed -- restart process and
1255 // retry.
1256 app.activities.remove(r);
1257 throw e;
1258 }
1259
1260 r.launchFailed = false;
1261 if (stack.updateLRUListLocked(r)) {
1262 Slog.w(TAG, "Activity " + r
1263 + " being launched, but already in LRU list");
1264 }
1265
1266 if (andResume) {
1267 // As part of the process of launching, ActivityThread also performs
1268 // a resume.
1269 stack.minimalResumeActivityLocked(r);
1270 } else {
1271 // This activity is not starting in the resumed state... which
1272 // should look like we asked it to pause+stop (but remain visible),
1273 // and it has done so and reported back the current icicle and
1274 // other state.
1275 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1276 + " (starting in stopped state)");
1277 r.state = ActivityState.STOPPED;
1278 r.stopped = true;
1279 }
1280
1281 // Launch the new version setup screen if needed. We do this -after-
1282 // launching the initial activity (that is, home), so that it can have
1283 // a chance to initialize itself while in the background, making the
1284 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001285 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001286 mService.startSetupActivityLocked();
1287 }
1288
Dianne Hackborn465fa392014-09-14 14:21:18 -07001289 // Update any services we are bound to that might care about whether
1290 // their client may have activities.
1291 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1292
Craig Mautner2420ead2013-04-01 17:13:20 -07001293 return true;
1294 }
1295
Craig Mautnere79d42682013-04-01 19:01:53 -07001296 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001297 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001298 // Is this activity's application already running?
1299 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001300 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001301
1302 r.task.stack.setLaunchTime(r);
1303
1304 if (app != null && app.thread != null) {
1305 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001306 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1307 || !"android".equals(r.info.packageName)) {
1308 // Don't add this if it is a platform component that is marked
1309 // to run in multiple processes, because this is actually
1310 // part of the framework so doesn't make sense to track as a
1311 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001312 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1313 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001314 }
George Mount2c92c972014-03-20 09:38:23 -07001315 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001316 return;
1317 } catch (RemoteException e) {
1318 Slog.w(TAG, "Exception when starting activity "
1319 + r.intent.getComponent().flattenToShortString(), e);
1320 }
1321
1322 // If a dead object exception was thrown -- fall through to
1323 // restart the application.
1324 }
1325
1326 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001327 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001328 }
1329
Craig Mautner6170f732013-04-02 13:05:23 -07001330 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001331 Intent intent, String resolvedType, ActivityInfo aInfo,
1332 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1333 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001334 int callingPid, int callingUid, String callingPackage,
1335 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001336 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1337 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001338 int err = ActivityManager.START_SUCCESS;
1339
1340 ProcessRecord callerApp = null;
1341 if (caller != null) {
1342 callerApp = mService.getRecordForAppLocked(caller);
1343 if (callerApp != null) {
1344 callingPid = callerApp.pid;
1345 callingUid = callerApp.info.uid;
1346 } else {
1347 Slog.w(TAG, "Unable to find app for caller " + caller
1348 + " (pid=" + callingPid + ") when starting: "
1349 + intent.toString());
1350 err = ActivityManager.START_PERMISSION_DENIED;
1351 }
1352 }
1353
1354 if (err == ActivityManager.START_SUCCESS) {
1355 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1356 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001357 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001358 + " on display " + (container == null ? (mFocusedStack == null ?
1359 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1360 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1361 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001362 }
1363
1364 ActivityRecord sourceRecord = null;
1365 ActivityRecord resultRecord = null;
1366 if (resultTo != null) {
1367 sourceRecord = isInAnyStackLocked(resultTo);
1368 if (DEBUG_RESULTS) Slog.v(
1369 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1370 if (sourceRecord != null) {
1371 if (requestCode >= 0 && !sourceRecord.finishing) {
1372 resultRecord = sourceRecord;
1373 }
1374 }
1375 }
Craig Mautner6170f732013-04-02 13:05:23 -07001376
Dianne Hackborn91097de2014-04-04 18:02:06 -07001377 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001378
Dianne Hackborn95465202014-09-15 16:21:55 -07001379 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001380 // Transfer the result target from the source activity to the new
1381 // one being started, including any failures.
1382 if (requestCode >= 0) {
1383 ActivityOptions.abort(options);
1384 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1385 }
1386 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001387 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1388 resultRecord = null;
1389 }
Craig Mautner6170f732013-04-02 13:05:23 -07001390 resultWho = sourceRecord.resultWho;
1391 requestCode = sourceRecord.requestCode;
1392 sourceRecord.resultTo = null;
1393 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001394 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001395 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001396 if (sourceRecord.launchedFromUid == callingUid) {
1397 // The new activity is being launched from the same uid as the previous
1398 // activity in the flow, and asking to forward its result back to the
1399 // previous. In this case the activity is serving as a trampoline between
1400 // the two, so we also want to update its launchedFromPackage to be the
1401 // same as the previous activity. Note that this is safe, since we know
1402 // these two packages come from the same uid; the caller could just as
1403 // well have supplied that same package name itself. This specifially
1404 // deals with the case of an intent picker/chooser being launched in the app
1405 // flow to redirect to an activity picked by the user, where we want the final
1406 // activity to consider it to have been launched by the previous app activity.
1407 callingPackage = sourceRecord.launchedFromPackage;
1408 }
Craig Mautner6170f732013-04-02 13:05:23 -07001409 }
1410
1411 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1412 // We couldn't find a class that can handle the given Intent.
1413 // That's the end of that!
1414 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1415 }
1416
1417 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1418 // We couldn't find the specific class specified in the Intent.
1419 // Also the end of the line.
1420 err = ActivityManager.START_CLASS_NOT_FOUND;
1421 }
1422
Dianne Hackborn91097de2014-04-04 18:02:06 -07001423 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1424 && sourceRecord.task.voiceSession != null) {
1425 // If this activity is being launched as part of a voice session, we need
1426 // to ensure that it is safe to do so. If the upcoming activity will also
1427 // be part of the voice session, we can only launch it if it has explicitly
1428 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001429 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001430 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1431 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001432 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1433 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001434 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1435 }
1436 } catch (RemoteException e) {
1437 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1438 }
1439 }
1440 }
1441
1442 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1443 // If the caller is starting a new voice session, just make sure the target
1444 // is actually allowing it to run this way.
1445 try {
1446 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1447 intent, resolvedType)) {
1448 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1449 }
1450 } catch (RemoteException e) {
1451 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1452 }
1453 }
1454
louis_changcd5d1982014-08-01 10:09:08 +08001455 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1456
Craig Mautner6170f732013-04-02 13:05:23 -07001457 if (err != ActivityManager.START_SUCCESS) {
1458 if (resultRecord != null) {
1459 resultStack.sendActivityResultLocked(-1,
1460 resultRecord, resultWho, requestCode,
1461 Activity.RESULT_CANCELED, null);
1462 }
Craig Mautner6170f732013-04-02 13:05:23 -07001463 ActivityOptions.abort(options);
1464 return err;
1465 }
1466
1467 final int startAnyPerm = mService.checkPermission(
1468 START_ANY_ACTIVITY, callingPid, callingUid);
1469 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1470 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1471 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1472 if (resultRecord != null) {
1473 resultStack.sendActivityResultLocked(-1,
1474 resultRecord, resultWho, requestCode,
1475 Activity.RESULT_CANCELED, null);
1476 }
Craig Mautner6170f732013-04-02 13:05:23 -07001477 String msg;
1478 if (!aInfo.exported) {
1479 msg = "Permission Denial: starting " + intent.toString()
1480 + " from " + callerApp + " (pid=" + callingPid
1481 + ", uid=" + callingUid + ")"
1482 + " not exported from uid " + aInfo.applicationInfo.uid;
1483 } else {
1484 msg = "Permission Denial: starting " + intent.toString()
1485 + " from " + callerApp + " (pid=" + callingPid
1486 + ", uid=" + callingUid + ")"
1487 + " requires " + aInfo.permission;
1488 }
1489 Slog.w(TAG, msg);
1490 throw new SecurityException(msg);
1491 }
1492
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001493 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001494 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001495
Craig Mautner6170f732013-04-02 13:05:23 -07001496 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001497 try {
1498 // The Intent we give to the watcher has the extra data
1499 // stripped off, since it can contain private information.
1500 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001501 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001502 aInfo.applicationInfo.packageName);
1503 } catch (RemoteException e) {
1504 mService.mController = null;
1505 }
Ben Gruver5e207332013-04-03 17:41:37 -07001506 }
Craig Mautner6170f732013-04-02 13:05:23 -07001507
Ben Gruver5e207332013-04-03 17:41:37 -07001508 if (abort) {
1509 if (resultRecord != null) {
1510 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001511 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001512 }
Ben Gruver5e207332013-04-03 17:41:37 -07001513 // We pretend to the caller that it was really started, but
1514 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001515 ActivityOptions.abort(options);
1516 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001517 }
1518
1519 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001520 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001521 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001522 if (outActivity != null) {
1523 outActivity[0] = r;
1524 }
1525
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001526 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001527 if (voiceSession == null && (stack.mResumedActivity == null
1528 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001529 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1530 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001531 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001532 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001533 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001534 ActivityOptions.abort(options);
1535 return ActivityManager.START_SWITCHES_CANCELED;
1536 }
1537 }
1538
1539 if (mService.mDidAppSwitch) {
1540 // This is the second allowed switch since we stopped switches,
1541 // so now just generally allow switches. Use case: user presses
1542 // home (switches disabled, switch to home, mDidAppSwitch now true);
1543 // user taps a home icon (coming from home so allowed, we hit here
1544 // and now allow anyone to switch again).
1545 mService.mAppSwitchesAllowedTime = 0;
1546 } else {
1547 mService.mDidAppSwitch = true;
1548 }
1549
Craig Mautneree36c772014-07-16 14:56:05 -07001550 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001551
Dianne Hackborn91097de2014-04-04 18:02:06 -07001552 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001553 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001554
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001555 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001556 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001557 // activity start, but we are not actually doing an activity
1558 // switch... just dismiss the keyguard now, because we
1559 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001560 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001561 }
1562 return err;
1563 }
1564
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001565 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001566 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001567
1568 // On leanback only devices we should keep all activities in the same stack.
1569 if (!mLeanbackOnlyDevice &&
1570 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001571
1572 ActivityStack stack;
1573
Wale Ogunwale7d701172015-03-11 15:36:30 -07001574 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001575 stack = task.stack;
1576 if (stack.isOnHomeDisplay()) {
1577 if (mFocusedStack != stack) {
1578 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "computeStackFocus: Setting " +
Craig Mautnere0a38842013-12-16 16:14:02 -08001579 "focused stack to r=" + r + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001580 } else {
1581 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001582 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001583 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001584 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001585 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001586 }
1587
Craig Mautnere0a38842013-12-16 16:14:02 -08001588 final ActivityContainer container = r.mInitialActivityContainer;
1589 if (container != null) {
1590 // The first time put it on the desired stack, after this put on task stack.
1591 r.mInitialActivityContainer = null;
1592 return container.mStack;
1593 }
1594
Craig Mautner1b4bf852014-05-26 15:06:32 -07001595 if (mFocusedStack != mHomeStack && (!newTask ||
1596 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001597 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001598 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001599 return mFocusedStack;
1600 }
1601
Craig Mautnere0a38842013-12-16 16:14:02 -08001602 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1603 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001604 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001605 if (!stack.isHomeStack()) {
1606 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001607 "computeStackFocus: Setting focused stack=" + stack);
1608 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001609 }
1610 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001611
Craig Mautner4a1cb222013-12-04 16:14:06 -08001612 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001613 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
1614 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "computeStackFocus: New stack r=" + r +
1615 " stackId=" + stack.mStackId);
1616 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001617 }
1618 return mHomeStack;
1619 }
1620
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001621 boolean setFocusedStack(ActivityRecord r, String reason) {
1622 if (r == null) {
1623 // Not sure what you are trying to do, but it is not going to work...
1624 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001625 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001626 final TaskRecord task = r.task;
1627 if (task == null || task.stack == null) {
1628 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1629 return false;
1630 }
1631 task.stack.moveToFront(reason);
1632 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001633 }
1634
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001635 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001636 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001637 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001638 final Intent intent = r.intent;
1639 final int callingUid = r.launchedFromUid;
1640
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001641 // In some flows in to this function, we retrieve the task record and hold on to it
1642 // without a lock before calling back in to here... so the task at this point may
1643 // not actually be in recents. Check for that, and if it isn't in recents just
1644 // consider it invalid.
1645 if (inTask != null && !inTask.inRecents) {
1646 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1647 inTask = null;
1648 }
1649
Craig Mautnerad400af2014-08-13 16:41:36 -07001650 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001651 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1652 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001653
Craig Mautnera228ae92014-07-09 05:44:55 -07001654 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001655 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001656 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001657 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1658 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1659 "\"singleInstance\" or \"singleTask\"");
1660 launchFlags &=
1661 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1662 } else {
1663 switch (r.info.documentLaunchMode) {
1664 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1665 break;
1666 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1667 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1668 break;
1669 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1670 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1671 break;
1672 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1673 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1674 break;
1675 }
1676 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001677
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001678 final boolean launchTaskBehind = r.mLaunchTaskBehind
1679 && !launchSingleTask && !launchSingleInstance
1680 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001681
Wale Ogunwale7d701172015-03-11 15:36:30 -07001682 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1683 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001684 // For whatever reason this activity is being launched into a new
1685 // task... yet the caller has requested a result back. Well, that
1686 // is pretty messed up, so instead immediately send back a cancel
1687 // and let the new task continue launched as normal without a
1688 // dependency on its originator.
1689 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1690 r.resultTo.task.stack.sendActivityResultLocked(-1,
1691 r.resultTo, r.resultWho, r.requestCode,
1692 Activity.RESULT_CANCELED, null);
1693 r.resultTo = null;
1694 }
1695
1696 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1697 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1698 }
1699
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001700 // If we are actually going to launch in to a new task, there are some cases where
1701 // we further want to do multiple task.
1702 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1703 if (launchTaskBehind
1704 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1705 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1706 }
1707 }
1708
Craig Mautner8849a5e2013-04-02 16:41:03 -07001709 // We'll invoke onUserLeaving before onPause only if the launching
1710 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001711 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001712 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001713
1714 // If the caller has asked not to resume at this point, we make note
1715 // of this in the record so that we can skip it when trying to find
1716 // the top running activity.
1717 if (!doResume) {
1718 r.delayedResume = true;
1719 }
1720
Craig Mautnera254cd72014-05-25 16:47:39 -07001721 ActivityRecord notTop =
1722 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001723
1724 // If the onlyIfNeeded flag is set, then we can do this if the activity
1725 // being launched is the same as the one making the call... or, as
1726 // a special case, if we do not know the caller then we count the
1727 // current top activity as the caller.
1728 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1729 ActivityRecord checkedCaller = sourceRecord;
1730 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001731 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001732 }
1733 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1734 // Caller is not the same as launcher, so always needed.
1735 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1736 }
1737 }
1738
Craig Mautner8849a5e2013-04-02 16:41:03 -07001739 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001740 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001741
1742 // If the caller is not coming from another activity, but has given us an
1743 // explicit task into which they would like us to launch the new activity,
1744 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001745 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1746 final Intent baseIntent = inTask.getBaseIntent();
1747 final ActivityRecord root = inTask.getRootActivity();
1748 if (baseIntent == null) {
1749 ActivityOptions.abort(options);
1750 throw new IllegalArgumentException("Launching into task without base intent: "
1751 + inTask);
1752 }
1753
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001754 // If this task is empty, then we are adding the first activity -- it
1755 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001756 if (launchSingleInstance || launchSingleTask) {
1757 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1758 ActivityOptions.abort(options);
1759 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1760 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001761 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001762 if (root != null) {
1763 ActivityOptions.abort(options);
1764 throw new IllegalArgumentException("Caller with inTask " + inTask
1765 + " has root " + root + " but target is singleInstance/Task");
1766 }
1767 }
1768
1769 // If task is empty, then adopt the interesting intent launch flags in to the
1770 // activity being started.
1771 if (root == null) {
1772 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1773 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1774 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1775 launchFlags = (launchFlags&~flagsOfInterest)
1776 | (baseIntent.getFlags()&flagsOfInterest);
1777 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001778 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001779 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001780
Dianne Hackborn962d5352014-08-29 16:27:40 -07001781 // If the task is not empty and the caller is asking to start it as the root
1782 // of a new task, then we don't actually want to start this on the task. We
1783 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001784 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001785 addingToTask = false;
1786
1787 } else {
1788 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001789 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001790
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001791 reuseTask = inTask;
1792 } else {
1793 inTask = null;
1794 }
1795
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001796 if (inTask == null) {
1797 if (sourceRecord == null) {
1798 // This activity is not being started from another... in this
1799 // case we -always- start a new task.
1800 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1801 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1802 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1803 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1804 }
1805 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1806 // The original activity who is starting us is running as a single
1807 // instance... this new activity it is starting must go on its
1808 // own task.
1809 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1810 } else if (launchSingleInstance || launchSingleTask) {
1811 // The activity being started is a single instance... it always
1812 // gets launched into its own task.
1813 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1814 }
1815 }
1816
1817 ActivityInfo newTaskInfo = null;
1818 Intent newTaskIntent = null;
1819 ActivityStack sourceStack;
1820 if (sourceRecord != null) {
1821 if (sourceRecord.finishing) {
1822 // If the source is finishing, we can't further count it as our source. This
1823 // is because the task it is associated with may now be empty and on its way out,
1824 // so we don't want to blindly throw it in to that task. Instead we will take
1825 // the NEW_TASK flow and try to find a task for it. But save the task information
1826 // so it can be used when creating the new task.
1827 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1828 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1829 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1830 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1831 newTaskInfo = sourceRecord.info;
1832 newTaskIntent = sourceRecord.task.intent;
1833 }
1834 sourceRecord = null;
1835 sourceStack = null;
1836 } else {
1837 sourceStack = sourceRecord.task.stack;
1838 }
1839 } else {
1840 sourceStack = null;
1841 }
1842
1843 boolean movedHome = false;
1844 ActivityStack targetStack;
1845
1846 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001847 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001848
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001849 // We may want to try to place the new activity in to an existing task. We always
1850 // do this if the target activity is singleTask or singleInstance; we will also do
1851 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1852 // us to still place it in a new task: multi task, always doc mode, or being asked to
1853 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001854 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1855 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001856 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001857 // If bring to front is requested, and no result is requested and we have not
1858 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001859 // we can find a task that was started with this same
1860 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001861 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001862 // See if there is a task to bring to the front. If this is
1863 // a SINGLE_INSTANCE activity, there can be one and only one
1864 // instance of it in the history, and it is always in its own
1865 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001866 ActivityRecord intentActivity = !launchSingleInstance ?
1867 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001868 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001869 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001870 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001871 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001872 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1873 }
Craig Mautner29219d92013-04-16 20:19:12 -07001874 if (r.task == null) {
1875 r.task = intentActivity.task;
1876 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001877 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001878 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001879 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1880 + " from " + intentActivity);
Craig Mautner299f9602015-01-26 09:47:33 -08001881 targetStack.moveToFront("intentActivityFound");
Craig Mautner8849a5e2013-04-02 16:41:03 -07001882 if (intentActivity.task.intent == null) {
1883 // This task was started because of movement of
1884 // the activity based on affinity... now that we
1885 // are actually launching it, we can assign the
1886 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001887 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001888 }
1889 // If the target task is not in the front, then we need
1890 // to bring it to the front... except... well, with
1891 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1892 // to have the same behavior as if a new instance was
1893 // being started, which means not bringing it to the front
1894 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001895 final ActivityStack lastStack = getLastStack();
1896 ActivityRecord curTop = lastStack == null?
1897 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001898 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001899 if (curTop != null && (curTop.task != intentActivity.task ||
1900 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001901 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001902 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1903 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001904 // We really do want to push this one into the
1905 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001906 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001907 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1908 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001909 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001910 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1911 options, "bringingFoundTaskToFront");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001912 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001913 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1914 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001915 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001916 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001917 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001918 options = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001919 movedToFront = true;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920 }
1921 }
1922 // If the caller has requested that the target task be
1923 // reset, then do so.
1924 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1925 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1926 }
1927 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1928 // We don't need to start a new activity, and
1929 // the client said not to do anything if that
1930 // is the case, so this is it! And for paranoia, make
1931 // sure we have correctly resumed the top activity.
1932 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001933 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001934
1935 // Make sure to notify Keyguard as well if we are not running an app
1936 // transition later.
1937 if (!movedToFront) {
1938 notifyActivityDrawnForKeyguard();
1939 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001940 } else {
1941 ActivityOptions.abort(options);
1942 }
1943 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1944 }
1945 if ((launchFlags &
1946 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1947 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1948 // The caller has requested to completely replace any
1949 // existing task with its new activity. Well that should
1950 // not be too hard...
1951 reuseTask = intentActivity.task;
1952 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001953 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001954 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001955 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001956 // In this situation we want to remove all activities
1957 // from the task up to the one being started. In most
1958 // cases this means we are resetting the task to its
1959 // initial state.
1960 ActivityRecord top =
1961 intentActivity.task.performClearTaskLocked(r, launchFlags);
1962 if (top != null) {
1963 if (top.frontOfTask) {
1964 // Activity aliases may mean we use different
1965 // intents for the top activity, so make sure
1966 // the task now has the identity of the new
1967 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001968 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001969 }
1970 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1971 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001972 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001973 } else {
1974 // A special case: we need to
1975 // start the activity because it is not currently
1976 // running, and the caller has asked to clear the
1977 // current task to have this activity at the top.
1978 addingToTask = true;
1979 // Now pretend like this activity is being started
1980 // by the top of its task, so it is put in the
1981 // right place.
1982 sourceRecord = intentActivity;
1983 }
1984 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1985 // In this case the top activity on the task is the
1986 // same as the one being launched, so we take that
1987 // as a request to bring the task to the foreground.
1988 // If the top activity in the task is the root
1989 // activity, deliver this new intent to it if it
1990 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001991 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001992 && intentActivity.realActivity.equals(r.realActivity)) {
1993 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1994 intentActivity.task);
1995 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001996 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001997 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001998 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
1999 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002000 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2001 // In this case we are launching the root activity
2002 // of the task, but with a different intent. We
2003 // should start a new instance on top.
2004 addingToTask = true;
2005 sourceRecord = intentActivity;
2006 }
2007 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2008 // In this case an activity is being launched in to an
2009 // existing task, without resetting that task. This
2010 // is typically the situation of launching an activity
2011 // from a notification or shortcut. We want to place
2012 // the new activity on top of the current task.
2013 addingToTask = true;
2014 sourceRecord = intentActivity;
2015 } else if (!intentActivity.task.rootWasReset) {
2016 // In this case we are launching in to an existing task
2017 // that has not yet been started from its front door.
2018 // The current task has been brought to the front.
2019 // Ideally, we'd probably like to place this new task
2020 // at the bottom of its stack, but that's a little hard
2021 // to do with the current organization of the code so
2022 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002023 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002024 }
2025 if (!addingToTask && reuseTask == null) {
2026 // We didn't do anything... but it was needed (a.k.a., client
2027 // don't use that intent!) And for paranoia, make
2028 // sure we have correctly resumed the top activity.
2029 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002030 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002031 if (!movedToFront) {
2032 // Make sure to notify Keyguard as well if we are not running an app
2033 // transition later.
2034 notifyActivityDrawnForKeyguard();
2035 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002036 } else {
2037 ActivityOptions.abort(options);
2038 }
2039 return ActivityManager.START_TASK_TO_FRONT;
2040 }
2041 }
2042 }
2043 }
2044
2045 //String uri = r.intent.toURI();
2046 //Intent intent2 = new Intent(uri);
2047 //Slog.i(TAG, "Given intent: " + r.intent);
2048 //Slog.i(TAG, "URI is: " + uri);
2049 //Slog.i(TAG, "To intent: " + intent2);
2050
2051 if (r.packageName != null) {
2052 // If the activity being launched is the same as the one currently
2053 // at the top, then we need to check if it should only be launched
2054 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002055 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002056 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002057 if (top != null && r.resultTo == null) {
2058 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2059 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002060 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002061 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002062 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2063 top.task);
2064 // For paranoia, make sure we have correctly
2065 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002066 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002067 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002068 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002069 }
2070 ActivityOptions.abort(options);
2071 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2072 // We don't need to start a new activity, and
2073 // the client said not to do anything if that
2074 // is the case, so this is it!
2075 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2076 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002077 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002078 return ActivityManager.START_DELIVERED_TO_TOP;
2079 }
2080 }
2081 }
2082 }
2083
2084 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002085 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002086 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2087 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002088 }
2089 ActivityOptions.abort(options);
2090 return ActivityManager.START_CLASS_NOT_FOUND;
2091 }
2092
2093 boolean newTask = false;
2094 boolean keepCurTransition = false;
2095
Craig Mautnerbb742462014-07-07 15:28:55 -07002096 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002097 sourceRecord.task : null;
2098
Craig Mautner8849a5e2013-04-02 16:41:03 -07002099 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002100 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002101 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002102 if (isLockTaskModeViolation(reuseTask)) {
2103 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2104 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2105 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002106 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002107 targetStack = computeStackFocus(r, newTask);
2108 targetStack.moveToFront("startingNewTask");
2109
Craig Mautner8849a5e2013-04-02 16:41:03 -07002110 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002111 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2112 newTaskInfo != null ? newTaskInfo : r.info,
2113 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002114 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2115 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002116 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2117 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002118 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002119 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002120 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002122 if ((launchFlags &
2123 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2124 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2125 // Caller wants to appear on home activity, so before starting
2126 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002127 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002128 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002129 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002130 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002131 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002132 if (isLockTaskModeViolation(sourceTask)) {
2133 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2134 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2135 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002136 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002137 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002138 final TaskRecord topTask = targetStack.topTask();
2139 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002140 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2141 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002142 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002143 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002144 // In this case, we are adding the activity to an existing
2145 // task, but the caller has asked to clear that task if the
2146 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002147 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002148 keepCurTransition = true;
2149 if (top != null) {
2150 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002151 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002152 // For paranoia, make sure we have correctly
2153 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002154 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002155 if (doResume) {
2156 targetStack.resumeTopActivityLocked(null);
2157 }
2158 ActivityOptions.abort(options);
2159 return ActivityManager.START_DELIVERED_TO_TOP;
2160 }
2161 } else if (!addingToTask &&
2162 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2163 // In this case, we are launching an activity in our own task
2164 // that may already be running somewhere in the history, and
2165 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002166 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002167 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002168 final TaskRecord task = top.task;
2169 task.moveActivityToFrontLocked(top);
2170 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002171 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002172 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002173 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002174 if (doResume) {
2175 targetStack.resumeTopActivityLocked(null);
2176 }
2177 return ActivityManager.START_DELIVERED_TO_TOP;
2178 }
2179 }
2180 // An existing activity is starting this new activity, so we want
2181 // to keep the new one in the same task as the one that is starting
2182 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002183 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002184 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002185 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002186
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002187 } else if (inTask != null) {
2188 // The calling is asking that the new activity be started in an explicit
2189 // task it has provided to us.
2190 if (isLockTaskModeViolation(inTask)) {
2191 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2192 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2193 }
2194 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002195 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002196
2197 // Check whether we should actually launch the new activity in to the task,
2198 // or just reuse the current activity on top.
2199 ActivityRecord top = inTask.getTopActivity();
2200 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2201 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2202 || launchSingleTop || launchSingleTask) {
2203 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2204 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2205 // We don't need to start a new activity, and
2206 // the client said not to do anything if that
2207 // is the case, so this is it!
2208 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2209 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002210 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002211 return ActivityManager.START_DELIVERED_TO_TOP;
2212 }
2213 }
2214
Dianne Hackborn962d5352014-08-29 16:27:40 -07002215 if (!addingToTask) {
2216 // We don't actually want to have this activity added to the task, so just
2217 // stop here but still tell the caller that we consumed the intent.
2218 ActivityOptions.abort(options);
2219 return ActivityManager.START_TASK_TO_FRONT;
2220 }
2221
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002222 r.setTask(inTask, null);
2223 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2224 + " in explicit task " + r.task);
2225
Craig Mautner8849a5e2013-04-02 16:41:03 -07002226 } else {
2227 // This not being started from an existing activity, and not part
2228 // of a new task... just put it in the top task, though these days
2229 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002230 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002231 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002232 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002233 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002234 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002235 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002236 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2237 + " in new guessed " + r.task);
2238 }
2239
2240 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002241 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002242
Craig Mautner76e2a762014-06-24 09:05:43 -07002243 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2244 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2245 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002246 if (newTask) {
2247 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2248 }
2249 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002250 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002251 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002252 if (!launchTaskBehind) {
2253 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002254 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002255 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002256 return ActivityManager.START_SUCCESS;
2257 }
2258
Craig Mautneree36c772014-07-16 14:56:05 -07002259 final void doPendingActivityLaunchesLocked(boolean doResume) {
2260 while (!mPendingActivityLaunches.isEmpty()) {
2261 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002262 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002263 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002264 }
2265 }
2266
Craig Mautner7f13ed32014-07-28 14:00:35 -07002267 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002268 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2269 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002270 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002271 mPendingActivityLaunches.remove(palNdx);
2272 }
2273 }
2274 }
2275
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002276 void acquireLaunchWakelock() {
2277 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2278 throw new IllegalStateException("Calling must be system uid");
2279 }
2280 mLaunchingActivity.acquire();
2281 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2282 // To be safe, don't allow the wake lock to be held for too long.
2283 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2284 }
2285 }
2286
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002287 /**
2288 * Called when the frontmost task is idle.
2289 * @return the state of mService.mBooting before this was called.
2290 */
2291 private boolean checkFinishBootingLocked() {
2292 final boolean booting = mService.mBooting;
2293 boolean enableScreen = false;
2294 mService.mBooting = false;
2295 if (!mService.mBooted) {
2296 mService.mBooted = true;
2297 enableScreen = true;
2298 }
2299 if (booting || enableScreen) {
2300 mService.postFinishBooting(booting, enableScreen);
2301 }
2302 return booting;
2303 }
2304
Craig Mautnerf3333272013-04-22 10:55:53 -07002305 // Checked.
2306 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2307 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002308 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002309
Craig Mautnerf3333272013-04-22 10:55:53 -07002310 ArrayList<ActivityRecord> stops = null;
2311 ArrayList<ActivityRecord> finishes = null;
2312 ArrayList<UserStartedState> startingUsers = null;
2313 int NS = 0;
2314 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002315 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002316 boolean activityRemoved = false;
2317
Wale Ogunwale7d701172015-03-11 15:36:30 -07002318 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002319 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002320 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2321 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002322 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2323 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002324 if (fromTimeout) {
2325 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002326 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002327
2328 // This is a hack to semi-deal with a race condition
2329 // in the client where it can be constructed with a
2330 // newer configuration from when we asked it to launch.
2331 // We'll update with whatever configuration it now says
2332 // it used to launch.
2333 if (config != null) {
2334 r.configuration = config;
2335 }
2336
2337 // We are now idle. If someone is waiting for a thumbnail from
2338 // us, we can now deliver.
2339 r.idle = true;
2340
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002341 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002342 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002343 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002344 }
2345 }
2346
2347 if (allResumedActivitiesIdle()) {
2348 if (r != null) {
2349 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002350 }
2351
2352 if (mLaunchingActivity.isHeld()) {
2353 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2354 if (VALIDATE_WAKE_LOCK_CALLER &&
2355 Binder.getCallingUid() != Process.myUid()) {
2356 throw new IllegalStateException("Calling must be system uid");
2357 }
2358 mLaunchingActivity.release();
2359 }
2360 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002361 }
2362
2363 // Atomically retrieve all of the other things to do.
2364 stops = processStoppingActivitiesLocked(true);
2365 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002366 if ((NF = mFinishingActivities.size()) > 0) {
2367 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002368 mFinishingActivities.clear();
2369 }
2370
Craig Mautnerf3333272013-04-22 10:55:53 -07002371 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002372 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002373 mStartingUsers.clear();
2374 }
2375
Craig Mautnerf3333272013-04-22 10:55:53 -07002376 // Stop any activities that are scheduled to do so but have been
2377 // waiting for the next one to start.
2378 for (int i = 0; i < NS; i++) {
2379 r = stops.get(i);
2380 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002381 if (stack != null) {
2382 if (r.finishing) {
2383 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2384 } else {
2385 stack.stopActivityLocked(r);
2386 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002387 }
2388 }
2389
2390 // Finish any activities that are scheduled to do so but have been
2391 // waiting for the next one to start.
2392 for (int i = 0; i < NF; i++) {
2393 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002394 final ActivityStack stack = r.task.stack;
2395 if (stack != null) {
2396 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2397 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002398 }
2399
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002400 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002401 // Complete user switch
2402 if (startingUsers != null) {
2403 for (int i = 0; i < startingUsers.size(); i++) {
2404 mService.finishUserSwitch(startingUsers.get(i));
2405 }
2406 }
2407 // Complete starting up of background users
2408 if (mStartingBackgroundUsers.size() > 0) {
2409 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2410 mStartingBackgroundUsers.clear();
2411 for (int i = 0; i < startingUsers.size(); i++) {
2412 mService.finishUserBoot(startingUsers.get(i));
2413 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002414 }
2415 }
2416
2417 mService.trimApplications();
2418 //dump();
2419 //mWindowManager.dump();
2420
Craig Mautnerf3333272013-04-22 10:55:53 -07002421 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002422 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002423 }
2424
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002425 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002426 }
2427
Craig Mautner8e569572013-10-11 17:36:59 -07002428 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002429 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002430 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2431 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002432 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2433 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2434 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002435 }
Craig Mautner19091252013-10-05 00:03:53 -07002436 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002437 }
2438
2439 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002440 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2441 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002442 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2443 stacks.get(stackNdx).closeSystemDialogsLocked();
2444 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002445 }
2446 }
2447
Craig Mautner93529a42013-10-04 15:03:13 -07002448 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002449 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002450 }
2451
Craig Mautner8d341ef2013-03-26 09:03:27 -07002452 /**
2453 * @return true if some activity was finished (or would have finished if doit were true).
2454 */
2455 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2456 boolean didSomething = false;
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 final int numStacks = stacks.size();
2460 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2461 final ActivityStack stack = stacks.get(stackNdx);
2462 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2463 didSomething = true;
2464 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002465 }
2466 }
2467 return didSomething;
2468 }
2469
Dianne Hackborna413dc02013-07-12 12:02:55 -07002470 void updatePreviousProcessLocked(ActivityRecord r) {
2471 // Now that this process has stopped, we may want to consider
2472 // it to be the previous app to try to keep around in case
2473 // the user wants to return to it.
2474
2475 // First, found out what is currently the foreground app, so that
2476 // we don't blow away the previous app if this activity is being
2477 // hosted by the process that is actually still the foreground.
2478 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002479 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2480 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002481 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2482 final ActivityStack stack = stacks.get(stackNdx);
2483 if (isFrontStack(stack)) {
2484 if (stack.mResumedActivity != null) {
2485 fgApp = stack.mResumedActivity.app;
2486 } else if (stack.mPausingActivity != null) {
2487 fgApp = stack.mPausingActivity.app;
2488 }
2489 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002490 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002491 }
2492 }
2493
2494 // Now set this one as the previous process, only if that really
2495 // makes sense to.
2496 if (r.app != null && fgApp != null && r.app != fgApp
2497 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002498 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002499 mService.mPreviousProcess = r.app;
2500 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2501 }
2502 }
2503
Craig Mautner05d29032013-05-03 13:40:13 -07002504 boolean resumeTopActivitiesLocked() {
2505 return resumeTopActivitiesLocked(null, null, null);
2506 }
2507
2508 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2509 Bundle targetOptions) {
2510 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002511 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002512 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002513 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002514 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002515 if (isFrontStack(targetStack)) {
2516 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2517 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002518
Craig Mautnere0a38842013-12-16 16:14:02 -08002519 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2520 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002521 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2522 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002523 if (stack == targetStack) {
2524 // Already started above.
2525 continue;
2526 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002527 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002528 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002529 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002530 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002531 }
Craig Mautner05d29032013-05-03 13:40:13 -07002532 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002533 }
2534
Todd Kennedy539db512014-12-15 09:57:55 -08002535 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002536 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2537 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002538 final int numStacks = stacks.size();
2539 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2540 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002541 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002542 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002543 }
2544 }
2545
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002546 void finishVoiceTask(IVoiceInteractionSession session) {
2547 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2548 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2549 final int numStacks = stacks.size();
2550 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2551 final ActivityStack stack = stacks.get(stackNdx);
2552 stack.finishVoiceTask(session);
2553 }
2554 }
2555 }
2556
Craig Mautner299f9602015-01-26 09:47:33 -08002557 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002558 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2559 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002560 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002561 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2562 // Caller wants the home activity moved with it. To accomplish this,
2563 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002564 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002565 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002566 if (task.stack == null) {
2567 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2568 + task + " to front. Stack is null");
2569 return;
2570 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002571 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08002572 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2573 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002574 }
2575
Craig Mautner967212c2013-04-13 21:10:58 -07002576 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002577 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2578 if (activityContainer != null) {
2579 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002580 }
2581 return null;
2582 }
2583
Craig Mautner967212c2013-04-13 21:10:58 -07002584 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002585 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002586 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2587 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002588 }
2589 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002590 }
2591
Craig Mautner4a1cb222013-12-04 16:14:06 -08002592 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002593 ActivityRecord homeActivity = getHomeActivity();
2594 if (homeActivity != null) {
2595 return homeActivity.appToken;
2596 }
2597 return null;
2598 }
2599
2600 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002601 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2602 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2603 final TaskRecord task = tasks.get(taskNdx);
2604 if (task.isHomeTask()) {
2605 final ArrayList<ActivityRecord> activities = task.mActivities;
2606 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2607 final ActivityRecord r = activities.get(activityNdx);
2608 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002609 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002610 }
2611 }
2612 }
2613 }
2614 return null;
2615 }
2616
Todd Kennedyca4d8422015-01-15 15:19:22 -08002617 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002618 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002619 ActivityContainer activityContainer =
2620 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002621 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002622 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002623 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002624 return activityContainer;
2625 }
2626
Craig Mautner34b73df2014-01-12 21:11:08 -08002627 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002628 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2629 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2630 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002631 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2632 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002633 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002634 }
2635 }
2636
Craig Mautner95da1082014-02-24 17:54:35 -08002637 void deleteActivityContainer(IActivityContainer container) {
2638 ActivityContainer activityContainer = (ActivityContainer)container;
2639 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002640 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2641 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002642 final int stackId = activityContainer.mStackId;
2643 mActivityContainers.remove(stackId);
2644 mWindowManager.removeStack(stackId);
2645 }
2646 }
2647
Wale Ogunwale60454db2015-01-23 16:05:07 -08002648 void resizeStackLocked(int stackId, Rect bounds) {
2649 final ActivityStack stack = getStack(stackId);
2650 if (stack == null) {
2651 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2652 return;
2653 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002654
2655 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002656 if (r != null && !r.task.mResizeable) {
2657 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002658 return;
2659 }
2660
Wale Ogunwale60454db2015-01-23 16:05:07 -08002661 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2662 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002663 if (r != null) {
2664 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2665 // And we need to make sure at this point that all other activities
2666 // are made visible with the correct configuration.
2667 ensureActivitiesVisibleLocked(r, 0);
2668 if (!updated) {
2669 resumeTopActivitiesLocked(stack, null, null);
2670 }
2671 }
2672 }
2673 }
2674
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002675 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2676 * current task stack if it only has one entry, moving the task to a stack that matches the
2677 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2678 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2679 task.mResizeable = true;
2680 final ActivityStack currentStack = task.stack;
2681 if (currentStack.isHomeStack()) {
2682 // Can't move task off the home stack. Sorry!
2683 return;
2684 }
2685
2686 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2687 if (matchingStackId != -1) {
2688 // There is already a stack with the right bounds!
2689 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2690 // Nothing to do here. Already in the right stack...
2691 return;
2692 }
2693 // Move task to stack with matching bounds.
2694 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2695 return;
2696 }
2697
2698 if (currentStack != null && currentStack.numTasks() == 1) {
2699 // Just resize the current stack since this is the task in it.
2700 resizeStackLocked(currentStack.mStackId, bounds);
2701 return;
2702 }
2703
2704 // Create new stack and move the task to it.
2705 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2706 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2707 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2708
2709 if (newStack == null) {
2710 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2711 return;
2712 }
2713 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2714 resizeStackLocked(newStack.mStackId, bounds);
2715 }
2716
Todd Kennedy4900bf92015-01-16 16:05:14 -08002717 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002718 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2719 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002720 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002721 }
2722
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002723 ActivityContainer activityContainer = new ActivityContainer(stackId);
2724 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002725 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002726 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002727 }
2728
2729 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002730 while (true) {
2731 if (++mLastStackId <= HOME_STACK_ID) {
2732 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002733 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002734 if (getStack(mLastStackId) == null) {
2735 break;
2736 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002737 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002738 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002739 }
2740
Wale Ogunwale7de05352014-12-12 15:21:33 -08002741 private boolean restoreRecentTaskLocked(TaskRecord task) {
2742 ActivityStack stack = null;
2743 // Determine stack to restore task to.
2744 if (mLeanbackOnlyDevice) {
2745 // There is only one stack for lean back devices.
2746 stack = mHomeStack;
2747 } else {
2748 // Look for the top stack on the home display that isn't the home stack.
2749 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2750 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2751 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002752 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002753 stack = tmpStack;
2754 break;
2755 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002756 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002757 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002758
2759 if (stack == null) {
2760 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2761 // before an application stack is created...Go ahead and create one on the default
2762 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002763 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002764 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002765 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002766 if (DEBUG_RECENTS)
2767 Slog.v(TAG, "Created stack=" + stack + " for recents restoration.");
2768 }
2769
2770 if (stack == null) {
2771 // What does this mean??? Not sure how we would get here...
2772 if (DEBUG_RECENTS)
2773 Slog.v(TAG, "Unable to find/create stack to restore recent task=" + task);
2774 return false;
2775 }
2776
2777 stack.addTask(task, false, false);
2778 if (DEBUG_RECENTS)
2779 Slog.v(TAG, "Added restored task=" + task + " to stack=" + stack);
2780 final ArrayList<ActivityRecord> activities = task.mActivities;
2781 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2782 final ActivityRecord r = activities.get(activityNdx);
2783 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2784 r.info.screenOrientation, r.fullscreen,
2785 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2786 r.userId, r.info.configChanges, task.voiceSession != null,
2787 r.mLaunchTaskBehind);
2788 }
2789 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002790 }
2791
Craig Mautner299f9602015-01-26 09:47:33 -08002792 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002793 final TaskRecord task = anyTaskForIdLocked(taskId);
2794 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002795 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002796 return;
2797 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002798 final ActivityStack stack = getStack(stackId);
2799 if (stack == null) {
2800 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2801 return;
2802 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002803 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2804 if (task.stack != null) {
2805 task.stack.removeTask(task, "moveTaskToStack", false);
2806 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002807 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002808 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002809 }
2810
Craig Mautnerac6f8432013-07-17 13:24:59 -07002811 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002812 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002813 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2814 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002815 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2816 final ActivityStack stack = stacks.get(stackNdx);
2817 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002818 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2819 continue;
2820 }
2821 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2822 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2823 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002824 continue;
2825 }
2826 final ActivityRecord ar = stack.findTaskLocked(r);
2827 if (ar != null) {
2828 return ar;
2829 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002830 }
2831 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002832 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002833 return null;
2834 }
2835
2836 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002837 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2838 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002839 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2840 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2841 if (ar != null) {
2842 return ar;
2843 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002844 }
2845 }
2846 return null;
2847 }
2848
Craig Mautner8d341ef2013-03-26 09:03:27 -07002849 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002850 scheduleSleepTimeout();
2851 if (!mGoingToSleep.isHeld()) {
2852 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002853 if (mLaunchingActivity.isHeld()) {
2854 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2855 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002856 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002857 mLaunchingActivity.release();
2858 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002859 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002860 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002861 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002862 }
2863
2864 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002865 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002866
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002867 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002868 final long endTime = System.currentTimeMillis() + timeout;
2869 while (true) {
2870 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002871 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2872 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002873 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2874 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2875 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002876 }
2877 if (cantShutdown) {
2878 long timeRemaining = endTime - System.currentTimeMillis();
2879 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002880 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002881 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002882 } catch (InterruptedException e) {
2883 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002884 } else {
2885 Slog.w(TAG, "Activity manager shutdown timed out");
2886 timedout = true;
2887 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002888 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002889 } else {
2890 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002891 }
2892 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002893
2894 // Force checkReadyForSleep to complete.
2895 mSleepTimeout = true;
2896 checkReadyForSleepLocked();
2897
Craig Mautner8d341ef2013-03-26 09:03:27 -07002898 return timedout;
2899 }
2900
2901 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002902 removeSleepTimeouts();
2903 if (mGoingToSleep.isHeld()) {
2904 mGoingToSleep.release();
2905 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002906 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2907 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002908 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2909 final ActivityStack stack = stacks.get(stackNdx);
2910 stack.awakeFromSleepingLocked();
2911 if (isFrontStack(stack)) {
2912 resumeTopActivitiesLocked();
2913 }
Craig Mautner5314a402013-09-26 12:40:16 -07002914 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002915 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002916 mGoingToSleepActivities.clear();
2917 }
2918
2919 void activitySleptLocked(ActivityRecord r) {
2920 mGoingToSleepActivities.remove(r);
2921 checkReadyForSleepLocked();
2922 }
2923
2924 void checkReadyForSleepLocked() {
2925 if (!mService.isSleepingOrShuttingDown()) {
2926 // Do not care.
2927 return;
2928 }
2929
2930 if (!mSleepTimeout) {
2931 boolean dontSleep = false;
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 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2936 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002937 }
2938
2939 if (mStoppingActivities.size() > 0) {
2940 // Still need to tell some activities to stop; can't sleep yet.
2941 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2942 + mStoppingActivities.size() + " activities");
2943 scheduleIdleLocked();
2944 dontSleep = true;
2945 }
2946
2947 if (mGoingToSleepActivities.size() > 0) {
2948 // Still need to tell some activities to sleep; can't sleep yet.
2949 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2950 + mGoingToSleepActivities.size() + " activities");
2951 dontSleep = true;
2952 }
2953
2954 if (dontSleep) {
2955 return;
2956 }
2957 }
2958
Craig Mautnere0a38842013-12-16 16:14:02 -08002959 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2960 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002961 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2962 stacks.get(stackNdx).goToSleep();
2963 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002964 }
2965
2966 removeSleepTimeouts();
2967
2968 if (mGoingToSleep.isHeld()) {
2969 mGoingToSleep.release();
2970 }
2971 if (mService.mShuttingDown) {
2972 mService.notifyAll();
2973 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002974 }
2975
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002976 boolean reportResumedActivityLocked(ActivityRecord r) {
2977 final ActivityStack stack = r.task.stack;
2978 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002979 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002980 }
2981 if (allResumedActivitiesComplete()) {
2982 ensureActivitiesVisibleLocked(null, 0);
2983 mWindowManager.executeAppTransition();
2984 return true;
2985 }
2986 return false;
2987 }
2988
Craig Mautner8d341ef2013-03-26 09:03:27 -07002989 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2991 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002992 final int numStacks = stacks.size();
2993 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2994 final ActivityStack stack = stacks.get(stackNdx);
2995 stack.handleAppCrashLocked(app);
2996 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002997 }
2998 }
2999
Jose Lima4b6c6692014-08-12 17:41:12 -07003000 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003001 final ActivityStack stack = r.task.stack;
3002 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003003 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3004 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003005 return false;
3006 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003007 final boolean isVisible = stack.hasVisibleBehindActivity();
3008 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3009 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003010
3011 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003012 if (top == null || top == r || (visible == isVisible)) {
3013 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3014 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003015 return true;
3016 }
3017
3018 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003019 if (visible && top.fullscreen) {
3020 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003021 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003022 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003023 " top.app.thread=" + top.app.thread);
3024 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003025 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3026 // Only the activity set as currently visible behind should actively reset its
3027 // visible behind state.
3028 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3029 + visible + " stack.getVisibleBehindActivity()=" +
3030 stack.getVisibleBehindActivity() + " r=" + r);
3031 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003032 }
3033
Jose Lima4b6c6692014-08-12 17:41:12 -07003034 stack.setVisibleBehindActivity(visible ? r : null);
3035 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003036 // Make the activity immediately above r opaque.
3037 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3038 if (next != null) {
3039 mService.convertFromTranslucent(next.appToken);
3040 }
3041 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003042 if (top.app != null && top.app.thread != null) {
3043 // Notify the top app of the change.
3044 try {
3045 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3046 } catch (RemoteException e) {
3047 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003048 }
3049 return true;
3050 }
3051
Craig Mautnerbb742462014-07-07 15:28:55 -07003052 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3053 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3054 r.mLaunchTaskBehind = false;
3055 final TaskRecord task = r.task;
3056 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003057 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003058 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003059 mWindowManager.setAppVisibility(r.appToken, false);
3060 }
3061
3062 void scheduleLaunchTaskBehindComplete(IBinder token) {
3063 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3064 }
3065
Craig Mautnerde4ef022013-04-07 19:01:33 -07003066 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003067 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003068 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3069 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003070 final int topStackNdx = stacks.size() - 1;
3071 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3072 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003073 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003074 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003075 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003076 }
3077
3078 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003079 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3080 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003081 final int numStacks = stacks.size();
3082 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3083 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003084 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003085 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003086 }
3087 }
3088
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003089 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3090 // Examine all activities currently running in the process.
3091 TaskRecord firstTask = null;
3092 // Tasks is non-null only if two or more tasks are found.
3093 ArraySet<TaskRecord> tasks = null;
3094 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3095 for (int i=0; i<app.activities.size(); i++) {
3096 ActivityRecord r = app.activities.get(i);
3097 // First, if we find an activity that is in the process of being destroyed,
3098 // then we just aren't going to do anything for now; we want things to settle
3099 // down before we try to prune more activities.
3100 if (r.finishing || r.state == ActivityState.DESTROYING
3101 || r.state == ActivityState.DESTROYED) {
3102 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3103 return;
3104 }
3105 // Don't consider any activies that are currently not in a state where they
3106 // can be destroyed.
3107 if (r.visible || !r.stopped || !r.haveState
3108 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
3109 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
3110 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3111 continue;
3112 }
3113 if (r.task != null) {
3114 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3115 + " from " + r);
3116 if (firstTask == null) {
3117 firstTask = r.task;
3118 } else if (firstTask != r.task) {
3119 if (tasks == null) {
3120 tasks = new ArraySet<>();
3121 tasks.add(firstTask);
3122 }
3123 tasks.add(r.task);
3124 }
3125 }
3126 }
3127 if (tasks == null) {
3128 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3129 return;
3130 }
3131 // If we have activities in multiple tasks that are in a position to be destroyed,
3132 // let's iterate through the tasks and release the oldest one.
3133 final int numDisplays = mActivityDisplays.size();
3134 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3135 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3136 // Step through all stacks starting from behind, to hit the oldest things first.
3137 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3138 final ActivityStack stack = stacks.get(stackNdx);
3139 // Try to release activities in this stack; if we manage to, we are done.
3140 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3141 return;
3142 }
3143 }
3144 }
3145 }
3146
Craig Mautner8d341ef2013-03-26 09:03:27 -07003147 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003148 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003149 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003150 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003151
Craig Mautner858d8a62013-04-23 17:08:34 -07003152 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003153 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3154 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003155 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003156 final ActivityStack stack = stacks.get(stackNdx);
3157 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003158 TaskRecord task = stack.topTask();
3159 if (task != null) {
3160 mWindowManager.moveTaskToTop(task.taskId);
3161 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003162 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003163 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003164
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003165 ActivityStack stack = getStack(restoreStackId);
3166 if (stack == null) {
3167 stack = mHomeStack;
3168 }
3169 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003170 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003171 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003172 TaskRecord task = stack.topTask();
3173 if (task != null) {
3174 mWindowManager.moveTaskToTop(task.taskId);
3175 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003176 } else {
3177 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003178 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003179 }
Craig Mautner93529a42013-10-04 15:03:13 -07003180 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003181 }
3182
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003183 /**
3184 * Add background users to send boot completed events to.
3185 * @param userId The user being started in the background
3186 * @param uss The state object for the user.
3187 */
3188 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3189 mStartingBackgroundUsers.add(uss);
3190 }
3191
Craig Mautnerde4ef022013-04-07 19:01:33 -07003192 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003193 ArrayList<ActivityRecord> stops = null;
3194
3195 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003196 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3197 ActivityRecord s = mStoppingActivities.get(activityNdx);
3198 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003199 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003200 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3201 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003202 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003203 if (s.finishing) {
3204 // If this activity is finishing, it is sitting on top of
3205 // everyone else but we now know it is no longer needed...
3206 // so get rid of it. Otherwise, we need to go through the
3207 // normal flow and hide it once we determine that it is
3208 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003209 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003210 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003211 }
3212 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003213 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003214 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003215 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003216 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003217 }
3218 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003219 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003220 }
3221 }
3222
3223 return stops;
3224 }
3225
Craig Mautnercf910b02013-04-23 11:23:27 -07003226 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003227 // FIXME
3228/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3229 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003230 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003231 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003232 if (isFrontStack(stack)) {
3233 if (r == null) {
3234 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3235 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003236 final ActivityRecord pausing = stack.mPausingActivity;
3237 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003238 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003239 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003240 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003241 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003242 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003243 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003244 }
3245 }
3246 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003247 final ActivityRecord resumed = stack.mResumedActivity;
3248 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003249 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003250 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003251 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003252 if (r != null && (state == ActivityState.INITIALIZING
3253 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003254 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003255 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003256 }
3257 }
3258 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003259*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003260 }
3261
Craig Mautner27084302013-03-25 08:05:25 -07003262 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003263 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003264 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003265 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3266 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3267 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003268 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003269 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003270
Craig Mautner20e72272013-04-01 13:45:53 -07003271 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003272 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003273 }
3274
Dianne Hackborn390517b2013-05-30 15:03:32 -07003275 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3276 boolean needSep, String prefix) {
3277 if (activity != null) {
3278 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3279 if (needSep) {
3280 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003281 }
3282 pw.print(prefix);
3283 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003284 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003285 }
3286 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003287 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003288 }
3289
Craig Mautner8d341ef2013-03-26 09:03:27 -07003290 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3291 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003292 boolean printed = false;
3293 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003294 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3295 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003296 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003297 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003298 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003299 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003300 final ActivityStack stack = stacks.get(stackNdx);
3301 StringBuilder stackHeader = new StringBuilder(128);
3302 stackHeader.append(" Stack #");
3303 stackHeader.append(stack.mStackId);
3304 stackHeader.append(":");
3305 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3306 needSep, stackHeader.toString());
3307 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3308 !dumpAll, false, dumpPackage, true,
3309 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003310
Craig Mautner4a1cb222013-12-04 16:14:06 -08003311 needSep = printed;
3312 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3313 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003314 if (pr) {
3315 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003316 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003317 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003318 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3319 " mResumedActivity: ");
3320 if (pr) {
3321 printed = true;
3322 needSep = false;
3323 }
3324 if (dumpAll) {
3325 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3326 " mLastPausedActivity: ");
3327 if (pr) {
3328 printed = true;
3329 needSep = true;
3330 }
3331 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3332 needSep, " mLastNoHistoryActivity: ");
3333 }
3334 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003335 }
3336 }
3337
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003338 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3339 false, dumpPackage, true, " Activities waiting to finish:", null);
3340 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3341 false, dumpPackage, true, " Activities waiting to stop:", null);
3342 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3343 false, dumpPackage, true, " Activities waiting for another to become visible:",
3344 null);
3345 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3346 false, dumpPackage, true, " Activities waiting to sleep:", null);
3347 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3348 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003349
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003350 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003351 }
3352
Dianne Hackborn390517b2013-05-30 15:03:32 -07003353 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003354 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003355 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003356 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003357 String innerPrefix = null;
3358 String[] args = null;
3359 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003360 for (int i=list.size()-1; i>=0; i--) {
3361 final ActivityRecord r = list.get(i);
3362 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3363 continue;
3364 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003365 if (innerPrefix == null) {
3366 innerPrefix = prefix + " ";
3367 args = new String[0];
3368 }
3369 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003370 final boolean full = !brief && (complete || !r.isInHistory());
3371 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003372 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003373 needNL = false;
3374 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003375 if (header1 != null) {
3376 pw.println(header1);
3377 header1 = null;
3378 }
3379 if (header2 != null) {
3380 pw.println(header2);
3381 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003382 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003383 if (lastTask != r.task) {
3384 lastTask = r.task;
3385 pw.print(prefix);
3386 pw.print(full ? "* " : " ");
3387 pw.println(lastTask);
3388 if (full) {
3389 lastTask.dump(pw, prefix + " ");
3390 } else if (complete) {
3391 // Complete + brief == give a summary. Isn't that obvious?!?
3392 if (lastTask.intent != null) {
3393 pw.print(prefix); pw.print(" ");
3394 pw.println(lastTask.intent.toInsecureStringWithClip());
3395 }
3396 }
3397 }
3398 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3399 pw.print(" #"); pw.print(i); pw.print(": ");
3400 pw.println(r);
3401 if (full) {
3402 r.dump(pw, innerPrefix);
3403 } else if (complete) {
3404 // Complete + brief == give a summary. Isn't that obvious?!?
3405 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3406 if (r.app != null) {
3407 pw.print(innerPrefix); pw.println(r.app);
3408 }
3409 }
3410 if (client && r.app != null && r.app.thread != null) {
3411 // flush anything that is already in the PrintWriter since the thread is going
3412 // to write to the file descriptor directly
3413 pw.flush();
3414 try {
3415 TransferPipe tp = new TransferPipe();
3416 try {
3417 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3418 r.appToken, innerPrefix, args);
3419 // Short timeout, since blocking here can
3420 // deadlock with the application.
3421 tp.go(fd, 2000);
3422 } finally {
3423 tp.kill();
3424 }
3425 } catch (IOException e) {
3426 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3427 } catch (RemoteException e) {
3428 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3429 }
3430 needNL = true;
3431 }
3432 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003433 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003434 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003435
Craig Mautnerf3333272013-04-22 10:55:53 -07003436 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003437 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003438 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3439 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003440 }
3441
3442 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003443 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003444 }
3445
3446 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003447 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003448 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3449 }
3450
Craig Mautner05d29032013-05-03 13:40:13 -07003451 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003452 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3453 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3454 }
Craig Mautner05d29032013-05-03 13:40:13 -07003455 }
3456
Craig Mautner0eea92c2013-05-16 13:35:39 -07003457 void removeSleepTimeouts() {
3458 mSleepTimeout = false;
3459 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3460 }
3461
3462 final void scheduleSleepTimeout() {
3463 removeSleepTimeouts();
3464 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3465 }
3466
Craig Mautner4a1cb222013-12-04 16:14:06 -08003467 @Override
3468 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003469 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003470 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3471 }
3472
3473 @Override
3474 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003475 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003476 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3477 }
3478
3479 @Override
3480 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003481 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003482 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3483 }
3484
3485 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003486 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003487 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003488 newDisplay = mActivityDisplays.get(displayId) == null;
3489 if (newDisplay) {
3490 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003491 if (activityDisplay.mDisplay == null) {
3492 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3493 return;
3494 }
Craig Mautner4504de52013-12-20 09:06:56 -08003495 mActivityDisplays.put(displayId, activityDisplay);
3496 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003497 }
Craig Mautner4504de52013-12-20 09:06:56 -08003498 if (newDisplay) {
3499 mWindowManager.onDisplayAdded(displayId);
3500 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003501 }
3502
3503 public void handleDisplayRemovedLocked(int displayId) {
3504 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003505 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3506 if (activityDisplay != null) {
3507 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003508 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003509 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003510 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003511 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003512 }
3513 }
3514 mWindowManager.onDisplayRemoved(displayId);
3515 }
3516
3517 public void handleDisplayChangedLocked(int displayId) {
3518 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003519 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3520 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003521 // TODO: Update the bounds.
3522 }
3523 }
3524 mWindowManager.onDisplayChanged(displayId);
3525 }
3526
3527 StackInfo getStackInfo(ActivityStack stack) {
3528 StackInfo info = new StackInfo();
3529 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3530 info.displayId = Display.DEFAULT_DISPLAY;
3531 info.stackId = stack.mStackId;
3532
3533 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3534 final int numTasks = tasks.size();
3535 int[] taskIds = new int[numTasks];
3536 String[] taskNames = new String[numTasks];
3537 for (int i = 0; i < numTasks; ++i) {
3538 final TaskRecord task = tasks.get(i);
3539 taskIds[i] = task.taskId;
3540 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3541 : task.realActivity != null ? task.realActivity.flattenToString()
3542 : task.getTopActivity() != null ? task.getTopActivity().packageName
3543 : "unknown";
3544 }
3545 info.taskIds = taskIds;
3546 info.taskNames = taskNames;
3547 return info;
3548 }
3549
3550 StackInfo getStackInfoLocked(int stackId) {
3551 ActivityStack stack = getStack(stackId);
3552 if (stack != null) {
3553 return getStackInfo(stack);
3554 }
3555 return null;
3556 }
3557
3558 ArrayList<StackInfo> getAllStackInfosLocked() {
3559 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003560 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3561 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003562 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3563 list.add(getStackInfo(stacks.get(ndx)));
3564 }
3565 }
3566 return list;
3567 }
3568
Jason Monka8f569c2014-07-07 12:15:21 -04003569 void showLockTaskToast() {
Benjamin Franz43261142015-02-11 15:59:44 +00003570 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003571 }
3572
Benjamin Franz43261142015-02-11 15:59:44 +00003573 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003574 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003575 // Take out of lock task mode if necessary
3576 if (mLockTaskModeTask != null) {
3577 final Message lockTaskMsg = Message.obtain();
3578 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3579 lockTaskMsg.what = LOCK_TASK_END_MSG;
3580 mLockTaskModeTask = null;
3581 mHandler.sendMessage(lockTaskMsg);
3582 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003583 return;
3584 }
3585 if (isLockTaskModeViolation(task)) {
3586 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3587 return;
3588 }
3589 mLockTaskModeTask = task;
Craig Mautner299f9602015-01-26 09:47:33 -08003590 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003591 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003592
3593 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003594 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3595 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003596 lockTaskMsg.what = LOCK_TASK_START_MSG;
Benjamin Franz43261142015-02-11 15:59:44 +00003597 lockTaskMsg.arg2 = lockTaskModeState;
justinzhang5286d3f2014-05-12 17:06:01 -04003598 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003599 }
3600
3601 boolean isLockTaskModeViolation(TaskRecord task) {
3602 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3603 }
3604
3605 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3606 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003607 final Message lockTaskMsg = Message.obtain();
3608 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3609 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003610 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003611 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003612 }
3613 }
3614
Benjamin Franz43261142015-02-11 15:59:44 +00003615 int getLockTaskModeState() {
3616 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003617 }
3618
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003619 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003620
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003621 public ActivityStackSupervisorHandler(Looper looper) {
3622 super(looper);
3623 }
3624
Craig Mautnerf3333272013-04-22 10:55:53 -07003625 void activityIdleInternal(ActivityRecord r) {
3626 synchronized (mService) {
3627 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3628 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003629 }
3630
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003631 @Override
3632 public void handleMessage(Message msg) {
3633 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003634 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003635 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003636 if (mService.mDidDexOpt) {
3637 mService.mDidDexOpt = false;
3638 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3639 nmsg.obj = msg.obj;
3640 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3641 return;
3642 }
3643 // We don't at this point know if the activity is fullscreen,
3644 // so we need to be conservative and assume it isn't.
3645 activityIdleInternal((ActivityRecord)msg.obj);
3646 } break;
3647 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003648 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003649 activityIdleInternal((ActivityRecord)msg.obj);
3650 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003651 case RESUME_TOP_ACTIVITY_MSG: {
3652 synchronized (mService) {
3653 resumeTopActivitiesLocked();
3654 }
3655 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003656 case SLEEP_TIMEOUT_MSG: {
3657 synchronized (mService) {
3658 if (mService.isSleepingOrShuttingDown()) {
3659 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3660 mSleepTimeout = true;
3661 checkReadyForSleepLocked();
3662 }
3663 }
3664 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003665 case LAUNCH_TIMEOUT_MSG: {
3666 if (mService.mDidDexOpt) {
3667 mService.mDidDexOpt = false;
3668 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3669 return;
3670 }
3671 synchronized (mService) {
3672 if (mLaunchingActivity.isHeld()) {
3673 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3674 if (VALIDATE_WAKE_LOCK_CALLER
3675 && Binder.getCallingUid() != Process.myUid()) {
3676 throw new IllegalStateException("Calling must be system uid");
3677 }
3678 mLaunchingActivity.release();
3679 }
3680 }
3681 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003682 case HANDLE_DISPLAY_ADDED: {
3683 handleDisplayAddedLocked(msg.arg1);
3684 } break;
3685 case HANDLE_DISPLAY_CHANGED: {
3686 handleDisplayChangedLocked(msg.arg1);
3687 } break;
3688 case HANDLE_DISPLAY_REMOVED: {
3689 handleDisplayRemovedLocked(msg.arg1);
3690 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003691 case CONTAINER_CALLBACK_VISIBILITY: {
3692 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003693 final IActivityContainerCallback callback = container.mCallback;
3694 if (callback != null) {
3695 try {
3696 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3697 } catch (RemoteException e) {
3698 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003699 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003700 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003701 case LOCK_TASK_START_MSG: {
3702 // When lock task starts, we disable the status bars.
3703 try {
Jason Monk62515be2014-05-21 16:06:19 -04003704 if (mLockTaskNotify == null) {
3705 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003706 }
Jason Monk62515be2014-05-21 16:06:19 -04003707 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003708 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003709 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003710 int flags = 0;
3711 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_LOCKED) {
3712 flags = StatusBarManager.DISABLE_MASK
3713 & (~StatusBarManager.DISABLE_BACK);
3714 } else if (mLockTaskModeState ==
3715 ActivityManager.LOCK_TASK_MODE_PINNED) {
3716 flags = StatusBarManager.DISABLE_MASK
3717 & (~StatusBarManager.DISABLE_BACK)
3718 & (~StatusBarManager.DISABLE_HOME)
3719 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003720 }
3721 getStatusBarService().disable(flags, mToken,
3722 mService.mContext.getPackageName());
3723 }
3724 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003725 if (getDevicePolicyManager() != null) {
3726 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003727 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003728 }
justinzhang5286d3f2014-05-12 17:06:01 -04003729 } catch (RemoteException ex) {
3730 throw new RuntimeException(ex);
3731 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003732 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003733 case LOCK_TASK_END_MSG: {
3734 // When lock task ends, we enable the status bars.
3735 try {
Jason Monk62515be2014-05-21 16:06:19 -04003736 if (getStatusBarService() != null) {
3737 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3738 mService.mContext.getPackageName());
3739 }
3740 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003741 if (getDevicePolicyManager() != null) {
3742 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3743 msg.arg1);
3744 }
Jason Monk62515be2014-05-21 16:06:19 -04003745 if (mLockTaskNotify == null) {
3746 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3747 }
3748 mLockTaskNotify.show(false);
3749 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003750 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003751 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003752 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Benjamin Franz43261142015-02-11 15:59:44 +00003753 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_PINNED &&
3754 shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003755 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003756 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003757 new LockPatternUtils(mService.mContext)
3758 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003759 }
3760 } catch (SettingNotFoundException e) {
3761 // No setting, don't lock.
3762 }
justinzhang5286d3f2014-05-12 17:06:01 -04003763 } catch (RemoteException ex) {
3764 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003765 } finally {
3766 mLockTaskModeState = ActivityManager.LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003767 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003768 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003769 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3770 final ActivityContainer container = (ActivityContainer) msg.obj;
3771 final IActivityContainerCallback callback = container.mCallback;
3772 if (callback != null) {
3773 try {
3774 callback.onAllActivitiesComplete(container.asBinder());
3775 } catch (RemoteException e) {
3776 }
3777 }
3778 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003779 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3780 synchronized (mService) {
3781 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3782 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003783 final ActivityContainer container = (ActivityContainer) msg.obj;
3784 container.mStack.finishAllActivitiesLocked(true);
3785 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003786 }
3787 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003788 case LAUNCH_TASK_BEHIND_COMPLETE: {
3789 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003790 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003791 if (r != null) {
3792 handleLaunchTaskBehindCompleteLocked(r);
3793 }
3794 }
3795 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003796 }
3797 }
3798 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003799
Ying Wangb081a592014-04-22 15:20:16 -07003800 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003801 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003802 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003803 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003804 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003805 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003806 ActivityRecord mParentActivity = null;
3807 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003808
Craig Mautnere3a00d72014-04-16 08:31:19 -07003809 boolean mVisible = true;
3810
Craig Mautner4a1cb222013-12-04 16:14:06 -08003811 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003812 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003813
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003814 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3815 final static int CONTAINER_STATE_NO_SURFACE = 1;
3816 final static int CONTAINER_STATE_FINISHING = 2;
3817 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3818
3819 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003820 synchronized (mService) {
3821 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003822 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003823 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003824 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003825 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003826 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003827
Craig Mautnere0a38842013-12-16 16:14:02 -08003828 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003829 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3830 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003831 mActivityDisplay = activityDisplay;
3832 mStack.mDisplayId = activityDisplay.mDisplayId;
3833 mStack.mStacks = activityDisplay.mStacks;
3834
3835 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003836 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003837 }
3838
3839 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003840 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003842 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3843 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003844 return;
3845 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003846 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003847 }
3848 }
3849
3850 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003851 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003852 synchronized (mService) {
3853 if (mActivityDisplay != null) {
3854 return mActivityDisplay.mDisplayId;
3855 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003856 }
3857 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003858 }
3859
Jeff Brownca9bc702014-02-11 14:32:56 -08003860 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003861 public int getStackId() {
3862 synchronized (mService) {
3863 return mStackId;
3864 }
3865 }
3866
3867 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003868 public boolean injectEvent(InputEvent event) {
3869 final long origId = Binder.clearCallingIdentity();
3870 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003871 synchronized (mService) {
3872 if (mActivityDisplay != null) {
3873 return mInputManagerInternal.injectInputEvent(event,
3874 mActivityDisplay.mDisplayId,
3875 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3876 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003877 }
3878 return false;
3879 } finally {
3880 Binder.restoreCallingIdentity(origId);
3881 }
3882 }
3883
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003884 @Override
3885 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003886 synchronized (mService) {
3887 if (mContainerState == CONTAINER_STATE_FINISHING) {
3888 return;
3889 }
3890 mContainerState = CONTAINER_STATE_FINISHING;
3891
3892 final Message msg =
3893 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003894 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003895
3896 long origId = Binder.clearCallingIdentity();
3897 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003898 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003899 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003900 } finally {
3901 Binder.restoreCallingIdentity(origId);
3902 }
3903 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003904 }
3905
Craig Mautner60257702014-09-17 15:02:33 -07003906 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003907 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3908 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003909 if (mActivityDisplay != null) {
3910 mActivityDisplay.detachActivitiesLocked(mStack);
3911 mActivityDisplay = null;
3912 mStack.mDisplayId = -1;
3913 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003914 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003915 }
3916 }
3917
3918 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003919 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003920 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08003921 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003922 Binder.getCallingUid(), mCurrentUser, false,
3923 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003924
Craig Mautnere0a38842013-12-16 16:14:02 -08003925 // TODO: Switch to user app stacks here.
3926 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08003927 final Uri data = intent.getData();
3928 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
3929 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08003930 }
Craig Mautnerb9168362015-02-26 20:40:19 -08003931 checkEmbeddedAllowedInner(userId, intent, mimeType);
3932
3933 intent.addFlags(FORCE_NEW_TASK_FLAGS);
3934 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
3935 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003936 }
3937
3938 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003939 public final int startActivityIntentSender(IIntentSender intentSender) {
3940 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3941
3942 if (!(intentSender instanceof PendingIntentRecord)) {
3943 throw new IllegalArgumentException("Bad PendingIntent object");
3944 }
3945
Craig Mautnerb9168362015-02-26 20:40:19 -08003946 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003947 Binder.getCallingUid(), mCurrentUser, false,
3948 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003949
3950 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3951 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3952 pendingIntent.key.requestResolvedType);
3953
3954 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3955 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3956 }
3957
3958 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003959 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003960 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003961 throw new SecurityException(
3962 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3963 }
3964 }
3965
Craig Mautnerdf88d732014-01-27 09:21:32 -08003966 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003967 public IBinder asBinder() {
3968 return this;
3969 }
3970
Craig Mautner4504de52013-12-20 09:06:56 -08003971 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003972 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003973 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003974 }
3975
Craig Mautner4a1cb222013-12-04 16:14:06 -08003976 ActivityStackSupervisor getOuter() {
3977 return ActivityStackSupervisor.this;
3978 }
3979
Craig Mautnerd163e752014-06-13 17:18:47 -07003980 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003981 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003982 }
3983
3984 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003985 synchronized (mService) {
3986 if (mActivityDisplay != null) {
3987 mActivityDisplay.getBounds(outBounds);
3988 } else {
3989 outBounds.set(0, 0);
3990 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003991 }
3992 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003993
Craig Mautner6985bad2014-04-21 15:22:06 -07003994 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003995 void setVisible(boolean visible) {
3996 if (mVisible != visible) {
3997 mVisible = visible;
3998 if (mCallback != null) {
3999 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4000 0 /* unused */, this).sendToTarget();
4001 }
4002 }
4003 }
4004
Craig Mautner6985bad2014-04-21 15:22:06 -07004005 void setDrawn() {
4006 }
4007
Craig Mautner1b4bf852014-05-26 15:06:32 -07004008 // You can always start a new task on a regular ActivityStack.
4009 boolean isEligibleForNewTasks() {
4010 return true;
4011 }
4012
Craig Mautnerd163e752014-06-13 17:18:47 -07004013 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004014 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
4015 detachLocked();
4016 deleteActivityContainer(this);
4017 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004018 }
4019
Craig Mautner34b73df2014-01-12 21:11:08 -08004020 @Override
4021 public String toString() {
4022 return mIdString + (mActivityDisplay == null ? "N" : "A");
4023 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004024 }
4025
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004026 private class VirtualActivityContainer extends ActivityContainer {
4027 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004028 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004029
4030 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4031 super(getNextStackId());
4032 mParentActivity = parent;
4033 mCallback = callback;
4034 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004035 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004036 }
4037
4038 @Override
4039 public void setSurface(Surface surface, int width, int height, int density) {
4040 super.setSurface(surface, width, height, density);
4041
4042 synchronized (mService) {
4043 final long origId = Binder.clearCallingIdentity();
4044 try {
4045 setSurfaceLocked(surface, width, height, density);
4046 } finally {
4047 Binder.restoreCallingIdentity(origId);
4048 }
4049 }
4050 }
4051
4052 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4053 if (mContainerState == CONTAINER_STATE_FINISHING) {
4054 return;
4055 }
4056 VirtualActivityDisplay virtualActivityDisplay =
4057 (VirtualActivityDisplay) mActivityDisplay;
4058 if (virtualActivityDisplay == null) {
4059 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004060 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004061 mActivityDisplay = virtualActivityDisplay;
4062 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4063 attachToDisplayLocked(virtualActivityDisplay);
4064 }
4065
4066 if (mSurface != null) {
4067 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004068 }
4069
Craig Mautner6985bad2014-04-21 15:22:06 -07004070 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004071 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004072 mStack.resumeTopActivityLocked(null);
4073 } else {
4074 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004075 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004076 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004077 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004078 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004079 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004080
Craig Mautnerd163e752014-06-13 17:18:47 -07004081 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004082
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004083 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
4084 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004085 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004086
Craig Mautner6985bad2014-04-21 15:22:06 -07004087 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004088 boolean isAttachedLocked() {
4089 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004090 }
4091
4092 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004093 void setDrawn() {
4094 synchronized (mService) {
4095 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004096 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004097 }
4098 }
4099
Craig Mautner1b4bf852014-05-26 15:06:32 -07004100 // Never start a new task on an ActivityView if it isn't explicitly specified.
4101 @Override
4102 boolean isEligibleForNewTasks() {
4103 return false;
4104 }
4105
Craig Mautnerd163e752014-06-13 17:18:47 -07004106 private void setSurfaceIfReadyLocked() {
4107 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004108 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4109 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4110 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4111 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4112 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004113 }
4114 }
4115
Craig Mautner4a1cb222013-12-04 16:14:06 -08004116 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4117 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004118 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004119 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004120 int mDisplayId;
4121 Display mDisplay;
4122 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004123
Craig Mautner4a1cb222013-12-04 16:14:06 -08004124 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4125 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004126 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004127
Jose Lima4b6c6692014-08-12 17:41:12 -07004128 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004129
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004130 ActivityDisplay() {
4131 }
Craig Mautner4504de52013-12-20 09:06:56 -08004132
Craig Mautner1a70a162014-09-13 12:09:31 -07004133 // After instantiation, check that mDisplay is not null before using this. The alternative
4134 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004135 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004136 final Display display = mDisplayManager.getDisplay(displayId);
4137 if (display == null) {
4138 return;
4139 }
4140 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004141 }
4142
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004143 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004144 mDisplay = display;
4145 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004146 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004147 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004148
4149 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004150 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4151 + mDisplayId);
4152 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004153 }
4154
Craig Mautnere0a38842013-12-16 16:14:02 -08004155 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004156 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004157 + " from displayId=" + mDisplayId);
4158 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004159 }
4160
4161 void getBounds(Point bounds) {
4162 mDisplay.getDisplayInfo(mDisplayInfo);
4163 bounds.x = mDisplayInfo.appWidth;
4164 bounds.y = mDisplayInfo.appHeight;
4165 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004166
Jose Lima4b6c6692014-08-12 17:41:12 -07004167 void setVisibleBehindActivity(ActivityRecord r) {
4168 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004169 }
4170
Jose Lima4b6c6692014-08-12 17:41:12 -07004171 boolean hasVisibleBehindActivity() {
4172 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004173 }
4174
Craig Mautner34b73df2014-01-12 21:11:08 -08004175 @Override
4176 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004177 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4178 }
4179 }
4180
4181 class VirtualActivityDisplay extends ActivityDisplay {
4182 VirtualDisplay mVirtualDisplay;
4183
Craig Mautner6985bad2014-04-21 15:22:06 -07004184 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004185 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004186 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4187 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4188 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4189 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004190
4191 init(mVirtualDisplay.getDisplay());
4192
4193 mWindowManager.handleDisplayAdded(mDisplayId);
4194 }
4195
4196 void setSurface(Surface surface) {
4197 if (mVirtualDisplay != null) {
4198 mVirtualDisplay.setSurface(surface);
4199 }
4200 }
4201
4202 @Override
4203 void detachActivitiesLocked(ActivityStack stack) {
4204 super.detachActivitiesLocked(stack);
4205 if (mVirtualDisplay != null) {
4206 mVirtualDisplay.release();
4207 mVirtualDisplay = null;
4208 }
4209 }
4210
4211 @Override
4212 public String toString() {
4213 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004214 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004215 }
Jose Lima58e66d62014-05-27 20:00:27 -07004216
4217 private boolean isLeanbackOnlyDevice() {
4218 boolean onLeanbackOnly = false;
4219 try {
4220 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4221 PackageManager.FEATURE_LEANBACK_ONLY);
4222 } catch (RemoteException e) {
4223 // noop
4224 }
4225
4226 return onLeanbackOnly;
4227 }
Craig Mautner27084302013-03-25 08:05:25 -07004228}