blob: b8aa7b2c3e979f3a2fb1495bd48364aa1449084e [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 Mautner2420ead2013-04-01 17:13:20 -070024import static com.android.server.am.ActivityManagerService.localLOGV;
Craig Mautner23ac33b2013-04-01 16:26:35 -070025import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070026import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070027import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Wale Ogunwale7de05352014-12-12 15:21:33 -080028import static com.android.server.am.ActivityManagerService.DEBUG_RECENTS;
Craig Mautner6170f732013-04-02 13:05:23 -070029import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070030import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070031import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070032import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
33import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070034import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070035import static com.android.server.am.ActivityManagerService.TAG;
Craig Mautner84984fa2014-06-19 11:19:20 -070036import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
37import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
38import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070039
Craig Mautner2420ead2013-04-01 17:13:20 -070040import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080042import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityOptions;
44import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080045import android.app.IActivityContainer;
46import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070047import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070048import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070050import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070051import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070053import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040054import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040055import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070056import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070057import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070061import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.content.pm.ApplicationInfo;
63import android.content.pm.PackageManager;
64import android.content.pm.ResolveInfo;
65import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080066import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080067import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080068import android.hardware.display.DisplayManager;
69import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080070import android.hardware.display.DisplayManagerGlobal;
71import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080072import android.hardware.input.InputManager;
73import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080074import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070075import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070076import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070077import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070078import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070079import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070080import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070081import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070082import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070083import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070084import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070085import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040086import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070087import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070088import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040089import android.provider.Settings;
90import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070091import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070092import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070093import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070094import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080095import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070096
Craig Mautner4a1cb222013-12-04 16:14:06 -080097import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080098import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080099import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800100import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800101import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700103import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800104import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700105import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400106import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400107import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800108import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700109import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700110import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700111
justinzhang5286d3f2014-05-12 17:06:01 -0400112
Craig Mautner8d341ef2013-03-26 09:03:27 -0700113import java.io.FileDescriptor;
114import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700115import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700116import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700117import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700118
Craig Mautner4a1cb222013-12-04 16:14:06 -0800119public final class ActivityStackSupervisor implements DisplayListener {
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;
Jason Monka8f569c2014-07-07 12:15:21 -0400271 /** Whether lock task has been entered by an authorized app and cannot
272 * be exited. */
273 private boolean mLockTaskIsLocked;
Jason Monk62515be2014-05-21 16:06:19 -0400274 /**
275 * Notifies the user when entering/exiting lock-task.
276 */
277 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800278
Craig Mautneree36c772014-07-16 14:56:05 -0700279 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
280 = new ArrayList<PendingActivityLaunch>();
281
Craig Mautner42d04db2014-11-06 12:13:23 -0800282 /** Used to keep resumeTopActivityLocked() from being entered recursively */
283 boolean inResumeTopActivity;
284
Craig Mautneree36c772014-07-16 14:56:05 -0700285 /**
286 * Description of a request to start a new activity, which has been held
287 * due to app switches being disabled.
288 */
289 static class PendingActivityLaunch {
290 final ActivityRecord r;
291 final ActivityRecord sourceRecord;
292 final int startFlags;
293 final ActivityStack stack;
294
295 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
296 int _startFlags, ActivityStack _stack) {
297 r = _r;
298 sourceRecord = _sourceRecord;
299 startFlags = _startFlags;
300 stack = _stack;
301 }
302 }
303
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800304 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700305 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800306 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700307 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
308 }
309
310 /**
311 * At the time when the constructor runs, the power manager has not yet been
312 * initialized. So we initialize our wakelocks afterwards.
313 */
314 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800315 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700316 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700317 mLaunchingActivity =
318 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
319 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700320 }
321
justinzhang5286d3f2014-05-12 17:06:01 -0400322 // This function returns a IStatusBarService. The value is from ServiceManager.
323 // getService and is cached.
324 private IStatusBarService getStatusBarService() {
325 synchronized (mService) {
326 if (mStatusBarService == null) {
327 mStatusBarService = IStatusBarService.Stub.asInterface(
328 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
329 if (mStatusBarService == null) {
330 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
331 }
332 }
333 return mStatusBarService;
334 }
335 }
336
Jason Monk35c62a42014-06-17 10:24:47 -0400337 private IDevicePolicyManager getDevicePolicyManager() {
338 synchronized (mService) {
339 if (mDevicePolicyManager == null) {
340 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
341 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
342 if (mDevicePolicyManager == null) {
343 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
344 }
345 }
346 return mDevicePolicyManager;
347 }
348 }
349
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700350 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800351 synchronized (mService) {
352 mWindowManager = wm;
353
354 mDisplayManager =
355 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
356 mDisplayManager.registerDisplayListener(this, null);
357
358 Display[] displays = mDisplayManager.getDisplays();
359 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
360 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800361 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700362 if (activityDisplay.mDisplay == null) {
363 throw new IllegalStateException("Default Display does not exist");
364 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800365 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800366 }
367
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700368 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800369 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800370
371 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700372
373 // Initialize this here, now that we can get a valid reference to PackageManager.
374 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375 }
Craig Mautner27084302013-03-25 08:05:25 -0700376 }
377
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200378 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700379 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200380 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700381 }
382
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700383 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800384 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700385 }
386
Craig Mautnerde4ef022013-04-07 19:01:33 -0700387 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800388 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700389 }
390
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800391 /** Top of all visible stacks is/should always be equal to the focused stack.
392 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
393 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700394 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800395 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
396 if (parent != null) {
397 stack = parent.task.stack;
398 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800399 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700400 }
401
Craig Mautner299f9602015-01-26 09:47:33 -0800402 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800403 moveHomeStack(toFront, reason, null);
404 }
405
406 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800407 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800408 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800409 if (topNdx <= 0) {
410 return;
411 }
412 ActivityStack topStack = stacks.get(topNdx);
413 final boolean homeInFront = topStack == mHomeStack;
414 if (homeInFront != toFront) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800415 stacks.remove(mHomeStack);
416 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800417 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
418 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700419 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800420
421 if (lastFocusedStack != null) {
422 mLastFocusedStack = lastFocusedStack;
423 }
424 mFocusedStack = stacks.get(topNdx);
425
Craig Mautnerde313752015-01-22 14:28:03 -0800426 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
427 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800428 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800429
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800430 if (mService.mBooting || !mService.mBooted) {
431 final ActivityRecord r = topRunningActivityLocked();
432 if (r != null && r.idle) {
433 checkFinishBootingLocked();
434 }
435 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700436 }
437
Craig Mautner299f9602015-01-26 09:47:33 -0800438 void moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700439 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
440 mWindowManager.showRecentApps();
441 return;
442 }
Craig Mautner299f9602015-01-26 09:47:33 -0800443 moveHomeStack(true, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700444 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700445 }
446
Craig Mautner299f9602015-01-26 09:47:33 -0800447 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700448 if (!mService.mBooting && !mService.mBooted) {
449 // Not ready yet!
450 return false;
451 }
452
Craig Mautner84984fa2014-06-19 11:19:20 -0700453 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
454 mWindowManager.showRecentApps();
455 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700456 }
Craig Mautner299f9602015-01-26 09:47:33 -0800457 moveHomeStackTaskToTop(homeStackTaskType, reason);
Craig Mautner84984fa2014-06-19 11:19:20 -0700458 if (prev != null) {
459 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
460 }
461
Craig Mautnera8a90e02013-06-28 15:24:50 -0700462 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700463 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000464 if (r != null && r.isHomeActivity()) {
Craig Mautner299f9602015-01-26 09:47:33 -0800465 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700466 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700467 }
Craig Mautner299f9602015-01-26 09:47:33 -0800468 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700469 }
470
Craig Mautner8d341ef2013-03-26 09:03:27 -0700471 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800472 return anyTaskForIdLocked(id, true);
473 }
474
475 /**
476 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
477 * @param id Id of the task we would like returned.
478 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
479 * restore the task from recents to the active list.
480 */
481 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800482 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800483 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800484 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800485 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
486 ActivityStack stack = stacks.get(stackNdx);
487 TaskRecord task = stack.taskForIdLocked(id);
488 if (task != null) {
489 return task;
490 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700491 }
492 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800493
494 // Don't give up! Look in recents.
495 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800496 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800497 if (task == null) {
498 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
499 return null;
500 }
501
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800502 if (!restoreFromRecents) {
503 return task;
504 }
505
Wale Ogunwale7de05352014-12-12 15:21:33 -0800506 if (!restoreRecentTaskLocked(task)) {
507 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
508 return null;
509 }
510 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
511 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700512 }
513
Craig Mautner6170f732013-04-02 13:05:23 -0700514 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800515 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800516 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800517 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800518 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
519 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
520 if (r != null) {
521 return r;
522 }
Craig Mautner6170f732013-04-02 13:05:23 -0700523 }
524 }
525 return null;
526 }
527
Craig Mautneref73ee12014-04-23 11:45:37 -0700528 void setNextTaskId(int taskId) {
529 if (taskId > mCurTaskId) {
530 mCurTaskId = taskId;
531 }
532 }
533
Craig Mautner8d341ef2013-03-26 09:03:27 -0700534 int getNextTaskId() {
535 do {
536 mCurTaskId++;
537 if (mCurTaskId <= 0) {
538 mCurTaskId = 1;
539 }
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -0800540 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700541 return mCurTaskId;
542 }
543
Craig Mautnerde4ef022013-04-07 19:01:33 -0700544 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800545 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700546 if (stack == null) {
547 return null;
548 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700549 ActivityRecord resumedActivity = stack.mResumedActivity;
550 if (resumedActivity == null || resumedActivity.app == null) {
551 resumedActivity = stack.mPausingActivity;
552 if (resumedActivity == null || resumedActivity.app == null) {
553 resumedActivity = stack.topRunningActivityLocked(null);
554 }
555 }
556 return resumedActivity;
557 }
558
Dianne Hackbornff072722014-09-24 10:56:28 -0700559 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700560 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800562 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
563 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800564 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
565 final ActivityStack stack = stacks.get(stackNdx);
566 if (!isFrontStack(stack)) {
567 continue;
568 }
569 ActivityRecord hr = stack.topRunningActivityLocked(null);
570 if (hr != null) {
571 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
572 && processName.equals(hr.processName)) {
573 try {
George Mount2c92c972014-03-20 09:38:23 -0700574 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800575 didSomething = true;
576 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700577 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800578 Slog.w(TAG, "Exception in new application when starting activity "
579 + hr.intent.getComponent().flattenToShortString(), e);
580 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700581 }
Craig Mautner20e72272013-04-01 13:45:53 -0700582 }
Craig Mautner20e72272013-04-01 13:45:53 -0700583 }
584 }
585 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700586 if (!didSomething) {
587 ensureActivitiesVisibleLocked(null, 0);
588 }
Craig Mautner20e72272013-04-01 13:45:53 -0700589 return didSomething;
590 }
591
592 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800593 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
594 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800595 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
596 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800597 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800598 continue;
599 }
600 final ActivityRecord resumedActivity = stack.mResumedActivity;
601 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800602 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
603 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800604 return false;
605 }
Craig Mautner20e72272013-04-01 13:45:53 -0700606 }
607 }
608 return true;
609 }
610
Craig Mautnerde4ef022013-04-07 19:01:33 -0700611 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800612 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
613 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800614 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
615 final ActivityStack stack = stacks.get(stackNdx);
616 if (isFrontStack(stack)) {
617 final ActivityRecord r = stack.mResumedActivity;
618 if (r != null && r.state != ActivityState.RESUMED) {
619 return false;
620 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700621 }
622 }
623 }
624 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 if (DEBUG_STACK) Slog.d(TAG,
626 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
627 mLastFocusedStack + " to=" + mFocusedStack);
628 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700629 return true;
630 }
631
632 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800633 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
634 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800635 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
636 final ActivityStack stack = stacks.get(stackNdx);
637 final ActivityRecord r = stack.mResumedActivity;
Craig Mautner8c14c152015-01-15 17:32:07 -0800638 if (r != null && (!r.nowVisible || mWaitingVisibleActivities.contains(r))) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800639 return false;
640 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700641 }
642 }
643 return true;
644 }
645
Craig Mautner2acc3892013-09-23 10:28:14 -0700646 /**
647 * Pause all activities in either all of the stacks or just the back stacks.
648 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700649 * @return true if any activity was paused as a result of this call.
650 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700651 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700652 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
654 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
656 final ActivityStack stack = stacks.get(stackNdx);
657 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
658 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
659 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700660 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
661 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800662 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700663 }
664 }
665 return someActivityPaused;
666 }
667
Craig Mautnerde4ef022013-04-07 19:01:33 -0700668 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700669 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800670 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
671 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
673 final ActivityStack stack = stacks.get(stackNdx);
674 final ActivityRecord r = stack.mPausingActivity;
675 if (r != null && r.state != ActivityState.PAUSED
676 && r.state != ActivityState.STOPPED
677 && r.state != ActivityState.STOPPING) {
678 if (DEBUG_STATES) {
679 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
680 pausing = false;
681 } else {
682 return false;
683 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700684 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700685 }
686 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700687 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700688 }
689
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700690 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
691 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500692 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800693 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
694 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
695 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
696 final ActivityStack stack = stacks.get(stackNdx);
697 if (stack.mResumedActivity != null &&
698 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700699 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800700 }
701 }
702 }
703 }
704
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700705 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700706 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700707 }
708
709 void sendWaitingVisibleReportLocked(ActivityRecord r) {
710 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700711 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700712 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700713 if (w.who == null) {
714 changed = true;
715 w.timeout = false;
716 if (r != null) {
717 w.who = new ComponentName(r.info.packageName, r.info.name);
718 }
719 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
720 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700721 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700722 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700723 if (changed) {
724 mService.notifyAll();
725 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700726 }
727
728 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
729 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700730 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700731 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700732 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700733 if (w.who == null) {
734 changed = true;
735 w.timeout = timeout;
736 if (r != null) {
737 w.who = new ComponentName(r.info.packageName, r.info.name);
738 }
739 w.thisTime = thisTime;
740 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700741 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700742 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700743 if (changed) {
744 mService.notifyAll();
745 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700746 }
747
Craig Mautner29219d92013-04-16 20:19:12 -0700748 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800749 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700750 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
751 if (r != null) {
752 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700753 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700754
Craig Mautner4a1cb222013-12-04 16:14:06 -0800755 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800756 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800757 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
758 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700759 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700760 r = stack.topRunningActivityLocked(null);
761 if (r != null) {
762 return r;
763 }
764 }
765 }
766 return null;
767 }
768
Dianne Hackborn09233282014-04-30 11:33:59 -0700769 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700770 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
772 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800773 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800775 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
777 final ActivityStack stack = stacks.get(stackNdx);
778 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
779 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700780 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700781 }
782 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700783
784 // The lists are already sorted from most recent to oldest. Just pull the most recent off
785 // each list and add it to list. Stop when all lists are empty or maxNum reached.
786 while (maxNum > 0) {
787 long mostRecentActiveTime = Long.MIN_VALUE;
788 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800789 final int numTaskLists = runningTaskLists.size();
790 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
791 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700792 if (!stackTaskList.isEmpty()) {
793 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
794 if (lastActiveTime > mostRecentActiveTime) {
795 mostRecentActiveTime = lastActiveTime;
796 selectedStackList = stackTaskList;
797 }
798 }
799 }
800 if (selectedStackList != null) {
801 list.add(selectedStackList.remove(0));
802 --maxNum;
803 } else {
804 break;
805 }
806 }
Craig Mautner20e72272013-04-01 13:45:53 -0700807 }
808
Craig Mautner23ac33b2013-04-01 16:26:35 -0700809 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700810 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700811 // Collect information about the target of the Intent.
812 ActivityInfo aInfo;
813 try {
814 ResolveInfo rInfo =
815 AppGlobals.getPackageManager().resolveIntent(
816 intent, resolvedType,
817 PackageManager.MATCH_DEFAULT_ONLY
818 | ActivityManagerService.STOCK_PM_FLAGS, userId);
819 aInfo = rInfo != null ? rInfo.activityInfo : null;
820 } catch (RemoteException e) {
821 aInfo = null;
822 }
823
824 if (aInfo != null) {
825 // Store the found target back into the intent, because now that
826 // we have it we never want to do this again. For example, if the
827 // user navigates back to this point in the history, we should
828 // always restart the exact same activity.
829 intent.setComponent(new ComponentName(
830 aInfo.applicationInfo.packageName, aInfo.name));
831
832 // Don't debug things in the system process
833 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
834 if (!aInfo.processName.equals("system")) {
835 mService.setDebugApp(aInfo.processName, true, false);
836 }
837 }
838
839 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
840 if (!aInfo.processName.equals("system")) {
841 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
842 }
843 }
844
Jeff Hao1b012d32014-08-20 10:35:34 -0700845 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700846 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700847 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700848 }
849 }
850 }
851 return aInfo;
852 }
853
Craig Mautner299f9602015-01-26 09:47:33 -0800854 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
855 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700856 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
857 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700858 }
859
Craig Mautner23ac33b2013-04-01 16:26:35 -0700860 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700861 String callingPackage, Intent intent, String resolvedType,
862 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700863 IBinder resultTo, String resultWho, int requestCode, int startFlags,
864 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700865 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700866 // Refuse possible leaked file descriptors
867 if (intent != null && intent.hasFileDescriptors()) {
868 throw new IllegalArgumentException("File descriptors passed in Intent");
869 }
870 boolean componentSpecified = intent.getComponent() != null;
871
872 // Don't modify the client's object!
873 intent = new Intent(intent);
874
875 // Collect information about the target of the Intent.
876 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700877 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700878
Craig Mautnere0a38842013-12-16 16:14:02 -0800879 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700880 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800881 if (container != null && container.mParentActivity != null &&
882 container.mParentActivity.state != ActivityState.RESUMED) {
883 // Cannot start a child activity if the parent is not resumed.
884 return ActivityManager.START_CANCELED;
885 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700886 final int realCallingPid = Binder.getCallingPid();
887 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700888 int callingPid;
889 if (callingUid >= 0) {
890 callingPid = -1;
891 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700892 callingPid = realCallingPid;
893 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700894 } else {
895 callingPid = callingUid = -1;
896 }
897
Craig Mautnere0a38842013-12-16 16:14:02 -0800898 final ActivityStack stack;
899 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800900 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800901 } else {
902 stack = container.mStack;
903 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800904 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700905 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700906 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700907
908 final long origId = Binder.clearCallingIdentity();
909
910 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800911 (aInfo.applicationInfo.privateFlags
912 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700913 // This may be a heavy-weight process! Check to see if we already
914 // have another, different heavy-weight process running.
915 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
916 if (mService.mHeavyWeightProcess != null &&
917 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
918 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700919 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700920 if (caller != null) {
921 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
922 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700923 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924 } else {
925 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700926 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700927 + intent.toString());
928 ActivityOptions.abort(options);
929 return ActivityManager.START_PERMISSION_DENIED;
930 }
931 }
932
933 IIntentSender target = mService.getIntentSenderLocked(
934 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700935 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700936 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
937 | PendingIntent.FLAG_ONE_SHOT, null);
938
939 Intent newIntent = new Intent();
940 if (requestCode >= 0) {
941 // Caller is requesting a result.
942 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
943 }
944 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
945 new IntentSender(target));
946 if (mService.mHeavyWeightProcess.activities.size() > 0) {
947 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
948 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
949 hist.packageName);
950 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
951 hist.task.taskId);
952 }
953 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
954 aInfo.packageName);
955 newIntent.setFlags(intent.getFlags());
956 newIntent.setClassName("android",
957 HeavyWeightSwitcherActivity.class.getName());
958 intent = newIntent;
959 resolvedType = null;
960 caller = null;
961 callingUid = Binder.getCallingUid();
962 callingPid = Binder.getCallingPid();
963 componentSpecified = true;
964 try {
965 ResolveInfo rInfo =
966 AppGlobals.getPackageManager().resolveIntent(
967 intent, null,
968 PackageManager.MATCH_DEFAULT_ONLY
969 | ActivityManagerService.STOCK_PM_FLAGS, userId);
970 aInfo = rInfo != null ? rInfo.activityInfo : null;
971 aInfo = mService.getActivityInfoForUser(aInfo, userId);
972 } catch (RemoteException e) {
973 aInfo = null;
974 }
975 }
976 }
977 }
978
Dianne Hackborn91097de2014-04-04 18:02:06 -0700979 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
980 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700981 requestCode, callingPid, callingUid, callingPackage,
982 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700983 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700984
Craig Mautner85c11a82014-07-17 12:38:18 -0700985 Binder.restoreCallingIdentity(origId);
986
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700988 // If the caller also wants to switch to a new configuration,
989 // do so now. This allows a clean switch, as we are waiting
990 // for the current activity to pause (so we will not destroy
991 // it), and have not yet started the next activity.
992 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
993 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700994 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700995 if (DEBUG_CONFIGURATION) Slog.v(TAG,
996 "Updating to new configuration after starting activity.");
997 mService.updateConfigurationLocked(config, null, false, false);
998 }
999
Craig Mautner23ac33b2013-04-01 16:26:35 -07001000 if (outResult != null) {
1001 outResult.result = res;
1002 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001003 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001004 do {
1005 try {
1006 mService.wait();
1007 } catch (InterruptedException e) {
1008 }
1009 } while (!outResult.timeout && outResult.who == null);
1010 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001011 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001012 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001013 outResult.timeout = false;
1014 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1015 outResult.totalTime = 0;
1016 outResult.thisTime = 0;
1017 } else {
1018 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001019 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001020 do {
1021 try {
1022 mService.wait();
1023 } catch (InterruptedException e) {
1024 }
1025 } while (!outResult.timeout && outResult.who == null);
1026 }
1027 }
1028 }
1029
1030 return res;
1031 }
1032 }
1033
1034 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1035 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1036 Bundle options, int userId) {
1037 if (intents == null) {
1038 throw new NullPointerException("intents is null");
1039 }
1040 if (resolvedTypes == null) {
1041 throw new NullPointerException("resolvedTypes is null");
1042 }
1043 if (intents.length != resolvedTypes.length) {
1044 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1045 }
1046
Craig Mautner23ac33b2013-04-01 16:26:35 -07001047
1048 int callingPid;
1049 if (callingUid >= 0) {
1050 callingPid = -1;
1051 } else if (caller == null) {
1052 callingPid = Binder.getCallingPid();
1053 callingUid = Binder.getCallingUid();
1054 } else {
1055 callingPid = callingUid = -1;
1056 }
1057 final long origId = Binder.clearCallingIdentity();
1058 try {
1059 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001060 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001061 for (int i=0; i<intents.length; i++) {
1062 Intent intent = intents[i];
1063 if (intent == null) {
1064 continue;
1065 }
1066
1067 // Refuse possible leaked file descriptors
1068 if (intent != null && intent.hasFileDescriptors()) {
1069 throw new IllegalArgumentException("File descriptors passed in Intent");
1070 }
1071
1072 boolean componentSpecified = intent.getComponent() != null;
1073
1074 // Don't modify the client's object!
1075 intent = new Intent(intent);
1076
1077 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001078 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001079 // TODO: New, check if this is correct
1080 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1081
1082 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001083 (aInfo.applicationInfo.privateFlags
1084 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001085 throw new IllegalArgumentException(
1086 "FLAG_CANT_SAVE_STATE not supported here");
1087 }
1088
1089 Bundle theseOptions;
1090 if (options != null && i == intents.length-1) {
1091 theseOptions = options;
1092 } else {
1093 theseOptions = null;
1094 }
Craig Mautner6170f732013-04-02 13:05:23 -07001095 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001096 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1097 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001098 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001099 if (res < 0) {
1100 return res;
1101 }
1102
1103 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1104 }
1105 }
1106 } finally {
1107 Binder.restoreCallingIdentity(origId);
1108 }
1109
1110 return ActivityManager.START_SUCCESS;
1111 }
1112
Craig Mautner2420ead2013-04-01 17:13:20 -07001113 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001114 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001115 throws RemoteException {
1116
1117 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001118 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001119 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001120
1121 // schedule launch ticks to collect information about slow apps.
1122 r.startLaunchTickingLocked();
1123
1124 // Have the window manager re-evaluate the orientation of
1125 // the screen based on the new activity order. Note that
1126 // as a result of this, it can call back into the activity
1127 // manager with a new orientation. We don't care about that,
1128 // because the activity is not currently running so we are
1129 // just restarting it anyway.
1130 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001131 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001132 mService.mConfiguration,
1133 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1134 mService.updateConfigurationLocked(config, r, false, false);
1135 }
1136
1137 r.app = app;
1138 app.waitingToKill = null;
1139 r.launchCount++;
1140 r.lastLaunchTime = SystemClock.uptimeMillis();
1141
1142 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1143
1144 int idx = app.activities.indexOf(r);
1145 if (idx < 0) {
1146 app.activities.add(r);
1147 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001148 mService.updateLruProcessLocked(app, true, null);
1149 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001150
1151 final ActivityStack stack = r.task.stack;
1152 try {
1153 if (app.thread == null) {
1154 throw new RemoteException();
1155 }
1156 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001157 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001158 if (andResume) {
1159 results = r.results;
1160 newIntents = r.newIntents;
1161 }
1162 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1163 + " icicle=" + r.icicle
1164 + " with results=" + results + " newIntents=" + newIntents
1165 + " andResume=" + andResume);
1166 if (andResume) {
1167 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1168 r.userId, System.identityHashCode(r),
1169 r.task.taskId, r.shortComponentName);
1170 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001171 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001172 // Home process is the root process of the task.
1173 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001174 }
1175 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1176 r.sleeping = false;
1177 r.forceNewConfig = false;
1178 mService.showAskCompatModeDialogLocked(r);
1179 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1180 String profileFile = null;
1181 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001182 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1183 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1184 mService.mProfileProc = app;
1185 profileFile = mService.mProfileFile;
1186 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001187 }
1188 }
1189 app.hasShownUi = true;
1190 app.pendingUiClean = true;
1191 if (profileFd != null) {
1192 try {
1193 profileFd = profileFd.dup();
1194 } catch (IOException e) {
1195 if (profileFd != null) {
1196 try {
1197 profileFd.close();
1198 } catch (IOException o) {
1199 }
1200 profileFd = null;
1201 }
1202 }
1203 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001204
Jeff Hao1b012d32014-08-20 10:35:34 -07001205 ProfilerInfo profilerInfo = profileFile != null
1206 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1207 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001208 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001209 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001210 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001211 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1212 r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1213 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001214
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001215 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001216 // This may be a heavy-weight process! Note that the package
1217 // manager will ensure that only activity can run in the main
1218 // process of the .apk, which is the only thing that will be
1219 // considered heavy-weight.
1220 if (app.processName.equals(app.info.packageName)) {
1221 if (mService.mHeavyWeightProcess != null
1222 && mService.mHeavyWeightProcess != app) {
1223 Slog.w(TAG, "Starting new heavy weight process " + app
1224 + " when already running "
1225 + mService.mHeavyWeightProcess);
1226 }
1227 mService.mHeavyWeightProcess = app;
1228 Message msg = mService.mHandler.obtainMessage(
1229 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1230 msg.obj = r;
1231 mService.mHandler.sendMessage(msg);
1232 }
1233 }
1234
1235 } catch (RemoteException e) {
1236 if (r.launchFailed) {
1237 // This is the second time we failed -- finish activity
1238 // and give up.
1239 Slog.e(TAG, "Second failure launching "
1240 + r.intent.getComponent().flattenToShortString()
1241 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001242 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001243 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1244 "2nd-crash", false);
1245 return false;
1246 }
1247
1248 // This is the first time we failed -- restart process and
1249 // retry.
1250 app.activities.remove(r);
1251 throw e;
1252 }
1253
1254 r.launchFailed = false;
1255 if (stack.updateLRUListLocked(r)) {
1256 Slog.w(TAG, "Activity " + r
1257 + " being launched, but already in LRU list");
1258 }
1259
1260 if (andResume) {
1261 // As part of the process of launching, ActivityThread also performs
1262 // a resume.
1263 stack.minimalResumeActivityLocked(r);
1264 } else {
1265 // This activity is not starting in the resumed state... which
1266 // should look like we asked it to pause+stop (but remain visible),
1267 // and it has done so and reported back the current icicle and
1268 // other state.
1269 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1270 + " (starting in stopped state)");
1271 r.state = ActivityState.STOPPED;
1272 r.stopped = true;
1273 }
1274
1275 // Launch the new version setup screen if needed. We do this -after-
1276 // launching the initial activity (that is, home), so that it can have
1277 // a chance to initialize itself while in the background, making the
1278 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001279 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 mService.startSetupActivityLocked();
1281 }
1282
Dianne Hackborn465fa392014-09-14 14:21:18 -07001283 // Update any services we are bound to that might care about whether
1284 // their client may have activities.
1285 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1286
Craig Mautner2420ead2013-04-01 17:13:20 -07001287 return true;
1288 }
1289
Craig Mautnere79d42682013-04-01 19:01:53 -07001290 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001291 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001292 // Is this activity's application already running?
1293 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001294 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001295
1296 r.task.stack.setLaunchTime(r);
1297
1298 if (app != null && app.thread != null) {
1299 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001300 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1301 || !"android".equals(r.info.packageName)) {
1302 // Don't add this if it is a platform component that is marked
1303 // to run in multiple processes, because this is actually
1304 // part of the framework so doesn't make sense to track as a
1305 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001306 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1307 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001308 }
George Mount2c92c972014-03-20 09:38:23 -07001309 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001310 return;
1311 } catch (RemoteException e) {
1312 Slog.w(TAG, "Exception when starting activity "
1313 + r.intent.getComponent().flattenToShortString(), e);
1314 }
1315
1316 // If a dead object exception was thrown -- fall through to
1317 // restart the application.
1318 }
1319
1320 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001321 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001322 }
1323
Craig Mautner6170f732013-04-02 13:05:23 -07001324 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001325 Intent intent, String resolvedType, ActivityInfo aInfo,
1326 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1327 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001328 int callingPid, int callingUid, String callingPackage,
1329 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001330 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1331 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001332 int err = ActivityManager.START_SUCCESS;
1333
1334 ProcessRecord callerApp = null;
1335 if (caller != null) {
1336 callerApp = mService.getRecordForAppLocked(caller);
1337 if (callerApp != null) {
1338 callingPid = callerApp.pid;
1339 callingUid = callerApp.info.uid;
1340 } else {
1341 Slog.w(TAG, "Unable to find app for caller " + caller
1342 + " (pid=" + callingPid + ") when starting: "
1343 + intent.toString());
1344 err = ActivityManager.START_PERMISSION_DENIED;
1345 }
1346 }
1347
1348 if (err == ActivityManager.START_SUCCESS) {
1349 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1350 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001351 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001352 + " on display " + (container == null ? (mFocusedStack == null ?
1353 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1354 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1355 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001356 }
1357
1358 ActivityRecord sourceRecord = null;
1359 ActivityRecord resultRecord = null;
1360 if (resultTo != null) {
1361 sourceRecord = isInAnyStackLocked(resultTo);
1362 if (DEBUG_RESULTS) Slog.v(
1363 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1364 if (sourceRecord != null) {
1365 if (requestCode >= 0 && !sourceRecord.finishing) {
1366 resultRecord = sourceRecord;
1367 }
1368 }
1369 }
Craig Mautner6170f732013-04-02 13:05:23 -07001370
Dianne Hackborn91097de2014-04-04 18:02:06 -07001371 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001372
Dianne Hackborn95465202014-09-15 16:21:55 -07001373 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001374 // Transfer the result target from the source activity to the new
1375 // one being started, including any failures.
1376 if (requestCode >= 0) {
1377 ActivityOptions.abort(options);
1378 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1379 }
1380 resultRecord = sourceRecord.resultTo;
1381 resultWho = sourceRecord.resultWho;
1382 requestCode = sourceRecord.requestCode;
1383 sourceRecord.resultTo = null;
1384 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001385 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001386 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001387 if (sourceRecord.launchedFromUid == callingUid) {
1388 // The new activity is being launched from the same uid as the previous
1389 // activity in the flow, and asking to forward its result back to the
1390 // previous. In this case the activity is serving as a trampoline between
1391 // the two, so we also want to update its launchedFromPackage to be the
1392 // same as the previous activity. Note that this is safe, since we know
1393 // these two packages come from the same uid; the caller could just as
1394 // well have supplied that same package name itself. This specifially
1395 // deals with the case of an intent picker/chooser being launched in the app
1396 // flow to redirect to an activity picked by the user, where we want the final
1397 // activity to consider it to have been launched by the previous app activity.
1398 callingPackage = sourceRecord.launchedFromPackage;
1399 }
Craig Mautner6170f732013-04-02 13:05:23 -07001400 }
1401
1402 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1403 // We couldn't find a class that can handle the given Intent.
1404 // That's the end of that!
1405 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1406 }
1407
1408 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1409 // We couldn't find the specific class specified in the Intent.
1410 // Also the end of the line.
1411 err = ActivityManager.START_CLASS_NOT_FOUND;
1412 }
1413
Dianne Hackborn91097de2014-04-04 18:02:06 -07001414 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1415 && sourceRecord.task.voiceSession != null) {
1416 // If this activity is being launched as part of a voice session, we need
1417 // to ensure that it is safe to do so. If the upcoming activity will also
1418 // be part of the voice session, we can only launch it if it has explicitly
1419 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001420 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001421 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1422 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001423 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1424 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001425 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1426 }
1427 } catch (RemoteException e) {
1428 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1429 }
1430 }
1431 }
1432
1433 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1434 // If the caller is starting a new voice session, just make sure the target
1435 // is actually allowing it to run this way.
1436 try {
1437 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1438 intent, resolvedType)) {
1439 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1440 }
1441 } catch (RemoteException e) {
1442 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1443 }
1444 }
1445
louis_changcd5d1982014-08-01 10:09:08 +08001446 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1447
Craig Mautner6170f732013-04-02 13:05:23 -07001448 if (err != ActivityManager.START_SUCCESS) {
1449 if (resultRecord != null) {
1450 resultStack.sendActivityResultLocked(-1,
1451 resultRecord, resultWho, requestCode,
1452 Activity.RESULT_CANCELED, null);
1453 }
Craig Mautner6170f732013-04-02 13:05:23 -07001454 ActivityOptions.abort(options);
1455 return err;
1456 }
1457
1458 final int startAnyPerm = mService.checkPermission(
1459 START_ANY_ACTIVITY, callingPid, callingUid);
1460 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1461 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1462 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1463 if (resultRecord != null) {
1464 resultStack.sendActivityResultLocked(-1,
1465 resultRecord, resultWho, requestCode,
1466 Activity.RESULT_CANCELED, null);
1467 }
Craig Mautner6170f732013-04-02 13:05:23 -07001468 String msg;
1469 if (!aInfo.exported) {
1470 msg = "Permission Denial: starting " + intent.toString()
1471 + " from " + callerApp + " (pid=" + callingPid
1472 + ", uid=" + callingUid + ")"
1473 + " not exported from uid " + aInfo.applicationInfo.uid;
1474 } else {
1475 msg = "Permission Denial: starting " + intent.toString()
1476 + " from " + callerApp + " (pid=" + callingPid
1477 + ", uid=" + callingUid + ")"
1478 + " requires " + aInfo.permission;
1479 }
1480 Slog.w(TAG, msg);
1481 throw new SecurityException(msg);
1482 }
1483
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001484 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001485 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001486
Craig Mautner6170f732013-04-02 13:05:23 -07001487 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001488 try {
1489 // The Intent we give to the watcher has the extra data
1490 // stripped off, since it can contain private information.
1491 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001492 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001493 aInfo.applicationInfo.packageName);
1494 } catch (RemoteException e) {
1495 mService.mController = null;
1496 }
Ben Gruver5e207332013-04-03 17:41:37 -07001497 }
Craig Mautner6170f732013-04-02 13:05:23 -07001498
Ben Gruver5e207332013-04-03 17:41:37 -07001499 if (abort) {
1500 if (resultRecord != null) {
1501 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001502 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001503 }
Ben Gruver5e207332013-04-03 17:41:37 -07001504 // We pretend to the caller that it was really started, but
1505 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001506 ActivityOptions.abort(options);
1507 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001508 }
1509
1510 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001511 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001512 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001513 if (outActivity != null) {
1514 outActivity[0] = r;
1515 }
1516
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001517 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001518 if (voiceSession == null && (stack.mResumedActivity == null
1519 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001520 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1521 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001522 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001523 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001524 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001525 ActivityOptions.abort(options);
1526 return ActivityManager.START_SWITCHES_CANCELED;
1527 }
1528 }
1529
1530 if (mService.mDidAppSwitch) {
1531 // This is the second allowed switch since we stopped switches,
1532 // so now just generally allow switches. Use case: user presses
1533 // home (switches disabled, switch to home, mDidAppSwitch now true);
1534 // user taps a home icon (coming from home so allowed, we hit here
1535 // and now allow anyone to switch again).
1536 mService.mAppSwitchesAllowedTime = 0;
1537 } else {
1538 mService.mDidAppSwitch = true;
1539 }
1540
Craig Mautneree36c772014-07-16 14:56:05 -07001541 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001542
Dianne Hackborn91097de2014-04-04 18:02:06 -07001543 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001544 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001545
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001546 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001547 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001548 // activity start, but we are not actually doing an activity
1549 // switch... just dismiss the keyguard now, because we
1550 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001551 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001552 }
1553 return err;
1554 }
1555
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001556 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001557 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001558
1559 // On leanback only devices we should keep all activities in the same stack.
1560 if (!mLeanbackOnlyDevice &&
1561 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001562
1563 ActivityStack stack;
1564
Craig Mautnerac6f8432013-07-17 13:24:59 -07001565 if (task != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001566 stack = task.stack;
1567 if (stack.isOnHomeDisplay()) {
1568 if (mFocusedStack != stack) {
1569 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "computeStackFocus: Setting " +
Craig Mautnere0a38842013-12-16 16:14:02 -08001570 "focused stack to r=" + r + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001571 } else {
1572 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001573 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001574 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001575 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001576 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001577 }
1578
Craig Mautnere0a38842013-12-16 16:14:02 -08001579 final ActivityContainer container = r.mInitialActivityContainer;
1580 if (container != null) {
1581 // The first time put it on the desired stack, after this put on task stack.
1582 r.mInitialActivityContainer = null;
1583 return container.mStack;
1584 }
1585
Craig Mautner1b4bf852014-05-26 15:06:32 -07001586 if (mFocusedStack != mHomeStack && (!newTask ||
1587 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001588 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001589 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001590 return mFocusedStack;
1591 }
1592
Craig Mautnere0a38842013-12-16 16:14:02 -08001593 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1594 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001595 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001596 if (!stack.isHomeStack()) {
1597 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001598 "computeStackFocus: Setting focused stack=" + stack);
1599 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001600 }
1601 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001602
Craig Mautner4a1cb222013-12-04 16:14:06 -08001603 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001604 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
1605 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "computeStackFocus: New stack r=" + r +
1606 " stackId=" + stack.mStackId);
1607 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001608 }
1609 return mHomeStack;
1610 }
1611
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001612 boolean setFocusedStack(ActivityRecord r, String reason) {
1613 if (r == null) {
1614 // Not sure what you are trying to do, but it is not going to work...
1615 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001616 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001617 final TaskRecord task = r.task;
1618 if (task == null || task.stack == null) {
1619 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1620 return false;
1621 }
1622 task.stack.moveToFront(reason);
1623 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001624 }
1625
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001626 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001627 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001628 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001629 final Intent intent = r.intent;
1630 final int callingUid = r.launchedFromUid;
1631
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001632 // In some flows in to this function, we retrieve the task record and hold on to it
1633 // without a lock before calling back in to here... so the task at this point may
1634 // not actually be in recents. Check for that, and if it isn't in recents just
1635 // consider it invalid.
1636 if (inTask != null && !inTask.inRecents) {
1637 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1638 inTask = null;
1639 }
1640
Craig Mautnerad400af2014-08-13 16:41:36 -07001641 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001642 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1643 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001644
Craig Mautnera228ae92014-07-09 05:44:55 -07001645 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001646 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001647 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001648 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1649 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1650 "\"singleInstance\" or \"singleTask\"");
1651 launchFlags &=
1652 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1653 } else {
1654 switch (r.info.documentLaunchMode) {
1655 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1656 break;
1657 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1658 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1659 break;
1660 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1661 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1662 break;
1663 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1664 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1665 break;
1666 }
1667 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001668
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001669 final boolean launchTaskBehind = r.mLaunchTaskBehind
1670 && !launchSingleTask && !launchSingleInstance
1671 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001672
1673 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1674 // For whatever reason this activity is being launched into a new
1675 // task... yet the caller has requested a result back. Well, that
1676 // is pretty messed up, so instead immediately send back a cancel
1677 // and let the new task continue launched as normal without a
1678 // dependency on its originator.
1679 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1680 r.resultTo.task.stack.sendActivityResultLocked(-1,
1681 r.resultTo, r.resultWho, r.requestCode,
1682 Activity.RESULT_CANCELED, null);
1683 r.resultTo = null;
1684 }
1685
1686 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1687 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1688 }
1689
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001690 // If we are actually going to launch in to a new task, there are some cases where
1691 // we further want to do multiple task.
1692 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1693 if (launchTaskBehind
1694 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1695 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1696 }
1697 }
1698
Craig Mautner8849a5e2013-04-02 16:41:03 -07001699 // We'll invoke onUserLeaving before onPause only if the launching
1700 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001701 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001702 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001703
1704 // If the caller has asked not to resume at this point, we make note
1705 // of this in the record so that we can skip it when trying to find
1706 // the top running activity.
1707 if (!doResume) {
1708 r.delayedResume = true;
1709 }
1710
Craig Mautnera254cd72014-05-25 16:47:39 -07001711 ActivityRecord notTop =
1712 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001713
1714 // If the onlyIfNeeded flag is set, then we can do this if the activity
1715 // being launched is the same as the one making the call... or, as
1716 // a special case, if we do not know the caller then we count the
1717 // current top activity as the caller.
1718 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1719 ActivityRecord checkedCaller = sourceRecord;
1720 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001721 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001722 }
1723 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1724 // Caller is not the same as launcher, so always needed.
1725 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1726 }
1727 }
1728
Craig Mautner8849a5e2013-04-02 16:41:03 -07001729 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001730 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001731
1732 // If the caller is not coming from another activity, but has given us an
1733 // explicit task into which they would like us to launch the new activity,
1734 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001735 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1736 final Intent baseIntent = inTask.getBaseIntent();
1737 final ActivityRecord root = inTask.getRootActivity();
1738 if (baseIntent == null) {
1739 ActivityOptions.abort(options);
1740 throw new IllegalArgumentException("Launching into task without base intent: "
1741 + inTask);
1742 }
1743
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001744 // If this task is empty, then we are adding the first activity -- it
1745 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001746 if (launchSingleInstance || launchSingleTask) {
1747 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1748 ActivityOptions.abort(options);
1749 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1750 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001751 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001752 if (root != null) {
1753 ActivityOptions.abort(options);
1754 throw new IllegalArgumentException("Caller with inTask " + inTask
1755 + " has root " + root + " but target is singleInstance/Task");
1756 }
1757 }
1758
1759 // If task is empty, then adopt the interesting intent launch flags in to the
1760 // activity being started.
1761 if (root == null) {
1762 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1763 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1764 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1765 launchFlags = (launchFlags&~flagsOfInterest)
1766 | (baseIntent.getFlags()&flagsOfInterest);
1767 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001768 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001769 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001770
Dianne Hackborn962d5352014-08-29 16:27:40 -07001771 // If the task is not empty and the caller is asking to start it as the root
1772 // of a new task, then we don't actually want to start this on the task. We
1773 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001774 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001775 addingToTask = false;
1776
1777 } else {
1778 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001779 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001780
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001781 reuseTask = inTask;
1782 } else {
1783 inTask = null;
1784 }
1785
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001786 if (inTask == null) {
1787 if (sourceRecord == null) {
1788 // This activity is not being started from another... in this
1789 // case we -always- start a new task.
1790 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1791 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1792 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1793 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1794 }
1795 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1796 // The original activity who is starting us is running as a single
1797 // instance... this new activity it is starting must go on its
1798 // own task.
1799 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1800 } else if (launchSingleInstance || launchSingleTask) {
1801 // The activity being started is a single instance... it always
1802 // gets launched into its own task.
1803 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1804 }
1805 }
1806
1807 ActivityInfo newTaskInfo = null;
1808 Intent newTaskIntent = null;
1809 ActivityStack sourceStack;
1810 if (sourceRecord != null) {
1811 if (sourceRecord.finishing) {
1812 // If the source is finishing, we can't further count it as our source. This
1813 // is because the task it is associated with may now be empty and on its way out,
1814 // so we don't want to blindly throw it in to that task. Instead we will take
1815 // the NEW_TASK flow and try to find a task for it. But save the task information
1816 // so it can be used when creating the new task.
1817 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1818 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1819 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1820 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1821 newTaskInfo = sourceRecord.info;
1822 newTaskIntent = sourceRecord.task.intent;
1823 }
1824 sourceRecord = null;
1825 sourceStack = null;
1826 } else {
1827 sourceStack = sourceRecord.task.stack;
1828 }
1829 } else {
1830 sourceStack = null;
1831 }
1832
1833 boolean movedHome = false;
1834 ActivityStack targetStack;
1835
1836 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001837 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001838
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001839 // We may want to try to place the new activity in to an existing task. We always
1840 // do this if the target activity is singleTask or singleInstance; we will also do
1841 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1842 // us to still place it in a new task: multi task, always doc mode, or being asked to
1843 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001844 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1845 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001846 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001847 // If bring to front is requested, and no result is requested and we have not
1848 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001849 // we can find a task that was started with this same
1850 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001851 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001852 // See if there is a task to bring to the front. If this is
1853 // a SINGLE_INSTANCE activity, there can be one and only one
1854 // instance of it in the history, and it is always in its own
1855 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001856 ActivityRecord intentActivity = !launchSingleInstance ?
1857 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001858 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001859 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001860 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001861 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001862 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1863 }
Craig Mautner29219d92013-04-16 20:19:12 -07001864 if (r.task == null) {
1865 r.task = intentActivity.task;
1866 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001867 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001868 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001869 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1870 + " from " + intentActivity);
Craig Mautner299f9602015-01-26 09:47:33 -08001871 targetStack.moveToFront("intentActivityFound");
Craig Mautner8849a5e2013-04-02 16:41:03 -07001872 if (intentActivity.task.intent == null) {
1873 // This task was started because of movement of
1874 // the activity based on affinity... now that we
1875 // are actually launching it, we can assign the
1876 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001877 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001878 }
1879 // If the target task is not in the front, then we need
1880 // to bring it to the front... except... well, with
1881 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1882 // to have the same behavior as if a new instance was
1883 // being started, which means not bringing it to the front
1884 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001885 final ActivityStack lastStack = getLastStack();
1886 ActivityRecord curTop = lastStack == null?
1887 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001888 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001889 if (curTop != null && (curTop.task != intentActivity.task ||
1890 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001891 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001892 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1893 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001894 // We really do want to push this one into the
1895 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001896 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001897 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1898 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001899 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001900 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1901 options, "bringingFoundTaskToFront");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001902 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001903 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1904 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001905 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001906 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001907 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001908 options = null;
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001909 movedToFront = true;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001910 }
1911 }
1912 // If the caller has requested that the target task be
1913 // reset, then do so.
1914 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1915 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1916 }
1917 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1918 // We don't need to start a new activity, and
1919 // the client said not to do anything if that
1920 // is the case, so this is it! And for paranoia, make
1921 // sure we have correctly resumed the top activity.
1922 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001923 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001924
1925 // Make sure to notify Keyguard as well if we are not running an app
1926 // transition later.
1927 if (!movedToFront) {
1928 notifyActivityDrawnForKeyguard();
1929 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001930 } else {
1931 ActivityOptions.abort(options);
1932 }
1933 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1934 }
1935 if ((launchFlags &
1936 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1937 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1938 // The caller has requested to completely replace any
1939 // existing task with its new activity. Well that should
1940 // not be too hard...
1941 reuseTask = intentActivity.task;
1942 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001943 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001944 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001945 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001946 // In this situation we want to remove all activities
1947 // from the task up to the one being started. In most
1948 // cases this means we are resetting the task to its
1949 // initial state.
1950 ActivityRecord top =
1951 intentActivity.task.performClearTaskLocked(r, launchFlags);
1952 if (top != null) {
1953 if (top.frontOfTask) {
1954 // Activity aliases may mean we use different
1955 // intents for the top activity, so make sure
1956 // the task now has the identity of the new
1957 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001958 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001959 }
1960 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1961 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001962 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001963 } else {
1964 // A special case: we need to
1965 // start the activity because it is not currently
1966 // running, and the caller has asked to clear the
1967 // current task to have this activity at the top.
1968 addingToTask = true;
1969 // Now pretend like this activity is being started
1970 // by the top of its task, so it is put in the
1971 // right place.
1972 sourceRecord = intentActivity;
1973 }
1974 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1975 // In this case the top activity on the task is the
1976 // same as the one being launched, so we take that
1977 // as a request to bring the task to the foreground.
1978 // If the top activity in the task is the root
1979 // activity, deliver this new intent to it if it
1980 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001981 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001982 && intentActivity.realActivity.equals(r.realActivity)) {
1983 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1984 intentActivity.task);
1985 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001986 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001987 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001988 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
1989 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001990 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1991 // In this case we are launching the root activity
1992 // of the task, but with a different intent. We
1993 // should start a new instance on top.
1994 addingToTask = true;
1995 sourceRecord = intentActivity;
1996 }
1997 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1998 // In this case an activity is being launched in to an
1999 // existing task, without resetting that task. This
2000 // is typically the situation of launching an activity
2001 // from a notification or shortcut. We want to place
2002 // the new activity on top of the current task.
2003 addingToTask = true;
2004 sourceRecord = intentActivity;
2005 } else if (!intentActivity.task.rootWasReset) {
2006 // In this case we are launching in to an existing task
2007 // that has not yet been started from its front door.
2008 // The current task has been brought to the front.
2009 // Ideally, we'd probably like to place this new task
2010 // at the bottom of its stack, but that's a little hard
2011 // to do with the current organization of the code so
2012 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002013 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002014 }
2015 if (!addingToTask && reuseTask == null) {
2016 // We didn't do anything... but it was needed (a.k.a., client
2017 // don't use that intent!) And for paranoia, make
2018 // sure we have correctly resumed the top activity.
2019 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002020 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002021 if (!movedToFront) {
2022 // Make sure to notify Keyguard as well if we are not running an app
2023 // transition later.
2024 notifyActivityDrawnForKeyguard();
2025 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002026 } else {
2027 ActivityOptions.abort(options);
2028 }
2029 return ActivityManager.START_TASK_TO_FRONT;
2030 }
2031 }
2032 }
2033 }
2034
2035 //String uri = r.intent.toURI();
2036 //Intent intent2 = new Intent(uri);
2037 //Slog.i(TAG, "Given intent: " + r.intent);
2038 //Slog.i(TAG, "URI is: " + uri);
2039 //Slog.i(TAG, "To intent: " + intent2);
2040
2041 if (r.packageName != null) {
2042 // If the activity being launched is the same as the one currently
2043 // at the top, then we need to check if it should only be launched
2044 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002045 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002046 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002047 if (top != null && r.resultTo == null) {
2048 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2049 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002050 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002051 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002052 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2053 top.task);
2054 // For paranoia, make sure we have correctly
2055 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002056 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002057 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002058 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002059 }
2060 ActivityOptions.abort(options);
2061 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2062 // We don't need to start a new activity, and
2063 // the client said not to do anything if that
2064 // is the case, so this is it!
2065 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2066 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002067 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002068 return ActivityManager.START_DELIVERED_TO_TOP;
2069 }
2070 }
2071 }
2072 }
2073
2074 } else {
2075 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002076 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2077 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002078 }
2079 ActivityOptions.abort(options);
2080 return ActivityManager.START_CLASS_NOT_FOUND;
2081 }
2082
2083 boolean newTask = false;
2084 boolean keepCurTransition = false;
2085
Craig Mautnerbb742462014-07-07 15:28:55 -07002086 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002087 sourceRecord.task : null;
2088
Craig Mautner8849a5e2013-04-02 16:41:03 -07002089 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002090 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002091 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002092 if (isLockTaskModeViolation(reuseTask)) {
2093 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2094 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2095 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002096 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002097 targetStack = computeStackFocus(r, newTask);
2098 targetStack.moveToFront("startingNewTask");
2099
Craig Mautner8849a5e2013-04-02 16:41:03 -07002100 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002101 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2102 newTaskInfo != null ? newTaskInfo : r.info,
2103 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002104 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2105 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002106 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2107 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002108 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002109 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002110 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002111 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002112 if ((launchFlags &
2113 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2114 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2115 // Caller wants to appear on home activity, so before starting
2116 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002117 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002118 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002119 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002120 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002121 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002122 if (isLockTaskModeViolation(sourceTask)) {
2123 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2124 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2125 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002126 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002127 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002128 final TaskRecord topTask = targetStack.topTask();
2129 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002130 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2131 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002132 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002133 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002134 // In this case, we are adding the activity to an existing
2135 // task, but the caller has asked to clear that task if the
2136 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002137 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002138 keepCurTransition = true;
2139 if (top != null) {
2140 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002141 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002142 // For paranoia, make sure we have correctly
2143 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002144 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002145 if (doResume) {
2146 targetStack.resumeTopActivityLocked(null);
2147 }
2148 ActivityOptions.abort(options);
2149 return ActivityManager.START_DELIVERED_TO_TOP;
2150 }
2151 } else if (!addingToTask &&
2152 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2153 // In this case, we are launching an activity in our own task
2154 // that may already be running somewhere in the history, and
2155 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002156 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002157 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002158 final TaskRecord task = top.task;
2159 task.moveActivityToFrontLocked(top);
2160 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002161 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002162 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002163 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002164 if (doResume) {
2165 targetStack.resumeTopActivityLocked(null);
2166 }
2167 return ActivityManager.START_DELIVERED_TO_TOP;
2168 }
2169 }
2170 // An existing activity is starting this new activity, so we want
2171 // to keep the new one in the same task as the one that is starting
2172 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002173 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002174 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002175 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002176
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002177 } else if (inTask != null) {
2178 // The calling is asking that the new activity be started in an explicit
2179 // task it has provided to us.
2180 if (isLockTaskModeViolation(inTask)) {
2181 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2182 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2183 }
2184 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002185 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002186
2187 // Check whether we should actually launch the new activity in to the task,
2188 // or just reuse the current activity on top.
2189 ActivityRecord top = inTask.getTopActivity();
2190 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2191 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2192 || launchSingleTop || launchSingleTask) {
2193 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2194 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2195 // We don't need to start a new activity, and
2196 // the client said not to do anything if that
2197 // is the case, so this is it!
2198 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2199 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002200 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002201 return ActivityManager.START_DELIVERED_TO_TOP;
2202 }
2203 }
2204
Dianne Hackborn962d5352014-08-29 16:27:40 -07002205 if (!addingToTask) {
2206 // We don't actually want to have this activity added to the task, so just
2207 // stop here but still tell the caller that we consumed the intent.
2208 ActivityOptions.abort(options);
2209 return ActivityManager.START_TASK_TO_FRONT;
2210 }
2211
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002212 r.setTask(inTask, null);
2213 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2214 + " in explicit task " + r.task);
2215
Craig Mautner8849a5e2013-04-02 16:41:03 -07002216 } else {
2217 // This not being started from an existing activity, and not part
2218 // of a new task... just put it in the top task, though these days
2219 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002220 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002221 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002222 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002223 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002224 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002225 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002226 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2227 + " in new guessed " + r.task);
2228 }
2229
2230 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002231 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002232
Craig Mautner76e2a762014-06-24 09:05:43 -07002233 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2234 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2235 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002236 if (newTask) {
2237 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2238 }
2239 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002240 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002241 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002242 if (!launchTaskBehind) {
2243 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002244 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002245 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002246 return ActivityManager.START_SUCCESS;
2247 }
2248
Craig Mautneree36c772014-07-16 14:56:05 -07002249 final void doPendingActivityLaunchesLocked(boolean doResume) {
2250 while (!mPendingActivityLaunches.isEmpty()) {
2251 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002252 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002253 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002254 }
2255 }
2256
Craig Mautner7f13ed32014-07-28 14:00:35 -07002257 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002258 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2259 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002260 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002261 mPendingActivityLaunches.remove(palNdx);
2262 }
2263 }
2264 }
2265
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002266 void acquireLaunchWakelock() {
2267 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2268 throw new IllegalStateException("Calling must be system uid");
2269 }
2270 mLaunchingActivity.acquire();
2271 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2272 // To be safe, don't allow the wake lock to be held for too long.
2273 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2274 }
2275 }
2276
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002277 /**
2278 * Called when the frontmost task is idle.
2279 * @return the state of mService.mBooting before this was called.
2280 */
2281 private boolean checkFinishBootingLocked() {
2282 final boolean booting = mService.mBooting;
2283 boolean enableScreen = false;
2284 mService.mBooting = false;
2285 if (!mService.mBooted) {
2286 mService.mBooted = true;
2287 enableScreen = true;
2288 }
2289 if (booting || enableScreen) {
2290 mService.postFinishBooting(booting, enableScreen);
2291 }
2292 return booting;
2293 }
2294
Craig Mautnerf3333272013-04-22 10:55:53 -07002295 // Checked.
2296 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2297 Configuration config) {
2298 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2299
Craig Mautnerf3333272013-04-22 10:55:53 -07002300 ArrayList<ActivityRecord> stops = null;
2301 ArrayList<ActivityRecord> finishes = null;
2302 ArrayList<UserStartedState> startingUsers = null;
2303 int NS = 0;
2304 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002305 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002306 boolean activityRemoved = false;
2307
2308 ActivityRecord r = ActivityRecord.forToken(token);
2309 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002310 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2311 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002312 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2313 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002314 if (fromTimeout) {
2315 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002316 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002317
2318 // This is a hack to semi-deal with a race condition
2319 // in the client where it can be constructed with a
2320 // newer configuration from when we asked it to launch.
2321 // We'll update with whatever configuration it now says
2322 // it used to launch.
2323 if (config != null) {
2324 r.configuration = config;
2325 }
2326
2327 // We are now idle. If someone is waiting for a thumbnail from
2328 // us, we can now deliver.
2329 r.idle = true;
2330
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002331 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002332 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002333 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002334 }
2335 }
2336
2337 if (allResumedActivitiesIdle()) {
2338 if (r != null) {
2339 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002340 }
2341
2342 if (mLaunchingActivity.isHeld()) {
2343 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2344 if (VALIDATE_WAKE_LOCK_CALLER &&
2345 Binder.getCallingUid() != Process.myUid()) {
2346 throw new IllegalStateException("Calling must be system uid");
2347 }
2348 mLaunchingActivity.release();
2349 }
2350 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002351 }
2352
2353 // Atomically retrieve all of the other things to do.
2354 stops = processStoppingActivitiesLocked(true);
2355 NS = stops != null ? stops.size() : 0;
2356 if ((NF=mFinishingActivities.size()) > 0) {
2357 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2358 mFinishingActivities.clear();
2359 }
2360
Craig Mautnerf3333272013-04-22 10:55:53 -07002361 if (mStartingUsers.size() > 0) {
2362 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2363 mStartingUsers.clear();
2364 }
2365
Craig Mautnerf3333272013-04-22 10:55:53 -07002366 // Stop any activities that are scheduled to do so but have been
2367 // waiting for the next one to start.
2368 for (int i = 0; i < NS; i++) {
2369 r = stops.get(i);
2370 final ActivityStack stack = r.task.stack;
2371 if (r.finishing) {
2372 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2373 } else {
2374 stack.stopActivityLocked(r);
2375 }
2376 }
2377
2378 // Finish any activities that are scheduled to do so but have been
2379 // waiting for the next one to start.
2380 for (int i = 0; i < NF; i++) {
2381 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002382 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002383 }
2384
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002385 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002386 // Complete user switch
2387 if (startingUsers != null) {
2388 for (int i = 0; i < startingUsers.size(); i++) {
2389 mService.finishUserSwitch(startingUsers.get(i));
2390 }
2391 }
2392 // Complete starting up of background users
2393 if (mStartingBackgroundUsers.size() > 0) {
2394 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2395 mStartingBackgroundUsers.clear();
2396 for (int i = 0; i < startingUsers.size(); i++) {
2397 mService.finishUserBoot(startingUsers.get(i));
2398 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002399 }
2400 }
2401
2402 mService.trimApplications();
2403 //dump();
2404 //mWindowManager.dump();
2405
Craig Mautnerf3333272013-04-22 10:55:53 -07002406 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002407 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002408 }
2409
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002410 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002411 }
2412
Craig Mautner8e569572013-10-11 17:36:59 -07002413 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002414 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002415 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2416 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002417 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2418 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2419 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002420 }
Craig Mautner19091252013-10-05 00:03:53 -07002421 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002422 }
2423
2424 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002425 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2426 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002427 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2428 stacks.get(stackNdx).closeSystemDialogsLocked();
2429 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002430 }
2431 }
2432
Craig Mautner93529a42013-10-04 15:03:13 -07002433 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002434 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002435 }
2436
Craig Mautner8d341ef2013-03-26 09:03:27 -07002437 /**
2438 * @return true if some activity was finished (or would have finished if doit were true).
2439 */
2440 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2441 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002442 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2443 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002444 final int numStacks = stacks.size();
2445 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2446 final ActivityStack stack = stacks.get(stackNdx);
2447 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2448 didSomething = true;
2449 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002450 }
2451 }
2452 return didSomething;
2453 }
2454
Dianne Hackborna413dc02013-07-12 12:02:55 -07002455 void updatePreviousProcessLocked(ActivityRecord r) {
2456 // Now that this process has stopped, we may want to consider
2457 // it to be the previous app to try to keep around in case
2458 // the user wants to return to it.
2459
2460 // First, found out what is currently the foreground app, so that
2461 // we don't blow away the previous app if this activity is being
2462 // hosted by the process that is actually still the foreground.
2463 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002464 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2465 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002466 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2467 final ActivityStack stack = stacks.get(stackNdx);
2468 if (isFrontStack(stack)) {
2469 if (stack.mResumedActivity != null) {
2470 fgApp = stack.mResumedActivity.app;
2471 } else if (stack.mPausingActivity != null) {
2472 fgApp = stack.mPausingActivity.app;
2473 }
2474 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002475 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002476 }
2477 }
2478
2479 // Now set this one as the previous process, only if that really
2480 // makes sense to.
2481 if (r.app != null && fgApp != null && r.app != fgApp
2482 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002483 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002484 mService.mPreviousProcess = r.app;
2485 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2486 }
2487 }
2488
Craig Mautner05d29032013-05-03 13:40:13 -07002489 boolean resumeTopActivitiesLocked() {
2490 return resumeTopActivitiesLocked(null, null, null);
2491 }
2492
2493 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2494 Bundle targetOptions) {
2495 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002496 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002497 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002498 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002499 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002500 if (isFrontStack(targetStack)) {
2501 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2502 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002503
Craig Mautnere0a38842013-12-16 16:14:02 -08002504 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2505 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002506 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2507 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002508 if (stack == targetStack) {
2509 // Already started above.
2510 continue;
2511 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002512 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002513 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002514 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002515 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002516 }
Craig Mautner05d29032013-05-03 13:40:13 -07002517 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002518 }
2519
Todd Kennedy539db512014-12-15 09:57:55 -08002520 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002521 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2522 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002523 final int numStacks = stacks.size();
2524 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2525 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002526 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002527 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002528 }
2529 }
2530
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002531 void finishVoiceTask(IVoiceInteractionSession session) {
2532 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2533 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2534 final int numStacks = stacks.size();
2535 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2536 final ActivityStack stack = stacks.get(stackNdx);
2537 stack.finishVoiceTask(session);
2538 }
2539 }
2540 }
2541
Craig Mautner299f9602015-01-26 09:47:33 -08002542 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002543 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2544 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002545 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002546 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2547 // Caller wants the home activity moved with it. To accomplish this,
2548 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002549 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002550 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002551 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08002552 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2553 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002554 }
2555
Craig Mautner967212c2013-04-13 21:10:58 -07002556 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002557 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2558 if (activityContainer != null) {
2559 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002560 }
2561 return null;
2562 }
2563
Craig Mautner967212c2013-04-13 21:10:58 -07002564 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002565 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002566 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2567 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002568 }
2569 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002570 }
2571
Craig Mautner4a1cb222013-12-04 16:14:06 -08002572 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002573 ActivityRecord homeActivity = getHomeActivity();
2574 if (homeActivity != null) {
2575 return homeActivity.appToken;
2576 }
2577 return null;
2578 }
2579
2580 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002581 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2582 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2583 final TaskRecord task = tasks.get(taskNdx);
2584 if (task.isHomeTask()) {
2585 final ArrayList<ActivityRecord> activities = task.mActivities;
2586 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2587 final ActivityRecord r = activities.get(activityNdx);
2588 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002589 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002590 }
2591 }
2592 }
2593 }
2594 return null;
2595 }
2596
Todd Kennedyca4d8422015-01-15 15:19:22 -08002597 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002598 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002599 ActivityContainer activityContainer =
2600 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002601 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002602 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002603 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002604 return activityContainer;
2605 }
2606
Craig Mautner34b73df2014-01-12 21:11:08 -08002607 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002608 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2609 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2610 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002611 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2612 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002613 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002614 }
2615 }
2616
Craig Mautner95da1082014-02-24 17:54:35 -08002617 void deleteActivityContainer(IActivityContainer container) {
2618 ActivityContainer activityContainer = (ActivityContainer)container;
2619 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002620 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2621 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002622 final int stackId = activityContainer.mStackId;
2623 mActivityContainers.remove(stackId);
2624 mWindowManager.removeStack(stackId);
2625 }
2626 }
2627
Wale Ogunwale60454db2015-01-23 16:05:07 -08002628 void resizeStackLocked(int stackId, Rect bounds) {
2629 final ActivityStack stack = getStack(stackId);
2630 if (stack == null) {
2631 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2632 return;
2633 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002634
2635 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002636 if (r != null && !r.task.mResizeable) {
2637 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002638 return;
2639 }
2640
Wale Ogunwale60454db2015-01-23 16:05:07 -08002641 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2642 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002643 if (r != null) {
2644 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2645 // And we need to make sure at this point that all other activities
2646 // are made visible with the correct configuration.
2647 ensureActivitiesVisibleLocked(r, 0);
2648 if (!updated) {
2649 resumeTopActivitiesLocked(stack, null, null);
2650 }
2651 }
2652 }
2653 }
2654
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002655 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2656 * current task stack if it only has one entry, moving the task to a stack that matches the
2657 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2658 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2659 task.mResizeable = true;
2660 final ActivityStack currentStack = task.stack;
2661 if (currentStack.isHomeStack()) {
2662 // Can't move task off the home stack. Sorry!
2663 return;
2664 }
2665
2666 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2667 if (matchingStackId != -1) {
2668 // There is already a stack with the right bounds!
2669 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2670 // Nothing to do here. Already in the right stack...
2671 return;
2672 }
2673 // Move task to stack with matching bounds.
2674 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2675 return;
2676 }
2677
2678 if (currentStack != null && currentStack.numTasks() == 1) {
2679 // Just resize the current stack since this is the task in it.
2680 resizeStackLocked(currentStack.mStackId, bounds);
2681 return;
2682 }
2683
2684 // Create new stack and move the task to it.
2685 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2686 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2687 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2688
2689 if (newStack == null) {
2690 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2691 return;
2692 }
2693 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2694 resizeStackLocked(newStack.mStackId, bounds);
2695 }
2696
Todd Kennedy4900bf92015-01-16 16:05:14 -08002697 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002698 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2699 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002700 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002701 }
2702
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002703 ActivityContainer activityContainer = new ActivityContainer(stackId);
2704 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002705 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002706 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002707 }
2708
2709 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002710 while (true) {
2711 if (++mLastStackId <= HOME_STACK_ID) {
2712 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002713 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002714 if (getStack(mLastStackId) == null) {
2715 break;
2716 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002717 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002718 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002719 }
2720
Wale Ogunwale7de05352014-12-12 15:21:33 -08002721 private boolean restoreRecentTaskLocked(TaskRecord task) {
2722 ActivityStack stack = null;
2723 // Determine stack to restore task to.
2724 if (mLeanbackOnlyDevice) {
2725 // There is only one stack for lean back devices.
2726 stack = mHomeStack;
2727 } else {
2728 // Look for the top stack on the home display that isn't the home stack.
2729 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2730 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2731 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -08002732 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002733 stack = tmpStack;
2734 break;
2735 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002736 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002737 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002738
2739 if (stack == null) {
2740 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2741 // before an application stack is created...Go ahead and create one on the default
2742 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002743 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002744 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002745 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002746 if (DEBUG_RECENTS)
2747 Slog.v(TAG, "Created stack=" + stack + " for recents restoration.");
2748 }
2749
2750 if (stack == null) {
2751 // What does this mean??? Not sure how we would get here...
2752 if (DEBUG_RECENTS)
2753 Slog.v(TAG, "Unable to find/create stack to restore recent task=" + task);
2754 return false;
2755 }
2756
2757 stack.addTask(task, false, false);
2758 if (DEBUG_RECENTS)
2759 Slog.v(TAG, "Added restored task=" + task + " to stack=" + stack);
2760 final ArrayList<ActivityRecord> activities = task.mActivities;
2761 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2762 final ActivityRecord r = activities.get(activityNdx);
2763 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2764 r.info.screenOrientation, r.fullscreen,
2765 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2766 r.userId, r.info.configChanges, task.voiceSession != null,
2767 r.mLaunchTaskBehind);
2768 }
2769 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002770 }
2771
Craig Mautner299f9602015-01-26 09:47:33 -08002772 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002773 final TaskRecord task = anyTaskForIdLocked(taskId);
2774 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002775 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002776 return;
2777 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002778 final ActivityStack stack = getStack(stackId);
2779 if (stack == null) {
2780 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2781 return;
2782 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002783 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2784 if (task.stack != null) {
2785 task.stack.removeTask(task, "moveTaskToStack", false);
2786 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002787 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002788 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002789 }
2790
Craig Mautnerac6f8432013-07-17 13:24:59 -07002791 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002792 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002793 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2794 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002795 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2796 final ActivityStack stack = stacks.get(stackNdx);
2797 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002798 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2799 continue;
2800 }
2801 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2802 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2803 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002804 continue;
2805 }
2806 final ActivityRecord ar = stack.findTaskLocked(r);
2807 if (ar != null) {
2808 return ar;
2809 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002810 }
2811 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002812 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002813 return null;
2814 }
2815
2816 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002817 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2818 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002819 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2820 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2821 if (ar != null) {
2822 return ar;
2823 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002824 }
2825 }
2826 return null;
2827 }
2828
Craig Mautner8d341ef2013-03-26 09:03:27 -07002829 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002830 scheduleSleepTimeout();
2831 if (!mGoingToSleep.isHeld()) {
2832 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002833 if (mLaunchingActivity.isHeld()) {
2834 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2835 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002836 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002837 mLaunchingActivity.release();
2838 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002839 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002840 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002841 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002842 }
2843
2844 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002845 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002846
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002847 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002848 final long endTime = System.currentTimeMillis() + timeout;
2849 while (true) {
2850 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002851 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2852 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002853 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2854 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2855 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002856 }
2857 if (cantShutdown) {
2858 long timeRemaining = endTime - System.currentTimeMillis();
2859 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002860 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002861 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002862 } catch (InterruptedException e) {
2863 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002864 } else {
2865 Slog.w(TAG, "Activity manager shutdown timed out");
2866 timedout = true;
2867 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002868 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002869 } else {
2870 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002871 }
2872 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002873
2874 // Force checkReadyForSleep to complete.
2875 mSleepTimeout = true;
2876 checkReadyForSleepLocked();
2877
Craig Mautner8d341ef2013-03-26 09:03:27 -07002878 return timedout;
2879 }
2880
2881 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002882 removeSleepTimeouts();
2883 if (mGoingToSleep.isHeld()) {
2884 mGoingToSleep.release();
2885 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002886 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2887 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002888 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2889 final ActivityStack stack = stacks.get(stackNdx);
2890 stack.awakeFromSleepingLocked();
2891 if (isFrontStack(stack)) {
2892 resumeTopActivitiesLocked();
2893 }
Craig Mautner5314a402013-09-26 12:40:16 -07002894 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002895 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002896 mGoingToSleepActivities.clear();
2897 }
2898
2899 void activitySleptLocked(ActivityRecord r) {
2900 mGoingToSleepActivities.remove(r);
2901 checkReadyForSleepLocked();
2902 }
2903
2904 void checkReadyForSleepLocked() {
2905 if (!mService.isSleepingOrShuttingDown()) {
2906 // Do not care.
2907 return;
2908 }
2909
2910 if (!mSleepTimeout) {
2911 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002912 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2913 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002914 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2915 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2916 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002917 }
2918
2919 if (mStoppingActivities.size() > 0) {
2920 // Still need to tell some activities to stop; can't sleep yet.
2921 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2922 + mStoppingActivities.size() + " activities");
2923 scheduleIdleLocked();
2924 dontSleep = true;
2925 }
2926
2927 if (mGoingToSleepActivities.size() > 0) {
2928 // Still need to tell some activities to sleep; can't sleep yet.
2929 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2930 + mGoingToSleepActivities.size() + " activities");
2931 dontSleep = true;
2932 }
2933
2934 if (dontSleep) {
2935 return;
2936 }
2937 }
2938
Craig Mautnere0a38842013-12-16 16:14:02 -08002939 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2940 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002941 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2942 stacks.get(stackNdx).goToSleep();
2943 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002944 }
2945
2946 removeSleepTimeouts();
2947
2948 if (mGoingToSleep.isHeld()) {
2949 mGoingToSleep.release();
2950 }
2951 if (mService.mShuttingDown) {
2952 mService.notifyAll();
2953 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002954 }
2955
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002956 boolean reportResumedActivityLocked(ActivityRecord r) {
2957 final ActivityStack stack = r.task.stack;
2958 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002959 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002960 }
2961 if (allResumedActivitiesComplete()) {
2962 ensureActivitiesVisibleLocked(null, 0);
2963 mWindowManager.executeAppTransition();
2964 return true;
2965 }
2966 return false;
2967 }
2968
Craig Mautner8d341ef2013-03-26 09:03:27 -07002969 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002970 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2971 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002972 final int numStacks = stacks.size();
2973 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2974 final ActivityStack stack = stacks.get(stackNdx);
2975 stack.handleAppCrashLocked(app);
2976 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002977 }
2978 }
2979
Jose Lima4b6c6692014-08-12 17:41:12 -07002980 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002981 final ActivityStack stack = r.task.stack;
2982 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002983 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2984 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002985 return false;
2986 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002987 final boolean isVisible = stack.hasVisibleBehindActivity();
2988 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2989 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002990
2991 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002992 if (top == null || top == r || (visible == isVisible)) {
2993 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2994 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002995 return true;
2996 }
2997
2998 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002999 if (visible && top.fullscreen) {
3000 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003001 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003002 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003003 " top.app.thread=" + top.app.thread);
3004 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003005 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3006 // Only the activity set as currently visible behind should actively reset its
3007 // visible behind state.
3008 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3009 + visible + " stack.getVisibleBehindActivity()=" +
3010 stack.getVisibleBehindActivity() + " r=" + r);
3011 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003012 }
3013
Jose Lima4b6c6692014-08-12 17:41:12 -07003014 stack.setVisibleBehindActivity(visible ? r : null);
3015 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003016 // Make the activity immediately above r opaque.
3017 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3018 if (next != null) {
3019 mService.convertFromTranslucent(next.appToken);
3020 }
3021 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003022 if (top.app != null && top.app.thread != null) {
3023 // Notify the top app of the change.
3024 try {
3025 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3026 } catch (RemoteException e) {
3027 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003028 }
3029 return true;
3030 }
3031
Craig Mautnerbb742462014-07-07 15:28:55 -07003032 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3033 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3034 r.mLaunchTaskBehind = false;
3035 final TaskRecord task = r.task;
3036 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003037 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003038 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003039 mWindowManager.setAppVisibility(r.appToken, false);
3040 }
3041
3042 void scheduleLaunchTaskBehindComplete(IBinder token) {
3043 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3044 }
3045
Craig Mautnerde4ef022013-04-07 19:01:33 -07003046 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003047 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003048 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3049 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003050 final int topStackNdx = stacks.size() - 1;
3051 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3052 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003053 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003054 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003055 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003056 }
3057
3058 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003059 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3060 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003061 final int numStacks = stacks.size();
3062 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3063 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003064 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003065 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003066 }
3067 }
3068
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003069 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3070 // Examine all activities currently running in the process.
3071 TaskRecord firstTask = null;
3072 // Tasks is non-null only if two or more tasks are found.
3073 ArraySet<TaskRecord> tasks = null;
3074 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3075 for (int i=0; i<app.activities.size(); i++) {
3076 ActivityRecord r = app.activities.get(i);
3077 // First, if we find an activity that is in the process of being destroyed,
3078 // then we just aren't going to do anything for now; we want things to settle
3079 // down before we try to prune more activities.
3080 if (r.finishing || r.state == ActivityState.DESTROYING
3081 || r.state == ActivityState.DESTROYED) {
3082 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3083 return;
3084 }
3085 // Don't consider any activies that are currently not in a state where they
3086 // can be destroyed.
3087 if (r.visible || !r.stopped || !r.haveState
3088 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
3089 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
3090 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3091 continue;
3092 }
3093 if (r.task != null) {
3094 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3095 + " from " + r);
3096 if (firstTask == null) {
3097 firstTask = r.task;
3098 } else if (firstTask != r.task) {
3099 if (tasks == null) {
3100 tasks = new ArraySet<>();
3101 tasks.add(firstTask);
3102 }
3103 tasks.add(r.task);
3104 }
3105 }
3106 }
3107 if (tasks == null) {
3108 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3109 return;
3110 }
3111 // If we have activities in multiple tasks that are in a position to be destroyed,
3112 // let's iterate through the tasks and release the oldest one.
3113 final int numDisplays = mActivityDisplays.size();
3114 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3115 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3116 // Step through all stacks starting from behind, to hit the oldest things first.
3117 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3118 final ActivityStack stack = stacks.get(stackNdx);
3119 // Try to release activities in this stack; if we manage to, we are done.
3120 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3121 return;
3122 }
3123 }
3124 }
3125 }
3126
Craig Mautner8d341ef2013-03-26 09:03:27 -07003127 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003128 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003129 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003130 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003131
Craig Mautner858d8a62013-04-23 17:08:34 -07003132 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003133 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3134 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003135 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003136 final ActivityStack stack = stacks.get(stackNdx);
3137 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003138 TaskRecord task = stack.topTask();
3139 if (task != null) {
3140 mWindowManager.moveTaskToTop(task.taskId);
3141 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003142 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003143 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003144
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003145 ActivityStack stack = getStack(restoreStackId);
3146 if (stack == null) {
3147 stack = mHomeStack;
3148 }
3149 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003150 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003151 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003152 TaskRecord task = stack.topTask();
3153 if (task != null) {
3154 mWindowManager.moveTaskToTop(task.taskId);
3155 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003156 } else {
3157 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003158 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003159 }
Craig Mautner93529a42013-10-04 15:03:13 -07003160 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003161 }
3162
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003163 /**
3164 * Add background users to send boot completed events to.
3165 * @param userId The user being started in the background
3166 * @param uss The state object for the user.
3167 */
3168 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3169 mStartingBackgroundUsers.add(uss);
3170 }
3171
Craig Mautnerde4ef022013-04-07 19:01:33 -07003172 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003173 ArrayList<ActivityRecord> stops = null;
3174
3175 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003176 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3177 ActivityRecord s = mStoppingActivities.get(activityNdx);
3178 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
3179 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
3180 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3181 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003182 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003183 if (s.finishing) {
3184 // If this activity is finishing, it is sitting on top of
3185 // everyone else but we now know it is no longer needed...
3186 // so get rid of it. Otherwise, we need to go through the
3187 // normal flow and hide it once we determine that it is
3188 // hidden by the activities in front of it.
3189 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003190 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003191 }
3192 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003193 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003194 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3195 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003196 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003197 }
3198 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003199 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003200 }
3201 }
3202
3203 return stops;
3204 }
3205
Craig Mautnercf910b02013-04-23 11:23:27 -07003206 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003207 // FIXME
3208/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3209 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003210 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003211 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003212 if (isFrontStack(stack)) {
3213 if (r == null) {
3214 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3215 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003216 final ActivityRecord pausing = stack.mPausingActivity;
3217 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003218 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003219 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003220 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003221 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003222 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003223 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003224 }
3225 }
3226 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003227 final ActivityRecord resumed = stack.mResumedActivity;
3228 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003229 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003230 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003231 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003232 if (r != null && (state == ActivityState.INITIALIZING
3233 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003234 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003235 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003236 }
3237 }
3238 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003239*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003240 }
3241
Craig Mautner27084302013-03-25 08:05:25 -07003242 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003243 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003244 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003245 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3246 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3247 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003248 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003249 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003250
Craig Mautner20e72272013-04-01 13:45:53 -07003251 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003252 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003253 }
3254
Dianne Hackborn390517b2013-05-30 15:03:32 -07003255 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3256 boolean needSep, String prefix) {
3257 if (activity != null) {
3258 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3259 if (needSep) {
3260 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003261 }
3262 pw.print(prefix);
3263 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003264 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003265 }
3266 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003267 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003268 }
3269
Craig Mautner8d341ef2013-03-26 09:03:27 -07003270 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3271 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003272 boolean printed = false;
3273 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003274 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3275 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003276 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003277 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003278 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003279 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003280 final ActivityStack stack = stacks.get(stackNdx);
3281 StringBuilder stackHeader = new StringBuilder(128);
3282 stackHeader.append(" Stack #");
3283 stackHeader.append(stack.mStackId);
3284 stackHeader.append(":");
3285 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3286 needSep, stackHeader.toString());
3287 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3288 !dumpAll, false, dumpPackage, true,
3289 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003290
Craig Mautner4a1cb222013-12-04 16:14:06 -08003291 needSep = printed;
3292 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3293 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003294 if (pr) {
3295 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003296 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003297 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003298 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3299 " mResumedActivity: ");
3300 if (pr) {
3301 printed = true;
3302 needSep = false;
3303 }
3304 if (dumpAll) {
3305 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3306 " mLastPausedActivity: ");
3307 if (pr) {
3308 printed = true;
3309 needSep = true;
3310 }
3311 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3312 needSep, " mLastNoHistoryActivity: ");
3313 }
3314 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003315 }
3316 }
3317
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003318 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3319 false, dumpPackage, true, " Activities waiting to finish:", null);
3320 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3321 false, dumpPackage, true, " Activities waiting to stop:", null);
3322 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3323 false, dumpPackage, true, " Activities waiting for another to become visible:",
3324 null);
3325 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3326 false, dumpPackage, true, " Activities waiting to sleep:", null);
3327 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3328 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003329
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003330 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003331 }
3332
Dianne Hackborn390517b2013-05-30 15:03:32 -07003333 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003334 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003335 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003336 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003337 String innerPrefix = null;
3338 String[] args = null;
3339 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003340 for (int i=list.size()-1; i>=0; i--) {
3341 final ActivityRecord r = list.get(i);
3342 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3343 continue;
3344 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003345 if (innerPrefix == null) {
3346 innerPrefix = prefix + " ";
3347 args = new String[0];
3348 }
3349 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003350 final boolean full = !brief && (complete || !r.isInHistory());
3351 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003352 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003353 needNL = false;
3354 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003355 if (header1 != null) {
3356 pw.println(header1);
3357 header1 = null;
3358 }
3359 if (header2 != null) {
3360 pw.println(header2);
3361 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003362 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003363 if (lastTask != r.task) {
3364 lastTask = r.task;
3365 pw.print(prefix);
3366 pw.print(full ? "* " : " ");
3367 pw.println(lastTask);
3368 if (full) {
3369 lastTask.dump(pw, prefix + " ");
3370 } else if (complete) {
3371 // Complete + brief == give a summary. Isn't that obvious?!?
3372 if (lastTask.intent != null) {
3373 pw.print(prefix); pw.print(" ");
3374 pw.println(lastTask.intent.toInsecureStringWithClip());
3375 }
3376 }
3377 }
3378 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3379 pw.print(" #"); pw.print(i); pw.print(": ");
3380 pw.println(r);
3381 if (full) {
3382 r.dump(pw, innerPrefix);
3383 } else if (complete) {
3384 // Complete + brief == give a summary. Isn't that obvious?!?
3385 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3386 if (r.app != null) {
3387 pw.print(innerPrefix); pw.println(r.app);
3388 }
3389 }
3390 if (client && r.app != null && r.app.thread != null) {
3391 // flush anything that is already in the PrintWriter since the thread is going
3392 // to write to the file descriptor directly
3393 pw.flush();
3394 try {
3395 TransferPipe tp = new TransferPipe();
3396 try {
3397 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3398 r.appToken, innerPrefix, args);
3399 // Short timeout, since blocking here can
3400 // deadlock with the application.
3401 tp.go(fd, 2000);
3402 } finally {
3403 tp.kill();
3404 }
3405 } catch (IOException e) {
3406 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3407 } catch (RemoteException e) {
3408 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3409 }
3410 needNL = true;
3411 }
3412 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003413 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003414 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003415
Craig Mautnerf3333272013-04-22 10:55:53 -07003416 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003417 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003418 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3419 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003420 }
3421
3422 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003423 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003424 }
3425
3426 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003427 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003428 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3429 }
3430
Craig Mautner05d29032013-05-03 13:40:13 -07003431 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003432 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3433 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3434 }
Craig Mautner05d29032013-05-03 13:40:13 -07003435 }
3436
Craig Mautner0eea92c2013-05-16 13:35:39 -07003437 void removeSleepTimeouts() {
3438 mSleepTimeout = false;
3439 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3440 }
3441
3442 final void scheduleSleepTimeout() {
3443 removeSleepTimeouts();
3444 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3445 }
3446
Craig Mautner4a1cb222013-12-04 16:14:06 -08003447 @Override
3448 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003449 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003450 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3451 }
3452
3453 @Override
3454 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003455 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003456 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3457 }
3458
3459 @Override
3460 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003461 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003462 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3463 }
3464
3465 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003466 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003467 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003468 newDisplay = mActivityDisplays.get(displayId) == null;
3469 if (newDisplay) {
3470 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003471 if (activityDisplay.mDisplay == null) {
3472 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3473 return;
3474 }
Craig Mautner4504de52013-12-20 09:06:56 -08003475 mActivityDisplays.put(displayId, activityDisplay);
3476 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003477 }
Craig Mautner4504de52013-12-20 09:06:56 -08003478 if (newDisplay) {
3479 mWindowManager.onDisplayAdded(displayId);
3480 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003481 }
3482
3483 public void handleDisplayRemovedLocked(int displayId) {
3484 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003485 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3486 if (activityDisplay != null) {
3487 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003488 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003489 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003490 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003491 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003492 }
3493 }
3494 mWindowManager.onDisplayRemoved(displayId);
3495 }
3496
3497 public void handleDisplayChangedLocked(int displayId) {
3498 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003499 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3500 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003501 // TODO: Update the bounds.
3502 }
3503 }
3504 mWindowManager.onDisplayChanged(displayId);
3505 }
3506
3507 StackInfo getStackInfo(ActivityStack stack) {
3508 StackInfo info = new StackInfo();
3509 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3510 info.displayId = Display.DEFAULT_DISPLAY;
3511 info.stackId = stack.mStackId;
3512
3513 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3514 final int numTasks = tasks.size();
3515 int[] taskIds = new int[numTasks];
3516 String[] taskNames = new String[numTasks];
3517 for (int i = 0; i < numTasks; ++i) {
3518 final TaskRecord task = tasks.get(i);
3519 taskIds[i] = task.taskId;
3520 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3521 : task.realActivity != null ? task.realActivity.flattenToString()
3522 : task.getTopActivity() != null ? task.getTopActivity().packageName
3523 : "unknown";
3524 }
3525 info.taskIds = taskIds;
3526 info.taskNames = taskNames;
3527 return info;
3528 }
3529
3530 StackInfo getStackInfoLocked(int stackId) {
3531 ActivityStack stack = getStack(stackId);
3532 if (stack != null) {
3533 return getStackInfo(stack);
3534 }
3535 return null;
3536 }
3537
3538 ArrayList<StackInfo> getAllStackInfosLocked() {
3539 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003540 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3541 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003542 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3543 list.add(getStackInfo(stacks.get(ndx)));
3544 }
3545 }
3546 return list;
3547 }
3548
Jason Monka8f569c2014-07-07 12:15:21 -04003549 void showLockTaskToast() {
3550 mLockTaskNotify.showToast(mLockTaskIsLocked);
3551 }
3552
Craig Mautner299f9602015-01-26 09:47:33 -08003553 void setLockTaskModeLocked(TaskRecord task, boolean isLocked, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003554 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003555 // Take out of lock task mode if necessary
3556 if (mLockTaskModeTask != null) {
3557 final Message lockTaskMsg = Message.obtain();
3558 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3559 lockTaskMsg.what = LOCK_TASK_END_MSG;
3560 mLockTaskModeTask = null;
3561 mHandler.sendMessage(lockTaskMsg);
3562 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003563 return;
3564 }
3565 if (isLockTaskModeViolation(task)) {
3566 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3567 return;
3568 }
3569 mLockTaskModeTask = task;
Craig Mautner299f9602015-01-26 09:47:33 -08003570 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003571 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003572
3573 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003574 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3575 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003576 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003577 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003578 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003579 }
3580
3581 boolean isLockTaskModeViolation(TaskRecord task) {
3582 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3583 }
3584
3585 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3586 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003587 final Message lockTaskMsg = Message.obtain();
3588 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3589 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003590 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003591 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003592 }
3593 }
3594
3595 boolean isInLockTaskMode() {
3596 return mLockTaskModeTask != null;
3597 }
3598
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003599 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003600
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003601 public ActivityStackSupervisorHandler(Looper looper) {
3602 super(looper);
3603 }
3604
Craig Mautnerf3333272013-04-22 10:55:53 -07003605 void activityIdleInternal(ActivityRecord r) {
3606 synchronized (mService) {
3607 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3608 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003609 }
3610
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003611 @Override
3612 public void handleMessage(Message msg) {
3613 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003614 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003615 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003616 if (mService.mDidDexOpt) {
3617 mService.mDidDexOpt = false;
3618 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3619 nmsg.obj = msg.obj;
3620 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3621 return;
3622 }
3623 // We don't at this point know if the activity is fullscreen,
3624 // so we need to be conservative and assume it isn't.
3625 activityIdleInternal((ActivityRecord)msg.obj);
3626 } break;
3627 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003628 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003629 activityIdleInternal((ActivityRecord)msg.obj);
3630 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003631 case RESUME_TOP_ACTIVITY_MSG: {
3632 synchronized (mService) {
3633 resumeTopActivitiesLocked();
3634 }
3635 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003636 case SLEEP_TIMEOUT_MSG: {
3637 synchronized (mService) {
3638 if (mService.isSleepingOrShuttingDown()) {
3639 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3640 mSleepTimeout = true;
3641 checkReadyForSleepLocked();
3642 }
3643 }
3644 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003645 case LAUNCH_TIMEOUT_MSG: {
3646 if (mService.mDidDexOpt) {
3647 mService.mDidDexOpt = false;
3648 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3649 return;
3650 }
3651 synchronized (mService) {
3652 if (mLaunchingActivity.isHeld()) {
3653 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3654 if (VALIDATE_WAKE_LOCK_CALLER
3655 && Binder.getCallingUid() != Process.myUid()) {
3656 throw new IllegalStateException("Calling must be system uid");
3657 }
3658 mLaunchingActivity.release();
3659 }
3660 }
3661 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003662 case HANDLE_DISPLAY_ADDED: {
3663 handleDisplayAddedLocked(msg.arg1);
3664 } break;
3665 case HANDLE_DISPLAY_CHANGED: {
3666 handleDisplayChangedLocked(msg.arg1);
3667 } break;
3668 case HANDLE_DISPLAY_REMOVED: {
3669 handleDisplayRemovedLocked(msg.arg1);
3670 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003671 case CONTAINER_CALLBACK_VISIBILITY: {
3672 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003673 final IActivityContainerCallback callback = container.mCallback;
3674 if (callback != null) {
3675 try {
3676 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3677 } catch (RemoteException e) {
3678 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003679 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003680 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003681 case LOCK_TASK_START_MSG: {
3682 // When lock task starts, we disable the status bars.
3683 try {
Jason Monk62515be2014-05-21 16:06:19 -04003684 if (mLockTaskNotify == null) {
3685 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003686 }
Jason Monk62515be2014-05-21 16:06:19 -04003687 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003688 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003689 if (getStatusBarService() != null) {
3690 int flags =
3691 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003692 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003693 flags ^= StatusBarManager.DISABLE_HOME
3694 | StatusBarManager.DISABLE_RECENT;
3695 }
3696 getStatusBarService().disable(flags, mToken,
3697 mService.mContext.getPackageName());
3698 }
3699 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003700 if (getDevicePolicyManager() != null) {
3701 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003702 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003703 }
justinzhang5286d3f2014-05-12 17:06:01 -04003704 } catch (RemoteException ex) {
3705 throw new RuntimeException(ex);
3706 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003707 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003708 case LOCK_TASK_END_MSG: {
3709 // When lock task ends, we enable the status bars.
3710 try {
Jason Monk62515be2014-05-21 16:06:19 -04003711 if (getStatusBarService() != null) {
3712 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3713 mService.mContext.getPackageName());
3714 }
3715 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003716 if (getDevicePolicyManager() != null) {
3717 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3718 msg.arg1);
3719 }
Jason Monk62515be2014-05-21 16:06:19 -04003720 if (mLockTaskNotify == null) {
3721 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3722 }
3723 mLockTaskNotify.show(false);
3724 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003725 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003726 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003727 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003728 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003729 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003730 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003731 new LockPatternUtils(mService.mContext)
3732 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003733 }
3734 } catch (SettingNotFoundException e) {
3735 // No setting, don't lock.
3736 }
justinzhang5286d3f2014-05-12 17:06:01 -04003737 } catch (RemoteException ex) {
3738 throw new RuntimeException(ex);
3739 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003740 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003741 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3742 final ActivityContainer container = (ActivityContainer) msg.obj;
3743 final IActivityContainerCallback callback = container.mCallback;
3744 if (callback != null) {
3745 try {
3746 callback.onAllActivitiesComplete(container.asBinder());
3747 } catch (RemoteException e) {
3748 }
3749 }
3750 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003751 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3752 synchronized (mService) {
3753 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3754 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003755 final ActivityContainer container = (ActivityContainer) msg.obj;
3756 container.mStack.finishAllActivitiesLocked(true);
3757 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003758 }
3759 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003760 case LAUNCH_TASK_BEHIND_COMPLETE: {
3761 synchronized (mService) {
3762 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3763 if (r != null) {
3764 handleLaunchTaskBehindCompleteLocked(r);
3765 }
3766 }
3767 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003768 }
3769 }
3770 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003771
Ying Wangb081a592014-04-22 15:20:16 -07003772 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003773 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003774 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003775 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003776 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003777 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003778 ActivityRecord mParentActivity = null;
3779 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003780
Craig Mautnere3a00d72014-04-16 08:31:19 -07003781 boolean mVisible = true;
3782
Craig Mautner4a1cb222013-12-04 16:14:06 -08003783 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003784 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003785
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003786 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3787 final static int CONTAINER_STATE_NO_SURFACE = 1;
3788 final static int CONTAINER_STATE_FINISHING = 2;
3789 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3790
3791 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003792 synchronized (mService) {
3793 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003794 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003795 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003796 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003798 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003799
Craig Mautnere0a38842013-12-16 16:14:02 -08003800 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003801 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3802 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003803 mActivityDisplay = activityDisplay;
3804 mStack.mDisplayId = activityDisplay.mDisplayId;
3805 mStack.mStacks = activityDisplay.mStacks;
3806
3807 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003808 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003809 }
3810
3811 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003812 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003813 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003814 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3815 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003816 return;
3817 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003818 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003819 }
3820 }
3821
3822 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003823 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003824 synchronized (mService) {
3825 if (mActivityDisplay != null) {
3826 return mActivityDisplay.mDisplayId;
3827 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003828 }
3829 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003830 }
3831
Jeff Brownca9bc702014-02-11 14:32:56 -08003832 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003833 public int getStackId() {
3834 synchronized (mService) {
3835 return mStackId;
3836 }
3837 }
3838
3839 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003840 public boolean injectEvent(InputEvent event) {
3841 final long origId = Binder.clearCallingIdentity();
3842 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003843 synchronized (mService) {
3844 if (mActivityDisplay != null) {
3845 return mInputManagerInternal.injectInputEvent(event,
3846 mActivityDisplay.mDisplayId,
3847 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3848 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003849 }
3850 return false;
3851 } finally {
3852 Binder.restoreCallingIdentity(origId);
3853 }
3854 }
3855
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003856 @Override
3857 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003858 synchronized (mService) {
3859 if (mContainerState == CONTAINER_STATE_FINISHING) {
3860 return;
3861 }
3862 mContainerState = CONTAINER_STATE_FINISHING;
3863
3864 final Message msg =
3865 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003866 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003867
3868 long origId = Binder.clearCallingIdentity();
3869 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003870 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003871 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003872 } finally {
3873 Binder.restoreCallingIdentity(origId);
3874 }
3875 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003876 }
3877
Craig Mautner60257702014-09-17 15:02:33 -07003878 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003879 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3880 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003881 if (mActivityDisplay != null) {
3882 mActivityDisplay.detachActivitiesLocked(mStack);
3883 mActivityDisplay = null;
3884 mStack.mDisplayId = -1;
3885 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003886 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003887 }
3888 }
3889
3890 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003891 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003892 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08003893 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003894 Binder.getCallingUid(), mCurrentUser, false,
3895 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003896
Craig Mautnere0a38842013-12-16 16:14:02 -08003897 // TODO: Switch to user app stacks here.
3898 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08003899 final Uri data = intent.getData();
3900 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
3901 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08003902 }
Craig Mautnerb9168362015-02-26 20:40:19 -08003903 checkEmbeddedAllowedInner(userId, intent, mimeType);
3904
3905 intent.addFlags(FORCE_NEW_TASK_FLAGS);
3906 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
3907 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003908 }
3909
3910 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003911 public final int startActivityIntentSender(IIntentSender intentSender) {
3912 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3913
3914 if (!(intentSender instanceof PendingIntentRecord)) {
3915 throw new IllegalArgumentException("Bad PendingIntent object");
3916 }
3917
Craig Mautnerb9168362015-02-26 20:40:19 -08003918 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003919 Binder.getCallingUid(), mCurrentUser, false,
3920 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003921
3922 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3923 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3924 pendingIntent.key.requestResolvedType);
3925
3926 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3927 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3928 }
3929
3930 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003931 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003932 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003933 throw new SecurityException(
3934 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3935 }
3936 }
3937
Craig Mautnerdf88d732014-01-27 09:21:32 -08003938 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003939 public IBinder asBinder() {
3940 return this;
3941 }
3942
Craig Mautner4504de52013-12-20 09:06:56 -08003943 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003944 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003945 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003946 }
3947
Craig Mautner4a1cb222013-12-04 16:14:06 -08003948 ActivityStackSupervisor getOuter() {
3949 return ActivityStackSupervisor.this;
3950 }
3951
Craig Mautnerd163e752014-06-13 17:18:47 -07003952 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003953 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003954 }
3955
3956 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003957 synchronized (mService) {
3958 if (mActivityDisplay != null) {
3959 mActivityDisplay.getBounds(outBounds);
3960 } else {
3961 outBounds.set(0, 0);
3962 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003963 }
3964 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003965
Craig Mautner6985bad2014-04-21 15:22:06 -07003966 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003967 void setVisible(boolean visible) {
3968 if (mVisible != visible) {
3969 mVisible = visible;
3970 if (mCallback != null) {
3971 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3972 0 /* unused */, this).sendToTarget();
3973 }
3974 }
3975 }
3976
Craig Mautner6985bad2014-04-21 15:22:06 -07003977 void setDrawn() {
3978 }
3979
Craig Mautner1b4bf852014-05-26 15:06:32 -07003980 // You can always start a new task on a regular ActivityStack.
3981 boolean isEligibleForNewTasks() {
3982 return true;
3983 }
3984
Craig Mautnerd163e752014-06-13 17:18:47 -07003985 void onTaskListEmptyLocked() {
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -08003986 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3987 detachLocked();
3988 deleteActivityContainer(this);
3989 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003990 }
3991
Craig Mautner34b73df2014-01-12 21:11:08 -08003992 @Override
3993 public String toString() {
3994 return mIdString + (mActivityDisplay == null ? "N" : "A");
3995 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003996 }
3997
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003998 private class VirtualActivityContainer extends ActivityContainer {
3999 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004000 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004001
4002 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4003 super(getNextStackId());
4004 mParentActivity = parent;
4005 mCallback = callback;
4006 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004007 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004008 }
4009
4010 @Override
4011 public void setSurface(Surface surface, int width, int height, int density) {
4012 super.setSurface(surface, width, height, density);
4013
4014 synchronized (mService) {
4015 final long origId = Binder.clearCallingIdentity();
4016 try {
4017 setSurfaceLocked(surface, width, height, density);
4018 } finally {
4019 Binder.restoreCallingIdentity(origId);
4020 }
4021 }
4022 }
4023
4024 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4025 if (mContainerState == CONTAINER_STATE_FINISHING) {
4026 return;
4027 }
4028 VirtualActivityDisplay virtualActivityDisplay =
4029 (VirtualActivityDisplay) mActivityDisplay;
4030 if (virtualActivityDisplay == null) {
4031 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004032 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004033 mActivityDisplay = virtualActivityDisplay;
4034 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4035 attachToDisplayLocked(virtualActivityDisplay);
4036 }
4037
4038 if (mSurface != null) {
4039 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004040 }
4041
Craig Mautner6985bad2014-04-21 15:22:06 -07004042 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004043 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004044 mStack.resumeTopActivityLocked(null);
4045 } else {
4046 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004047 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004048 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004049 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004050 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004051 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004052
Craig Mautnerd163e752014-06-13 17:18:47 -07004053 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004054
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004055 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
4056 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004057 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004058
Craig Mautner6985bad2014-04-21 15:22:06 -07004059 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004060 boolean isAttachedLocked() {
4061 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004062 }
4063
4064 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004065 void setDrawn() {
4066 synchronized (mService) {
4067 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004068 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004069 }
4070 }
4071
Craig Mautner1b4bf852014-05-26 15:06:32 -07004072 // Never start a new task on an ActivityView if it isn't explicitly specified.
4073 @Override
4074 boolean isEligibleForNewTasks() {
4075 return false;
4076 }
4077
Craig Mautnerd163e752014-06-13 17:18:47 -07004078 private void setSurfaceIfReadyLocked() {
4079 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004080 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4081 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4082 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4083 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4084 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004085 }
4086 }
4087
Craig Mautner4a1cb222013-12-04 16:14:06 -08004088 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4089 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004090 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004091 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004092 int mDisplayId;
4093 Display mDisplay;
4094 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004095
Craig Mautner4a1cb222013-12-04 16:14:06 -08004096 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4097 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004098 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004099
Jose Lima4b6c6692014-08-12 17:41:12 -07004100 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004101
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004102 ActivityDisplay() {
4103 }
Craig Mautner4504de52013-12-20 09:06:56 -08004104
Craig Mautner1a70a162014-09-13 12:09:31 -07004105 // After instantiation, check that mDisplay is not null before using this. The alternative
4106 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004107 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004108 final Display display = mDisplayManager.getDisplay(displayId);
4109 if (display == null) {
4110 return;
4111 }
4112 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004113 }
4114
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004115 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004116 mDisplay = display;
4117 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004118 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004119 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004120
4121 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004122 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4123 + mDisplayId);
4124 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004125 }
4126
Craig Mautnere0a38842013-12-16 16:14:02 -08004127 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004128 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004129 + " from displayId=" + mDisplayId);
4130 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004131 }
4132
4133 void getBounds(Point bounds) {
4134 mDisplay.getDisplayInfo(mDisplayInfo);
4135 bounds.x = mDisplayInfo.appWidth;
4136 bounds.y = mDisplayInfo.appHeight;
4137 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004138
Jose Lima4b6c6692014-08-12 17:41:12 -07004139 void setVisibleBehindActivity(ActivityRecord r) {
4140 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004141 }
4142
Jose Lima4b6c6692014-08-12 17:41:12 -07004143 boolean hasVisibleBehindActivity() {
4144 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004145 }
4146
Craig Mautner34b73df2014-01-12 21:11:08 -08004147 @Override
4148 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004149 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4150 }
4151 }
4152
4153 class VirtualActivityDisplay extends ActivityDisplay {
4154 VirtualDisplay mVirtualDisplay;
4155
Craig Mautner6985bad2014-04-21 15:22:06 -07004156 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004157 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004158 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4159 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4160 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4161 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004162
4163 init(mVirtualDisplay.getDisplay());
4164
4165 mWindowManager.handleDisplayAdded(mDisplayId);
4166 }
4167
4168 void setSurface(Surface surface) {
4169 if (mVirtualDisplay != null) {
4170 mVirtualDisplay.setSurface(surface);
4171 }
4172 }
4173
4174 @Override
4175 void detachActivitiesLocked(ActivityStack stack) {
4176 super.detachActivitiesLocked(stack);
4177 if (mVirtualDisplay != null) {
4178 mVirtualDisplay.release();
4179 mVirtualDisplay = null;
4180 }
4181 }
4182
4183 @Override
4184 public String toString() {
4185 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004186 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004187 }
Jose Lima58e66d62014-05-27 20:00:27 -07004188
4189 private boolean isLeanbackOnlyDevice() {
4190 boolean onLeanbackOnly = false;
4191 try {
4192 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4193 PackageManager.FEATURE_LEANBACK_ONLY);
4194 } catch (RemoteException e) {
4195 // noop
4196 }
4197
4198 return onLeanbackOnly;
4199 }
Craig Mautner27084302013-03-25 08:05:25 -07004200}