blob: 39df9d1558fb163abf024f1de4a3e0ec8eb7db9e [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 Mautner15df08a2015-04-01 12:17:18 -070020import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070023import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070025import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
26import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070027import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner15df08a2015-04-01 12:17:18 -070028import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
Craig Mautner6170f732013-04-02 13:05:23 -070029import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080030import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070031import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070032import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
33import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070035import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
38import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
39import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070040
Craig Mautner2420ead2013-04-01 17:13:20 -070041import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080043import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.ActivityOptions;
45import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080046import android.app.IActivityContainer;
47import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070048import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070051import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070052import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070054import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040055import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040056import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070062import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.content.pm.ApplicationInfo;
64import android.content.pm.PackageManager;
65import android.content.pm.ResolveInfo;
66import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080067import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080068import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080069import android.hardware.display.DisplayManager;
70import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080071import android.hardware.display.DisplayManagerGlobal;
72import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080073import android.hardware.input.InputManager;
74import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080075import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070077import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070078import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070079import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070081import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070082import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070083import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070084import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070085import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070086import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040087import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070088import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070089import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070090import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070091import android.os.WorkSource;
Jason Monk62515be2014-05-21 16:06:19 -040092import android.provider.Settings;
93import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070094import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070095import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070096import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070097import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080098import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070099
Craig Mautner4a1cb222013-12-04 16:14:06 -0800100import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800101import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800103import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800104import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700105import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700106import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800107import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700108import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400109import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400110import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800111import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700112import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700113import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114
justinzhang5286d3f2014-05-12 17:06:01 -0400115
Craig Mautner8d341ef2013-03-26 09:03:27 -0700116import java.io.FileDescriptor;
117import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700118import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700119import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700120import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700121import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700122import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700123
Craig Mautner4a1cb222013-12-04 16:14:06 -0800124public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800125 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700126 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
127 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Craig Mautnere0570202015-05-13 13:06:11 -0700128 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700129 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
130 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
131 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
132 private static final String TAG_STACK = TAG + POSTFIX_STACK;
133 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
134 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
135 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800136
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800137 static final boolean DEBUG = DEBUG_ALL || false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700138 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
139 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700140 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700141 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700142 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700143 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
144 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautnereb0e38a2015-04-02 20:22:54 -0700145 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700146 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700147
Craig Mautner2219a1b2013-03-25 09:44:30 -0700148 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700149
Craig Mautnerf3333272013-04-22 10:55:53 -0700150 /** How long we wait until giving up on the last activity telling us it is idle. */
151 static final int IDLE_TIMEOUT = 10*1000;
152
Craig Mautner0eea92c2013-05-16 13:35:39 -0700153 /** How long we can hold the sleep wake lock before giving up. */
154 static final int SLEEP_TIMEOUT = 5*1000;
155
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700156 // How long we can hold the launch wake lock before giving up.
157 static final int LAUNCH_TIMEOUT = 10*1000;
158
Craig Mautner05d29032013-05-03 13:40:13 -0700159 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
160 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
161 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700162 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700163 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800164 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
165 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
166 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700167 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400168 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
169 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700170 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700171 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700172 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800173
Craig Mautner4504de52013-12-20 09:06:56 -0800174 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700175
Jason Monk62515be2014-05-21 16:06:19 -0400176 private static final String LOCK_TASK_TAG = "Lock-to-App";
177
justinzhang5286d3f2014-05-12 17:06:01 -0400178 /** Status Bar Service **/
179 private IBinder mToken = new Binder();
180 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400181 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400182
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700183 // For debugging to make sure the caller when acquiring/releasing our
184 // wake lock is the system process.
185 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700186
Craig Mautner27084302013-03-25 08:05:25 -0700187 final ActivityManagerService mService;
188
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800189 private final RecentTasks mRecentTasks;
190
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700191 final ActivityStackSupervisorHandler mHandler;
192
193 /** Short cut */
194 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800195 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700196
Craig Mautner8d341ef2013-03-26 09:03:27 -0700197 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700198 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700199
200 /** Task identifier that activities are currently being started in. Incremented each time a
201 * new task is created. */
202 private int mCurTaskId = 0;
203
Craig Mautner2420ead2013-04-01 17:13:20 -0700204 /** The current user */
205 private int mCurrentUser;
206
Craig Mautnere0a38842013-12-16 16:14:02 -0800207 /** The stack containing the launcher app. Assumed to always be attached to
208 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700209 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700210
Craig Mautnere0a38842013-12-16 16:14:02 -0800211 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700212 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700213
Craig Mautner4a1cb222013-12-04 16:14:06 -0800214 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
215 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800216 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800217 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700218
219 /** List of activities that are waiting for a new activity to become visible before completing
220 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800221 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700222
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700223 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800224 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700225
226 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800227 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700228
Craig Mautnerde4ef022013-04-07 19:01:33 -0700229 /** List of activities that are ready to be stopped, but waiting for the next activity to
230 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800231 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700232
Craig Mautnerf3333272013-04-22 10:55:53 -0700233 /** List of activities that are ready to be finished, but waiting for the previous activity to
234 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800235 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700236
Craig Mautner0eea92c2013-05-16 13:35:39 -0700237 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800238 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700239
Craig Mautnerf3333272013-04-22 10:55:53 -0700240 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800241 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700242
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700243 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800244 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700245
Craig Mautnerde4ef022013-04-07 19:01:33 -0700246 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
247 * is being brought in front of us. */
248 boolean mUserLeaving = false;
249
Craig Mautner0eea92c2013-05-16 13:35:39 -0700250 /** Set when we have taken too long waiting to go to sleep. */
251 boolean mSleepTimeout = false;
252
Jose Lima58e66d62014-05-27 20:00:27 -0700253 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
254 * setWindowManager is called. **/
255 private boolean mLeanbackOnlyDevice;
256
Craig Mautner0eea92c2013-05-16 13:35:39 -0700257 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700258 * We don't want to allow the device to go to sleep while in the process
259 * of launching an activity. This is primarily to allow alarm intent
260 * receivers to launch an activity and get that to run before the device
261 * goes back to sleep.
262 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700263 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700264
265 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700266 * Set when the system is going to sleep, until we have
267 * successfully paused the current activity and released our wake lock.
268 * At that point the system is allowed to actually sleep.
269 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700270 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700271
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700272 /** Stack id of the front stack when user switched, indexed by userId. */
273 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700274
Craig Mautner4504de52013-12-20 09:06:56 -0800275 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800276 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700277 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800278
279 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700280 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800281
Jeff Brownca9bc702014-02-11 14:32:56 -0800282 InputManagerInternal mInputManagerInternal;
283
Craig Mautner15df08a2015-04-01 12:17:18 -0700284 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
285 * may be finished until there is only one entry left. If this is empty the system is not
286 * in lockTask mode. */
287 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000288 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700289 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
290 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000291 */
292 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400293 /**
294 * Notifies the user when entering/exiting lock-task.
295 */
296 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800297
Craig Mautner15df08a2015-04-01 12:17:18 -0700298 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700299
Craig Mautner42d04db2014-11-06 12:13:23 -0800300 /** Used to keep resumeTopActivityLocked() from being entered recursively */
301 boolean inResumeTopActivity;
302
Craig Mautneree36c772014-07-16 14:56:05 -0700303 /**
304 * Description of a request to start a new activity, which has been held
305 * due to app switches being disabled.
306 */
307 static class PendingActivityLaunch {
308 final ActivityRecord r;
309 final ActivityRecord sourceRecord;
310 final int startFlags;
311 final ActivityStack stack;
312
313 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
314 int _startFlags, ActivityStack _stack) {
315 r = _r;
316 sourceRecord = _sourceRecord;
317 startFlags = _startFlags;
318 stack = _stack;
319 }
320 }
321
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800322 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700323 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800324 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700325 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
326 }
327
328 /**
329 * At the time when the constructor runs, the power manager has not yet been
330 * initialized. So we initialize our wakelocks afterwards.
331 */
332 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800333 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700334 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700335 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700336 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700337 }
338
justinzhang5286d3f2014-05-12 17:06:01 -0400339 // This function returns a IStatusBarService. The value is from ServiceManager.
340 // getService and is cached.
341 private IStatusBarService getStatusBarService() {
342 synchronized (mService) {
343 if (mStatusBarService == null) {
344 mStatusBarService = IStatusBarService.Stub.asInterface(
345 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
346 if (mStatusBarService == null) {
347 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
348 }
349 }
350 return mStatusBarService;
351 }
352 }
353
Jason Monk35c62a42014-06-17 10:24:47 -0400354 private IDevicePolicyManager getDevicePolicyManager() {
355 synchronized (mService) {
356 if (mDevicePolicyManager == null) {
357 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
358 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
359 if (mDevicePolicyManager == null) {
360 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
361 }
362 }
363 return mDevicePolicyManager;
364 }
365 }
366
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700367 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800368 synchronized (mService) {
369 mWindowManager = wm;
370
371 mDisplayManager =
372 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
373 mDisplayManager.registerDisplayListener(this, null);
374
375 Display[] displays = mDisplayManager.getDisplays();
376 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
377 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800378 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700379 if (activityDisplay.mDisplay == null) {
380 throw new IllegalStateException("Default Display does not exist");
381 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800382 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383 }
384
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700385 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800386 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800387
388 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700389
390 // Initialize this here, now that we can get a valid reference to PackageManager.
391 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800392 }
Craig Mautner27084302013-03-25 08:05:25 -0700393 }
394
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200395 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700396 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200397 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700398 }
399
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700400 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800401 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700402 }
403
Craig Mautnerde4ef022013-04-07 19:01:33 -0700404 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800405 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700406 }
407
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800408 /** Top of all visible stacks is/should always be equal to the focused stack.
409 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
410 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700411 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700412 if (stack == null) {
413 return false;
414 }
415
Craig Mautnerdf88d732014-01-27 09:21:32 -0800416 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
417 if (parent != null) {
418 stack = parent.task.stack;
419 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800420 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700421 }
422
Craig Mautner299f9602015-01-26 09:47:33 -0800423 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800424 moveHomeStack(toFront, reason, null);
425 }
426
427 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800428 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800429 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800430 if (topNdx <= 0) {
431 return;
432 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700433
434 // The home stack should either be at the top or bottom of the stack list.
435 if ((toFront && (stacks.get(topNdx) != mHomeStack))
436 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700437 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700438 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
439 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800440 stacks.remove(mHomeStack);
441 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700442 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800443
444 if (lastFocusedStack != null) {
445 mLastFocusedStack = lastFocusedStack;
446 }
447 mFocusedStack = stacks.get(topNdx);
448
Craig Mautnerde313752015-01-22 14:28:03 -0800449 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
450 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800451 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800452
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800453 if (mService.mBooting || !mService.mBooted) {
454 final ActivityRecord r = topRunningActivityLocked();
455 if (r != null && r.idle) {
456 checkFinishBootingLocked();
457 }
458 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700459 }
460
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700461 /** Returns true if the focus activity was adjusted to the home stack top activity. */
462 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700463 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
464 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700465 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700466 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700467
Craig Mautner84984fa2014-06-19 11:19:20 -0700468 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700469
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700470 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700471 if (top == null) {
472 return false;
473 }
474 mService.setFocusedActivityLocked(top, reason);
475 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700476 }
477
Craig Mautner299f9602015-01-26 09:47:33 -0800478 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700479 if (!mService.mBooting && !mService.mBooted) {
480 // Not ready yet!
481 return false;
482 }
483
Craig Mautner84984fa2014-06-19 11:19:20 -0700484 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
485 mWindowManager.showRecentApps();
486 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700487 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700488
Craig Mautner84984fa2014-06-19 11:19:20 -0700489 if (prev != null) {
490 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
491 }
492
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700493 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
494 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700495 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800496 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700497 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700498 }
Craig Mautner299f9602015-01-26 09:47:33 -0800499 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700500 }
501
Craig Mautner8d341ef2013-03-26 09:03:27 -0700502 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700503 return anyTaskForIdLocked(id, true);
504 }
505
506 /**
507 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
508 * @param id Id of the task we would like returned.
509 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
510 * restore the task from recents to the active list.
511 */
512 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800513 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800514 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800515 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800516 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
517 ActivityStack stack = stacks.get(stackNdx);
518 TaskRecord task = stack.taskForIdLocked(id);
519 if (task != null) {
520 return task;
521 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700522 }
523 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800524
525 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700526 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800527 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800528 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700529 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800530 return null;
531 }
532
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700533 if (!restoreFromRecents) {
534 return task;
535 }
536
Wale Ogunwale7de05352014-12-12 15:21:33 -0800537 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700538 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
539 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800540 return null;
541 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700542 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800543 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700544 }
545
Craig Mautner6170f732013-04-02 13:05:23 -0700546 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800547 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800548 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800549 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
551 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
552 if (r != null) {
553 return r;
554 }
Craig Mautner6170f732013-04-02 13:05:23 -0700555 }
556 }
557 return null;
558 }
559
Craig Mautneref73ee12014-04-23 11:45:37 -0700560 void setNextTaskId(int taskId) {
561 if (taskId > mCurTaskId) {
562 mCurTaskId = taskId;
563 }
564 }
565
Craig Mautner8d341ef2013-03-26 09:03:27 -0700566 int getNextTaskId() {
567 do {
568 mCurTaskId++;
569 if (mCurTaskId <= 0) {
570 mCurTaskId = 1;
571 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700572 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700573 return mCurTaskId;
574 }
575
Craig Mautnerde4ef022013-04-07 19:01:33 -0700576 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800577 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700578 if (stack == null) {
579 return null;
580 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700581 ActivityRecord resumedActivity = stack.mResumedActivity;
582 if (resumedActivity == null || resumedActivity.app == null) {
583 resumedActivity = stack.mPausingActivity;
584 if (resumedActivity == null || resumedActivity.app == null) {
585 resumedActivity = stack.topRunningActivityLocked(null);
586 }
587 }
588 return resumedActivity;
589 }
590
Dianne Hackbornff072722014-09-24 10:56:28 -0700591 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700592 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800593 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800594 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
595 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800596 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
597 final ActivityStack stack = stacks.get(stackNdx);
598 if (!isFrontStack(stack)) {
599 continue;
600 }
601 ActivityRecord hr = stack.topRunningActivityLocked(null);
602 if (hr != null) {
603 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
604 && processName.equals(hr.processName)) {
605 try {
George Mount2c92c972014-03-20 09:38:23 -0700606 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607 didSomething = true;
608 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700609 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610 Slog.w(TAG, "Exception in new application when starting activity "
611 + hr.intent.getComponent().flattenToShortString(), e);
612 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700613 }
Craig Mautner20e72272013-04-01 13:45:53 -0700614 }
Craig Mautner20e72272013-04-01 13:45:53 -0700615 }
616 }
617 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700618 if (!didSomething) {
619 ensureActivitiesVisibleLocked(null, 0);
620 }
Craig Mautner20e72272013-04-01 13:45:53 -0700621 return didSomething;
622 }
623
624 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800625 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
626 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
628 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800629 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800630 continue;
631 }
632 final ActivityRecord resumedActivity = stack.mResumedActivity;
633 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800634 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
635 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800636 return false;
637 }
Craig Mautner20e72272013-04-01 13:45:53 -0700638 }
639 }
640 return true;
641 }
642
Craig Mautnerde4ef022013-04-07 19:01:33 -0700643 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800644 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
645 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800646 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
647 final ActivityStack stack = stacks.get(stackNdx);
648 if (isFrontStack(stack)) {
649 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700650 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800651 return false;
652 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700653 }
654 }
655 }
656 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700657 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800658 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
659 mLastFocusedStack + " to=" + mFocusedStack);
660 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700661 return true;
662 }
663
664 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800665 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800666 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
667 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
669 final ActivityStack stack = stacks.get(stackNdx);
670 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800671 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700672 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800673 return false;
674 }
675 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800676 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700677 }
678 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800679 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700680 }
681
Craig Mautner2acc3892013-09-23 10:28:14 -0700682 /**
683 * Pause all activities in either all of the stacks or just the back stacks.
684 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700685 * @return true if any activity was paused as a result of this call.
686 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700687 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700688 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800689 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
690 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800691 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
692 final ActivityStack stack = stacks.get(stackNdx);
693 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
694 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
695 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700696 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
697 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800698 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700699 }
700 }
701 return someActivityPaused;
702 }
703
Craig Mautnerde4ef022013-04-07 19:01:33 -0700704 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700705 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800706 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
707 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800708 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
709 final ActivityStack stack = stacks.get(stackNdx);
710 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700711 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 if (DEBUG_STATES) {
713 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
714 pausing = false;
715 } else {
716 return false;
717 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700718 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700719 }
720 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700721 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700722 }
723
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700724 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
725 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500726 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800727 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
728 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
729 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
730 final ActivityStack stack = stacks.get(stackNdx);
731 if (stack.mResumedActivity != null &&
732 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700733 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800734 }
735 }
736 }
737 }
738
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700739 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700740 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700741 }
742
743 void sendWaitingVisibleReportLocked(ActivityRecord r) {
744 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700745 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700746 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700747 if (w.who == null) {
748 changed = true;
749 w.timeout = false;
750 if (r != null) {
751 w.who = new ComponentName(r.info.packageName, r.info.name);
752 }
753 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
754 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700755 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700756 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700757 if (changed) {
758 mService.notifyAll();
759 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700760 }
761
762 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
763 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700764 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700765 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700766 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700767 if (w.who == null) {
768 changed = true;
769 w.timeout = timeout;
770 if (r != null) {
771 w.who = new ComponentName(r.info.packageName, r.info.name);
772 }
773 w.thisTime = thisTime;
774 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700775 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700776 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700777 if (changed) {
778 mService.notifyAll();
779 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700780 }
781
Craig Mautner29219d92013-04-16 20:19:12 -0700782 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800783 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700784 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
785 if (r != null) {
786 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700787 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700788
Craig Mautner4a1cb222013-12-04 16:14:06 -0800789 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800790 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800791 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
792 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700793 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700794 r = stack.topRunningActivityLocked(null);
795 if (r != null) {
796 return r;
797 }
798 }
799 }
800 return null;
801 }
802
Dianne Hackborn09233282014-04-30 11:33:59 -0700803 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700804 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800805 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
806 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800807 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800808 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800809 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800810 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
811 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700812 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800813 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700814 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700815 }
816 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700817
818 // The lists are already sorted from most recent to oldest. Just pull the most recent off
819 // each list and add it to list. Stop when all lists are empty or maxNum reached.
820 while (maxNum > 0) {
821 long mostRecentActiveTime = Long.MIN_VALUE;
822 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800823 final int numTaskLists = runningTaskLists.size();
824 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
825 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700826 if (!stackTaskList.isEmpty()) {
827 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
828 if (lastActiveTime > mostRecentActiveTime) {
829 mostRecentActiveTime = lastActiveTime;
830 selectedStackList = stackTaskList;
831 }
832 }
833 }
834 if (selectedStackList != null) {
835 list.add(selectedStackList.remove(0));
836 --maxNum;
837 } else {
838 break;
839 }
840 }
Craig Mautner20e72272013-04-01 13:45:53 -0700841 }
842
Craig Mautner23ac33b2013-04-01 16:26:35 -0700843 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700844 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700845 // Collect information about the target of the Intent.
846 ActivityInfo aInfo;
847 try {
848 ResolveInfo rInfo =
849 AppGlobals.getPackageManager().resolveIntent(
850 intent, resolvedType,
851 PackageManager.MATCH_DEFAULT_ONLY
852 | ActivityManagerService.STOCK_PM_FLAGS, userId);
853 aInfo = rInfo != null ? rInfo.activityInfo : null;
854 } catch (RemoteException e) {
855 aInfo = null;
856 }
857
858 if (aInfo != null) {
859 // Store the found target back into the intent, because now that
860 // we have it we never want to do this again. For example, if the
861 // user navigates back to this point in the history, we should
862 // always restart the exact same activity.
863 intent.setComponent(new ComponentName(
864 aInfo.applicationInfo.packageName, aInfo.name));
865
866 // Don't debug things in the system process
867 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
868 if (!aInfo.processName.equals("system")) {
869 mService.setDebugApp(aInfo.processName, true, false);
870 }
871 }
872
873 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
874 if (!aInfo.processName.equals("system")) {
875 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
876 }
877 }
878
Jeff Hao1b012d32014-08-20 10:35:34 -0700879 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700880 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700881 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700882 }
883 }
884 }
885 return aInfo;
886 }
887
Craig Mautner299f9602015-01-26 09:47:33 -0800888 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
889 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700890 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
891 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700892 }
893
Craig Mautner23ac33b2013-04-01 16:26:35 -0700894 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700895 String callingPackage, Intent intent, String resolvedType,
896 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700897 IBinder resultTo, String resultWho, int requestCode, int startFlags,
898 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700899 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700900 // Refuse possible leaked file descriptors
901 if (intent != null && intent.hasFileDescriptors()) {
902 throw new IllegalArgumentException("File descriptors passed in Intent");
903 }
904 boolean componentSpecified = intent.getComponent() != null;
905
906 // Don't modify the client's object!
907 intent = new Intent(intent);
908
909 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700910 ActivityInfo aInfo =
911 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700912
Craig Mautnere0a38842013-12-16 16:14:02 -0800913 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700914 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800915 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700916 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800917 // Cannot start a child activity if the parent is not resumed.
918 return ActivityManager.START_CANCELED;
919 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700920 final int realCallingPid = Binder.getCallingPid();
921 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922 int callingPid;
923 if (callingUid >= 0) {
924 callingPid = -1;
925 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700926 callingPid = realCallingPid;
927 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700928 } else {
929 callingPid = callingUid = -1;
930 }
931
Craig Mautnere0a38842013-12-16 16:14:02 -0800932 final ActivityStack stack;
933 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800934 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800935 } else {
936 stack = container.mStack;
937 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800938 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700939 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700940 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700941
942 final long origId = Binder.clearCallingIdentity();
943
944 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800945 (aInfo.applicationInfo.privateFlags
946 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700947 // This may be a heavy-weight process! Check to see if we already
948 // have another, different heavy-weight process running.
949 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
950 if (mService.mHeavyWeightProcess != null &&
951 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
952 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700953 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700954 if (caller != null) {
955 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
956 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700957 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700958 } else {
959 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700960 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700961 + intent.toString());
962 ActivityOptions.abort(options);
963 return ActivityManager.START_PERMISSION_DENIED;
964 }
965 }
966
967 IIntentSender target = mService.getIntentSenderLocked(
968 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700969 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700970 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
971 | PendingIntent.FLAG_ONE_SHOT, null);
972
973 Intent newIntent = new Intent();
974 if (requestCode >= 0) {
975 // Caller is requesting a result.
976 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
977 }
978 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
979 new IntentSender(target));
980 if (mService.mHeavyWeightProcess.activities.size() > 0) {
981 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
982 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
983 hist.packageName);
984 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
985 hist.task.taskId);
986 }
987 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
988 aInfo.packageName);
989 newIntent.setFlags(intent.getFlags());
990 newIntent.setClassName("android",
991 HeavyWeightSwitcherActivity.class.getName());
992 intent = newIntent;
993 resolvedType = null;
994 caller = null;
995 callingUid = Binder.getCallingUid();
996 callingPid = Binder.getCallingPid();
997 componentSpecified = true;
998 try {
999 ResolveInfo rInfo =
1000 AppGlobals.getPackageManager().resolveIntent(
1001 intent, null,
1002 PackageManager.MATCH_DEFAULT_ONLY
1003 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1004 aInfo = rInfo != null ? rInfo.activityInfo : null;
1005 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1006 } catch (RemoteException e) {
1007 aInfo = null;
1008 }
1009 }
1010 }
1011 }
1012
Dianne Hackborn91097de2014-04-04 18:02:06 -07001013 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1014 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001015 requestCode, callingPid, callingUid, callingPackage,
1016 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001017 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018
Craig Mautner85c11a82014-07-17 12:38:18 -07001019 Binder.restoreCallingIdentity(origId);
1020
Craig Mautnerde4ef022013-04-07 19:01:33 -07001021 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001022 // If the caller also wants to switch to a new configuration,
1023 // do so now. This allows a clean switch, as we are waiting
1024 // for the current activity to pause (so we will not destroy
1025 // it), and have not yet started the next activity.
1026 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1027 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001028 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001029 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001030 "Updating to new configuration after starting activity.");
1031 mService.updateConfigurationLocked(config, null, false, false);
1032 }
1033
Craig Mautner23ac33b2013-04-01 16:26:35 -07001034 if (outResult != null) {
1035 outResult.result = res;
1036 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001037 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001038 do {
1039 try {
1040 mService.wait();
1041 } catch (InterruptedException e) {
1042 }
1043 } while (!outResult.timeout && outResult.who == null);
1044 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001045 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001046 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001047 outResult.timeout = false;
1048 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1049 outResult.totalTime = 0;
1050 outResult.thisTime = 0;
1051 } else {
1052 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001053 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001054 do {
1055 try {
1056 mService.wait();
1057 } catch (InterruptedException e) {
1058 }
1059 } while (!outResult.timeout && outResult.who == null);
1060 }
1061 }
1062 }
1063
1064 return res;
1065 }
1066 }
1067
1068 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1069 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1070 Bundle options, int userId) {
1071 if (intents == null) {
1072 throw new NullPointerException("intents is null");
1073 }
1074 if (resolvedTypes == null) {
1075 throw new NullPointerException("resolvedTypes is null");
1076 }
1077 if (intents.length != resolvedTypes.length) {
1078 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1079 }
1080
Craig Mautner23ac33b2013-04-01 16:26:35 -07001081
1082 int callingPid;
1083 if (callingUid >= 0) {
1084 callingPid = -1;
1085 } else if (caller == null) {
1086 callingPid = Binder.getCallingPid();
1087 callingUid = Binder.getCallingUid();
1088 } else {
1089 callingPid = callingUid = -1;
1090 }
1091 final long origId = Binder.clearCallingIdentity();
1092 try {
1093 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001094 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095 for (int i=0; i<intents.length; i++) {
1096 Intent intent = intents[i];
1097 if (intent == null) {
1098 continue;
1099 }
1100
1101 // Refuse possible leaked file descriptors
1102 if (intent != null && intent.hasFileDescriptors()) {
1103 throw new IllegalArgumentException("File descriptors passed in Intent");
1104 }
1105
1106 boolean componentSpecified = intent.getComponent() != null;
1107
1108 // Don't modify the client's object!
1109 intent = new Intent(intent);
1110
1111 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001112 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001113 // TODO: New, check if this is correct
1114 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1115
1116 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001117 (aInfo.applicationInfo.privateFlags
1118 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001119 throw new IllegalArgumentException(
1120 "FLAG_CANT_SAVE_STATE not supported here");
1121 }
1122
1123 Bundle theseOptions;
1124 if (options != null && i == intents.length-1) {
1125 theseOptions = options;
1126 } else {
1127 theseOptions = null;
1128 }
Craig Mautner6170f732013-04-02 13:05:23 -07001129 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001130 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1131 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001132 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001133 if (res < 0) {
1134 return res;
1135 }
1136
1137 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1138 }
1139 }
1140 } finally {
1141 Binder.restoreCallingIdentity(origId);
1142 }
1143
1144 return ActivityManager.START_SUCCESS;
1145 }
1146
Craig Mautner2420ead2013-04-01 17:13:20 -07001147 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001148 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001149 throws RemoteException {
1150
Craig Mautner2568c3a2015-03-26 14:22:34 -07001151 if (andResume) {
1152 r.startFreezingScreenLocked(app, 0);
1153 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001154
Craig Mautner2568c3a2015-03-26 14:22:34 -07001155 // schedule launch ticks to collect information about slow apps.
1156 r.startLaunchTickingLocked();
1157 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001158
1159 // Have the window manager re-evaluate the orientation of
1160 // the screen based on the new activity order. Note that
1161 // as a result of this, it can call back into the activity
1162 // manager with a new orientation. We don't care about that,
1163 // because the activity is not currently running so we are
1164 // just restarting it anyway.
1165 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001166 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001167 mService.mConfiguration,
1168 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1169 mService.updateConfigurationLocked(config, r, false, false);
1170 }
1171
1172 r.app = app;
1173 app.waitingToKill = null;
1174 r.launchCount++;
1175 r.lastLaunchTime = SystemClock.uptimeMillis();
1176
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001177 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001178
1179 int idx = app.activities.indexOf(r);
1180 if (idx < 0) {
1181 app.activities.add(r);
1182 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001183 mService.updateLruProcessLocked(app, true, null);
1184 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001185
Craig Mautner15df08a2015-04-01 12:17:18 -07001186 final TaskRecord task = r.task;
1187 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001188 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001189 }
1190
1191 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001192 try {
1193 if (app.thread == null) {
1194 throw new RemoteException();
1195 }
1196 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001197 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001198 if (andResume) {
1199 results = r.results;
1200 newIntents = r.newIntents;
1201 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001202 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1203 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1204 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001205 if (andResume) {
1206 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1207 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001208 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001209 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001210 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001211 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001212 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001213 }
1214 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1215 r.sleeping = false;
1216 r.forceNewConfig = false;
1217 mService.showAskCompatModeDialogLocked(r);
1218 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001219 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001220 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1221 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1222 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001223 final String profileFile = mService.mProfileFile;
1224 if (profileFile != null) {
1225 ParcelFileDescriptor profileFd = mService.mProfileFd;
1226 if (profileFd != null) {
1227 try {
1228 profileFd = profileFd.dup();
1229 } catch (IOException e) {
1230 if (profileFd != null) {
1231 try {
1232 profileFd.close();
1233 } catch (IOException o) {
1234 }
1235 profileFd = null;
1236 }
1237 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001238 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001239
1240 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1241 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001242 }
1243 }
1244 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001245
Craig Mautner2568c3a2015-03-26 14:22:34 -07001246 if (andResume) {
1247 app.hasShownUi = true;
1248 app.pendingUiClean = true;
1249 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001250 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001252 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001253 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001254 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001255 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001256
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001257 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001258 // This may be a heavy-weight process! Note that the package
1259 // manager will ensure that only activity can run in the main
1260 // process of the .apk, which is the only thing that will be
1261 // considered heavy-weight.
1262 if (app.processName.equals(app.info.packageName)) {
1263 if (mService.mHeavyWeightProcess != null
1264 && mService.mHeavyWeightProcess != app) {
1265 Slog.w(TAG, "Starting new heavy weight process " + app
1266 + " when already running "
1267 + mService.mHeavyWeightProcess);
1268 }
1269 mService.mHeavyWeightProcess = app;
1270 Message msg = mService.mHandler.obtainMessage(
1271 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1272 msg.obj = r;
1273 mService.mHandler.sendMessage(msg);
1274 }
1275 }
1276
1277 } catch (RemoteException e) {
1278 if (r.launchFailed) {
1279 // This is the second time we failed -- finish activity
1280 // and give up.
1281 Slog.e(TAG, "Second failure launching "
1282 + r.intent.getComponent().flattenToShortString()
1283 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001284 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001285 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1286 "2nd-crash", false);
1287 return false;
1288 }
1289
1290 // This is the first time we failed -- restart process and
1291 // retry.
1292 app.activities.remove(r);
1293 throw e;
1294 }
1295
1296 r.launchFailed = false;
1297 if (stack.updateLRUListLocked(r)) {
1298 Slog.w(TAG, "Activity " + r
1299 + " being launched, but already in LRU list");
1300 }
1301
1302 if (andResume) {
1303 // As part of the process of launching, ActivityThread also performs
1304 // a resume.
1305 stack.minimalResumeActivityLocked(r);
1306 } else {
1307 // This activity is not starting in the resumed state... which
1308 // should look like we asked it to pause+stop (but remain visible),
1309 // and it has done so and reported back the current icicle and
1310 // other state.
1311 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1312 + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001313 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001314 r.stopped = true;
1315 }
1316
1317 // Launch the new version setup screen if needed. We do this -after-
1318 // launching the initial activity (that is, home), so that it can have
1319 // a chance to initialize itself while in the background, making the
1320 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001321 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001322 mService.startSetupActivityLocked();
1323 }
1324
Dianne Hackborn465fa392014-09-14 14:21:18 -07001325 // Update any services we are bound to that might care about whether
1326 // their client may have activities.
1327 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1328
Craig Mautner2420ead2013-04-01 17:13:20 -07001329 return true;
1330 }
1331
Craig Mautnere79d42682013-04-01 19:01:53 -07001332 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001333 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001334 // Is this activity's application already running?
1335 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001336 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001337
1338 r.task.stack.setLaunchTime(r);
1339
1340 if (app != null && app.thread != null) {
1341 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001342 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1343 || !"android".equals(r.info.packageName)) {
1344 // Don't add this if it is a platform component that is marked
1345 // to run in multiple processes, because this is actually
1346 // part of the framework so doesn't make sense to track as a
1347 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001348 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1349 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001350 }
George Mount2c92c972014-03-20 09:38:23 -07001351 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001352 return;
1353 } catch (RemoteException e) {
1354 Slog.w(TAG, "Exception when starting activity "
1355 + r.intent.getComponent().flattenToShortString(), e);
1356 }
1357
1358 // If a dead object exception was thrown -- fall through to
1359 // restart the application.
1360 }
1361
1362 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001363 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001364 }
1365
Craig Mautner6170f732013-04-02 13:05:23 -07001366 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001367 Intent intent, String resolvedType, ActivityInfo aInfo,
1368 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1369 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001370 int callingPid, int callingUid, String callingPackage,
1371 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001372 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1373 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001374 int err = ActivityManager.START_SUCCESS;
1375
1376 ProcessRecord callerApp = null;
1377 if (caller != null) {
1378 callerApp = mService.getRecordForAppLocked(caller);
1379 if (callerApp != null) {
1380 callingPid = callerApp.pid;
1381 callingUid = callerApp.info.uid;
1382 } else {
1383 Slog.w(TAG, "Unable to find app for caller " + caller
1384 + " (pid=" + callingPid + ") when starting: "
1385 + intent.toString());
1386 err = ActivityManager.START_PERMISSION_DENIED;
1387 }
1388 }
1389
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001390 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1391
Craig Mautner6170f732013-04-02 13:05:23 -07001392 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001393 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001394 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001395 + " on display " + (container == null ? (mFocusedStack == null ?
1396 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1397 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1398 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001399 }
1400
1401 ActivityRecord sourceRecord = null;
1402 ActivityRecord resultRecord = null;
1403 if (resultTo != null) {
1404 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001405 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1406 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001407 if (sourceRecord != null) {
1408 if (requestCode >= 0 && !sourceRecord.finishing) {
1409 resultRecord = sourceRecord;
1410 }
1411 }
1412 }
Craig Mautner6170f732013-04-02 13:05:23 -07001413
Dianne Hackborn91097de2014-04-04 18:02:06 -07001414 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001415
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001416 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001417 // Transfer the result target from the source activity to the new
1418 // one being started, including any failures.
1419 if (requestCode >= 0) {
1420 ActivityOptions.abort(options);
1421 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1422 }
1423 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001424 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1425 resultRecord = null;
1426 }
Craig Mautner6170f732013-04-02 13:05:23 -07001427 resultWho = sourceRecord.resultWho;
1428 requestCode = sourceRecord.requestCode;
1429 sourceRecord.resultTo = null;
1430 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001431 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001432 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001433 if (sourceRecord.launchedFromUid == callingUid) {
1434 // The new activity is being launched from the same uid as the previous
1435 // activity in the flow, and asking to forward its result back to the
1436 // previous. In this case the activity is serving as a trampoline between
1437 // the two, so we also want to update its launchedFromPackage to be the
1438 // same as the previous activity. Note that this is safe, since we know
1439 // these two packages come from the same uid; the caller could just as
1440 // well have supplied that same package name itself. This specifially
1441 // deals with the case of an intent picker/chooser being launched in the app
1442 // flow to redirect to an activity picked by the user, where we want the final
1443 // activity to consider it to have been launched by the previous app activity.
1444 callingPackage = sourceRecord.launchedFromPackage;
1445 }
Craig Mautner6170f732013-04-02 13:05:23 -07001446 }
1447
1448 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1449 // We couldn't find a class that can handle the given Intent.
1450 // That's the end of that!
1451 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1452 }
1453
1454 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1455 // We couldn't find the specific class specified in the Intent.
1456 // Also the end of the line.
1457 err = ActivityManager.START_CLASS_NOT_FOUND;
1458 }
1459
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001460 if (err == ActivityManager.START_SUCCESS
1461 && !isCurrentProfileLocked(userId)
1462 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1463 // Trying to launch a background activity that doesn't show for all users.
1464 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1465 }
1466
Dianne Hackborn91097de2014-04-04 18:02:06 -07001467 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1468 && sourceRecord.task.voiceSession != null) {
1469 // If this activity is being launched as part of a voice session, we need
1470 // to ensure that it is safe to do so. If the upcoming activity will also
1471 // be part of the voice session, we can only launch it if it has explicitly
1472 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001473 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001474 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1475 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001476 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1477 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001478 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1479 }
1480 } catch (RemoteException e) {
1481 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1482 }
1483 }
1484 }
1485
1486 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1487 // If the caller is starting a new voice session, just make sure the target
1488 // is actually allowing it to run this way.
1489 try {
1490 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1491 intent, resolvedType)) {
1492 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1493 }
1494 } catch (RemoteException e) {
1495 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1496 }
1497 }
1498
louis_changcd5d1982014-08-01 10:09:08 +08001499 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1500
Craig Mautner6170f732013-04-02 13:05:23 -07001501 if (err != ActivityManager.START_SUCCESS) {
1502 if (resultRecord != null) {
1503 resultStack.sendActivityResultLocked(-1,
1504 resultRecord, resultWho, requestCode,
1505 Activity.RESULT_CANCELED, null);
1506 }
Craig Mautner6170f732013-04-02 13:05:23 -07001507 ActivityOptions.abort(options);
1508 return err;
1509 }
1510
1511 final int startAnyPerm = mService.checkPermission(
1512 START_ANY_ACTIVITY, callingPid, callingUid);
1513 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1514 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1515 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1516 if (resultRecord != null) {
1517 resultStack.sendActivityResultLocked(-1,
1518 resultRecord, resultWho, requestCode,
1519 Activity.RESULT_CANCELED, null);
1520 }
Craig Mautner6170f732013-04-02 13:05:23 -07001521 String msg;
1522 if (!aInfo.exported) {
1523 msg = "Permission Denial: starting " + intent.toString()
1524 + " from " + callerApp + " (pid=" + callingPid
1525 + ", uid=" + callingUid + ")"
1526 + " not exported from uid " + aInfo.applicationInfo.uid;
1527 } else {
1528 msg = "Permission Denial: starting " + intent.toString()
1529 + " from " + callerApp + " (pid=" + callingPid
1530 + ", uid=" + callingUid + ")"
1531 + " requires " + aInfo.permission;
1532 }
1533 Slog.w(TAG, msg);
1534 throw new SecurityException(msg);
1535 }
1536
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001537 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001538 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001539
Craig Mautner6170f732013-04-02 13:05:23 -07001540 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001541 try {
1542 // The Intent we give to the watcher has the extra data
1543 // stripped off, since it can contain private information.
1544 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001545 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001546 aInfo.applicationInfo.packageName);
1547 } catch (RemoteException e) {
1548 mService.mController = null;
1549 }
Ben Gruver5e207332013-04-03 17:41:37 -07001550 }
Craig Mautner6170f732013-04-02 13:05:23 -07001551
Ben Gruver5e207332013-04-03 17:41:37 -07001552 if (abort) {
1553 if (resultRecord != null) {
1554 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001555 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001556 }
Ben Gruver5e207332013-04-03 17:41:37 -07001557 // We pretend to the caller that it was really started, but
1558 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001559 ActivityOptions.abort(options);
1560 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001561 }
1562
1563 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001564 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001565 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001566 if (outActivity != null) {
1567 outActivity[0] = r;
1568 }
1569
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001570 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001571 if (voiceSession == null && (stack.mResumedActivity == null
1572 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001573 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1574 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001575 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001576 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001577 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001578 ActivityOptions.abort(options);
1579 return ActivityManager.START_SWITCHES_CANCELED;
1580 }
1581 }
1582
1583 if (mService.mDidAppSwitch) {
1584 // This is the second allowed switch since we stopped switches,
1585 // so now just generally allow switches. Use case: user presses
1586 // home (switches disabled, switch to home, mDidAppSwitch now true);
1587 // user taps a home icon (coming from home so allowed, we hit here
1588 // and now allow anyone to switch again).
1589 mService.mAppSwitchesAllowedTime = 0;
1590 } else {
1591 mService.mDidAppSwitch = true;
1592 }
1593
Craig Mautneree36c772014-07-16 14:56:05 -07001594 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001595
Dianne Hackborn91097de2014-04-04 18:02:06 -07001596 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001597 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001598
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001599 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001600 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001601 // activity start, but we are not actually doing an activity
1602 // switch... just dismiss the keyguard now, because we
1603 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001604 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001605 }
1606 return err;
1607 }
1608
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001609 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001610 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001611
1612 // On leanback only devices we should keep all activities in the same stack.
1613 if (!mLeanbackOnlyDevice &&
1614 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001615
1616 ActivityStack stack;
1617
Wale Ogunwale7d701172015-03-11 15:36:30 -07001618 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001619 stack = task.stack;
1620 if (stack.isOnHomeDisplay()) {
1621 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001622 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1623 "computeStackFocus: Setting " + "focused stack to r=" + r
1624 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001625 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001626 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001627 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001628 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001629 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001630 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001631 }
1632
Craig Mautnere0a38842013-12-16 16:14:02 -08001633 final ActivityContainer container = r.mInitialActivityContainer;
1634 if (container != null) {
1635 // The first time put it on the desired stack, after this put on task stack.
1636 r.mInitialActivityContainer = null;
1637 return container.mStack;
1638 }
1639
Craig Mautner1b4bf852014-05-26 15:06:32 -07001640 if (mFocusedStack != mHomeStack && (!newTask ||
1641 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001642 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001643 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001644 return mFocusedStack;
1645 }
1646
Craig Mautnere0a38842013-12-16 16:14:02 -08001647 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1648 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001649 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001650 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001651 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001652 "computeStackFocus: Setting focused stack=" + stack);
1653 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001654 }
1655 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001656
Craig Mautner4a1cb222013-12-04 16:14:06 -08001657 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001658 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001659 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1660 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001661 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001662 }
1663 return mHomeStack;
1664 }
1665
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001666 boolean setFocusedStack(ActivityRecord r, String reason) {
1667 if (r == null) {
1668 // Not sure what you are trying to do, but it is not going to work...
1669 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001670 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001671 final TaskRecord task = r.task;
1672 if (task == null || task.stack == null) {
1673 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1674 return false;
1675 }
1676 task.stack.moveToFront(reason);
1677 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001678 }
1679
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001680 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001681 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001682 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001683 final Intent intent = r.intent;
1684 final int callingUid = r.launchedFromUid;
1685
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001686 // In some flows in to this function, we retrieve the task record and hold on to it
1687 // without a lock before calling back in to here... so the task at this point may
1688 // not actually be in recents. Check for that, and if it isn't in recents just
1689 // consider it invalid.
1690 if (inTask != null && !inTask.inRecents) {
1691 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1692 inTask = null;
1693 }
1694
Craig Mautnerad400af2014-08-13 16:41:36 -07001695 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001696 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1697 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001698
Craig Mautnera228ae92014-07-09 05:44:55 -07001699 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001700 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001701 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001702 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1703 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1704 "\"singleInstance\" or \"singleTask\"");
1705 launchFlags &=
1706 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1707 } else {
1708 switch (r.info.documentLaunchMode) {
1709 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1710 break;
1711 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1712 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1713 break;
1714 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1715 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1716 break;
1717 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1718 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1719 break;
1720 }
1721 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001722
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001723 final boolean launchTaskBehind = r.mLaunchTaskBehind
1724 && !launchSingleTask && !launchSingleInstance
1725 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001726
Wale Ogunwale7d701172015-03-11 15:36:30 -07001727 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1728 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001729 // For whatever reason this activity is being launched into a new
1730 // task... yet the caller has requested a result back. Well, that
1731 // is pretty messed up, so instead immediately send back a cancel
1732 // and let the new task continue launched as normal without a
1733 // dependency on its originator.
1734 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1735 r.resultTo.task.stack.sendActivityResultLocked(-1,
1736 r.resultTo, r.resultWho, r.requestCode,
1737 Activity.RESULT_CANCELED, null);
1738 r.resultTo = null;
1739 }
1740
1741 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1742 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1743 }
1744
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001745 // If we are actually going to launch in to a new task, there are some cases where
1746 // we further want to do multiple task.
1747 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1748 if (launchTaskBehind
1749 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1750 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1751 }
1752 }
1753
Craig Mautner8849a5e2013-04-02 16:41:03 -07001754 // We'll invoke onUserLeaving before onPause only if the launching
1755 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001756 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001757 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1758 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001759
1760 // If the caller has asked not to resume at this point, we make note
1761 // of this in the record so that we can skip it when trying to find
1762 // the top running activity.
1763 if (!doResume) {
1764 r.delayedResume = true;
1765 }
1766
Craig Mautnera254cd72014-05-25 16:47:39 -07001767 ActivityRecord notTop =
1768 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001769
1770 // If the onlyIfNeeded flag is set, then we can do this if the activity
1771 // being launched is the same as the one making the call... or, as
1772 // a special case, if we do not know the caller then we count the
1773 // current top activity as the caller.
1774 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1775 ActivityRecord checkedCaller = sourceRecord;
1776 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001777 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001778 }
1779 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1780 // Caller is not the same as launcher, so always needed.
1781 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1782 }
1783 }
1784
Craig Mautner8849a5e2013-04-02 16:41:03 -07001785 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001786 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001787
1788 // If the caller is not coming from another activity, but has given us an
1789 // explicit task into which they would like us to launch the new activity,
1790 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001791 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1792 final Intent baseIntent = inTask.getBaseIntent();
1793 final ActivityRecord root = inTask.getRootActivity();
1794 if (baseIntent == null) {
1795 ActivityOptions.abort(options);
1796 throw new IllegalArgumentException("Launching into task without base intent: "
1797 + inTask);
1798 }
1799
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001800 // If this task is empty, then we are adding the first activity -- it
1801 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001802 if (launchSingleInstance || launchSingleTask) {
1803 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1804 ActivityOptions.abort(options);
1805 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1806 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001807 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001808 if (root != null) {
1809 ActivityOptions.abort(options);
1810 throw new IllegalArgumentException("Caller with inTask " + inTask
1811 + " has root " + root + " but target is singleInstance/Task");
1812 }
1813 }
1814
1815 // If task is empty, then adopt the interesting intent launch flags in to the
1816 // activity being started.
1817 if (root == null) {
1818 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1819 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1820 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1821 launchFlags = (launchFlags&~flagsOfInterest)
1822 | (baseIntent.getFlags()&flagsOfInterest);
1823 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001824 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001825 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001826
Dianne Hackborn962d5352014-08-29 16:27:40 -07001827 // If the task is not empty and the caller is asking to start it as the root
1828 // of a new task, then we don't actually want to start this on the task. We
1829 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001830 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001831 addingToTask = false;
1832
1833 } else {
1834 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001835 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001836
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001837 reuseTask = inTask;
1838 } else {
1839 inTask = null;
1840 }
1841
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001842 if (inTask == null) {
1843 if (sourceRecord == null) {
1844 // This activity is not being started from another... in this
1845 // case we -always- start a new task.
1846 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1847 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1848 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1849 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1850 }
1851 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1852 // The original activity who is starting us is running as a single
1853 // instance... this new activity it is starting must go on its
1854 // own task.
1855 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1856 } else if (launchSingleInstance || launchSingleTask) {
1857 // The activity being started is a single instance... it always
1858 // gets launched into its own task.
1859 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1860 }
1861 }
1862
1863 ActivityInfo newTaskInfo = null;
1864 Intent newTaskIntent = null;
1865 ActivityStack sourceStack;
1866 if (sourceRecord != null) {
1867 if (sourceRecord.finishing) {
1868 // If the source is finishing, we can't further count it as our source. This
1869 // is because the task it is associated with may now be empty and on its way out,
1870 // so we don't want to blindly throw it in to that task. Instead we will take
1871 // the NEW_TASK flow and try to find a task for it. But save the task information
1872 // so it can be used when creating the new task.
1873 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1874 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1875 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1876 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1877 newTaskInfo = sourceRecord.info;
1878 newTaskIntent = sourceRecord.task.intent;
1879 }
1880 sourceRecord = null;
1881 sourceStack = null;
1882 } else {
1883 sourceStack = sourceRecord.task.stack;
1884 }
1885 } else {
1886 sourceStack = null;
1887 }
1888
1889 boolean movedHome = false;
1890 ActivityStack targetStack;
1891
1892 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001893 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001894
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001895 // We may want to try to place the new activity in to an existing task. We always
1896 // do this if the target activity is singleTask or singleInstance; we will also do
1897 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1898 // us to still place it in a new task: multi task, always doc mode, or being asked to
1899 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001900 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1901 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001902 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001903 // If bring to front is requested, and no result is requested and we have not
1904 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001905 // we can find a task that was started with this same
1906 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001907 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001908 // See if there is a task to bring to the front. If this is
1909 // a SINGLE_INSTANCE activity, there can be one and only one
1910 // instance of it in the history, and it is always in its own
1911 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001912 ActivityRecord intentActivity = !launchSingleInstance ?
1913 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001914 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001915 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001916 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001917 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001918 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1919 }
Craig Mautner29219d92013-04-16 20:19:12 -07001920 if (r.task == null) {
1921 r.task = intentActivity.task;
1922 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001923 if (intentActivity.task.intent == null) {
1924 // This task was started because of movement of
1925 // the activity based on affinity... now that we
1926 // are actually launching it, we can assign the
1927 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001928 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001929 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001930 targetStack = intentActivity.task.stack;
1931 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001932 // If the target task is not in the front, then we need
1933 // to bring it to the front... except... well, with
1934 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1935 // to have the same behavior as if a new instance was
1936 // being started, which means not bringing it to the front
1937 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001938 final ActivityStack focusStack = getFocusedStack();
1939 ActivityRecord curTop = (focusStack == null)
1940 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001941 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001942 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001943 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001944 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001945 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1946 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001947 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001948 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001949 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1950 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001951 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001952 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1953 options, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001954 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001955 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001956 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1957 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001958 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001959 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001960 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001961 options = null;
1962 }
1963 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001964 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001965 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001966 + " from " + intentActivity);
1967 targetStack.moveToFront("intentActivityFound");
1968 }
1969
Craig Mautner8849a5e2013-04-02 16:41:03 -07001970 // If the caller has requested that the target task be
1971 // reset, then do so.
1972 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1973 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1974 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001975 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001976 // We don't need to start a new activity, and
1977 // the client said not to do anything if that
1978 // is the case, so this is it! And for paranoia, make
1979 // sure we have correctly resumed the top activity.
1980 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001981 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001982
1983 // Make sure to notify Keyguard as well if we are not running an app
1984 // transition later.
1985 if (!movedToFront) {
1986 notifyActivityDrawnForKeyguard();
1987 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001988 } else {
1989 ActivityOptions.abort(options);
1990 }
1991 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1992 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001993 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001994 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001995 // The caller has requested to completely replace any
1996 // existing task with its new activity. Well that should
1997 // not be too hard...
1998 reuseTask = intentActivity.task;
1999 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002000 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002001 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002002 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002003 // In this situation we want to remove all activities
2004 // from the task up to the one being started. In most
2005 // cases this means we are resetting the task to its
2006 // initial state.
2007 ActivityRecord top =
2008 intentActivity.task.performClearTaskLocked(r, launchFlags);
2009 if (top != null) {
2010 if (top.frontOfTask) {
2011 // Activity aliases may mean we use different
2012 // intents for the top activity, so make sure
2013 // the task now has the identity of the new
2014 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002015 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002016 }
2017 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2018 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002019 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002020 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002021 // A special case: we need to start the activity because it is not
2022 // currently running, and the caller has asked to clear the current
2023 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002024 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002025 // Now pretend like this activity is being started by the top of its
2026 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002027 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002028 TaskRecord task = sourceRecord.task;
2029 if (task != null && task.stack == null) {
2030 // Target stack got cleared when we all activities were removed
2031 // above. Go ahead and reset it.
2032 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2033 targetStack.addTask(
2034 task, !launchTaskBehind /* toTop */, false /* moving */);
2035 }
2036
Craig Mautner8849a5e2013-04-02 16:41:03 -07002037 }
2038 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2039 // In this case the top activity on the task is the
2040 // same as the one being launched, so we take that
2041 // as a request to bring the task to the foreground.
2042 // If the top activity in the task is the root
2043 // activity, deliver this new intent to it if it
2044 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002045 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002046 && intentActivity.realActivity.equals(r.realActivity)) {
2047 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2048 intentActivity.task);
2049 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002050 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002051 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002052 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2053 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002054 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2055 // In this case we are launching the root activity
2056 // of the task, but with a different intent. We
2057 // should start a new instance on top.
2058 addingToTask = true;
2059 sourceRecord = intentActivity;
2060 }
2061 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2062 // In this case an activity is being launched in to an
2063 // existing task, without resetting that task. This
2064 // is typically the situation of launching an activity
2065 // from a notification or shortcut. We want to place
2066 // the new activity on top of the current task.
2067 addingToTask = true;
2068 sourceRecord = intentActivity;
2069 } else if (!intentActivity.task.rootWasReset) {
2070 // In this case we are launching in to an existing task
2071 // that has not yet been started from its front door.
2072 // The current task has been brought to the front.
2073 // Ideally, we'd probably like to place this new task
2074 // at the bottom of its stack, but that's a little hard
2075 // to do with the current organization of the code so
2076 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002077 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002078 }
2079 if (!addingToTask && reuseTask == null) {
2080 // We didn't do anything... but it was needed (a.k.a., client
2081 // don't use that intent!) And for paranoia, make
2082 // sure we have correctly resumed the top activity.
2083 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002084 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002085 if (!movedToFront) {
2086 // Make sure to notify Keyguard as well if we are not running an app
2087 // transition later.
2088 notifyActivityDrawnForKeyguard();
2089 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002090 } else {
2091 ActivityOptions.abort(options);
2092 }
2093 return ActivityManager.START_TASK_TO_FRONT;
2094 }
2095 }
2096 }
2097 }
2098
2099 //String uri = r.intent.toURI();
2100 //Intent intent2 = new Intent(uri);
2101 //Slog.i(TAG, "Given intent: " + r.intent);
2102 //Slog.i(TAG, "URI is: " + uri);
2103 //Slog.i(TAG, "To intent: " + intent2);
2104
2105 if (r.packageName != null) {
2106 // If the activity being launched is the same as the one currently
2107 // at the top, then we need to check if it should only be launched
2108 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002109 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002110 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002111 if (top != null && r.resultTo == null) {
2112 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2113 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002114 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002115 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002116 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2117 top.task);
2118 // For paranoia, make sure we have correctly
2119 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002120 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002122 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002123 }
2124 ActivityOptions.abort(options);
2125 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2126 // We don't need to start a new activity, and
2127 // the client said not to do anything if that
2128 // is the case, so this is it!
2129 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2130 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002131 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002132 return ActivityManager.START_DELIVERED_TO_TOP;
2133 }
2134 }
2135 }
2136 }
2137
2138 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002139 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002140 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2141 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002142 }
2143 ActivityOptions.abort(options);
2144 return ActivityManager.START_CLASS_NOT_FOUND;
2145 }
2146
2147 boolean newTask = false;
2148 boolean keepCurTransition = false;
2149
Craig Mautnerbb742462014-07-07 15:28:55 -07002150 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002151 sourceRecord.task : null;
2152
Craig Mautner8849a5e2013-04-02 16:41:03 -07002153 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002154 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002155 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002156 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002157 targetStack = computeStackFocus(r, newTask);
2158 targetStack.moveToFront("startingNewTask");
2159
Craig Mautner8849a5e2013-04-02 16:41:03 -07002160 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002161 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2162 newTaskInfo != null ? newTaskInfo : r.info,
2163 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002164 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2165 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002166 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2167 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002168 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002169 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002170 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002171 if (isLockTaskModeViolation(r.task)) {
2172 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2173 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2174 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002175 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002176 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002177 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2178 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002179 // Caller wants to appear on home activity, so before starting
2180 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002181 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002182 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002183 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002184 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002185 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002186 if (isLockTaskModeViolation(sourceTask)) {
2187 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2188 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2189 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002190 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002191 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002192 final TaskRecord topTask = targetStack.topTask();
2193 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002194 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2195 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002196 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002197 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002198 // In this case, we are adding the activity to an existing
2199 // task, but the caller has asked to clear that task if the
2200 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002201 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002202 keepCurTransition = true;
2203 if (top != null) {
2204 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002205 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002206 // For paranoia, make sure we have correctly
2207 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002208 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002209 if (doResume) {
2210 targetStack.resumeTopActivityLocked(null);
2211 }
2212 ActivityOptions.abort(options);
2213 return ActivityManager.START_DELIVERED_TO_TOP;
2214 }
2215 } else if (!addingToTask &&
2216 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2217 // In this case, we are launching an activity in our own task
2218 // that may already be running somewhere in the history, and
2219 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002220 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002221 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002222 final TaskRecord task = top.task;
2223 task.moveActivityToFrontLocked(top);
2224 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002225 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002226 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002227 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002228 if (doResume) {
2229 targetStack.resumeTopActivityLocked(null);
2230 }
2231 return ActivityManager.START_DELIVERED_TO_TOP;
2232 }
2233 }
2234 // An existing activity is starting this new activity, so we want
2235 // to keep the new one in the same task as the one that is starting
2236 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002237 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002238 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002239 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002240
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002241 } else if (inTask != null) {
2242 // The calling is asking that the new activity be started in an explicit
2243 // task it has provided to us.
2244 if (isLockTaskModeViolation(inTask)) {
2245 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2246 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2247 }
2248 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002249 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002250
2251 // Check whether we should actually launch the new activity in to the task,
2252 // or just reuse the current activity on top.
2253 ActivityRecord top = inTask.getTopActivity();
2254 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2255 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2256 || launchSingleTop || launchSingleTask) {
2257 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2258 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2259 // We don't need to start a new activity, and
2260 // the client said not to do anything if that
2261 // is the case, so this is it!
2262 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2263 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002264 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002265 return ActivityManager.START_DELIVERED_TO_TOP;
2266 }
2267 }
2268
Dianne Hackborn962d5352014-08-29 16:27:40 -07002269 if (!addingToTask) {
2270 // We don't actually want to have this activity added to the task, so just
2271 // stop here but still tell the caller that we consumed the intent.
2272 ActivityOptions.abort(options);
2273 return ActivityManager.START_TASK_TO_FRONT;
2274 }
2275
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002276 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002277 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002278 + " in explicit task " + r.task);
2279
Craig Mautner8849a5e2013-04-02 16:41:03 -07002280 } else {
2281 // This not being started from an existing activity, and not part
2282 // of a new task... just put it in the top task, though these days
2283 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002284 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002285 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002286 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002287 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002288 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002289 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002290 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002291 + " in new guessed " + r.task);
2292 }
2293
2294 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002295 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002296
Craig Mautner76e2a762014-06-24 09:05:43 -07002297 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2298 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2299 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002300 if (newTask) {
2301 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2302 }
2303 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002304 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002305 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002306 if (!launchTaskBehind) {
2307 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002308 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002309 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002310 return ActivityManager.START_SUCCESS;
2311 }
2312
Craig Mautneree36c772014-07-16 14:56:05 -07002313 final void doPendingActivityLaunchesLocked(boolean doResume) {
2314 while (!mPendingActivityLaunches.isEmpty()) {
2315 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002316 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002317 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002318 }
2319 }
2320
Craig Mautner7f13ed32014-07-28 14:00:35 -07002321 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002322 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2323 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002324 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002325 mPendingActivityLaunches.remove(palNdx);
2326 }
2327 }
2328 }
2329
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002330 void setLaunchSource(int uid) {
2331 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2332 }
2333
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002334 void acquireLaunchWakelock() {
2335 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2336 throw new IllegalStateException("Calling must be system uid");
2337 }
2338 mLaunchingActivity.acquire();
2339 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2340 // To be safe, don't allow the wake lock to be held for too long.
2341 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2342 }
2343 }
2344
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002345 /**
2346 * Called when the frontmost task is idle.
2347 * @return the state of mService.mBooting before this was called.
2348 */
2349 private boolean checkFinishBootingLocked() {
2350 final boolean booting = mService.mBooting;
2351 boolean enableScreen = false;
2352 mService.mBooting = false;
2353 if (!mService.mBooted) {
2354 mService.mBooted = true;
2355 enableScreen = true;
2356 }
2357 if (booting || enableScreen) {
2358 mService.postFinishBooting(booting, enableScreen);
2359 }
2360 return booting;
2361 }
2362
Craig Mautnerf3333272013-04-22 10:55:53 -07002363 // Checked.
2364 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2365 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002366 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002367
Craig Mautnerf3333272013-04-22 10:55:53 -07002368 ArrayList<ActivityRecord> stops = null;
2369 ArrayList<ActivityRecord> finishes = null;
2370 ArrayList<UserStartedState> startingUsers = null;
2371 int NS = 0;
2372 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002373 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002374 boolean activityRemoved = false;
2375
Wale Ogunwale7d701172015-03-11 15:36:30 -07002376 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002377 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002378 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2379 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002380 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2381 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002382 if (fromTimeout) {
2383 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002384 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002385
2386 // This is a hack to semi-deal with a race condition
2387 // in the client where it can be constructed with a
2388 // newer configuration from when we asked it to launch.
2389 // We'll update with whatever configuration it now says
2390 // it used to launch.
2391 if (config != null) {
2392 r.configuration = config;
2393 }
2394
2395 // We are now idle. If someone is waiting for a thumbnail from
2396 // us, we can now deliver.
2397 r.idle = true;
2398
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002399 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002400 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002401 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002402 }
2403 }
2404
2405 if (allResumedActivitiesIdle()) {
2406 if (r != null) {
2407 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002408 }
2409
2410 if (mLaunchingActivity.isHeld()) {
2411 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2412 if (VALIDATE_WAKE_LOCK_CALLER &&
2413 Binder.getCallingUid() != Process.myUid()) {
2414 throw new IllegalStateException("Calling must be system uid");
2415 }
2416 mLaunchingActivity.release();
2417 }
2418 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002419 }
2420
2421 // Atomically retrieve all of the other things to do.
2422 stops = processStoppingActivitiesLocked(true);
2423 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002424 if ((NF = mFinishingActivities.size()) > 0) {
2425 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002426 mFinishingActivities.clear();
2427 }
2428
Craig Mautnerf3333272013-04-22 10:55:53 -07002429 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002430 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002431 mStartingUsers.clear();
2432 }
2433
Craig Mautnerf3333272013-04-22 10:55:53 -07002434 // Stop any activities that are scheduled to do so but have been
2435 // waiting for the next one to start.
2436 for (int i = 0; i < NS; i++) {
2437 r = stops.get(i);
2438 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002439 if (stack != null) {
2440 if (r.finishing) {
2441 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2442 } else {
2443 stack.stopActivityLocked(r);
2444 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002445 }
2446 }
2447
2448 // Finish any activities that are scheduled to do so but have been
2449 // waiting for the next one to start.
2450 for (int i = 0; i < NF; i++) {
2451 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002452 final ActivityStack stack = r.task.stack;
2453 if (stack != null) {
2454 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2455 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002456 }
2457
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002458 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002459 // Complete user switch
2460 if (startingUsers != null) {
2461 for (int i = 0; i < startingUsers.size(); i++) {
2462 mService.finishUserSwitch(startingUsers.get(i));
2463 }
2464 }
2465 // Complete starting up of background users
2466 if (mStartingBackgroundUsers.size() > 0) {
2467 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2468 mStartingBackgroundUsers.clear();
2469 for (int i = 0; i < startingUsers.size(); i++) {
2470 mService.finishUserBoot(startingUsers.get(i));
2471 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002472 }
2473 }
2474
2475 mService.trimApplications();
2476 //dump();
2477 //mWindowManager.dump();
2478
Craig Mautnerf3333272013-04-22 10:55:53 -07002479 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002480 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002481 }
2482
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002483 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002484 }
2485
Craig Mautner8e569572013-10-11 17:36:59 -07002486 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002487 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002488 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2489 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002490 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2491 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2492 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002493 }
Craig Mautner19091252013-10-05 00:03:53 -07002494 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002495 }
2496
2497 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002498 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2499 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002500 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2501 stacks.get(stackNdx).closeSystemDialogsLocked();
2502 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002503 }
2504 }
2505
Craig Mautner93529a42013-10-04 15:03:13 -07002506 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002507 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002508 }
2509
Craig Mautner8d341ef2013-03-26 09:03:27 -07002510 /**
2511 * @return true if some activity was finished (or would have finished if doit were true).
2512 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002513 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2514 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002515 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002516 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2517 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002518 final int numStacks = stacks.size();
2519 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2520 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002521 if (stack.finishDisabledPackageActivitiesLocked(
2522 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002523 didSomething = true;
2524 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002525 }
2526 }
2527 return didSomething;
2528 }
2529
Dianne Hackborna413dc02013-07-12 12:02:55 -07002530 void updatePreviousProcessLocked(ActivityRecord r) {
2531 // Now that this process has stopped, we may want to consider
2532 // it to be the previous app to try to keep around in case
2533 // the user wants to return to it.
2534
2535 // First, found out what is currently the foreground app, so that
2536 // we don't blow away the previous app if this activity is being
2537 // hosted by the process that is actually still the foreground.
2538 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002539 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2540 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002541 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2542 final ActivityStack stack = stacks.get(stackNdx);
2543 if (isFrontStack(stack)) {
2544 if (stack.mResumedActivity != null) {
2545 fgApp = stack.mResumedActivity.app;
2546 } else if (stack.mPausingActivity != null) {
2547 fgApp = stack.mPausingActivity.app;
2548 }
2549 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002550 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002551 }
2552 }
2553
2554 // Now set this one as the previous process, only if that really
2555 // makes sense to.
2556 if (r.app != null && fgApp != null && r.app != fgApp
2557 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002558 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002559 mService.mPreviousProcess = r.app;
2560 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2561 }
2562 }
2563
Craig Mautner05d29032013-05-03 13:40:13 -07002564 boolean resumeTopActivitiesLocked() {
2565 return resumeTopActivitiesLocked(null, null, null);
2566 }
2567
2568 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2569 Bundle targetOptions) {
2570 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002571 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002572 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002573 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002574 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002575 if (isFrontStack(targetStack)) {
2576 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2577 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002578
Craig Mautnere0a38842013-12-16 16:14:02 -08002579 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2580 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002581 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2582 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002583 if (stack == targetStack) {
2584 // Already started above.
2585 continue;
2586 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002587 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002588 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002589 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002590 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002591 }
Craig Mautner05d29032013-05-03 13:40:13 -07002592 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002593 }
2594
Todd Kennedy539db512014-12-15 09:57:55 -08002595 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002596 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2597 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002598 final int numStacks = stacks.size();
2599 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2600 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002601 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002602 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002603 }
2604 }
2605
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002606 void finishVoiceTask(IVoiceInteractionSession session) {
2607 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2608 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2609 final int numStacks = stacks.size();
2610 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2611 final ActivityStack stack = stacks.get(stackNdx);
2612 stack.finishVoiceTask(session);
2613 }
2614 }
2615 }
2616
Craig Mautner299f9602015-01-26 09:47:33 -08002617 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002618 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2619 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002620 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002621 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2622 // Caller wants the home activity moved with it. To accomplish this,
2623 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002624 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002625 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002626 if (task.stack == null) {
2627 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2628 + task + " to front. Stack is null");
2629 return;
2630 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002631 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002632 if (DEBUG_STACK) Slog.d(TAG_STACK,
2633 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002634 }
2635
Craig Mautner967212c2013-04-13 21:10:58 -07002636 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002637 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2638 if (activityContainer != null) {
2639 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002640 }
2641 return null;
2642 }
2643
Craig Mautner967212c2013-04-13 21:10:58 -07002644 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002645 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002646 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2647 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002648 }
2649 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002650 }
2651
Craig Mautner4a1cb222013-12-04 16:14:06 -08002652 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002653 ActivityRecord homeActivity = getHomeActivity();
2654 if (homeActivity != null) {
2655 return homeActivity.appToken;
2656 }
2657 return null;
2658 }
2659
2660 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002661 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002662 }
2663
2664 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002665 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2666 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2667 final TaskRecord task = tasks.get(taskNdx);
2668 if (task.isHomeTask()) {
2669 final ArrayList<ActivityRecord> activities = task.mActivities;
2670 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2671 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002672 if (r.isHomeActivity()
2673 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002674 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002675 }
2676 }
2677 }
2678 }
2679 return null;
2680 }
2681
Todd Kennedyca4d8422015-01-15 15:19:22 -08002682 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002683 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002684 ActivityContainer activityContainer =
2685 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002686 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002687 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002688 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002689 return activityContainer;
2690 }
2691
Craig Mautner34b73df2014-01-12 21:11:08 -08002692 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002693 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2694 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2695 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002696 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2697 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002698 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002699 }
2700 }
2701
Craig Mautner95da1082014-02-24 17:54:35 -08002702 void deleteActivityContainer(IActivityContainer container) {
2703 ActivityContainer activityContainer = (ActivityContainer)container;
2704 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002705 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2706 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002707 final int stackId = activityContainer.mStackId;
2708 mActivityContainers.remove(stackId);
2709 mWindowManager.removeStack(stackId);
2710 }
2711 }
2712
Wale Ogunwale60454db2015-01-23 16:05:07 -08002713 void resizeStackLocked(int stackId, Rect bounds) {
2714 final ActivityStack stack = getStack(stackId);
2715 if (stack == null) {
2716 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2717 return;
2718 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002719
2720 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002721 if (r != null && !r.task.mResizeable) {
2722 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002723 return;
2724 }
2725
Wale Ogunwale60454db2015-01-23 16:05:07 -08002726 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2727 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002728 if (r != null) {
2729 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2730 // And we need to make sure at this point that all other activities
2731 // are made visible with the correct configuration.
2732 ensureActivitiesVisibleLocked(r, 0);
2733 if (!updated) {
2734 resumeTopActivitiesLocked(stack, null, null);
2735 }
2736 }
2737 }
2738 }
2739
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002740 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2741 * current task stack if it only has one entry, moving the task to a stack that matches the
2742 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2743 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2744 task.mResizeable = true;
2745 final ActivityStack currentStack = task.stack;
2746 if (currentStack.isHomeStack()) {
2747 // Can't move task off the home stack. Sorry!
2748 return;
2749 }
2750
2751 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2752 if (matchingStackId != -1) {
2753 // There is already a stack with the right bounds!
2754 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2755 // Nothing to do here. Already in the right stack...
2756 return;
2757 }
2758 // Move task to stack with matching bounds.
2759 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2760 return;
2761 }
2762
2763 if (currentStack != null && currentStack.numTasks() == 1) {
2764 // Just resize the current stack since this is the task in it.
2765 resizeStackLocked(currentStack.mStackId, bounds);
2766 return;
2767 }
2768
2769 // Create new stack and move the task to it.
2770 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2771 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2772 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2773
2774 if (newStack == null) {
2775 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2776 return;
2777 }
2778 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2779 resizeStackLocked(newStack.mStackId, bounds);
2780 }
2781
Todd Kennedy4900bf92015-01-16 16:05:14 -08002782 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002783 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2784 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002785 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002786 }
2787
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002788 ActivityContainer activityContainer = new ActivityContainer(stackId);
2789 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002790 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002791 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002792 }
2793
2794 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002795 while (true) {
2796 if (++mLastStackId <= HOME_STACK_ID) {
2797 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002798 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002799 if (getStack(mLastStackId) == null) {
2800 break;
2801 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002802 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002803 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002804 }
2805
Wale Ogunwale7de05352014-12-12 15:21:33 -08002806 private boolean restoreRecentTaskLocked(TaskRecord task) {
2807 ActivityStack stack = null;
2808 // Determine stack to restore task to.
2809 if (mLeanbackOnlyDevice) {
2810 // There is only one stack for lean back devices.
2811 stack = mHomeStack;
2812 } else {
2813 // Look for the top stack on the home display that isn't the home stack.
2814 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2815 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2816 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002817 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002818 stack = tmpStack;
2819 break;
2820 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002821 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002822 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002823
2824 if (stack == null) {
2825 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2826 // before an application stack is created...Go ahead and create one on the default
2827 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002828 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002829 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002830 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002831 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2832 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002833 }
2834
2835 if (stack == null) {
2836 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002837 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2838 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002839 return false;
2840 }
2841
2842 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002843 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2844 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002845 final ArrayList<ActivityRecord> activities = task.mActivities;
2846 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2847 final ActivityRecord r = activities.get(activityNdx);
2848 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2849 r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002850 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Wale Ogunwale7de05352014-12-12 15:21:33 -08002851 r.userId, r.info.configChanges, task.voiceSession != null,
2852 r.mLaunchTaskBehind);
2853 }
2854 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002855 }
2856
Craig Mautner299f9602015-01-26 09:47:33 -08002857 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002858 final TaskRecord task = anyTaskForIdLocked(taskId);
2859 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002860 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002861 return;
2862 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002863 final ActivityStack stack = getStack(stackId);
2864 if (stack == null) {
2865 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2866 return;
2867 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002868 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2869 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07002870 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002871 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002872 stack.addTask(task, toTop, true);
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002873 // The task might have already been running and its visibility needs to be synchronized with
2874 // the visibility of the stack / windows.
2875 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07002876 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002877 }
2878
Craig Mautnerac6f8432013-07-17 13:24:59 -07002879 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002880 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002881 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2882 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002883 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2884 final ActivityStack stack = stacks.get(stackNdx);
2885 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002886 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002887 continue;
2888 }
2889 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002890 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2891 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002892 continue;
2893 }
2894 final ActivityRecord ar = stack.findTaskLocked(r);
2895 if (ar != null) {
2896 return ar;
2897 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002898 }
2899 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002900 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002901 return null;
2902 }
2903
2904 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002905 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2906 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002907 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2908 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2909 if (ar != null) {
2910 return ar;
2911 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002912 }
2913 }
2914 return null;
2915 }
2916
Craig Mautner8d341ef2013-03-26 09:03:27 -07002917 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002918 scheduleSleepTimeout();
2919 if (!mGoingToSleep.isHeld()) {
2920 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002921 if (mLaunchingActivity.isHeld()) {
2922 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2923 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002924 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002925 mLaunchingActivity.release();
2926 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002927 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002928 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002929 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002930 }
2931
2932 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002933 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002934
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002935 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002936 final long endTime = System.currentTimeMillis() + timeout;
2937 while (true) {
2938 boolean cantShutdown = false;
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 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2943 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002944 }
2945 if (cantShutdown) {
2946 long timeRemaining = endTime - System.currentTimeMillis();
2947 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002948 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002949 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002950 } catch (InterruptedException e) {
2951 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002952 } else {
2953 Slog.w(TAG, "Activity manager shutdown timed out");
2954 timedout = true;
2955 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002956 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002957 } else {
2958 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002959 }
2960 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002961
2962 // Force checkReadyForSleep to complete.
2963 mSleepTimeout = true;
2964 checkReadyForSleepLocked();
2965
Craig Mautner8d341ef2013-03-26 09:03:27 -07002966 return timedout;
2967 }
2968
2969 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002970 removeSleepTimeouts();
2971 if (mGoingToSleep.isHeld()) {
2972 mGoingToSleep.release();
2973 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002974 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2975 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002976 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2977 final ActivityStack stack = stacks.get(stackNdx);
2978 stack.awakeFromSleepingLocked();
2979 if (isFrontStack(stack)) {
2980 resumeTopActivitiesLocked();
2981 }
Craig Mautner5314a402013-09-26 12:40:16 -07002982 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002983 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002984 mGoingToSleepActivities.clear();
2985 }
2986
2987 void activitySleptLocked(ActivityRecord r) {
2988 mGoingToSleepActivities.remove(r);
2989 checkReadyForSleepLocked();
2990 }
2991
2992 void checkReadyForSleepLocked() {
2993 if (!mService.isSleepingOrShuttingDown()) {
2994 // Do not care.
2995 return;
2996 }
2997
2998 if (!mSleepTimeout) {
2999 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003000 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3001 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003002 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3003 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3004 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003005 }
3006
3007 if (mStoppingActivities.size() > 0) {
3008 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003009 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003010 + mStoppingActivities.size() + " activities");
3011 scheduleIdleLocked();
3012 dontSleep = true;
3013 }
3014
3015 if (mGoingToSleepActivities.size() > 0) {
3016 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003017 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003018 + mGoingToSleepActivities.size() + " activities");
3019 dontSleep = true;
3020 }
3021
3022 if (dontSleep) {
3023 return;
3024 }
3025 }
3026
Craig Mautnere0a38842013-12-16 16:14:02 -08003027 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3028 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003029 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3030 stacks.get(stackNdx).goToSleep();
3031 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003032 }
3033
3034 removeSleepTimeouts();
3035
3036 if (mGoingToSleep.isHeld()) {
3037 mGoingToSleep.release();
3038 }
3039 if (mService.mShuttingDown) {
3040 mService.notifyAll();
3041 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003042 }
3043
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003044 boolean reportResumedActivityLocked(ActivityRecord r) {
3045 final ActivityStack stack = r.task.stack;
3046 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003047 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003048 }
3049 if (allResumedActivitiesComplete()) {
3050 ensureActivitiesVisibleLocked(null, 0);
3051 mWindowManager.executeAppTransition();
3052 return true;
3053 }
3054 return false;
3055 }
3056
Craig Mautner8d341ef2013-03-26 09:03:27 -07003057 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003058 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3059 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003060 final int numStacks = stacks.size();
3061 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3062 final ActivityStack stack = stacks.get(stackNdx);
3063 stack.handleAppCrashLocked(app);
3064 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003065 }
3066 }
3067
Jose Lima4b6c6692014-08-12 17:41:12 -07003068 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003069 final ActivityStack stack = r.task.stack;
3070 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003071 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3072 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003073 return false;
3074 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003075 final boolean isVisible = stack.hasVisibleBehindActivity();
3076 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3077 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003078
3079 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003080 if (top == null || top == r || (visible == isVisible)) {
3081 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3082 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003083 return true;
3084 }
3085
3086 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003087 if (visible && top.fullscreen) {
3088 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003089 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003090 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003091 " top.app.thread=" + top.app.thread);
3092 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003093 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3094 // Only the activity set as currently visible behind should actively reset its
3095 // visible behind state.
3096 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3097 + visible + " stack.getVisibleBehindActivity()=" +
3098 stack.getVisibleBehindActivity() + " r=" + r);
3099 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003100 }
3101
Jose Lima4b6c6692014-08-12 17:41:12 -07003102 stack.setVisibleBehindActivity(visible ? r : null);
3103 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003104 // Make the activity immediately above r opaque.
3105 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3106 if (next != null) {
3107 mService.convertFromTranslucent(next.appToken);
3108 }
3109 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003110 if (top.app != null && top.app.thread != null) {
3111 // Notify the top app of the change.
3112 try {
3113 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3114 } catch (RemoteException e) {
3115 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003116 }
3117 return true;
3118 }
3119
Craig Mautnerbb742462014-07-07 15:28:55 -07003120 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3121 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3122 r.mLaunchTaskBehind = false;
3123 final TaskRecord task = r.task;
3124 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003125 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003126 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003127 mWindowManager.setAppVisibility(r.appToken, false);
3128 }
3129
3130 void scheduleLaunchTaskBehindComplete(IBinder token) {
3131 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3132 }
3133
Craig Mautnerde4ef022013-04-07 19:01:33 -07003134 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003135 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003136 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3137 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003138 final int topStackNdx = stacks.size() - 1;
3139 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3140 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003141 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003142 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003143 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003144 }
3145
3146 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003147 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3148 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003149 final int numStacks = stacks.size();
3150 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3151 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003152 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003153 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003154 }
3155 }
3156
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003157 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3158 // Examine all activities currently running in the process.
3159 TaskRecord firstTask = null;
3160 // Tasks is non-null only if two or more tasks are found.
3161 ArraySet<TaskRecord> tasks = null;
3162 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3163 for (int i=0; i<app.activities.size(); i++) {
3164 ActivityRecord r = app.activities.get(i);
3165 // First, if we find an activity that is in the process of being destroyed,
3166 // then we just aren't going to do anything for now; we want things to settle
3167 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003168 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003169 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3170 return;
3171 }
3172 // Don't consider any activies that are currently not in a state where they
3173 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003174 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3175 || r.state == PAUSED || r.state == STOPPING) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003176 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3177 continue;
3178 }
3179 if (r.task != null) {
3180 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3181 + " from " + r);
3182 if (firstTask == null) {
3183 firstTask = r.task;
3184 } else if (firstTask != r.task) {
3185 if (tasks == null) {
3186 tasks = new ArraySet<>();
3187 tasks.add(firstTask);
3188 }
3189 tasks.add(r.task);
3190 }
3191 }
3192 }
3193 if (tasks == null) {
3194 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3195 return;
3196 }
3197 // If we have activities in multiple tasks that are in a position to be destroyed,
3198 // let's iterate through the tasks and release the oldest one.
3199 final int numDisplays = mActivityDisplays.size();
3200 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3201 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3202 // Step through all stacks starting from behind, to hit the oldest things first.
3203 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3204 final ActivityStack stack = stacks.get(stackNdx);
3205 // Try to release activities in this stack; if we manage to, we are done.
3206 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3207 return;
3208 }
3209 }
3210 }
3211 }
3212
Craig Mautner8d341ef2013-03-26 09:03:27 -07003213 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003214 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003215 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003216 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003217
Craig Mautner858d8a62013-04-23 17:08:34 -07003218 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003219 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3220 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003221 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003222 final ActivityStack stack = stacks.get(stackNdx);
3223 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003224 TaskRecord task = stack.topTask();
3225 if (task != null) {
3226 mWindowManager.moveTaskToTop(task.taskId);
3227 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003228 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003229 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003230
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003231 ActivityStack stack = getStack(restoreStackId);
3232 if (stack == null) {
3233 stack = mHomeStack;
3234 }
3235 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003236 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003237 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003238 TaskRecord task = stack.topTask();
3239 if (task != null) {
3240 mWindowManager.moveTaskToTop(task.taskId);
3241 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003242 } else {
3243 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003244 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003245 }
Craig Mautner93529a42013-10-04 15:03:13 -07003246 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003247 }
3248
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003249 /**
3250 * Add background users to send boot completed events to.
3251 * @param userId The user being started in the background
3252 * @param uss The state object for the user.
3253 */
3254 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3255 mStartingBackgroundUsers.add(uss);
3256 }
3257
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003258 /** Checks whether the userid is a profile of the current user. */
3259 boolean isCurrentProfileLocked(int userId) {
3260 if (userId == mCurrentUser) return true;
3261 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3262 if (mService.mCurrentProfileIds[i] == userId) return true;
3263 }
3264 return false;
3265 }
3266
Craig Mautnerde4ef022013-04-07 19:01:33 -07003267 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003268 ArrayList<ActivityRecord> stops = null;
3269
3270 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003271 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3272 ActivityRecord s = mStoppingActivities.get(activityNdx);
3273 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003274 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003275 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3276 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003277 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003278 if (s.finishing) {
3279 // If this activity is finishing, it is sitting on top of
3280 // everyone else but we now know it is no longer needed...
3281 // so get rid of it. Otherwise, we need to go through the
3282 // normal flow and hide it once we determine that it is
3283 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003284 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003285 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003286 }
3287 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003288 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003289 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003290 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003291 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003292 }
3293 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003294 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003295 }
3296 }
3297
3298 return stops;
3299 }
3300
Craig Mautnercf910b02013-04-23 11:23:27 -07003301 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003302 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3303 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3304 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3305 final ActivityStack stack = stacks.get(stackNdx);
3306 final ActivityRecord r = stack.topRunningActivityLocked(null);
3307 final ActivityState state = r == null ? DESTROYED : r.state;
3308 if (isFrontStack(stack)) {
3309 if (r == null) Slog.e(TAG,
3310 "validateTop...: null top activity, stack=" + stack);
3311 else {
3312 final ActivityRecord pausing = stack.mPausingActivity;
3313 if (pausing != null && pausing == r) Slog.e(TAG,
3314 "validateTop...: top stack has pausing activity r=" + r
3315 + " state=" + state);
3316 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3317 "validateTop...: activity in front not resumed r=" + r
3318 + " state=" + state);
3319 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003320 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003321 final ActivityRecord resumed = stack.mResumedActivity;
3322 if (resumed != null && resumed == r) Slog.e(TAG,
3323 "validateTop...: back stack has resumed activity r=" + r
3324 + " state=" + state);
3325 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3326 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003327 }
3328 }
3329 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003330 }
3331
Craig Mautnere0570202015-05-13 13:06:11 -07003332 private String lockTaskModeToString() {
3333 switch (mLockTaskModeState) {
3334 case LOCK_TASK_MODE_LOCKED:
3335 return "LOCKED";
3336 case LOCK_TASK_MODE_PINNED:
3337 return "PINNED";
3338 case LOCK_TASK_MODE_NONE:
3339 return "NONE";
3340 default: return "unknown=" + mLockTaskModeState;
3341 }
3342 }
3343
Craig Mautner27084302013-03-25 08:05:25 -07003344 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003345 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003346 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003347 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3348 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3349 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003350 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003351 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3352 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3353 if (packages.size() > 0) {
3354 pw.println(" mLockTaskPackages (userId:packages)=");
3355 for (int i = 0; i < packages.size(); ++i) {
3356 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3357 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3358 }
3359 }
3360 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003361 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003362
Craig Mautner20e72272013-04-01 13:45:53 -07003363 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003364 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003365 }
3366
Dianne Hackborn390517b2013-05-30 15:03:32 -07003367 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3368 boolean needSep, String prefix) {
3369 if (activity != null) {
3370 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3371 if (needSep) {
3372 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003373 }
3374 pw.print(prefix);
3375 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003376 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003377 }
3378 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003379 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003380 }
3381
Craig Mautner8d341ef2013-03-26 09:03:27 -07003382 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3383 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003384 boolean printed = false;
3385 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003386 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3387 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003388 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003389 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003390 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003391 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003392 final ActivityStack stack = stacks.get(stackNdx);
3393 StringBuilder stackHeader = new StringBuilder(128);
3394 stackHeader.append(" Stack #");
3395 stackHeader.append(stack.mStackId);
3396 stackHeader.append(":");
3397 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3398 needSep, stackHeader.toString());
3399 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3400 !dumpAll, false, dumpPackage, true,
3401 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003402
Craig Mautner4a1cb222013-12-04 16:14:06 -08003403 needSep = printed;
3404 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3405 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003406 if (pr) {
3407 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003408 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003409 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003410 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3411 " mResumedActivity: ");
3412 if (pr) {
3413 printed = true;
3414 needSep = false;
3415 }
3416 if (dumpAll) {
3417 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3418 " mLastPausedActivity: ");
3419 if (pr) {
3420 printed = true;
3421 needSep = true;
3422 }
3423 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3424 needSep, " mLastNoHistoryActivity: ");
3425 }
3426 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003427 }
3428 }
3429
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003430 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3431 false, dumpPackage, true, " Activities waiting to finish:", null);
3432 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3433 false, dumpPackage, true, " Activities waiting to stop:", null);
3434 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3435 false, dumpPackage, true, " Activities waiting for another to become visible:",
3436 null);
3437 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3438 false, dumpPackage, true, " Activities waiting to sleep:", null);
3439 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3440 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003441
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003442 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003443 }
3444
Dianne Hackborn390517b2013-05-30 15:03:32 -07003445 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003446 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003447 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003448 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003449 String innerPrefix = null;
3450 String[] args = null;
3451 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003452 for (int i=list.size()-1; i>=0; i--) {
3453 final ActivityRecord r = list.get(i);
3454 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3455 continue;
3456 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003457 if (innerPrefix == null) {
3458 innerPrefix = prefix + " ";
3459 args = new String[0];
3460 }
3461 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003462 final boolean full = !brief && (complete || !r.isInHistory());
3463 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003464 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003465 needNL = false;
3466 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003467 if (header1 != null) {
3468 pw.println(header1);
3469 header1 = null;
3470 }
3471 if (header2 != null) {
3472 pw.println(header2);
3473 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003474 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003475 if (lastTask != r.task) {
3476 lastTask = r.task;
3477 pw.print(prefix);
3478 pw.print(full ? "* " : " ");
3479 pw.println(lastTask);
3480 if (full) {
3481 lastTask.dump(pw, prefix + " ");
3482 } else if (complete) {
3483 // Complete + brief == give a summary. Isn't that obvious?!?
3484 if (lastTask.intent != null) {
3485 pw.print(prefix); pw.print(" ");
3486 pw.println(lastTask.intent.toInsecureStringWithClip());
3487 }
3488 }
3489 }
3490 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3491 pw.print(" #"); pw.print(i); pw.print(": ");
3492 pw.println(r);
3493 if (full) {
3494 r.dump(pw, innerPrefix);
3495 } else if (complete) {
3496 // Complete + brief == give a summary. Isn't that obvious?!?
3497 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3498 if (r.app != null) {
3499 pw.print(innerPrefix); pw.println(r.app);
3500 }
3501 }
3502 if (client && r.app != null && r.app.thread != null) {
3503 // flush anything that is already in the PrintWriter since the thread is going
3504 // to write to the file descriptor directly
3505 pw.flush();
3506 try {
3507 TransferPipe tp = new TransferPipe();
3508 try {
3509 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3510 r.appToken, innerPrefix, args);
3511 // Short timeout, since blocking here can
3512 // deadlock with the application.
3513 tp.go(fd, 2000);
3514 } finally {
3515 tp.kill();
3516 }
3517 } catch (IOException e) {
3518 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3519 } catch (RemoteException e) {
3520 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3521 }
3522 needNL = true;
3523 }
3524 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003525 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003526 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003527
Craig Mautnerf3333272013-04-22 10:55:53 -07003528 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003529 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003530 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3531 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003532 }
3533
3534 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003535 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003536 }
3537
3538 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003539 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003540 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3541 }
3542
Craig Mautner05d29032013-05-03 13:40:13 -07003543 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003544 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3545 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3546 }
Craig Mautner05d29032013-05-03 13:40:13 -07003547 }
3548
Craig Mautner0eea92c2013-05-16 13:35:39 -07003549 void removeSleepTimeouts() {
3550 mSleepTimeout = false;
3551 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3552 }
3553
3554 final void scheduleSleepTimeout() {
3555 removeSleepTimeouts();
3556 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3557 }
3558
Craig Mautner4a1cb222013-12-04 16:14:06 -08003559 @Override
3560 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003561 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003562 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3563 }
3564
3565 @Override
3566 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003567 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003568 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3569 }
3570
3571 @Override
3572 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003573 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003574 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3575 }
3576
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003577 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003578 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003579 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003580 newDisplay = mActivityDisplays.get(displayId) == null;
3581 if (newDisplay) {
3582 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003583 if (activityDisplay.mDisplay == null) {
3584 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3585 return;
3586 }
Craig Mautner4504de52013-12-20 09:06:56 -08003587 mActivityDisplays.put(displayId, activityDisplay);
3588 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003589 }
Craig Mautner4504de52013-12-20 09:06:56 -08003590 if (newDisplay) {
3591 mWindowManager.onDisplayAdded(displayId);
3592 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003593 }
3594
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003595 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003596 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003597 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3598 if (activityDisplay != null) {
3599 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003600 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003601 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003602 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003603 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003604 }
3605 }
3606 mWindowManager.onDisplayRemoved(displayId);
3607 }
3608
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003609 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003610 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003611 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3612 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003613 // TODO: Update the bounds.
3614 }
3615 }
3616 mWindowManager.onDisplayChanged(displayId);
3617 }
3618
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003619 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 StackInfo info = new StackInfo();
3621 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3622 info.displayId = Display.DEFAULT_DISPLAY;
3623 info.stackId = stack.mStackId;
3624
3625 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3626 final int numTasks = tasks.size();
3627 int[] taskIds = new int[numTasks];
3628 String[] taskNames = new String[numTasks];
3629 for (int i = 0; i < numTasks; ++i) {
3630 final TaskRecord task = tasks.get(i);
3631 taskIds[i] = task.taskId;
3632 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3633 : task.realActivity != null ? task.realActivity.flattenToString()
3634 : task.getTopActivity() != null ? task.getTopActivity().packageName
3635 : "unknown";
3636 }
3637 info.taskIds = taskIds;
3638 info.taskNames = taskNames;
3639 return info;
3640 }
3641
3642 StackInfo getStackInfoLocked(int stackId) {
3643 ActivityStack stack = getStack(stackId);
3644 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003645 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003646 }
3647 return null;
3648 }
3649
3650 ArrayList<StackInfo> getAllStackInfosLocked() {
3651 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003652 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3653 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003654 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003655 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003656 }
3657 }
3658 return list;
3659 }
3660
Craig Mautner15df08a2015-04-01 12:17:18 -07003661 TaskRecord getLockedTaskLocked() {
3662 final int top = mLockTaskModeTasks.size() - 1;
3663 if (top >= 0) {
3664 return mLockTaskModeTasks.get(top);
3665 }
3666 return null;
3667 }
3668
3669 boolean isLockedTask(TaskRecord task) {
3670 return mLockTaskModeTasks.contains(task);
3671 }
3672
3673 boolean isLastLockedTask(TaskRecord task) {
3674 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3675 }
3676
3677 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003678 if (!mLockTaskModeTasks.remove(task)) {
3679 return;
3680 }
3681 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3682 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003683 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003684 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3685 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003686 final Message lockTaskMsg = Message.obtain();
3687 lockTaskMsg.arg1 = task.userId;
3688 lockTaskMsg.what = LOCK_TASK_END_MSG;
3689 mHandler.sendMessage(lockTaskMsg);
3690 }
3691 }
3692
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003693 void showLockTaskToast() {
3694 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003695 }
3696
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003697 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3698 if (mLockTaskModeTasks.contains(task)) {
3699 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3700 }
3701 }
3702
Craig Mautner432f64e2015-05-20 14:59:57 -07003703 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3704 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003705 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003706 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003707 final TaskRecord lockedTask = getLockedTaskLocked();
3708 if (lockedTask != null) {
3709 removeLockedTaskLocked(lockedTask);
3710 if (!mLockTaskModeTasks.isEmpty()) {
3711 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003712 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3713 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003714 lockedTask.performClearTaskLocked();
3715 resumeTopActivitiesLocked();
3716 return;
3717 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003718 }
Craig Mautnere0570202015-05-13 13:06:11 -07003719 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3720 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003721 return;
3722 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003723
3724 // Should have already been checked, but do it again.
3725 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003726 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3727 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003728 return;
3729 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003730 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003731 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003732 return;
3733 }
3734
3735 if (mLockTaskModeTasks.isEmpty()) {
3736 // First locktask.
3737 final Message lockTaskMsg = Message.obtain();
3738 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3739 lockTaskMsg.arg1 = task.userId;
3740 lockTaskMsg.what = LOCK_TASK_START_MSG;
3741 lockTaskMsg.arg2 = lockTaskModeState;
3742 mHandler.sendMessage(lockTaskMsg);
3743 }
3744 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003745 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3746 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003747 mLockTaskModeTasks.remove(task);
3748 mLockTaskModeTasks.add(task);
3749
3750 if (task.mLockTaskUid == -1) {
3751 task.mLockTaskUid = task.mCallingUid;
3752 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003753
3754 if (andResume) {
3755 findTaskToMoveToFrontLocked(task, 0, null, reason);
3756 resumeTopActivitiesLocked();
3757 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003758 }
3759
3760 boolean isLockTaskModeViolation(TaskRecord task) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003761 if (getLockedTaskLocked() == task) {
3762 return false;
3763 }
3764 final int lockTaskAuth = task.mLockTaskAuth;
3765 switch (lockTaskAuth) {
3766 case LOCK_TASK_AUTH_DONT_LOCK:
3767 return !mLockTaskModeTasks.isEmpty();
3768 case LOCK_TASK_AUTH_LAUNCHABLE:
3769 case LOCK_TASK_AUTH_WHITELISTED:
3770 return false;
3771 case LOCK_TASK_AUTH_PINNABLE:
3772 // Pinnable tasks can't be launched on top of locktask tasks.
3773 return !mLockTaskModeTasks.isEmpty();
3774 default:
3775 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3776 return true;
3777 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003778 }
3779
Craig Mautner15df08a2015-04-01 12:17:18 -07003780 void onLockTaskPackagesUpdatedLocked() {
3781 boolean didSomething = false;
3782 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3783 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
3784 if (lockedTask.mLockTaskMode != LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED) {
3785 continue;
3786 }
3787 final boolean wasLaunchable = lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE;
3788 lockedTask.setLockTaskAuth();
3789 if (wasLaunchable && lockedTask.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE) {
3790 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003791 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3792 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003793 removeLockedTaskLocked(lockedTask);
3794 lockedTask.performClearTaskLocked();
3795 didSomething = true;
3796 }
3797 }
3798 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3799 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3800 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3801 final ActivityStack stack = stacks.get(stackNdx);
3802 stack.onLockTaskPackagesUpdatedLocked();
3803 }
3804 }
Craig Mautnere0570202015-05-13 13:06:11 -07003805 final ActivityRecord r = topRunningActivityLocked();
3806 final TaskRecord task = r != null ? r.task : null;
3807 if (mLockTaskModeTasks.isEmpty() && task != null
3808 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3809 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003810 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3811 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3812 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3813 false);
3814 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003815 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003816 if (didSomething) {
3817 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003818 }
3819 }
3820
Benjamin Franz43261142015-02-11 15:59:44 +00003821 int getLockTaskModeState() {
3822 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003823 }
3824
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003825 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003826
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003827 public ActivityStackSupervisorHandler(Looper looper) {
3828 super(looper);
3829 }
3830
Craig Mautnerf3333272013-04-22 10:55:53 -07003831 void activityIdleInternal(ActivityRecord r) {
3832 synchronized (mService) {
3833 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3834 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003835 }
3836
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003837 @Override
3838 public void handleMessage(Message msg) {
3839 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003840 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003841 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003842 if (mService.mDidDexOpt) {
3843 mService.mDidDexOpt = false;
3844 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3845 nmsg.obj = msg.obj;
3846 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3847 return;
3848 }
3849 // We don't at this point know if the activity is fullscreen,
3850 // so we need to be conservative and assume it isn't.
3851 activityIdleInternal((ActivityRecord)msg.obj);
3852 } break;
3853 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003854 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003855 activityIdleInternal((ActivityRecord)msg.obj);
3856 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003857 case RESUME_TOP_ACTIVITY_MSG: {
3858 synchronized (mService) {
3859 resumeTopActivitiesLocked();
3860 }
3861 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003862 case SLEEP_TIMEOUT_MSG: {
3863 synchronized (mService) {
3864 if (mService.isSleepingOrShuttingDown()) {
3865 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3866 mSleepTimeout = true;
3867 checkReadyForSleepLocked();
3868 }
3869 }
3870 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003871 case LAUNCH_TIMEOUT_MSG: {
3872 if (mService.mDidDexOpt) {
3873 mService.mDidDexOpt = false;
3874 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3875 return;
3876 }
3877 synchronized (mService) {
3878 if (mLaunchingActivity.isHeld()) {
3879 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3880 if (VALIDATE_WAKE_LOCK_CALLER
3881 && Binder.getCallingUid() != Process.myUid()) {
3882 throw new IllegalStateException("Calling must be system uid");
3883 }
3884 mLaunchingActivity.release();
3885 }
3886 }
3887 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003888 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003889 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003890 } break;
3891 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003892 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003893 } break;
3894 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003895 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003896 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003897 case CONTAINER_CALLBACK_VISIBILITY: {
3898 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003899 final IActivityContainerCallback callback = container.mCallback;
3900 if (callback != null) {
3901 try {
3902 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3903 } catch (RemoteException e) {
3904 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003905 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003906 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003907 case LOCK_TASK_START_MSG: {
3908 // When lock task starts, we disable the status bars.
3909 try {
Jason Monk62515be2014-05-21 16:06:19 -04003910 if (mLockTaskNotify == null) {
3911 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003912 }
Jason Monk62515be2014-05-21 16:06:19 -04003913 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003914 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003915 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003916 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003917 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003918 flags = StatusBarManager.DISABLE_MASK
3919 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003920 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003921 flags = StatusBarManager.DISABLE_MASK
3922 & (~StatusBarManager.DISABLE_BACK)
3923 & (~StatusBarManager.DISABLE_HOME)
3924 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003925 }
3926 getStatusBarService().disable(flags, mToken,
3927 mService.mContext.getPackageName());
3928 }
3929 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003930 if (getDevicePolicyManager() != null) {
3931 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003932 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003933 }
justinzhang5286d3f2014-05-12 17:06:01 -04003934 } catch (RemoteException ex) {
3935 throw new RuntimeException(ex);
3936 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003937 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003938 case LOCK_TASK_END_MSG: {
3939 // When lock task ends, we enable the status bars.
3940 try {
Jason Monk62515be2014-05-21 16:06:19 -04003941 if (getStatusBarService() != null) {
3942 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3943 mService.mContext.getPackageName());
3944 }
3945 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003946 if (getDevicePolicyManager() != null) {
3947 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3948 msg.arg1);
3949 }
Jason Monk62515be2014-05-21 16:06:19 -04003950 if (mLockTaskNotify == null) {
3951 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3952 }
3953 mLockTaskNotify.show(false);
3954 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003955 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003956 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003957 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003958 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003959 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003960 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003961 new LockPatternUtils(mService.mContext)
3962 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003963 }
3964 } catch (SettingNotFoundException e) {
3965 // No setting, don't lock.
3966 }
justinzhang5286d3f2014-05-12 17:06:01 -04003967 } catch (RemoteException ex) {
3968 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003969 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003970 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003971 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003972 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003973 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3974 if (mLockTaskNotify == null) {
3975 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3976 }
3977 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3978 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003979 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3980 final ActivityContainer container = (ActivityContainer) msg.obj;
3981 final IActivityContainerCallback callback = container.mCallback;
3982 if (callback != null) {
3983 try {
3984 callback.onAllActivitiesComplete(container.asBinder());
3985 } catch (RemoteException e) {
3986 }
3987 }
3988 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003989 case LAUNCH_TASK_BEHIND_COMPLETE: {
3990 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003991 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003992 if (r != null) {
3993 handleLaunchTaskBehindCompleteLocked(r);
3994 }
3995 }
3996 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003997 }
3998 }
3999 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004000
Ying Wangb081a592014-04-22 15:20:16 -07004001 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004002 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004003 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004004 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004005 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004006 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004007 ActivityRecord mParentActivity = null;
4008 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004009
Craig Mautnere3a00d72014-04-16 08:31:19 -07004010 boolean mVisible = true;
4011
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004013 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004014
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004015 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4016 final static int CONTAINER_STATE_NO_SURFACE = 1;
4017 final static int CONTAINER_STATE_FINISHING = 2;
4018 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4019
4020 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004021 synchronized (mService) {
4022 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004023 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004024 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004025 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004026 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004027 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004028
Craig Mautnere0a38842013-12-16 16:14:02 -08004029 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004030 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08004031 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08004032 mActivityDisplay = activityDisplay;
4033 mStack.mDisplayId = activityDisplay.mDisplayId;
4034 mStack.mStacks = activityDisplay.mStacks;
4035
4036 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08004037 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004038 }
4039
4040 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004041 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004042 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004043 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4044 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004045 return;
4046 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004047 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004048 }
4049 }
4050
4051 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004052 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004053 synchronized (mService) {
4054 if (mActivityDisplay != null) {
4055 return mActivityDisplay.mDisplayId;
4056 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004057 }
4058 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004059 }
4060
Jeff Brownca9bc702014-02-11 14:32:56 -08004061 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004062 public int getStackId() {
4063 synchronized (mService) {
4064 return mStackId;
4065 }
4066 }
4067
4068 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004069 public boolean injectEvent(InputEvent event) {
4070 final long origId = Binder.clearCallingIdentity();
4071 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004072 synchronized (mService) {
4073 if (mActivityDisplay != null) {
4074 return mInputManagerInternal.injectInputEvent(event,
4075 mActivityDisplay.mDisplayId,
4076 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4077 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004078 }
4079 return false;
4080 } finally {
4081 Binder.restoreCallingIdentity(origId);
4082 }
4083 }
4084
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004085 @Override
4086 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004087 synchronized (mService) {
4088 if (mContainerState == CONTAINER_STATE_FINISHING) {
4089 return;
4090 }
4091 mContainerState = CONTAINER_STATE_FINISHING;
4092
Craig Mautnerd163e752014-06-13 17:18:47 -07004093 long origId = Binder.clearCallingIdentity();
4094 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004095 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004096 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004097 } finally {
4098 Binder.restoreCallingIdentity(origId);
4099 }
4100 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004101 }
4102
Craig Mautner60257702014-09-17 15:02:33 -07004103 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004104 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004105 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004106 if (mActivityDisplay != null) {
4107 mActivityDisplay.detachActivitiesLocked(mStack);
4108 mActivityDisplay = null;
4109 mStack.mDisplayId = -1;
4110 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004111 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004112 }
4113 }
4114
4115 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004116 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004117 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004118 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004119 Binder.getCallingUid(), mCurrentUser, false,
4120 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004121
Craig Mautnere0a38842013-12-16 16:14:02 -08004122 // TODO: Switch to user app stacks here.
4123 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004124 final Uri data = intent.getData();
4125 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4126 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004127 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004128 checkEmbeddedAllowedInner(userId, intent, mimeType);
4129
4130 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4131 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4132 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004133 }
4134
4135 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004136 public final int startActivityIntentSender(IIntentSender intentSender)
4137 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004138 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4139
4140 if (!(intentSender instanceof PendingIntentRecord)) {
4141 throw new IllegalArgumentException("Bad PendingIntent object");
4142 }
4143
Craig Mautnerb9168362015-02-26 20:40:19 -08004144 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004145 Binder.getCallingUid(), mCurrentUser, false,
4146 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004147
4148 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4149 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4150 pendingIntent.key.requestResolvedType);
4151
4152 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4153 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4154 }
4155
4156 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004157 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004158 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004159 throw new SecurityException(
4160 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4161 }
4162 }
4163
Craig Mautnerdf88d732014-01-27 09:21:32 -08004164 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004165 public IBinder asBinder() {
4166 return this;
4167 }
4168
Craig Mautner4504de52013-12-20 09:06:56 -08004169 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004170 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004171 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004172 }
4173
Craig Mautner4a1cb222013-12-04 16:14:06 -08004174 ActivityStackSupervisor getOuter() {
4175 return ActivityStackSupervisor.this;
4176 }
4177
Craig Mautnerd163e752014-06-13 17:18:47 -07004178 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004179 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004180 }
4181
4182 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004183 synchronized (mService) {
4184 if (mActivityDisplay != null) {
4185 mActivityDisplay.getBounds(outBounds);
4186 } else {
4187 outBounds.set(0, 0);
4188 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004189 }
4190 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004191
Craig Mautner6985bad2014-04-21 15:22:06 -07004192 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004193 void setVisible(boolean visible) {
4194 if (mVisible != visible) {
4195 mVisible = visible;
4196 if (mCallback != null) {
4197 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4198 0 /* unused */, this).sendToTarget();
4199 }
4200 }
4201 }
4202
Craig Mautner6985bad2014-04-21 15:22:06 -07004203 void setDrawn() {
4204 }
4205
Craig Mautner1b4bf852014-05-26 15:06:32 -07004206 // You can always start a new task on a regular ActivityStack.
4207 boolean isEligibleForNewTasks() {
4208 return true;
4209 }
4210
Craig Mautnerd163e752014-06-13 17:18:47 -07004211 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004212 detachLocked();
4213 deleteActivityContainer(this);
4214 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004215 }
4216
Craig Mautner34b73df2014-01-12 21:11:08 -08004217 @Override
4218 public String toString() {
4219 return mIdString + (mActivityDisplay == null ? "N" : "A");
4220 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004221 }
4222
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004223 private class VirtualActivityContainer extends ActivityContainer {
4224 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004225 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004226
4227 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4228 super(getNextStackId());
4229 mParentActivity = parent;
4230 mCallback = callback;
4231 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004232 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004233 }
4234
4235 @Override
4236 public void setSurface(Surface surface, int width, int height, int density) {
4237 super.setSurface(surface, width, height, density);
4238
4239 synchronized (mService) {
4240 final long origId = Binder.clearCallingIdentity();
4241 try {
4242 setSurfaceLocked(surface, width, height, density);
4243 } finally {
4244 Binder.restoreCallingIdentity(origId);
4245 }
4246 }
4247 }
4248
4249 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4250 if (mContainerState == CONTAINER_STATE_FINISHING) {
4251 return;
4252 }
4253 VirtualActivityDisplay virtualActivityDisplay =
4254 (VirtualActivityDisplay) mActivityDisplay;
4255 if (virtualActivityDisplay == null) {
4256 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004257 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004258 mActivityDisplay = virtualActivityDisplay;
4259 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4260 attachToDisplayLocked(virtualActivityDisplay);
4261 }
4262
4263 if (mSurface != null) {
4264 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004265 }
4266
Craig Mautner6985bad2014-04-21 15:22:06 -07004267 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004268 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004269 mStack.resumeTopActivityLocked(null);
4270 } else {
4271 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004272 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004273 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004274 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004275 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004276 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004277
Craig Mautnerd163e752014-06-13 17:18:47 -07004278 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004279
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004280 if (DEBUG_STACK) Slog.d(TAG_STACK,
4281 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004282 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004283
Craig Mautner6985bad2014-04-21 15:22:06 -07004284 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004285 boolean isAttachedLocked() {
4286 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004287 }
4288
4289 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004290 void setDrawn() {
4291 synchronized (mService) {
4292 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004293 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004294 }
4295 }
4296
Craig Mautner1b4bf852014-05-26 15:06:32 -07004297 // Never start a new task on an ActivityView if it isn't explicitly specified.
4298 @Override
4299 boolean isEligibleForNewTasks() {
4300 return false;
4301 }
4302
Craig Mautnerd163e752014-06-13 17:18:47 -07004303 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004304 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004305 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4306 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4307 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4308 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4309 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004310 }
4311 }
4312
Craig Mautner4a1cb222013-12-04 16:14:06 -08004313 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4314 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004315 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004316 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004317 int mDisplayId;
4318 Display mDisplay;
4319 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004320
Craig Mautner4a1cb222013-12-04 16:14:06 -08004321 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4322 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004323 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004324
Jose Lima4b6c6692014-08-12 17:41:12 -07004325 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004326
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004327 ActivityDisplay() {
4328 }
Craig Mautner4504de52013-12-20 09:06:56 -08004329
Craig Mautner1a70a162014-09-13 12:09:31 -07004330 // After instantiation, check that mDisplay is not null before using this. The alternative
4331 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004332 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004333 final Display display = mDisplayManager.getDisplay(displayId);
4334 if (display == null) {
4335 return;
4336 }
4337 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004338 }
4339
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004340 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004341 mDisplay = display;
4342 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004343 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004344 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004345
4346 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004347 if (DEBUG_STACK) Slog.v(TAG_STACK,
4348 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004349 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004350 }
4351
Craig Mautnere0a38842013-12-16 16:14:02 -08004352 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004353 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004354 + " from displayId=" + mDisplayId);
4355 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004356 }
4357
4358 void getBounds(Point bounds) {
4359 mDisplay.getDisplayInfo(mDisplayInfo);
4360 bounds.x = mDisplayInfo.appWidth;
4361 bounds.y = mDisplayInfo.appHeight;
4362 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004363
Jose Lima4b6c6692014-08-12 17:41:12 -07004364 void setVisibleBehindActivity(ActivityRecord r) {
4365 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004366 }
4367
Jose Lima4b6c6692014-08-12 17:41:12 -07004368 boolean hasVisibleBehindActivity() {
4369 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004370 }
4371
Craig Mautner34b73df2014-01-12 21:11:08 -08004372 @Override
4373 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004374 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4375 }
4376 }
4377
4378 class VirtualActivityDisplay extends ActivityDisplay {
4379 VirtualDisplay mVirtualDisplay;
4380
Craig Mautner6985bad2014-04-21 15:22:06 -07004381 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004382 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004383 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4384 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4385 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4386 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004387
4388 init(mVirtualDisplay.getDisplay());
4389
4390 mWindowManager.handleDisplayAdded(mDisplayId);
4391 }
4392
4393 void setSurface(Surface surface) {
4394 if (mVirtualDisplay != null) {
4395 mVirtualDisplay.setSurface(surface);
4396 }
4397 }
4398
4399 @Override
4400 void detachActivitiesLocked(ActivityStack stack) {
4401 super.detachActivitiesLocked(stack);
4402 if (mVirtualDisplay != null) {
4403 mVirtualDisplay.release();
4404 mVirtualDisplay = null;
4405 }
4406 }
4407
4408 @Override
4409 public String toString() {
4410 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004411 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004412 }
Jose Lima58e66d62014-05-27 20:00:27 -07004413
4414 private boolean isLeanbackOnlyDevice() {
4415 boolean onLeanbackOnly = false;
4416 try {
4417 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4418 PackageManager.FEATURE_LEANBACK_ONLY);
4419 } catch (RemoteException e) {
4420 // noop
4421 }
4422
4423 return onLeanbackOnly;
4424 }
Craig Mautner27084302013-03-25 08:05:25 -07004425}