blob: 690848349d5d2a3af4bf034764dfd11b09686705 [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;
Craig Mautner15df08a2015-04-01 12:17:18 -070027import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
Craig Mautner6170f732013-04-02 13:05:23 -070028import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080029import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070030import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070031import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
32import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
33import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070034import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070035import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
36import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
38import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070039
Craig Mautner2420ead2013-04-01 17:13:20 -070040import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080042import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityOptions;
44import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080045import android.app.IActivityContainer;
46import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070047import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070048import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070050import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070051import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070053import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040054import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040055import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070056import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070057import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070061import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.content.pm.ApplicationInfo;
63import android.content.pm.PackageManager;
64import android.content.pm.ResolveInfo;
65import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080066import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080067import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080068import android.hardware.display.DisplayManager;
69import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080070import android.hardware.display.DisplayManagerGlobal;
71import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080072import android.hardware.input.InputManager;
73import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080074import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070075import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070076import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070077import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070078import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070079import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070080import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070081import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070082import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070083import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070084import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070085import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040086import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070087import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070088import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070089import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070090import android.os.WorkSource;
Jason Monk62515be2014-05-21 16:06:19 -040091import android.provider.Settings;
92import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070093import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070094import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070095import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070096import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080097import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070098
Craig Mautner4a1cb222013-12-04 16:14:06 -080099import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800100import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800101import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800102import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800103import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700105import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800106import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700107import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400108import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400109import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800110import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700111import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700112import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113
justinzhang5286d3f2014-05-12 17:06:01 -0400114
Craig Mautner8d341ef2013-03-26 09:03:27 -0700115import java.io.FileDescriptor;
116import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700117import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700119import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700120
Craig Mautner4a1cb222013-12-04 16:14:06 -0800121public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800122 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700123 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
124 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700125 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
126 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
127 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
128 private static final String TAG_STACK = TAG + POSTFIX_STACK;
129 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
130 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
131 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800132
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800133 static final boolean DEBUG = DEBUG_ALL || false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700134 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
135 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700136 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700137 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700138 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700139 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
140 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautnereb0e38a2015-04-02 20:22:54 -0700141 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700142 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700143
Craig Mautner2219a1b2013-03-25 09:44:30 -0700144 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700145
Craig Mautnerf3333272013-04-22 10:55:53 -0700146 /** How long we wait until giving up on the last activity telling us it is idle. */
147 static final int IDLE_TIMEOUT = 10*1000;
148
Craig Mautner0eea92c2013-05-16 13:35:39 -0700149 /** How long we can hold the sleep wake lock before giving up. */
150 static final int SLEEP_TIMEOUT = 5*1000;
151
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700152 // How long we can hold the launch wake lock before giving up.
153 static final int LAUNCH_TIMEOUT = 10*1000;
154
Craig Mautner05d29032013-05-03 13:40:13 -0700155 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
156 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
157 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700158 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700159 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
161 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
162 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700163 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400164 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
165 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700166 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700167 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800168
Craig Mautner4504de52013-12-20 09:06:56 -0800169 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700170
Jason Monk62515be2014-05-21 16:06:19 -0400171 private static final String LOCK_TASK_TAG = "Lock-to-App";
172
justinzhang5286d3f2014-05-12 17:06:01 -0400173 /** Status Bar Service **/
174 private IBinder mToken = new Binder();
175 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400176 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400177
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700178 // For debugging to make sure the caller when acquiring/releasing our
179 // wake lock is the system process.
180 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700181
Craig Mautner27084302013-03-25 08:05:25 -0700182 final ActivityManagerService mService;
183
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800184 private final RecentTasks mRecentTasks;
185
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700186 final ActivityStackSupervisorHandler mHandler;
187
188 /** Short cut */
189 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800190 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700191
Craig Mautner8d341ef2013-03-26 09:03:27 -0700192 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700193 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700194
195 /** Task identifier that activities are currently being started in. Incremented each time a
196 * new task is created. */
197 private int mCurTaskId = 0;
198
Craig Mautner2420ead2013-04-01 17:13:20 -0700199 /** The current user */
200 private int mCurrentUser;
201
Craig Mautnere0a38842013-12-16 16:14:02 -0800202 /** The stack containing the launcher app. Assumed to always be attached to
203 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700204 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700205
Craig Mautnere0a38842013-12-16 16:14:02 -0800206 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700207 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700208
Craig Mautner4a1cb222013-12-04 16:14:06 -0800209 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
210 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800211 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800212 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700213
214 /** List of activities that are waiting for a new activity to become visible before completing
215 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800216 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700217
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700218 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800219 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700220
221 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800222 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700223
Craig Mautnerde4ef022013-04-07 19:01:33 -0700224 /** List of activities that are ready to be stopped, but waiting for the next activity to
225 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800226 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700227
Craig Mautnerf3333272013-04-22 10:55:53 -0700228 /** List of activities that are ready to be finished, but waiting for the previous activity to
229 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800230 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700231
Craig Mautner0eea92c2013-05-16 13:35:39 -0700232 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800233 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700234
Craig Mautnerf3333272013-04-22 10:55:53 -0700235 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800236 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700237
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700238 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800239 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700240
Craig Mautnerde4ef022013-04-07 19:01:33 -0700241 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
242 * is being brought in front of us. */
243 boolean mUserLeaving = false;
244
Craig Mautner0eea92c2013-05-16 13:35:39 -0700245 /** Set when we have taken too long waiting to go to sleep. */
246 boolean mSleepTimeout = false;
247
Jose Lima58e66d62014-05-27 20:00:27 -0700248 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
249 * setWindowManager is called. **/
250 private boolean mLeanbackOnlyDevice;
251
Craig Mautner0eea92c2013-05-16 13:35:39 -0700252 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700253 * We don't want to allow the device to go to sleep while in the process
254 * of launching an activity. This is primarily to allow alarm intent
255 * receivers to launch an activity and get that to run before the device
256 * goes back to sleep.
257 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700258 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700259
260 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700261 * Set when the system is going to sleep, until we have
262 * successfully paused the current activity and released our wake lock.
263 * At that point the system is allowed to actually sleep.
264 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700265 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700266
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700267 /** Stack id of the front stack when user switched, indexed by userId. */
268 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700269
Craig Mautner4504de52013-12-20 09:06:56 -0800270 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800271 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700272 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800273
274 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700275 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800276
Jeff Brownca9bc702014-02-11 14:32:56 -0800277 InputManagerInternal mInputManagerInternal;
278
Craig Mautner15df08a2015-04-01 12:17:18 -0700279 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
280 * may be finished until there is only one entry left. If this is empty the system is not
281 * in lockTask mode. */
282 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000283 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700284 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
285 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000286 */
287 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400288 /**
289 * Notifies the user when entering/exiting lock-task.
290 */
291 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800292
Craig Mautner15df08a2015-04-01 12:17:18 -0700293 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700294
Craig Mautner42d04db2014-11-06 12:13:23 -0800295 /** Used to keep resumeTopActivityLocked() from being entered recursively */
296 boolean inResumeTopActivity;
297
Craig Mautneree36c772014-07-16 14:56:05 -0700298 /**
299 * Description of a request to start a new activity, which has been held
300 * due to app switches being disabled.
301 */
302 static class PendingActivityLaunch {
303 final ActivityRecord r;
304 final ActivityRecord sourceRecord;
305 final int startFlags;
306 final ActivityStack stack;
307
308 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
309 int _startFlags, ActivityStack _stack) {
310 r = _r;
311 sourceRecord = _sourceRecord;
312 startFlags = _startFlags;
313 stack = _stack;
314 }
315 }
316
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800317 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700318 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800319 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700320 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
321 }
322
323 /**
324 * At the time when the constructor runs, the power manager has not yet been
325 * initialized. So we initialize our wakelocks afterwards.
326 */
327 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800328 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700329 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700330 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700331 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700332 }
333
justinzhang5286d3f2014-05-12 17:06:01 -0400334 // This function returns a IStatusBarService. The value is from ServiceManager.
335 // getService and is cached.
336 private IStatusBarService getStatusBarService() {
337 synchronized (mService) {
338 if (mStatusBarService == null) {
339 mStatusBarService = IStatusBarService.Stub.asInterface(
340 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
341 if (mStatusBarService == null) {
342 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
343 }
344 }
345 return mStatusBarService;
346 }
347 }
348
Jason Monk35c62a42014-06-17 10:24:47 -0400349 private IDevicePolicyManager getDevicePolicyManager() {
350 synchronized (mService) {
351 if (mDevicePolicyManager == null) {
352 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
353 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
354 if (mDevicePolicyManager == null) {
355 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
356 }
357 }
358 return mDevicePolicyManager;
359 }
360 }
361
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700362 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800363 synchronized (mService) {
364 mWindowManager = wm;
365
366 mDisplayManager =
367 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
368 mDisplayManager.registerDisplayListener(this, null);
369
370 Display[] displays = mDisplayManager.getDisplays();
371 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
372 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800373 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700374 if (activityDisplay.mDisplay == null) {
375 throw new IllegalStateException("Default Display does not exist");
376 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800377 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378 }
379
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700380 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800382
383 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700384
385 // Initialize this here, now that we can get a valid reference to PackageManager.
386 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800387 }
Craig Mautner27084302013-03-25 08:05:25 -0700388 }
389
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200390 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700391 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200392 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700393 }
394
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700395 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800396 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700397 }
398
Craig Mautnerde4ef022013-04-07 19:01:33 -0700399 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800400 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700401 }
402
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800403 /** Top of all visible stacks is/should always be equal to the focused stack.
404 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
405 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700406 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700407 if (stack == null) {
408 return false;
409 }
410
Craig Mautnerdf88d732014-01-27 09:21:32 -0800411 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
412 if (parent != null) {
413 stack = parent.task.stack;
414 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800415 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700416 }
417
Craig Mautner299f9602015-01-26 09:47:33 -0800418 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800419 moveHomeStack(toFront, reason, null);
420 }
421
422 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800423 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800424 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800425 if (topNdx <= 0) {
426 return;
427 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700428
429 // The home stack should either be at the top or bottom of the stack list.
430 if ((toFront && (stacks.get(topNdx) != mHomeStack))
431 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700432 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700433 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
434 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800435 stacks.remove(mHomeStack);
436 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700437 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800438
439 if (lastFocusedStack != null) {
440 mLastFocusedStack = lastFocusedStack;
441 }
442 mFocusedStack = stacks.get(topNdx);
443
Craig Mautnerde313752015-01-22 14:28:03 -0800444 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
445 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800446 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800447
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800448 if (mService.mBooting || !mService.mBooted) {
449 final ActivityRecord r = topRunningActivityLocked();
450 if (r != null && r.idle) {
451 checkFinishBootingLocked();
452 }
453 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700454 }
455
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700456 /** Returns true if the focus activity was adjusted to the home stack top activity. */
457 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700458 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
459 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700460 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700461 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700462
Craig Mautner84984fa2014-06-19 11:19:20 -0700463 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700464
465 final ActivityRecord top = mHomeStack.topRunningActivityLocked(null);
466 if (top == null) {
467 return false;
468 }
469 mService.setFocusedActivityLocked(top, reason);
470 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700471 }
472
Craig Mautner299f9602015-01-26 09:47:33 -0800473 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700474 if (!mService.mBooting && !mService.mBooted) {
475 // Not ready yet!
476 return false;
477 }
478
Craig Mautner84984fa2014-06-19 11:19:20 -0700479 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
480 mWindowManager.showRecentApps();
481 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700482 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700483
Craig Mautner84984fa2014-06-19 11:19:20 -0700484 if (prev != null) {
485 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
486 }
487
Craig Mautnera8a90e02013-06-28 15:24:50 -0700488 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700489 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800490 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700491 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700492 }
Craig Mautner299f9602015-01-26 09:47:33 -0800493 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700494 }
495
Craig Mautner8d341ef2013-03-26 09:03:27 -0700496 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700497 return anyTaskForIdLocked(id, true);
498 }
499
500 /**
501 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
502 * @param id Id of the task we would like returned.
503 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
504 * restore the task from recents to the active list.
505 */
506 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800507 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800509 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800510 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
511 ActivityStack stack = stacks.get(stackNdx);
512 TaskRecord task = stack.taskForIdLocked(id);
513 if (task != null) {
514 return task;
515 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700516 }
517 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800518
519 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700520 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800521 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800522 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700523 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800524 return null;
525 }
526
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700527 if (!restoreFromRecents) {
528 return task;
529 }
530
Wale Ogunwale7de05352014-12-12 15:21:33 -0800531 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700532 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
533 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800534 return null;
535 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700536 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800537 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700538 }
539
Craig Mautner6170f732013-04-02 13:05:23 -0700540 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800541 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800543 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800544 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
545 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
546 if (r != null) {
547 return r;
548 }
Craig Mautner6170f732013-04-02 13:05:23 -0700549 }
550 }
551 return null;
552 }
553
Craig Mautneref73ee12014-04-23 11:45:37 -0700554 void setNextTaskId(int taskId) {
555 if (taskId > mCurTaskId) {
556 mCurTaskId = taskId;
557 }
558 }
559
Craig Mautner8d341ef2013-03-26 09:03:27 -0700560 int getNextTaskId() {
561 do {
562 mCurTaskId++;
563 if (mCurTaskId <= 0) {
564 mCurTaskId = 1;
565 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700566 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700567 return mCurTaskId;
568 }
569
Craig Mautnerde4ef022013-04-07 19:01:33 -0700570 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800571 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700572 if (stack == null) {
573 return null;
574 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700575 ActivityRecord resumedActivity = stack.mResumedActivity;
576 if (resumedActivity == null || resumedActivity.app == null) {
577 resumedActivity = stack.mPausingActivity;
578 if (resumedActivity == null || resumedActivity.app == null) {
579 resumedActivity = stack.topRunningActivityLocked(null);
580 }
581 }
582 return resumedActivity;
583 }
584
Dianne Hackbornff072722014-09-24 10:56:28 -0700585 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700586 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800587 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800588 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
589 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
591 final ActivityStack stack = stacks.get(stackNdx);
592 if (!isFrontStack(stack)) {
593 continue;
594 }
595 ActivityRecord hr = stack.topRunningActivityLocked(null);
596 if (hr != null) {
597 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
598 && processName.equals(hr.processName)) {
599 try {
George Mount2c92c972014-03-20 09:38:23 -0700600 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800601 didSomething = true;
602 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700603 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800604 Slog.w(TAG, "Exception in new application when starting activity "
605 + hr.intent.getComponent().flattenToShortString(), e);
606 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700607 }
Craig Mautner20e72272013-04-01 13:45:53 -0700608 }
Craig Mautner20e72272013-04-01 13:45:53 -0700609 }
610 }
611 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700612 if (!didSomething) {
613 ensureActivitiesVisibleLocked(null, 0);
614 }
Craig Mautner20e72272013-04-01 13:45:53 -0700615 return didSomething;
616 }
617
618 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800619 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
620 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800621 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
622 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800623 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800624 continue;
625 }
626 final ActivityRecord resumedActivity = stack.mResumedActivity;
627 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800628 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
629 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800630 return false;
631 }
Craig Mautner20e72272013-04-01 13:45:53 -0700632 }
633 }
634 return true;
635 }
636
Craig Mautnerde4ef022013-04-07 19:01:33 -0700637 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800638 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
639 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800640 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
641 final ActivityStack stack = stacks.get(stackNdx);
642 if (isFrontStack(stack)) {
643 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800645 return false;
646 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700647 }
648 }
649 }
650 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700651 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800652 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
653 mLastFocusedStack + " to=" + mFocusedStack);
654 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700655 return true;
656 }
657
658 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800659 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800660 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
661 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800662 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
663 final ActivityStack stack = stacks.get(stackNdx);
664 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800665 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700666 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800667 return false;
668 }
669 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800670 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700671 }
672 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800673 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700674 }
675
Craig Mautner2acc3892013-09-23 10:28:14 -0700676 /**
677 * Pause all activities in either all of the stacks or just the back stacks.
678 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700679 * @return true if any activity was paused as a result of this call.
680 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700681 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700682 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800683 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
684 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
686 final ActivityStack stack = stacks.get(stackNdx);
687 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
688 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
689 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700690 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
691 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800692 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700693 }
694 }
695 return someActivityPaused;
696 }
697
Craig Mautnerde4ef022013-04-07 19:01:33 -0700698 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700699 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800700 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
701 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800702 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
703 final ActivityStack stack = stacks.get(stackNdx);
704 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700705 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 if (DEBUG_STATES) {
707 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
708 pausing = false;
709 } else {
710 return false;
711 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700712 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700713 }
714 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700715 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700716 }
717
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700718 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
719 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500720 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800721 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
722 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
723 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
724 final ActivityStack stack = stacks.get(stackNdx);
725 if (stack.mResumedActivity != null &&
726 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700727 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800728 }
729 }
730 }
731 }
732
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700733 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700734 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700735 }
736
737 void sendWaitingVisibleReportLocked(ActivityRecord r) {
738 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700739 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700740 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700741 if (w.who == null) {
742 changed = true;
743 w.timeout = false;
744 if (r != null) {
745 w.who = new ComponentName(r.info.packageName, r.info.name);
746 }
747 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
748 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700749 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700750 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700751 if (changed) {
752 mService.notifyAll();
753 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700754 }
755
756 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
757 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700758 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700759 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700760 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700761 if (w.who == null) {
762 changed = true;
763 w.timeout = timeout;
764 if (r != null) {
765 w.who = new ComponentName(r.info.packageName, r.info.name);
766 }
767 w.thisTime = thisTime;
768 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700769 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700770 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700771 if (changed) {
772 mService.notifyAll();
773 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700774 }
775
Craig Mautner29219d92013-04-16 20:19:12 -0700776 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800777 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700778 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
779 if (r != null) {
780 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700781 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700782
Craig Mautner4a1cb222013-12-04 16:14:06 -0800783 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800784 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800785 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
786 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700787 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700788 r = stack.topRunningActivityLocked(null);
789 if (r != null) {
790 return r;
791 }
792 }
793 }
794 return null;
795 }
796
Dianne Hackborn09233282014-04-30 11:33:59 -0700797 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700798 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800799 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
800 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800801 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800802 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800803 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800804 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
805 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700806 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800807 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700808 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700809 }
810 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700811
812 // The lists are already sorted from most recent to oldest. Just pull the most recent off
813 // each list and add it to list. Stop when all lists are empty or maxNum reached.
814 while (maxNum > 0) {
815 long mostRecentActiveTime = Long.MIN_VALUE;
816 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800817 final int numTaskLists = runningTaskLists.size();
818 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
819 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700820 if (!stackTaskList.isEmpty()) {
821 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
822 if (lastActiveTime > mostRecentActiveTime) {
823 mostRecentActiveTime = lastActiveTime;
824 selectedStackList = stackTaskList;
825 }
826 }
827 }
828 if (selectedStackList != null) {
829 list.add(selectedStackList.remove(0));
830 --maxNum;
831 } else {
832 break;
833 }
834 }
Craig Mautner20e72272013-04-01 13:45:53 -0700835 }
836
Craig Mautner23ac33b2013-04-01 16:26:35 -0700837 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700838 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700839 // Collect information about the target of the Intent.
840 ActivityInfo aInfo;
841 try {
842 ResolveInfo rInfo =
843 AppGlobals.getPackageManager().resolveIntent(
844 intent, resolvedType,
845 PackageManager.MATCH_DEFAULT_ONLY
846 | ActivityManagerService.STOCK_PM_FLAGS, userId);
847 aInfo = rInfo != null ? rInfo.activityInfo : null;
848 } catch (RemoteException e) {
849 aInfo = null;
850 }
851
852 if (aInfo != null) {
853 // Store the found target back into the intent, because now that
854 // we have it we never want to do this again. For example, if the
855 // user navigates back to this point in the history, we should
856 // always restart the exact same activity.
857 intent.setComponent(new ComponentName(
858 aInfo.applicationInfo.packageName, aInfo.name));
859
860 // Don't debug things in the system process
861 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
862 if (!aInfo.processName.equals("system")) {
863 mService.setDebugApp(aInfo.processName, true, false);
864 }
865 }
866
867 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
868 if (!aInfo.processName.equals("system")) {
869 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
870 }
871 }
872
Jeff Hao1b012d32014-08-20 10:35:34 -0700873 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700874 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700875 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700876 }
877 }
878 }
879 return aInfo;
880 }
881
Craig Mautner299f9602015-01-26 09:47:33 -0800882 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
883 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700884 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
885 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700886 }
887
Craig Mautner23ac33b2013-04-01 16:26:35 -0700888 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700889 String callingPackage, Intent intent, String resolvedType,
890 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700891 IBinder resultTo, String resultWho, int requestCode, int startFlags,
892 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700893 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700894 // Refuse possible leaked file descriptors
895 if (intent != null && intent.hasFileDescriptors()) {
896 throw new IllegalArgumentException("File descriptors passed in Intent");
897 }
898 boolean componentSpecified = intent.getComponent() != null;
899
900 // Don't modify the client's object!
901 intent = new Intent(intent);
902
903 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700904 ActivityInfo aInfo =
905 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700906
Craig Mautnere0a38842013-12-16 16:14:02 -0800907 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700908 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800909 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700910 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800911 // Cannot start a child activity if the parent is not resumed.
912 return ActivityManager.START_CANCELED;
913 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700914 final int realCallingPid = Binder.getCallingPid();
915 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700916 int callingPid;
917 if (callingUid >= 0) {
918 callingPid = -1;
919 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700920 callingPid = realCallingPid;
921 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922 } else {
923 callingPid = callingUid = -1;
924 }
925
Craig Mautnere0a38842013-12-16 16:14:02 -0800926 final ActivityStack stack;
927 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800928 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800929 } else {
930 stack = container.mStack;
931 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800932 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700933 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700934 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700935
936 final long origId = Binder.clearCallingIdentity();
937
938 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800939 (aInfo.applicationInfo.privateFlags
940 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700941 // This may be a heavy-weight process! Check to see if we already
942 // have another, different heavy-weight process running.
943 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
944 if (mService.mHeavyWeightProcess != null &&
945 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
946 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700947 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700948 if (caller != null) {
949 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
950 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700951 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700952 } else {
953 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700954 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700955 + intent.toString());
956 ActivityOptions.abort(options);
957 return ActivityManager.START_PERMISSION_DENIED;
958 }
959 }
960
961 IIntentSender target = mService.getIntentSenderLocked(
962 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700963 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700964 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
965 | PendingIntent.FLAG_ONE_SHOT, null);
966
967 Intent newIntent = new Intent();
968 if (requestCode >= 0) {
969 // Caller is requesting a result.
970 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
971 }
972 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
973 new IntentSender(target));
974 if (mService.mHeavyWeightProcess.activities.size() > 0) {
975 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
976 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
977 hist.packageName);
978 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
979 hist.task.taskId);
980 }
981 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
982 aInfo.packageName);
983 newIntent.setFlags(intent.getFlags());
984 newIntent.setClassName("android",
985 HeavyWeightSwitcherActivity.class.getName());
986 intent = newIntent;
987 resolvedType = null;
988 caller = null;
989 callingUid = Binder.getCallingUid();
990 callingPid = Binder.getCallingPid();
991 componentSpecified = true;
992 try {
993 ResolveInfo rInfo =
994 AppGlobals.getPackageManager().resolveIntent(
995 intent, null,
996 PackageManager.MATCH_DEFAULT_ONLY
997 | ActivityManagerService.STOCK_PM_FLAGS, userId);
998 aInfo = rInfo != null ? rInfo.activityInfo : null;
999 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1000 } catch (RemoteException e) {
1001 aInfo = null;
1002 }
1003 }
1004 }
1005 }
1006
Dianne Hackborn91097de2014-04-04 18:02:06 -07001007 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1008 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001009 requestCode, callingPid, callingUid, callingPackage,
1010 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001011 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001012
Craig Mautner85c11a82014-07-17 12:38:18 -07001013 Binder.restoreCallingIdentity(origId);
1014
Craig Mautnerde4ef022013-04-07 19:01:33 -07001015 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001016 // If the caller also wants to switch to a new configuration,
1017 // do so now. This allows a clean switch, as we are waiting
1018 // for the current activity to pause (so we will not destroy
1019 // it), and have not yet started the next activity.
1020 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1021 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001022 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001023 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001024 "Updating to new configuration after starting activity.");
1025 mService.updateConfigurationLocked(config, null, false, false);
1026 }
1027
Craig Mautner23ac33b2013-04-01 16:26:35 -07001028 if (outResult != null) {
1029 outResult.result = res;
1030 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001031 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001032 do {
1033 try {
1034 mService.wait();
1035 } catch (InterruptedException e) {
1036 }
1037 } while (!outResult.timeout && outResult.who == null);
1038 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001039 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001040 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001041 outResult.timeout = false;
1042 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1043 outResult.totalTime = 0;
1044 outResult.thisTime = 0;
1045 } else {
1046 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001047 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001048 do {
1049 try {
1050 mService.wait();
1051 } catch (InterruptedException e) {
1052 }
1053 } while (!outResult.timeout && outResult.who == null);
1054 }
1055 }
1056 }
1057
1058 return res;
1059 }
1060 }
1061
1062 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1063 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1064 Bundle options, int userId) {
1065 if (intents == null) {
1066 throw new NullPointerException("intents is null");
1067 }
1068 if (resolvedTypes == null) {
1069 throw new NullPointerException("resolvedTypes is null");
1070 }
1071 if (intents.length != resolvedTypes.length) {
1072 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1073 }
1074
Craig Mautner23ac33b2013-04-01 16:26:35 -07001075
1076 int callingPid;
1077 if (callingUid >= 0) {
1078 callingPid = -1;
1079 } else if (caller == null) {
1080 callingPid = Binder.getCallingPid();
1081 callingUid = Binder.getCallingUid();
1082 } else {
1083 callingPid = callingUid = -1;
1084 }
1085 final long origId = Binder.clearCallingIdentity();
1086 try {
1087 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001088 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001089 for (int i=0; i<intents.length; i++) {
1090 Intent intent = intents[i];
1091 if (intent == null) {
1092 continue;
1093 }
1094
1095 // Refuse possible leaked file descriptors
1096 if (intent != null && intent.hasFileDescriptors()) {
1097 throw new IllegalArgumentException("File descriptors passed in Intent");
1098 }
1099
1100 boolean componentSpecified = intent.getComponent() != null;
1101
1102 // Don't modify the client's object!
1103 intent = new Intent(intent);
1104
1105 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001106 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001107 // TODO: New, check if this is correct
1108 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1109
1110 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001111 (aInfo.applicationInfo.privateFlags
1112 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001113 throw new IllegalArgumentException(
1114 "FLAG_CANT_SAVE_STATE not supported here");
1115 }
1116
1117 Bundle theseOptions;
1118 if (options != null && i == intents.length-1) {
1119 theseOptions = options;
1120 } else {
1121 theseOptions = null;
1122 }
Craig Mautner6170f732013-04-02 13:05:23 -07001123 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001124 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1125 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001126 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001127 if (res < 0) {
1128 return res;
1129 }
1130
1131 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1132 }
1133 }
1134 } finally {
1135 Binder.restoreCallingIdentity(origId);
1136 }
1137
1138 return ActivityManager.START_SUCCESS;
1139 }
1140
Craig Mautner2420ead2013-04-01 17:13:20 -07001141 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001142 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001143 throws RemoteException {
1144
Craig Mautner2568c3a2015-03-26 14:22:34 -07001145 if (andResume) {
1146 r.startFreezingScreenLocked(app, 0);
1147 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001148
Craig Mautner2568c3a2015-03-26 14:22:34 -07001149 // schedule launch ticks to collect information about slow apps.
1150 r.startLaunchTickingLocked();
1151 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001152
1153 // Have the window manager re-evaluate the orientation of
1154 // the screen based on the new activity order. Note that
1155 // as a result of this, it can call back into the activity
1156 // manager with a new orientation. We don't care about that,
1157 // because the activity is not currently running so we are
1158 // just restarting it anyway.
1159 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001160 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001161 mService.mConfiguration,
1162 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1163 mService.updateConfigurationLocked(config, r, false, false);
1164 }
1165
1166 r.app = app;
1167 app.waitingToKill = null;
1168 r.launchCount++;
1169 r.lastLaunchTime = SystemClock.uptimeMillis();
1170
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001171 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001172
1173 int idx = app.activities.indexOf(r);
1174 if (idx < 0) {
1175 app.activities.add(r);
1176 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001177 mService.updateLruProcessLocked(app, true, null);
1178 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001179
Craig Mautner15df08a2015-04-01 12:17:18 -07001180 final TaskRecord task = r.task;
1181 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
1182 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "lockTaskLaunchMode attribute");
1183 }
1184
1185 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001186 try {
1187 if (app.thread == null) {
1188 throw new RemoteException();
1189 }
1190 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001191 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001192 if (andResume) {
1193 results = r.results;
1194 newIntents = r.newIntents;
1195 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001196 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1197 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1198 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001199 if (andResume) {
1200 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1201 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001202 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001203 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001204 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001205 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001206 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001207 }
1208 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1209 r.sleeping = false;
1210 r.forceNewConfig = false;
1211 mService.showAskCompatModeDialogLocked(r);
1212 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001213 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001214 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1215 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1216 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001217 final String profileFile = mService.mProfileFile;
1218 if (profileFile != null) {
1219 ParcelFileDescriptor profileFd = mService.mProfileFd;
1220 if (profileFd != null) {
1221 try {
1222 profileFd = profileFd.dup();
1223 } catch (IOException e) {
1224 if (profileFd != null) {
1225 try {
1226 profileFd.close();
1227 } catch (IOException o) {
1228 }
1229 profileFd = null;
1230 }
1231 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001232 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001233
1234 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1235 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001236 }
1237 }
1238 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001239
Craig Mautner2568c3a2015-03-26 14:22:34 -07001240 if (andResume) {
1241 app.hasShownUi = true;
1242 app.pendingUiClean = true;
1243 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001244 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001245 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001246 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001247 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001248 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001249 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001250
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001251 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001252 // This may be a heavy-weight process! Note that the package
1253 // manager will ensure that only activity can run in the main
1254 // process of the .apk, which is the only thing that will be
1255 // considered heavy-weight.
1256 if (app.processName.equals(app.info.packageName)) {
1257 if (mService.mHeavyWeightProcess != null
1258 && mService.mHeavyWeightProcess != app) {
1259 Slog.w(TAG, "Starting new heavy weight process " + app
1260 + " when already running "
1261 + mService.mHeavyWeightProcess);
1262 }
1263 mService.mHeavyWeightProcess = app;
1264 Message msg = mService.mHandler.obtainMessage(
1265 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1266 msg.obj = r;
1267 mService.mHandler.sendMessage(msg);
1268 }
1269 }
1270
1271 } catch (RemoteException e) {
1272 if (r.launchFailed) {
1273 // This is the second time we failed -- finish activity
1274 // and give up.
1275 Slog.e(TAG, "Second failure launching "
1276 + r.intent.getComponent().flattenToShortString()
1277 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001278 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001279 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1280 "2nd-crash", false);
1281 return false;
1282 }
1283
1284 // This is the first time we failed -- restart process and
1285 // retry.
1286 app.activities.remove(r);
1287 throw e;
1288 }
1289
1290 r.launchFailed = false;
1291 if (stack.updateLRUListLocked(r)) {
1292 Slog.w(TAG, "Activity " + r
1293 + " being launched, but already in LRU list");
1294 }
1295
1296 if (andResume) {
1297 // As part of the process of launching, ActivityThread also performs
1298 // a resume.
1299 stack.minimalResumeActivityLocked(r);
1300 } else {
1301 // This activity is not starting in the resumed state... which
1302 // should look like we asked it to pause+stop (but remain visible),
1303 // and it has done so and reported back the current icicle and
1304 // other state.
1305 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1306 + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001307 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001308 r.stopped = true;
1309 }
1310
1311 // Launch the new version setup screen if needed. We do this -after-
1312 // launching the initial activity (that is, home), so that it can have
1313 // a chance to initialize itself while in the background, making the
1314 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001315 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001316 mService.startSetupActivityLocked();
1317 }
1318
Dianne Hackborn465fa392014-09-14 14:21:18 -07001319 // Update any services we are bound to that might care about whether
1320 // their client may have activities.
1321 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1322
Craig Mautner2420ead2013-04-01 17:13:20 -07001323 return true;
1324 }
1325
Craig Mautnere79d42682013-04-01 19:01:53 -07001326 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001327 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001328 // Is this activity's application already running?
1329 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001330 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001331
1332 r.task.stack.setLaunchTime(r);
1333
1334 if (app != null && app.thread != null) {
1335 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001336 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1337 || !"android".equals(r.info.packageName)) {
1338 // Don't add this if it is a platform component that is marked
1339 // to run in multiple processes, because this is actually
1340 // part of the framework so doesn't make sense to track as a
1341 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001342 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1343 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001344 }
George Mount2c92c972014-03-20 09:38:23 -07001345 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001346 return;
1347 } catch (RemoteException e) {
1348 Slog.w(TAG, "Exception when starting activity "
1349 + r.intent.getComponent().flattenToShortString(), e);
1350 }
1351
1352 // If a dead object exception was thrown -- fall through to
1353 // restart the application.
1354 }
1355
1356 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001357 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001358 }
1359
Craig Mautner6170f732013-04-02 13:05:23 -07001360 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001361 Intent intent, String resolvedType, ActivityInfo aInfo,
1362 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1363 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001364 int callingPid, int callingUid, String callingPackage,
1365 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001366 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1367 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001368 int err = ActivityManager.START_SUCCESS;
1369
1370 ProcessRecord callerApp = null;
1371 if (caller != null) {
1372 callerApp = mService.getRecordForAppLocked(caller);
1373 if (callerApp != null) {
1374 callingPid = callerApp.pid;
1375 callingUid = callerApp.info.uid;
1376 } else {
1377 Slog.w(TAG, "Unable to find app for caller " + caller
1378 + " (pid=" + callingPid + ") when starting: "
1379 + intent.toString());
1380 err = ActivityManager.START_PERMISSION_DENIED;
1381 }
1382 }
1383
1384 if (err == ActivityManager.START_SUCCESS) {
1385 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1386 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001387 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001388 + " on display " + (container == null ? (mFocusedStack == null ?
1389 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1390 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1391 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001392 }
1393
1394 ActivityRecord sourceRecord = null;
1395 ActivityRecord resultRecord = null;
1396 if (resultTo != null) {
1397 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001398 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1399 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001400 if (sourceRecord != null) {
1401 if (requestCode >= 0 && !sourceRecord.finishing) {
1402 resultRecord = sourceRecord;
1403 }
1404 }
1405 }
Craig Mautner6170f732013-04-02 13:05:23 -07001406
Dianne Hackborn91097de2014-04-04 18:02:06 -07001407 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001408
Dianne Hackborn95465202014-09-15 16:21:55 -07001409 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001410 // Transfer the result target from the source activity to the new
1411 // one being started, including any failures.
1412 if (requestCode >= 0) {
1413 ActivityOptions.abort(options);
1414 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1415 }
1416 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001417 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1418 resultRecord = null;
1419 }
Craig Mautner6170f732013-04-02 13:05:23 -07001420 resultWho = sourceRecord.resultWho;
1421 requestCode = sourceRecord.requestCode;
1422 sourceRecord.resultTo = null;
1423 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001424 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001425 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001426 if (sourceRecord.launchedFromUid == callingUid) {
1427 // The new activity is being launched from the same uid as the previous
1428 // activity in the flow, and asking to forward its result back to the
1429 // previous. In this case the activity is serving as a trampoline between
1430 // the two, so we also want to update its launchedFromPackage to be the
1431 // same as the previous activity. Note that this is safe, since we know
1432 // these two packages come from the same uid; the caller could just as
1433 // well have supplied that same package name itself. This specifially
1434 // deals with the case of an intent picker/chooser being launched in the app
1435 // flow to redirect to an activity picked by the user, where we want the final
1436 // activity to consider it to have been launched by the previous app activity.
1437 callingPackage = sourceRecord.launchedFromPackage;
1438 }
Craig Mautner6170f732013-04-02 13:05:23 -07001439 }
1440
1441 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1442 // We couldn't find a class that can handle the given Intent.
1443 // That's the end of that!
1444 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1445 }
1446
1447 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1448 // We couldn't find the specific class specified in the Intent.
1449 // Also the end of the line.
1450 err = ActivityManager.START_CLASS_NOT_FOUND;
1451 }
1452
Dianne Hackborn91097de2014-04-04 18:02:06 -07001453 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1454 && sourceRecord.task.voiceSession != null) {
1455 // If this activity is being launched as part of a voice session, we need
1456 // to ensure that it is safe to do so. If the upcoming activity will also
1457 // be part of the voice session, we can only launch it if it has explicitly
1458 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001459 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001460 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1461 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001462 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1463 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001464 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1465 }
1466 } catch (RemoteException e) {
1467 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1468 }
1469 }
1470 }
1471
1472 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1473 // If the caller is starting a new voice session, just make sure the target
1474 // is actually allowing it to run this way.
1475 try {
1476 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1477 intent, resolvedType)) {
1478 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1479 }
1480 } catch (RemoteException e) {
1481 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1482 }
1483 }
1484
louis_changcd5d1982014-08-01 10:09:08 +08001485 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1486
Craig Mautner6170f732013-04-02 13:05:23 -07001487 if (err != ActivityManager.START_SUCCESS) {
1488 if (resultRecord != null) {
1489 resultStack.sendActivityResultLocked(-1,
1490 resultRecord, resultWho, requestCode,
1491 Activity.RESULT_CANCELED, null);
1492 }
Craig Mautner6170f732013-04-02 13:05:23 -07001493 ActivityOptions.abort(options);
1494 return err;
1495 }
1496
1497 final int startAnyPerm = mService.checkPermission(
1498 START_ANY_ACTIVITY, callingPid, callingUid);
1499 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1500 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1501 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
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 String msg;
1508 if (!aInfo.exported) {
1509 msg = "Permission Denial: starting " + intent.toString()
1510 + " from " + callerApp + " (pid=" + callingPid
1511 + ", uid=" + callingUid + ")"
1512 + " not exported from uid " + aInfo.applicationInfo.uid;
1513 } else {
1514 msg = "Permission Denial: starting " + intent.toString()
1515 + " from " + callerApp + " (pid=" + callingPid
1516 + ", uid=" + callingUid + ")"
1517 + " requires " + aInfo.permission;
1518 }
1519 Slog.w(TAG, msg);
1520 throw new SecurityException(msg);
1521 }
1522
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001523 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001524 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001525
Craig Mautner6170f732013-04-02 13:05:23 -07001526 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001527 try {
1528 // The Intent we give to the watcher has the extra data
1529 // stripped off, since it can contain private information.
1530 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001531 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001532 aInfo.applicationInfo.packageName);
1533 } catch (RemoteException e) {
1534 mService.mController = null;
1535 }
Ben Gruver5e207332013-04-03 17:41:37 -07001536 }
Craig Mautner6170f732013-04-02 13:05:23 -07001537
Ben Gruver5e207332013-04-03 17:41:37 -07001538 if (abort) {
1539 if (resultRecord != null) {
1540 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001541 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001542 }
Ben Gruver5e207332013-04-03 17:41:37 -07001543 // We pretend to the caller that it was really started, but
1544 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001545 ActivityOptions.abort(options);
1546 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001547 }
1548
1549 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001550 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001551 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001552 if (outActivity != null) {
1553 outActivity[0] = r;
1554 }
1555
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001556 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001557 if (voiceSession == null && (stack.mResumedActivity == null
1558 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001559 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1560 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001561 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001562 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001563 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001564 ActivityOptions.abort(options);
1565 return ActivityManager.START_SWITCHES_CANCELED;
1566 }
1567 }
1568
1569 if (mService.mDidAppSwitch) {
1570 // This is the second allowed switch since we stopped switches,
1571 // so now just generally allow switches. Use case: user presses
1572 // home (switches disabled, switch to home, mDidAppSwitch now true);
1573 // user taps a home icon (coming from home so allowed, we hit here
1574 // and now allow anyone to switch again).
1575 mService.mAppSwitchesAllowedTime = 0;
1576 } else {
1577 mService.mDidAppSwitch = true;
1578 }
1579
Craig Mautneree36c772014-07-16 14:56:05 -07001580 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001581
Dianne Hackborn91097de2014-04-04 18:02:06 -07001582 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001583 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001584
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001585 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001586 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001587 // activity start, but we are not actually doing an activity
1588 // switch... just dismiss the keyguard now, because we
1589 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001590 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001591 }
1592 return err;
1593 }
1594
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001595 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001596 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001597
1598 // On leanback only devices we should keep all activities in the same stack.
1599 if (!mLeanbackOnlyDevice &&
1600 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001601
1602 ActivityStack stack;
1603
Wale Ogunwale7d701172015-03-11 15:36:30 -07001604 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001605 stack = task.stack;
1606 if (stack.isOnHomeDisplay()) {
1607 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001608 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1609 "computeStackFocus: Setting " + "focused stack to r=" + r
1610 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001611 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001612 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001613 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001614 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001615 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001616 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001617 }
1618
Craig Mautnere0a38842013-12-16 16:14:02 -08001619 final ActivityContainer container = r.mInitialActivityContainer;
1620 if (container != null) {
1621 // The first time put it on the desired stack, after this put on task stack.
1622 r.mInitialActivityContainer = null;
1623 return container.mStack;
1624 }
1625
Craig Mautner1b4bf852014-05-26 15:06:32 -07001626 if (mFocusedStack != mHomeStack && (!newTask ||
1627 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001628 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001629 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001630 return mFocusedStack;
1631 }
1632
Craig Mautnere0a38842013-12-16 16:14:02 -08001633 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1634 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001635 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001636 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001637 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001638 "computeStackFocus: Setting focused stack=" + stack);
1639 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001640 }
1641 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001642
Craig Mautner4a1cb222013-12-04 16:14:06 -08001643 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001644 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001645 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1646 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001647 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001648 }
1649 return mHomeStack;
1650 }
1651
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001652 boolean setFocusedStack(ActivityRecord r, String reason) {
1653 if (r == null) {
1654 // Not sure what you are trying to do, but it is not going to work...
1655 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001656 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001657 final TaskRecord task = r.task;
1658 if (task == null || task.stack == null) {
1659 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1660 return false;
1661 }
1662 task.stack.moveToFront(reason);
1663 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001664 }
1665
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001666 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001667 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001668 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001669 final Intent intent = r.intent;
1670 final int callingUid = r.launchedFromUid;
1671
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001672 // In some flows in to this function, we retrieve the task record and hold on to it
1673 // without a lock before calling back in to here... so the task at this point may
1674 // not actually be in recents. Check for that, and if it isn't in recents just
1675 // consider it invalid.
1676 if (inTask != null && !inTask.inRecents) {
1677 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1678 inTask = null;
1679 }
1680
Craig Mautnerad400af2014-08-13 16:41:36 -07001681 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001682 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1683 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001684
Craig Mautnera228ae92014-07-09 05:44:55 -07001685 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001686 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001687 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001688 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1689 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1690 "\"singleInstance\" or \"singleTask\"");
1691 launchFlags &=
1692 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1693 } else {
1694 switch (r.info.documentLaunchMode) {
1695 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1696 break;
1697 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1698 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1699 break;
1700 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1701 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1702 break;
1703 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1704 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1705 break;
1706 }
1707 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001708
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001709 final boolean launchTaskBehind = r.mLaunchTaskBehind
1710 && !launchSingleTask && !launchSingleInstance
1711 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001712
Wale Ogunwale7d701172015-03-11 15:36:30 -07001713 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1714 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001715 // For whatever reason this activity is being launched into a new
1716 // task... yet the caller has requested a result back. Well, that
1717 // is pretty messed up, so instead immediately send back a cancel
1718 // and let the new task continue launched as normal without a
1719 // dependency on its originator.
1720 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1721 r.resultTo.task.stack.sendActivityResultLocked(-1,
1722 r.resultTo, r.resultWho, r.requestCode,
1723 Activity.RESULT_CANCELED, null);
1724 r.resultTo = null;
1725 }
1726
1727 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1728 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1729 }
1730
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001731 // If we are actually going to launch in to a new task, there are some cases where
1732 // we further want to do multiple task.
1733 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1734 if (launchTaskBehind
1735 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1736 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1737 }
1738 }
1739
Craig Mautner8849a5e2013-04-02 16:41:03 -07001740 // We'll invoke onUserLeaving before onPause only if the launching
1741 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001742 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001743 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1744 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001745
1746 // If the caller has asked not to resume at this point, we make note
1747 // of this in the record so that we can skip it when trying to find
1748 // the top running activity.
1749 if (!doResume) {
1750 r.delayedResume = true;
1751 }
1752
Craig Mautnera254cd72014-05-25 16:47:39 -07001753 ActivityRecord notTop =
1754 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001755
1756 // If the onlyIfNeeded flag is set, then we can do this if the activity
1757 // being launched is the same as the one making the call... or, as
1758 // a special case, if we do not know the caller then we count the
1759 // current top activity as the caller.
1760 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1761 ActivityRecord checkedCaller = sourceRecord;
1762 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001763 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001764 }
1765 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1766 // Caller is not the same as launcher, so always needed.
1767 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1768 }
1769 }
1770
Craig Mautner8849a5e2013-04-02 16:41:03 -07001771 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001772 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001773
1774 // If the caller is not coming from another activity, but has given us an
1775 // explicit task into which they would like us to launch the new activity,
1776 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001777 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1778 final Intent baseIntent = inTask.getBaseIntent();
1779 final ActivityRecord root = inTask.getRootActivity();
1780 if (baseIntent == null) {
1781 ActivityOptions.abort(options);
1782 throw new IllegalArgumentException("Launching into task without base intent: "
1783 + inTask);
1784 }
1785
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001786 // If this task is empty, then we are adding the first activity -- it
1787 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001788 if (launchSingleInstance || launchSingleTask) {
1789 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1790 ActivityOptions.abort(options);
1791 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1792 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001793 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001794 if (root != null) {
1795 ActivityOptions.abort(options);
1796 throw new IllegalArgumentException("Caller with inTask " + inTask
1797 + " has root " + root + " but target is singleInstance/Task");
1798 }
1799 }
1800
1801 // If task is empty, then adopt the interesting intent launch flags in to the
1802 // activity being started.
1803 if (root == null) {
1804 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1805 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1806 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1807 launchFlags = (launchFlags&~flagsOfInterest)
1808 | (baseIntent.getFlags()&flagsOfInterest);
1809 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001810 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001811 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001812
Dianne Hackborn962d5352014-08-29 16:27:40 -07001813 // If the task is not empty and the caller is asking to start it as the root
1814 // of a new task, then we don't actually want to start this on the task. We
1815 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001816 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001817 addingToTask = false;
1818
1819 } else {
1820 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001821 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001822
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001823 reuseTask = inTask;
1824 } else {
1825 inTask = null;
1826 }
1827
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001828 if (inTask == null) {
1829 if (sourceRecord == null) {
1830 // This activity is not being started from another... in this
1831 // case we -always- start a new task.
1832 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1833 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1834 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1835 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1836 }
1837 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1838 // The original activity who is starting us is running as a single
1839 // instance... this new activity it is starting must go on its
1840 // own task.
1841 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1842 } else if (launchSingleInstance || launchSingleTask) {
1843 // The activity being started is a single instance... it always
1844 // gets launched into its own task.
1845 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1846 }
1847 }
1848
1849 ActivityInfo newTaskInfo = null;
1850 Intent newTaskIntent = null;
1851 ActivityStack sourceStack;
1852 if (sourceRecord != null) {
1853 if (sourceRecord.finishing) {
1854 // If the source is finishing, we can't further count it as our source. This
1855 // is because the task it is associated with may now be empty and on its way out,
1856 // so we don't want to blindly throw it in to that task. Instead we will take
1857 // the NEW_TASK flow and try to find a task for it. But save the task information
1858 // so it can be used when creating the new task.
1859 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1860 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1861 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1862 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1863 newTaskInfo = sourceRecord.info;
1864 newTaskIntent = sourceRecord.task.intent;
1865 }
1866 sourceRecord = null;
1867 sourceStack = null;
1868 } else {
1869 sourceStack = sourceRecord.task.stack;
1870 }
1871 } else {
1872 sourceStack = null;
1873 }
1874
1875 boolean movedHome = false;
1876 ActivityStack targetStack;
1877
1878 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001879 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001880
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001881 // We may want to try to place the new activity in to an existing task. We always
1882 // do this if the target activity is singleTask or singleInstance; we will also do
1883 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1884 // us to still place it in a new task: multi task, always doc mode, or being asked to
1885 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001886 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1887 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001888 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001889 // If bring to front is requested, and no result is requested and we have not
1890 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001891 // we can find a task that was started with this same
1892 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001893 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001894 // See if there is a task to bring to the front. If this is
1895 // a SINGLE_INSTANCE activity, there can be one and only one
1896 // instance of it in the history, and it is always in its own
1897 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001898 ActivityRecord intentActivity = !launchSingleInstance ?
1899 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001900 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001901 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001902 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001903 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001904 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1905 }
Craig Mautner29219d92013-04-16 20:19:12 -07001906 if (r.task == null) {
1907 r.task = intentActivity.task;
1908 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001909 if (intentActivity.task.intent == null) {
1910 // This task was started because of movement of
1911 // the activity based on affinity... now that we
1912 // are actually launching it, we can assign the
1913 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001914 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001915 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001916 targetStack = intentActivity.task.stack;
1917 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001918 // If the target task is not in the front, then we need
1919 // to bring it to the front... except... well, with
1920 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1921 // to have the same behavior as if a new instance was
1922 // being started, which means not bringing it to the front
1923 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001924 final ActivityStack focusStack = getFocusedStack();
1925 ActivityRecord curTop = (focusStack == null)
1926 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001927 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001928 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001929 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001930 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001931 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1932 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001933 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001934 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001935 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1936 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001937 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001938 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1939 options, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001940 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001941 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001942 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1943 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001944 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001945 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001946 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001947 options = null;
1948 }
1949 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001950 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001951 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001952 + " from " + intentActivity);
1953 targetStack.moveToFront("intentActivityFound");
1954 }
1955
Craig Mautner8849a5e2013-04-02 16:41:03 -07001956 // If the caller has requested that the target task be
1957 // reset, then do so.
1958 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1959 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1960 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001961 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001962 // We don't need to start a new activity, and
1963 // the client said not to do anything if that
1964 // is the case, so this is it! And for paranoia, make
1965 // sure we have correctly resumed the top activity.
1966 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001967 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001968
1969 // Make sure to notify Keyguard as well if we are not running an app
1970 // transition later.
1971 if (!movedToFront) {
1972 notifyActivityDrawnForKeyguard();
1973 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001974 } else {
1975 ActivityOptions.abort(options);
1976 }
1977 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1978 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001979 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001980 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001981 // The caller has requested to completely replace any
1982 // existing task with its new activity. Well that should
1983 // not be too hard...
1984 reuseTask = intentActivity.task;
1985 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001986 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001987 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001988 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001989 // In this situation we want to remove all activities
1990 // from the task up to the one being started. In most
1991 // cases this means we are resetting the task to its
1992 // initial state.
1993 ActivityRecord top =
1994 intentActivity.task.performClearTaskLocked(r, launchFlags);
1995 if (top != null) {
1996 if (top.frontOfTask) {
1997 // Activity aliases may mean we use different
1998 // intents for the top activity, so make sure
1999 // the task now has the identity of the new
2000 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002001 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002002 }
2003 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2004 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002005 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002006 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002007 // A special case: we need to start the activity because it is not
2008 // currently running, and the caller has asked to clear the current
2009 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002010 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002011 // Now pretend like this activity is being started by the top of its
2012 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002013 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002014 TaskRecord task = sourceRecord.task;
2015 if (task != null && task.stack == null) {
2016 // Target stack got cleared when we all activities were removed
2017 // above. Go ahead and reset it.
2018 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2019 targetStack.addTask(
2020 task, !launchTaskBehind /* toTop */, false /* moving */);
2021 }
2022
Craig Mautner8849a5e2013-04-02 16:41:03 -07002023 }
2024 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2025 // In this case the top activity on the task is the
2026 // same as the one being launched, so we take that
2027 // as a request to bring the task to the foreground.
2028 // If the top activity in the task is the root
2029 // activity, deliver this new intent to it if it
2030 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002031 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002032 && intentActivity.realActivity.equals(r.realActivity)) {
2033 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2034 intentActivity.task);
2035 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002036 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002037 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002038 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2039 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002040 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2041 // In this case we are launching the root activity
2042 // of the task, but with a different intent. We
2043 // should start a new instance on top.
2044 addingToTask = true;
2045 sourceRecord = intentActivity;
2046 }
2047 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2048 // In this case an activity is being launched in to an
2049 // existing task, without resetting that task. This
2050 // is typically the situation of launching an activity
2051 // from a notification or shortcut. We want to place
2052 // the new activity on top of the current task.
2053 addingToTask = true;
2054 sourceRecord = intentActivity;
2055 } else if (!intentActivity.task.rootWasReset) {
2056 // In this case we are launching in to an existing task
2057 // that has not yet been started from its front door.
2058 // The current task has been brought to the front.
2059 // Ideally, we'd probably like to place this new task
2060 // at the bottom of its stack, but that's a little hard
2061 // to do with the current organization of the code so
2062 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002063 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002064 }
2065 if (!addingToTask && reuseTask == null) {
2066 // We didn't do anything... but it was needed (a.k.a., client
2067 // don't use that intent!) And for paranoia, make
2068 // sure we have correctly resumed the top activity.
2069 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002070 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002071 if (!movedToFront) {
2072 // Make sure to notify Keyguard as well if we are not running an app
2073 // transition later.
2074 notifyActivityDrawnForKeyguard();
2075 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002076 } else {
2077 ActivityOptions.abort(options);
2078 }
2079 return ActivityManager.START_TASK_TO_FRONT;
2080 }
2081 }
2082 }
2083 }
2084
2085 //String uri = r.intent.toURI();
2086 //Intent intent2 = new Intent(uri);
2087 //Slog.i(TAG, "Given intent: " + r.intent);
2088 //Slog.i(TAG, "URI is: " + uri);
2089 //Slog.i(TAG, "To intent: " + intent2);
2090
2091 if (r.packageName != null) {
2092 // If the activity being launched is the same as the one currently
2093 // at the top, then we need to check if it should only be launched
2094 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002095 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002096 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002097 if (top != null && r.resultTo == null) {
2098 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2099 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002100 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002101 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002102 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2103 top.task);
2104 // For paranoia, make sure we have correctly
2105 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002106 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002107 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002108 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002109 }
2110 ActivityOptions.abort(options);
2111 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2112 // We don't need to start a new activity, and
2113 // the client said not to do anything if that
2114 // is the case, so this is it!
2115 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2116 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002117 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002118 return ActivityManager.START_DELIVERED_TO_TOP;
2119 }
2120 }
2121 }
2122 }
2123
2124 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002125 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002126 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2127 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002128 }
2129 ActivityOptions.abort(options);
2130 return ActivityManager.START_CLASS_NOT_FOUND;
2131 }
2132
2133 boolean newTask = false;
2134 boolean keepCurTransition = false;
2135
Craig Mautnerbb742462014-07-07 15:28:55 -07002136 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002137 sourceRecord.task : null;
2138
Craig Mautner8849a5e2013-04-02 16:41:03 -07002139 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002140 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002141 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002142 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002143 targetStack = computeStackFocus(r, newTask);
2144 targetStack.moveToFront("startingNewTask");
2145
Craig Mautner8849a5e2013-04-02 16:41:03 -07002146 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002147 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2148 newTaskInfo != null ? newTaskInfo : r.info,
2149 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002150 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2151 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002152 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2153 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002154 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002155 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002156 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002157 if (isLockTaskModeViolation(r.task)) {
2158 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2159 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2160 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002161 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002162 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002163 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2164 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002165 // Caller wants to appear on home activity, so before starting
2166 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002167 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002168 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002169 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002170 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002171 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002172 if (isLockTaskModeViolation(sourceTask)) {
2173 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2174 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2175 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002176 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002177 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002178 final TaskRecord topTask = targetStack.topTask();
2179 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002180 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2181 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002182 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002183 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002184 // In this case, we are adding the activity to an existing
2185 // task, but the caller has asked to clear that task if the
2186 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002187 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002188 keepCurTransition = true;
2189 if (top != null) {
2190 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002191 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002192 // For paranoia, make sure we have correctly
2193 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002194 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002195 if (doResume) {
2196 targetStack.resumeTopActivityLocked(null);
2197 }
2198 ActivityOptions.abort(options);
2199 return ActivityManager.START_DELIVERED_TO_TOP;
2200 }
2201 } else if (!addingToTask &&
2202 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2203 // In this case, we are launching an activity in our own task
2204 // that may already be running somewhere in the history, and
2205 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002206 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002207 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002208 final TaskRecord task = top.task;
2209 task.moveActivityToFrontLocked(top);
2210 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002211 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002212 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002213 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002214 if (doResume) {
2215 targetStack.resumeTopActivityLocked(null);
2216 }
2217 return ActivityManager.START_DELIVERED_TO_TOP;
2218 }
2219 }
2220 // An existing activity is starting this new activity, so we want
2221 // to keep the new one in the same task as the one that is starting
2222 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002223 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002224 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002225 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002226
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002227 } else if (inTask != null) {
2228 // The calling is asking that the new activity be started in an explicit
2229 // task it has provided to us.
2230 if (isLockTaskModeViolation(inTask)) {
2231 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2232 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2233 }
2234 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002235 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002236
2237 // Check whether we should actually launch the new activity in to the task,
2238 // or just reuse the current activity on top.
2239 ActivityRecord top = inTask.getTopActivity();
2240 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2241 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2242 || launchSingleTop || launchSingleTask) {
2243 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2244 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2245 // We don't need to start a new activity, and
2246 // the client said not to do anything if that
2247 // is the case, so this is it!
2248 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2249 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002250 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002251 return ActivityManager.START_DELIVERED_TO_TOP;
2252 }
2253 }
2254
Dianne Hackborn962d5352014-08-29 16:27:40 -07002255 if (!addingToTask) {
2256 // We don't actually want to have this activity added to the task, so just
2257 // stop here but still tell the caller that we consumed the intent.
2258 ActivityOptions.abort(options);
2259 return ActivityManager.START_TASK_TO_FRONT;
2260 }
2261
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002262 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002263 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002264 + " in explicit task " + r.task);
2265
Craig Mautner8849a5e2013-04-02 16:41:03 -07002266 } else {
2267 // This not being started from an existing activity, and not part
2268 // of a new task... just put it in the top task, though these days
2269 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002270 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002271 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002272 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002273 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002274 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002275 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002276 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002277 + " in new guessed " + r.task);
2278 }
2279
2280 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002281 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002282
Craig Mautner76e2a762014-06-24 09:05:43 -07002283 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2284 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2285 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002286 if (newTask) {
2287 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2288 }
2289 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002290 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002291 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002292 if (!launchTaskBehind) {
2293 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002294 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002295 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002296 return ActivityManager.START_SUCCESS;
2297 }
2298
Craig Mautneree36c772014-07-16 14:56:05 -07002299 final void doPendingActivityLaunchesLocked(boolean doResume) {
2300 while (!mPendingActivityLaunches.isEmpty()) {
2301 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002302 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002303 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002304 }
2305 }
2306
Craig Mautner7f13ed32014-07-28 14:00:35 -07002307 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002308 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2309 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002310 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002311 mPendingActivityLaunches.remove(palNdx);
2312 }
2313 }
2314 }
2315
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002316 void setLaunchSource(int uid) {
2317 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2318 }
2319
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002320 void acquireLaunchWakelock() {
2321 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2322 throw new IllegalStateException("Calling must be system uid");
2323 }
2324 mLaunchingActivity.acquire();
2325 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2326 // To be safe, don't allow the wake lock to be held for too long.
2327 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2328 }
2329 }
2330
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002331 /**
2332 * Called when the frontmost task is idle.
2333 * @return the state of mService.mBooting before this was called.
2334 */
2335 private boolean checkFinishBootingLocked() {
2336 final boolean booting = mService.mBooting;
2337 boolean enableScreen = false;
2338 mService.mBooting = false;
2339 if (!mService.mBooted) {
2340 mService.mBooted = true;
2341 enableScreen = true;
2342 }
2343 if (booting || enableScreen) {
2344 mService.postFinishBooting(booting, enableScreen);
2345 }
2346 return booting;
2347 }
2348
Craig Mautnerf3333272013-04-22 10:55:53 -07002349 // Checked.
2350 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2351 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002352 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002353
Craig Mautnerf3333272013-04-22 10:55:53 -07002354 ArrayList<ActivityRecord> stops = null;
2355 ArrayList<ActivityRecord> finishes = null;
2356 ArrayList<UserStartedState> startingUsers = null;
2357 int NS = 0;
2358 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002359 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002360 boolean activityRemoved = false;
2361
Wale Ogunwale7d701172015-03-11 15:36:30 -07002362 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002363 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002364 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2365 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002366 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2367 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002368 if (fromTimeout) {
2369 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002370 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002371
2372 // This is a hack to semi-deal with a race condition
2373 // in the client where it can be constructed with a
2374 // newer configuration from when we asked it to launch.
2375 // We'll update with whatever configuration it now says
2376 // it used to launch.
2377 if (config != null) {
2378 r.configuration = config;
2379 }
2380
2381 // We are now idle. If someone is waiting for a thumbnail from
2382 // us, we can now deliver.
2383 r.idle = true;
2384
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002385 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002386 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002387 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002388 }
2389 }
2390
2391 if (allResumedActivitiesIdle()) {
2392 if (r != null) {
2393 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002394 }
2395
2396 if (mLaunchingActivity.isHeld()) {
2397 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2398 if (VALIDATE_WAKE_LOCK_CALLER &&
2399 Binder.getCallingUid() != Process.myUid()) {
2400 throw new IllegalStateException("Calling must be system uid");
2401 }
2402 mLaunchingActivity.release();
2403 }
2404 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002405 }
2406
2407 // Atomically retrieve all of the other things to do.
2408 stops = processStoppingActivitiesLocked(true);
2409 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002410 if ((NF = mFinishingActivities.size()) > 0) {
2411 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002412 mFinishingActivities.clear();
2413 }
2414
Craig Mautnerf3333272013-04-22 10:55:53 -07002415 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002416 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002417 mStartingUsers.clear();
2418 }
2419
Craig Mautnerf3333272013-04-22 10:55:53 -07002420 // Stop any activities that are scheduled to do so but have been
2421 // waiting for the next one to start.
2422 for (int i = 0; i < NS; i++) {
2423 r = stops.get(i);
2424 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002425 if (stack != null) {
2426 if (r.finishing) {
2427 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2428 } else {
2429 stack.stopActivityLocked(r);
2430 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002431 }
2432 }
2433
2434 // Finish 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 < NF; i++) {
2437 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002438 final ActivityStack stack = r.task.stack;
2439 if (stack != null) {
2440 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2441 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002442 }
2443
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002444 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002445 // Complete user switch
2446 if (startingUsers != null) {
2447 for (int i = 0; i < startingUsers.size(); i++) {
2448 mService.finishUserSwitch(startingUsers.get(i));
2449 }
2450 }
2451 // Complete starting up of background users
2452 if (mStartingBackgroundUsers.size() > 0) {
2453 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2454 mStartingBackgroundUsers.clear();
2455 for (int i = 0; i < startingUsers.size(); i++) {
2456 mService.finishUserBoot(startingUsers.get(i));
2457 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002458 }
2459 }
2460
2461 mService.trimApplications();
2462 //dump();
2463 //mWindowManager.dump();
2464
Craig Mautnerf3333272013-04-22 10:55:53 -07002465 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002466 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002467 }
2468
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002469 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002470 }
2471
Craig Mautner8e569572013-10-11 17:36:59 -07002472 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002473 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002474 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2475 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002476 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2477 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2478 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002479 }
Craig Mautner19091252013-10-05 00:03:53 -07002480 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002481 }
2482
2483 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002484 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2485 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002486 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2487 stacks.get(stackNdx).closeSystemDialogsLocked();
2488 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002489 }
2490 }
2491
Craig Mautner93529a42013-10-04 15:03:13 -07002492 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002493 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002494 }
2495
Craig Mautner8d341ef2013-03-26 09:03:27 -07002496 /**
2497 * @return true if some activity was finished (or would have finished if doit were true).
2498 */
2499 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2500 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002501 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2502 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002503 final int numStacks = stacks.size();
2504 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2505 final ActivityStack stack = stacks.get(stackNdx);
2506 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2507 didSomething = true;
2508 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002509 }
2510 }
2511 return didSomething;
2512 }
2513
Dianne Hackborna413dc02013-07-12 12:02:55 -07002514 void updatePreviousProcessLocked(ActivityRecord r) {
2515 // Now that this process has stopped, we may want to consider
2516 // it to be the previous app to try to keep around in case
2517 // the user wants to return to it.
2518
2519 // First, found out what is currently the foreground app, so that
2520 // we don't blow away the previous app if this activity is being
2521 // hosted by the process that is actually still the foreground.
2522 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002523 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2524 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002525 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2526 final ActivityStack stack = stacks.get(stackNdx);
2527 if (isFrontStack(stack)) {
2528 if (stack.mResumedActivity != null) {
2529 fgApp = stack.mResumedActivity.app;
2530 } else if (stack.mPausingActivity != null) {
2531 fgApp = stack.mPausingActivity.app;
2532 }
2533 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002534 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002535 }
2536 }
2537
2538 // Now set this one as the previous process, only if that really
2539 // makes sense to.
2540 if (r.app != null && fgApp != null && r.app != fgApp
2541 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002542 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002543 mService.mPreviousProcess = r.app;
2544 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2545 }
2546 }
2547
Craig Mautner05d29032013-05-03 13:40:13 -07002548 boolean resumeTopActivitiesLocked() {
2549 return resumeTopActivitiesLocked(null, null, null);
2550 }
2551
2552 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2553 Bundle targetOptions) {
2554 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002555 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002556 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002557 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002558 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002559 if (isFrontStack(targetStack)) {
2560 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2561 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002562
Craig Mautnere0a38842013-12-16 16:14:02 -08002563 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2564 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002565 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2566 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002567 if (stack == targetStack) {
2568 // Already started above.
2569 continue;
2570 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002571 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002572 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002573 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002574 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002575 }
Craig Mautner05d29032013-05-03 13:40:13 -07002576 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002577 }
2578
Todd Kennedy539db512014-12-15 09:57:55 -08002579 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002580 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2581 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002582 final int numStacks = stacks.size();
2583 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2584 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002585 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002586 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002587 }
2588 }
2589
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002590 void finishVoiceTask(IVoiceInteractionSession session) {
2591 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2592 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2593 final int numStacks = stacks.size();
2594 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2595 final ActivityStack stack = stacks.get(stackNdx);
2596 stack.finishVoiceTask(session);
2597 }
2598 }
2599 }
2600
Craig Mautner299f9602015-01-26 09:47:33 -08002601 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002602 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2603 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002604 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002605 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2606 // Caller wants the home activity moved with it. To accomplish this,
2607 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002608 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002609 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002610 if (task.stack == null) {
2611 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2612 + task + " to front. Stack is null");
2613 return;
2614 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002615 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002616 if (DEBUG_STACK) Slog.d(TAG_STACK,
2617 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002618 }
2619
Craig Mautner967212c2013-04-13 21:10:58 -07002620 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002621 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2622 if (activityContainer != null) {
2623 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002624 }
2625 return null;
2626 }
2627
Craig Mautner967212c2013-04-13 21:10:58 -07002628 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002629 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002630 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2631 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002632 }
2633 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002634 }
2635
Craig Mautner4a1cb222013-12-04 16:14:06 -08002636 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002637 ActivityRecord homeActivity = getHomeActivity();
2638 if (homeActivity != null) {
2639 return homeActivity.appToken;
2640 }
2641 return null;
2642 }
2643
2644 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002645 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2646 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2647 final TaskRecord task = tasks.get(taskNdx);
2648 if (task.isHomeTask()) {
2649 final ArrayList<ActivityRecord> activities = task.mActivities;
2650 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2651 final ActivityRecord r = activities.get(activityNdx);
2652 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002653 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002654 }
2655 }
2656 }
2657 }
2658 return null;
2659 }
2660
Todd Kennedyca4d8422015-01-15 15:19:22 -08002661 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002662 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002663 ActivityContainer activityContainer =
2664 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002665 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002666 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002667 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002668 return activityContainer;
2669 }
2670
Craig Mautner34b73df2014-01-12 21:11:08 -08002671 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002672 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2673 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2674 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002675 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2676 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002677 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002678 }
2679 }
2680
Craig Mautner95da1082014-02-24 17:54:35 -08002681 void deleteActivityContainer(IActivityContainer container) {
2682 ActivityContainer activityContainer = (ActivityContainer)container;
2683 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002684 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2685 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002686 final int stackId = activityContainer.mStackId;
2687 mActivityContainers.remove(stackId);
2688 mWindowManager.removeStack(stackId);
2689 }
2690 }
2691
Wale Ogunwale60454db2015-01-23 16:05:07 -08002692 void resizeStackLocked(int stackId, Rect bounds) {
2693 final ActivityStack stack = getStack(stackId);
2694 if (stack == null) {
2695 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2696 return;
2697 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002698
2699 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002700 if (r != null && !r.task.mResizeable) {
2701 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002702 return;
2703 }
2704
Wale Ogunwale60454db2015-01-23 16:05:07 -08002705 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2706 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002707 if (r != null) {
2708 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2709 // And we need to make sure at this point that all other activities
2710 // are made visible with the correct configuration.
2711 ensureActivitiesVisibleLocked(r, 0);
2712 if (!updated) {
2713 resumeTopActivitiesLocked(stack, null, null);
2714 }
2715 }
2716 }
2717 }
2718
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002719 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2720 * current task stack if it only has one entry, moving the task to a stack that matches the
2721 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2722 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2723 task.mResizeable = true;
2724 final ActivityStack currentStack = task.stack;
2725 if (currentStack.isHomeStack()) {
2726 // Can't move task off the home stack. Sorry!
2727 return;
2728 }
2729
2730 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2731 if (matchingStackId != -1) {
2732 // There is already a stack with the right bounds!
2733 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2734 // Nothing to do here. Already in the right stack...
2735 return;
2736 }
2737 // Move task to stack with matching bounds.
2738 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2739 return;
2740 }
2741
2742 if (currentStack != null && currentStack.numTasks() == 1) {
2743 // Just resize the current stack since this is the task in it.
2744 resizeStackLocked(currentStack.mStackId, bounds);
2745 return;
2746 }
2747
2748 // Create new stack and move the task to it.
2749 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2750 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2751 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2752
2753 if (newStack == null) {
2754 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2755 return;
2756 }
2757 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2758 resizeStackLocked(newStack.mStackId, bounds);
2759 }
2760
Todd Kennedy4900bf92015-01-16 16:05:14 -08002761 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002762 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2763 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002764 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002765 }
2766
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002767 ActivityContainer activityContainer = new ActivityContainer(stackId);
2768 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002769 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002770 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002771 }
2772
2773 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002774 while (true) {
2775 if (++mLastStackId <= HOME_STACK_ID) {
2776 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002777 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002778 if (getStack(mLastStackId) == null) {
2779 break;
2780 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002781 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002782 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002783 }
2784
Wale Ogunwale7de05352014-12-12 15:21:33 -08002785 private boolean restoreRecentTaskLocked(TaskRecord task) {
2786 ActivityStack stack = null;
2787 // Determine stack to restore task to.
2788 if (mLeanbackOnlyDevice) {
2789 // There is only one stack for lean back devices.
2790 stack = mHomeStack;
2791 } else {
2792 // Look for the top stack on the home display that isn't the home stack.
2793 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2794 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2795 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002796 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002797 stack = tmpStack;
2798 break;
2799 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002800 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002801 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002802
2803 if (stack == null) {
2804 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2805 // before an application stack is created...Go ahead and create one on the default
2806 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002807 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002808 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002809 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002810 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2811 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002812 }
2813
2814 if (stack == null) {
2815 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002816 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2817 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002818 return false;
2819 }
2820
2821 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002822 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2823 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002824 final ArrayList<ActivityRecord> activities = task.mActivities;
2825 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2826 final ActivityRecord r = activities.get(activityNdx);
2827 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2828 r.info.screenOrientation, r.fullscreen,
2829 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2830 r.userId, r.info.configChanges, task.voiceSession != null,
2831 r.mLaunchTaskBehind);
2832 }
2833 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002834 }
2835
Craig Mautner299f9602015-01-26 09:47:33 -08002836 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002837 final TaskRecord task = anyTaskForIdLocked(taskId);
2838 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002839 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002840 return;
2841 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002842 final ActivityStack stack = getStack(stackId);
2843 if (stack == null) {
2844 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2845 return;
2846 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002847 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2848 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07002849 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002850 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002851 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002852 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002853 }
2854
Craig Mautnerac6f8432013-07-17 13:24:59 -07002855 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002856 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002857 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2858 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002859 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2860 final ActivityStack stack = stacks.get(stackNdx);
2861 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002862 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002863 continue;
2864 }
2865 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002866 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2867 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002868 continue;
2869 }
2870 final ActivityRecord ar = stack.findTaskLocked(r);
2871 if (ar != null) {
2872 return ar;
2873 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002874 }
2875 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002876 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002877 return null;
2878 }
2879
2880 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
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 ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2885 if (ar != null) {
2886 return ar;
2887 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002888 }
2889 }
2890 return null;
2891 }
2892
Craig Mautner8d341ef2013-03-26 09:03:27 -07002893 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002894 scheduleSleepTimeout();
2895 if (!mGoingToSleep.isHeld()) {
2896 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002897 if (mLaunchingActivity.isHeld()) {
2898 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2899 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002900 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002901 mLaunchingActivity.release();
2902 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002903 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002904 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002905 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002906 }
2907
2908 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002909 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002910
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002911 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002912 final long endTime = System.currentTimeMillis() + timeout;
2913 while (true) {
2914 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002915 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2916 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002917 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2918 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2919 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002920 }
2921 if (cantShutdown) {
2922 long timeRemaining = endTime - System.currentTimeMillis();
2923 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002924 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002925 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002926 } catch (InterruptedException e) {
2927 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002928 } else {
2929 Slog.w(TAG, "Activity manager shutdown timed out");
2930 timedout = true;
2931 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002932 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002933 } else {
2934 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002935 }
2936 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002937
2938 // Force checkReadyForSleep to complete.
2939 mSleepTimeout = true;
2940 checkReadyForSleepLocked();
2941
Craig Mautner8d341ef2013-03-26 09:03:27 -07002942 return timedout;
2943 }
2944
2945 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002946 removeSleepTimeouts();
2947 if (mGoingToSleep.isHeld()) {
2948 mGoingToSleep.release();
2949 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002950 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2951 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002952 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2953 final ActivityStack stack = stacks.get(stackNdx);
2954 stack.awakeFromSleepingLocked();
2955 if (isFrontStack(stack)) {
2956 resumeTopActivitiesLocked();
2957 }
Craig Mautner5314a402013-09-26 12:40:16 -07002958 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002959 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002960 mGoingToSleepActivities.clear();
2961 }
2962
2963 void activitySleptLocked(ActivityRecord r) {
2964 mGoingToSleepActivities.remove(r);
2965 checkReadyForSleepLocked();
2966 }
2967
2968 void checkReadyForSleepLocked() {
2969 if (!mService.isSleepingOrShuttingDown()) {
2970 // Do not care.
2971 return;
2972 }
2973
2974 if (!mSleepTimeout) {
2975 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002976 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2977 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002978 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2979 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2980 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002981 }
2982
2983 if (mStoppingActivities.size() > 0) {
2984 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002985 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002986 + mStoppingActivities.size() + " activities");
2987 scheduleIdleLocked();
2988 dontSleep = true;
2989 }
2990
2991 if (mGoingToSleepActivities.size() > 0) {
2992 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002993 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002994 + mGoingToSleepActivities.size() + " activities");
2995 dontSleep = true;
2996 }
2997
2998 if (dontSleep) {
2999 return;
3000 }
3001 }
3002
Craig Mautnere0a38842013-12-16 16:14:02 -08003003 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3004 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003005 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3006 stacks.get(stackNdx).goToSleep();
3007 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003008 }
3009
3010 removeSleepTimeouts();
3011
3012 if (mGoingToSleep.isHeld()) {
3013 mGoingToSleep.release();
3014 }
3015 if (mService.mShuttingDown) {
3016 mService.notifyAll();
3017 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003018 }
3019
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003020 boolean reportResumedActivityLocked(ActivityRecord r) {
3021 final ActivityStack stack = r.task.stack;
3022 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003023 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003024 }
3025 if (allResumedActivitiesComplete()) {
3026 ensureActivitiesVisibleLocked(null, 0);
3027 mWindowManager.executeAppTransition();
3028 return true;
3029 }
3030 return false;
3031 }
3032
Craig Mautner8d341ef2013-03-26 09:03:27 -07003033 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003034 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3035 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003036 final int numStacks = stacks.size();
3037 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3038 final ActivityStack stack = stacks.get(stackNdx);
3039 stack.handleAppCrashLocked(app);
3040 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003041 }
3042 }
3043
Jose Lima4b6c6692014-08-12 17:41:12 -07003044 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003045 final ActivityStack stack = r.task.stack;
3046 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003047 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3048 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003049 return false;
3050 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003051 final boolean isVisible = stack.hasVisibleBehindActivity();
3052 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3053 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003054
3055 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003056 if (top == null || top == r || (visible == isVisible)) {
3057 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3058 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003059 return true;
3060 }
3061
3062 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003063 if (visible && top.fullscreen) {
3064 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003065 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003066 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003067 " top.app.thread=" + top.app.thread);
3068 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003069 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3070 // Only the activity set as currently visible behind should actively reset its
3071 // visible behind state.
3072 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3073 + visible + " stack.getVisibleBehindActivity()=" +
3074 stack.getVisibleBehindActivity() + " r=" + r);
3075 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003076 }
3077
Jose Lima4b6c6692014-08-12 17:41:12 -07003078 stack.setVisibleBehindActivity(visible ? r : null);
3079 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003080 // Make the activity immediately above r opaque.
3081 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3082 if (next != null) {
3083 mService.convertFromTranslucent(next.appToken);
3084 }
3085 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003086 if (top.app != null && top.app.thread != null) {
3087 // Notify the top app of the change.
3088 try {
3089 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3090 } catch (RemoteException e) {
3091 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003092 }
3093 return true;
3094 }
3095
Craig Mautnerbb742462014-07-07 15:28:55 -07003096 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3097 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3098 r.mLaunchTaskBehind = false;
3099 final TaskRecord task = r.task;
3100 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003101 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003102 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003103 mWindowManager.setAppVisibility(r.appToken, false);
3104 }
3105
3106 void scheduleLaunchTaskBehindComplete(IBinder token) {
3107 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3108 }
3109
Craig Mautnerde4ef022013-04-07 19:01:33 -07003110 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003111 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003112 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3113 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003114 final int topStackNdx = stacks.size() - 1;
3115 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3116 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003117 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003118 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003119 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003120 }
3121
3122 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003123 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3124 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003125 final int numStacks = stacks.size();
3126 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3127 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003128 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003129 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003130 }
3131 }
3132
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003133 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3134 // Examine all activities currently running in the process.
3135 TaskRecord firstTask = null;
3136 // Tasks is non-null only if two or more tasks are found.
3137 ArraySet<TaskRecord> tasks = null;
3138 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3139 for (int i=0; i<app.activities.size(); i++) {
3140 ActivityRecord r = app.activities.get(i);
3141 // First, if we find an activity that is in the process of being destroyed,
3142 // then we just aren't going to do anything for now; we want things to settle
3143 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003144 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003145 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3146 return;
3147 }
3148 // Don't consider any activies that are currently not in a state where they
3149 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003150 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3151 || r.state == PAUSED || r.state == STOPPING) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003152 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3153 continue;
3154 }
3155 if (r.task != null) {
3156 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3157 + " from " + r);
3158 if (firstTask == null) {
3159 firstTask = r.task;
3160 } else if (firstTask != r.task) {
3161 if (tasks == null) {
3162 tasks = new ArraySet<>();
3163 tasks.add(firstTask);
3164 }
3165 tasks.add(r.task);
3166 }
3167 }
3168 }
3169 if (tasks == null) {
3170 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3171 return;
3172 }
3173 // If we have activities in multiple tasks that are in a position to be destroyed,
3174 // let's iterate through the tasks and release the oldest one.
3175 final int numDisplays = mActivityDisplays.size();
3176 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3177 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3178 // Step through all stacks starting from behind, to hit the oldest things first.
3179 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3180 final ActivityStack stack = stacks.get(stackNdx);
3181 // Try to release activities in this stack; if we manage to, we are done.
3182 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3183 return;
3184 }
3185 }
3186 }
3187 }
3188
Craig Mautner8d341ef2013-03-26 09:03:27 -07003189 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003190 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003191 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003192 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003193
Craig Mautner858d8a62013-04-23 17:08:34 -07003194 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003195 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3196 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003197 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003198 final ActivityStack stack = stacks.get(stackNdx);
3199 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003200 TaskRecord task = stack.topTask();
3201 if (task != null) {
3202 mWindowManager.moveTaskToTop(task.taskId);
3203 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003204 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003205 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003206
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003207 ActivityStack stack = getStack(restoreStackId);
3208 if (stack == null) {
3209 stack = mHomeStack;
3210 }
3211 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003212 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003213 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003214 TaskRecord task = stack.topTask();
3215 if (task != null) {
3216 mWindowManager.moveTaskToTop(task.taskId);
3217 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003218 } else {
3219 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003220 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003221 }
Craig Mautner93529a42013-10-04 15:03:13 -07003222 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003223 }
3224
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003225 /**
3226 * Add background users to send boot completed events to.
3227 * @param userId The user being started in the background
3228 * @param uss The state object for the user.
3229 */
3230 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3231 mStartingBackgroundUsers.add(uss);
3232 }
3233
Craig Mautnerde4ef022013-04-07 19:01:33 -07003234 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003235 ArrayList<ActivityRecord> stops = null;
3236
3237 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003238 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3239 ActivityRecord s = mStoppingActivities.get(activityNdx);
3240 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003241 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003242 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3243 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003244 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003245 if (s.finishing) {
3246 // If this activity is finishing, it is sitting on top of
3247 // everyone else but we now know it is no longer needed...
3248 // so get rid of it. Otherwise, we need to go through the
3249 // normal flow and hide it once we determine that it is
3250 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003251 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003252 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003253 }
3254 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003255 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003256 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003257 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003258 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003259 }
3260 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003261 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003262 }
3263 }
3264
3265 return stops;
3266 }
3267
Craig Mautnercf910b02013-04-23 11:23:27 -07003268 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003269 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3270 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3271 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3272 final ActivityStack stack = stacks.get(stackNdx);
3273 final ActivityRecord r = stack.topRunningActivityLocked(null);
3274 final ActivityState state = r == null ? DESTROYED : r.state;
3275 if (isFrontStack(stack)) {
3276 if (r == null) Slog.e(TAG,
3277 "validateTop...: null top activity, stack=" + stack);
3278 else {
3279 final ActivityRecord pausing = stack.mPausingActivity;
3280 if (pausing != null && pausing == r) Slog.e(TAG,
3281 "validateTop...: top stack has pausing activity r=" + r
3282 + " state=" + state);
3283 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3284 "validateTop...: activity in front not resumed r=" + r
3285 + " state=" + state);
3286 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003287 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003288 final ActivityRecord resumed = stack.mResumedActivity;
3289 if (resumed != null && resumed == r) Slog.e(TAG,
3290 "validateTop...: back stack has resumed activity r=" + r
3291 + " state=" + state);
3292 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3293 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003294 }
3295 }
3296 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003297 }
3298
Craig Mautner27084302013-03-25 08:05:25 -07003299 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003300 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003301 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003302 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3303 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3304 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003305 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner15df08a2015-04-01 12:17:18 -07003306 pw.print(prefix); pw.println("mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003307 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003308
Craig Mautner20e72272013-04-01 13:45:53 -07003309 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003310 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003311 }
3312
Dianne Hackborn390517b2013-05-30 15:03:32 -07003313 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3314 boolean needSep, String prefix) {
3315 if (activity != null) {
3316 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3317 if (needSep) {
3318 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003319 }
3320 pw.print(prefix);
3321 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003322 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003323 }
3324 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003325 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003326 }
3327
Craig Mautner8d341ef2013-03-26 09:03:27 -07003328 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3329 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003330 boolean printed = false;
3331 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003332 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3333 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003334 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003335 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003336 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003337 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003338 final ActivityStack stack = stacks.get(stackNdx);
3339 StringBuilder stackHeader = new StringBuilder(128);
3340 stackHeader.append(" Stack #");
3341 stackHeader.append(stack.mStackId);
3342 stackHeader.append(":");
3343 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3344 needSep, stackHeader.toString());
3345 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3346 !dumpAll, false, dumpPackage, true,
3347 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003348
Craig Mautner4a1cb222013-12-04 16:14:06 -08003349 needSep = printed;
3350 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3351 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003352 if (pr) {
3353 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003354 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003355 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003356 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3357 " mResumedActivity: ");
3358 if (pr) {
3359 printed = true;
3360 needSep = false;
3361 }
3362 if (dumpAll) {
3363 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3364 " mLastPausedActivity: ");
3365 if (pr) {
3366 printed = true;
3367 needSep = true;
3368 }
3369 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3370 needSep, " mLastNoHistoryActivity: ");
3371 }
3372 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003373 }
3374 }
3375
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003376 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3377 false, dumpPackage, true, " Activities waiting to finish:", null);
3378 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3379 false, dumpPackage, true, " Activities waiting to stop:", null);
3380 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3381 false, dumpPackage, true, " Activities waiting for another to become visible:",
3382 null);
3383 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3384 false, dumpPackage, true, " Activities waiting to sleep:", null);
3385 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3386 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003387
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003388 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003389 }
3390
Dianne Hackborn390517b2013-05-30 15:03:32 -07003391 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003392 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003393 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003394 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003395 String innerPrefix = null;
3396 String[] args = null;
3397 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003398 for (int i=list.size()-1; i>=0; i--) {
3399 final ActivityRecord r = list.get(i);
3400 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3401 continue;
3402 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003403 if (innerPrefix == null) {
3404 innerPrefix = prefix + " ";
3405 args = new String[0];
3406 }
3407 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003408 final boolean full = !brief && (complete || !r.isInHistory());
3409 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003410 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003411 needNL = false;
3412 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003413 if (header1 != null) {
3414 pw.println(header1);
3415 header1 = null;
3416 }
3417 if (header2 != null) {
3418 pw.println(header2);
3419 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003420 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003421 if (lastTask != r.task) {
3422 lastTask = r.task;
3423 pw.print(prefix);
3424 pw.print(full ? "* " : " ");
3425 pw.println(lastTask);
3426 if (full) {
3427 lastTask.dump(pw, prefix + " ");
3428 } else if (complete) {
3429 // Complete + brief == give a summary. Isn't that obvious?!?
3430 if (lastTask.intent != null) {
3431 pw.print(prefix); pw.print(" ");
3432 pw.println(lastTask.intent.toInsecureStringWithClip());
3433 }
3434 }
3435 }
3436 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3437 pw.print(" #"); pw.print(i); pw.print(": ");
3438 pw.println(r);
3439 if (full) {
3440 r.dump(pw, innerPrefix);
3441 } else if (complete) {
3442 // Complete + brief == give a summary. Isn't that obvious?!?
3443 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3444 if (r.app != null) {
3445 pw.print(innerPrefix); pw.println(r.app);
3446 }
3447 }
3448 if (client && r.app != null && r.app.thread != null) {
3449 // flush anything that is already in the PrintWriter since the thread is going
3450 // to write to the file descriptor directly
3451 pw.flush();
3452 try {
3453 TransferPipe tp = new TransferPipe();
3454 try {
3455 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3456 r.appToken, innerPrefix, args);
3457 // Short timeout, since blocking here can
3458 // deadlock with the application.
3459 tp.go(fd, 2000);
3460 } finally {
3461 tp.kill();
3462 }
3463 } catch (IOException e) {
3464 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3465 } catch (RemoteException e) {
3466 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3467 }
3468 needNL = true;
3469 }
3470 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003471 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003472 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003473
Craig Mautnerf3333272013-04-22 10:55:53 -07003474 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003475 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003476 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3477 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003478 }
3479
3480 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003481 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003482 }
3483
3484 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003485 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003486 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3487 }
3488
Craig Mautner05d29032013-05-03 13:40:13 -07003489 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003490 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3491 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3492 }
Craig Mautner05d29032013-05-03 13:40:13 -07003493 }
3494
Craig Mautner0eea92c2013-05-16 13:35:39 -07003495 void removeSleepTimeouts() {
3496 mSleepTimeout = false;
3497 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3498 }
3499
3500 final void scheduleSleepTimeout() {
3501 removeSleepTimeouts();
3502 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3503 }
3504
Craig Mautner4a1cb222013-12-04 16:14:06 -08003505 @Override
3506 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003507 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003508 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3509 }
3510
3511 @Override
3512 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003513 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003514 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3515 }
3516
3517 @Override
3518 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003519 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003520 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3521 }
3522
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003523 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003524 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003525 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003526 newDisplay = mActivityDisplays.get(displayId) == null;
3527 if (newDisplay) {
3528 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003529 if (activityDisplay.mDisplay == null) {
3530 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3531 return;
3532 }
Craig Mautner4504de52013-12-20 09:06:56 -08003533 mActivityDisplays.put(displayId, activityDisplay);
3534 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003535 }
Craig Mautner4504de52013-12-20 09:06:56 -08003536 if (newDisplay) {
3537 mWindowManager.onDisplayAdded(displayId);
3538 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003539 }
3540
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003541 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003542 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003543 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3544 if (activityDisplay != null) {
3545 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003546 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003547 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003548 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003549 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003550 }
3551 }
3552 mWindowManager.onDisplayRemoved(displayId);
3553 }
3554
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003555 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003556 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003557 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3558 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003559 // TODO: Update the bounds.
3560 }
3561 }
3562 mWindowManager.onDisplayChanged(displayId);
3563 }
3564
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003565 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003566 StackInfo info = new StackInfo();
3567 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3568 info.displayId = Display.DEFAULT_DISPLAY;
3569 info.stackId = stack.mStackId;
3570
3571 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3572 final int numTasks = tasks.size();
3573 int[] taskIds = new int[numTasks];
3574 String[] taskNames = new String[numTasks];
3575 for (int i = 0; i < numTasks; ++i) {
3576 final TaskRecord task = tasks.get(i);
3577 taskIds[i] = task.taskId;
3578 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3579 : task.realActivity != null ? task.realActivity.flattenToString()
3580 : task.getTopActivity() != null ? task.getTopActivity().packageName
3581 : "unknown";
3582 }
3583 info.taskIds = taskIds;
3584 info.taskNames = taskNames;
3585 return info;
3586 }
3587
3588 StackInfo getStackInfoLocked(int stackId) {
3589 ActivityStack stack = getStack(stackId);
3590 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003591 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003592 }
3593 return null;
3594 }
3595
3596 ArrayList<StackInfo> getAllStackInfosLocked() {
3597 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003598 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3599 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003600 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003601 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003602 }
3603 }
3604 return list;
3605 }
3606
Craig Mautner15df08a2015-04-01 12:17:18 -07003607 TaskRecord getLockedTaskLocked() {
3608 final int top = mLockTaskModeTasks.size() - 1;
3609 if (top >= 0) {
3610 return mLockTaskModeTasks.get(top);
3611 }
3612 return null;
3613 }
3614
3615 boolean isLockedTask(TaskRecord task) {
3616 return mLockTaskModeTasks.contains(task);
3617 }
3618
3619 boolean isLastLockedTask(TaskRecord task) {
3620 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3621 }
3622
3623 void removeLockedTaskLocked(final TaskRecord task) {
3624 if (mLockTaskModeTasks.remove(task) && mLockTaskModeTasks.isEmpty()) {
3625 // Last one.
3626 final Message lockTaskMsg = Message.obtain();
3627 lockTaskMsg.arg1 = task.userId;
3628 lockTaskMsg.what = LOCK_TASK_END_MSG;
3629 mHandler.sendMessage(lockTaskMsg);
3630 }
3631 }
3632
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003633 void showLockTaskToast() {
3634 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003635 }
3636
Benjamin Franz43261142015-02-11 15:59:44 +00003637 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003638 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003639 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003640 final TaskRecord lockedTask = getLockedTaskLocked();
3641 if (lockedTask != null) {
3642 removeLockedTaskLocked(lockedTask);
3643 if (!mLockTaskModeTasks.isEmpty()) {
3644 // There are locked tasks remaining, can only finish this task, not unlock it.
3645 lockedTask.performClearTaskLocked();
3646 resumeTopActivitiesLocked();
3647 return;
3648 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003649 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003650 return;
3651 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003652
3653 // Should have already been checked, but do it again.
3654 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003655 return;
3656 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003657 if (isLockTaskModeViolation(task)) {
3658 Slog.e(TAG, "setLockTaskMode: Attempt to start an unauthorized lock task.");
3659 return;
3660 }
3661
3662 if (mLockTaskModeTasks.isEmpty()) {
3663 // First locktask.
3664 final Message lockTaskMsg = Message.obtain();
3665 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3666 lockTaskMsg.arg1 = task.userId;
3667 lockTaskMsg.what = LOCK_TASK_START_MSG;
3668 lockTaskMsg.arg2 = lockTaskModeState;
3669 mHandler.sendMessage(lockTaskMsg);
3670 }
3671 // Add it or move it to the top.
3672 mLockTaskModeTasks.remove(task);
3673 mLockTaskModeTasks.add(task);
3674
3675 if (task.mLockTaskUid == -1) {
3676 task.mLockTaskUid = task.mCallingUid;
3677 }
Craig Mautner299f9602015-01-26 09:47:33 -08003678 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003679 resumeTopActivitiesLocked();
3680 }
3681
3682 boolean isLockTaskModeViolation(TaskRecord task) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003683 if (getLockedTaskLocked() == task) {
3684 return false;
3685 }
3686 final int lockTaskAuth = task.mLockTaskAuth;
3687 switch (lockTaskAuth) {
3688 case LOCK_TASK_AUTH_DONT_LOCK:
3689 return !mLockTaskModeTasks.isEmpty();
3690 case LOCK_TASK_AUTH_LAUNCHABLE:
3691 case LOCK_TASK_AUTH_WHITELISTED:
3692 return false;
3693 case LOCK_TASK_AUTH_PINNABLE:
3694 // Pinnable tasks can't be launched on top of locktask tasks.
3695 return !mLockTaskModeTasks.isEmpty();
3696 default:
3697 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3698 return true;
3699 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003700 }
3701
Craig Mautner15df08a2015-04-01 12:17:18 -07003702 void onLockTaskPackagesUpdatedLocked() {
3703 boolean didSomething = false;
3704 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3705 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
3706 if (lockedTask.mLockTaskMode != LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED) {
3707 continue;
3708 }
3709 final boolean wasLaunchable = lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE;
3710 lockedTask.setLockTaskAuth();
3711 if (wasLaunchable && lockedTask.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE) {
3712 // Lost whitelisting authorization. End it now.
3713 removeLockedTaskLocked(lockedTask);
3714 lockedTask.performClearTaskLocked();
3715 didSomething = true;
3716 }
3717 }
3718 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3719 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3720 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3721 final ActivityStack stack = stacks.get(stackNdx);
3722 stack.onLockTaskPackagesUpdatedLocked();
3723 }
3724 }
3725 if (didSomething) {
3726 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003727 }
3728 }
3729
Benjamin Franz43261142015-02-11 15:59:44 +00003730 int getLockTaskModeState() {
3731 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003732 }
3733
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003734 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003735
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003736 public ActivityStackSupervisorHandler(Looper looper) {
3737 super(looper);
3738 }
3739
Craig Mautnerf3333272013-04-22 10:55:53 -07003740 void activityIdleInternal(ActivityRecord r) {
3741 synchronized (mService) {
3742 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3743 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003744 }
3745
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003746 @Override
3747 public void handleMessage(Message msg) {
3748 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003749 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003750 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003751 if (mService.mDidDexOpt) {
3752 mService.mDidDexOpt = false;
3753 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3754 nmsg.obj = msg.obj;
3755 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3756 return;
3757 }
3758 // We don't at this point know if the activity is fullscreen,
3759 // so we need to be conservative and assume it isn't.
3760 activityIdleInternal((ActivityRecord)msg.obj);
3761 } break;
3762 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003763 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003764 activityIdleInternal((ActivityRecord)msg.obj);
3765 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003766 case RESUME_TOP_ACTIVITY_MSG: {
3767 synchronized (mService) {
3768 resumeTopActivitiesLocked();
3769 }
3770 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003771 case SLEEP_TIMEOUT_MSG: {
3772 synchronized (mService) {
3773 if (mService.isSleepingOrShuttingDown()) {
3774 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3775 mSleepTimeout = true;
3776 checkReadyForSleepLocked();
3777 }
3778 }
3779 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003780 case LAUNCH_TIMEOUT_MSG: {
3781 if (mService.mDidDexOpt) {
3782 mService.mDidDexOpt = false;
3783 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3784 return;
3785 }
3786 synchronized (mService) {
3787 if (mLaunchingActivity.isHeld()) {
3788 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3789 if (VALIDATE_WAKE_LOCK_CALLER
3790 && Binder.getCallingUid() != Process.myUid()) {
3791 throw new IllegalStateException("Calling must be system uid");
3792 }
3793 mLaunchingActivity.release();
3794 }
3795 }
3796 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003798 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003799 } break;
3800 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003801 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003802 } break;
3803 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003804 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003805 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003806 case CONTAINER_CALLBACK_VISIBILITY: {
3807 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003808 final IActivityContainerCallback callback = container.mCallback;
3809 if (callback != null) {
3810 try {
3811 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3812 } catch (RemoteException e) {
3813 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003814 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003815 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003816 case LOCK_TASK_START_MSG: {
3817 // When lock task starts, we disable the status bars.
3818 try {
Jason Monk62515be2014-05-21 16:06:19 -04003819 if (mLockTaskNotify == null) {
3820 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003821 }
Jason Monk62515be2014-05-21 16:06:19 -04003822 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003823 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003824 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003825 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003826 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003827 flags = StatusBarManager.DISABLE_MASK
3828 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003829 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003830 flags = StatusBarManager.DISABLE_MASK
3831 & (~StatusBarManager.DISABLE_BACK)
3832 & (~StatusBarManager.DISABLE_HOME)
3833 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003834 }
3835 getStatusBarService().disable(flags, mToken,
3836 mService.mContext.getPackageName());
3837 }
3838 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003839 if (getDevicePolicyManager() != null) {
3840 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003841 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003842 }
justinzhang5286d3f2014-05-12 17:06:01 -04003843 } catch (RemoteException ex) {
3844 throw new RuntimeException(ex);
3845 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003846 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003847 case LOCK_TASK_END_MSG: {
3848 // When lock task ends, we enable the status bars.
3849 try {
Jason Monk62515be2014-05-21 16:06:19 -04003850 if (getStatusBarService() != null) {
3851 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3852 mService.mContext.getPackageName());
3853 }
3854 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003855 if (getDevicePolicyManager() != null) {
3856 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3857 msg.arg1);
3858 }
Jason Monk62515be2014-05-21 16:06:19 -04003859 if (mLockTaskNotify == null) {
3860 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3861 }
3862 mLockTaskNotify.show(false);
3863 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003864 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003865 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003866 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003867 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003868 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003869 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003870 new LockPatternUtils(mService.mContext)
3871 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003872 }
3873 } catch (SettingNotFoundException e) {
3874 // No setting, don't lock.
3875 }
justinzhang5286d3f2014-05-12 17:06:01 -04003876 } catch (RemoteException ex) {
3877 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003878 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003879 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003880 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003881 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003882 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3883 final ActivityContainer container = (ActivityContainer) msg.obj;
3884 final IActivityContainerCallback callback = container.mCallback;
3885 if (callback != null) {
3886 try {
3887 callback.onAllActivitiesComplete(container.asBinder());
3888 } catch (RemoteException e) {
3889 }
3890 }
3891 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003892 case LAUNCH_TASK_BEHIND_COMPLETE: {
3893 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003894 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003895 if (r != null) {
3896 handleLaunchTaskBehindCompleteLocked(r);
3897 }
3898 }
3899 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003900 }
3901 }
3902 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003903
Ying Wangb081a592014-04-22 15:20:16 -07003904 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003905 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003906 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003907 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003908 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003909 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003910 ActivityRecord mParentActivity = null;
3911 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003912
Craig Mautnere3a00d72014-04-16 08:31:19 -07003913 boolean mVisible = true;
3914
Craig Mautner4a1cb222013-12-04 16:14:06 -08003915 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003916 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003917
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003918 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3919 final static int CONTAINER_STATE_NO_SURFACE = 1;
3920 final static int CONTAINER_STATE_FINISHING = 2;
3921 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3922
3923 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003924 synchronized (mService) {
3925 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003926 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003927 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003928 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003929 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003930 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003931
Craig Mautnere0a38842013-12-16 16:14:02 -08003932 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003933 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08003934 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003935 mActivityDisplay = activityDisplay;
3936 mStack.mDisplayId = activityDisplay.mDisplayId;
3937 mStack.mStacks = activityDisplay.mStacks;
3938
3939 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003940 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003941 }
3942
3943 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003944 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003945 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003946 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3947 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003948 return;
3949 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003950 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003951 }
3952 }
3953
3954 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003955 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003956 synchronized (mService) {
3957 if (mActivityDisplay != null) {
3958 return mActivityDisplay.mDisplayId;
3959 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003960 }
3961 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003962 }
3963
Jeff Brownca9bc702014-02-11 14:32:56 -08003964 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003965 public int getStackId() {
3966 synchronized (mService) {
3967 return mStackId;
3968 }
3969 }
3970
3971 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003972 public boolean injectEvent(InputEvent event) {
3973 final long origId = Binder.clearCallingIdentity();
3974 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003975 synchronized (mService) {
3976 if (mActivityDisplay != null) {
3977 return mInputManagerInternal.injectInputEvent(event,
3978 mActivityDisplay.mDisplayId,
3979 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3980 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003981 }
3982 return false;
3983 } finally {
3984 Binder.restoreCallingIdentity(origId);
3985 }
3986 }
3987
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003988 @Override
3989 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003990 synchronized (mService) {
3991 if (mContainerState == CONTAINER_STATE_FINISHING) {
3992 return;
3993 }
3994 mContainerState = CONTAINER_STATE_FINISHING;
3995
Craig Mautnerd163e752014-06-13 17:18:47 -07003996 long origId = Binder.clearCallingIdentity();
3997 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003998 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003999 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004000 } finally {
4001 Binder.restoreCallingIdentity(origId);
4002 }
4003 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004004 }
4005
Craig Mautner60257702014-09-17 15:02:33 -07004006 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004007 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004008 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004009 if (mActivityDisplay != null) {
4010 mActivityDisplay.detachActivitiesLocked(mStack);
4011 mActivityDisplay = null;
4012 mStack.mDisplayId = -1;
4013 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004014 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004015 }
4016 }
4017
4018 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004019 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004020 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004021 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004022 Binder.getCallingUid(), mCurrentUser, false,
4023 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004024
Craig Mautnere0a38842013-12-16 16:14:02 -08004025 // TODO: Switch to user app stacks here.
4026 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004027 final Uri data = intent.getData();
4028 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4029 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004030 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004031 checkEmbeddedAllowedInner(userId, intent, mimeType);
4032
4033 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4034 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4035 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004036 }
4037
4038 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004039 public final int startActivityIntentSender(IIntentSender intentSender)
4040 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004041 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4042
4043 if (!(intentSender instanceof PendingIntentRecord)) {
4044 throw new IllegalArgumentException("Bad PendingIntent object");
4045 }
4046
Craig Mautnerb9168362015-02-26 20:40:19 -08004047 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004048 Binder.getCallingUid(), mCurrentUser, false,
4049 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004050
4051 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4052 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4053 pendingIntent.key.requestResolvedType);
4054
4055 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4056 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4057 }
4058
4059 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004060 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004061 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004062 throw new SecurityException(
4063 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4064 }
4065 }
4066
Craig Mautnerdf88d732014-01-27 09:21:32 -08004067 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004068 public IBinder asBinder() {
4069 return this;
4070 }
4071
Craig Mautner4504de52013-12-20 09:06:56 -08004072 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004073 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004074 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004075 }
4076
Craig Mautner4a1cb222013-12-04 16:14:06 -08004077 ActivityStackSupervisor getOuter() {
4078 return ActivityStackSupervisor.this;
4079 }
4080
Craig Mautnerd163e752014-06-13 17:18:47 -07004081 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004082 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004083 }
4084
4085 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004086 synchronized (mService) {
4087 if (mActivityDisplay != null) {
4088 mActivityDisplay.getBounds(outBounds);
4089 } else {
4090 outBounds.set(0, 0);
4091 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004092 }
4093 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004094
Craig Mautner6985bad2014-04-21 15:22:06 -07004095 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004096 void setVisible(boolean visible) {
4097 if (mVisible != visible) {
4098 mVisible = visible;
4099 if (mCallback != null) {
4100 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4101 0 /* unused */, this).sendToTarget();
4102 }
4103 }
4104 }
4105
Craig Mautner6985bad2014-04-21 15:22:06 -07004106 void setDrawn() {
4107 }
4108
Craig Mautner1b4bf852014-05-26 15:06:32 -07004109 // You can always start a new task on a regular ActivityStack.
4110 boolean isEligibleForNewTasks() {
4111 return true;
4112 }
4113
Craig Mautnerd163e752014-06-13 17:18:47 -07004114 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004115 detachLocked();
4116 deleteActivityContainer(this);
4117 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004118 }
4119
Craig Mautner34b73df2014-01-12 21:11:08 -08004120 @Override
4121 public String toString() {
4122 return mIdString + (mActivityDisplay == null ? "N" : "A");
4123 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004124 }
4125
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004126 private class VirtualActivityContainer extends ActivityContainer {
4127 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004128 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004129
4130 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4131 super(getNextStackId());
4132 mParentActivity = parent;
4133 mCallback = callback;
4134 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004135 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004136 }
4137
4138 @Override
4139 public void setSurface(Surface surface, int width, int height, int density) {
4140 super.setSurface(surface, width, height, density);
4141
4142 synchronized (mService) {
4143 final long origId = Binder.clearCallingIdentity();
4144 try {
4145 setSurfaceLocked(surface, width, height, density);
4146 } finally {
4147 Binder.restoreCallingIdentity(origId);
4148 }
4149 }
4150 }
4151
4152 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4153 if (mContainerState == CONTAINER_STATE_FINISHING) {
4154 return;
4155 }
4156 VirtualActivityDisplay virtualActivityDisplay =
4157 (VirtualActivityDisplay) mActivityDisplay;
4158 if (virtualActivityDisplay == null) {
4159 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004160 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004161 mActivityDisplay = virtualActivityDisplay;
4162 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4163 attachToDisplayLocked(virtualActivityDisplay);
4164 }
4165
4166 if (mSurface != null) {
4167 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004168 }
4169
Craig Mautner6985bad2014-04-21 15:22:06 -07004170 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004171 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004172 mStack.resumeTopActivityLocked(null);
4173 } else {
4174 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004175 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004176 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004177 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004178 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004179 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004180
Craig Mautnerd163e752014-06-13 17:18:47 -07004181 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004182
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004183 if (DEBUG_STACK) Slog.d(TAG_STACK,
4184 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004185 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004186
Craig Mautner6985bad2014-04-21 15:22:06 -07004187 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004188 boolean isAttachedLocked() {
4189 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004190 }
4191
4192 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004193 void setDrawn() {
4194 synchronized (mService) {
4195 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004196 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004197 }
4198 }
4199
Craig Mautner1b4bf852014-05-26 15:06:32 -07004200 // Never start a new task on an ActivityView if it isn't explicitly specified.
4201 @Override
4202 boolean isEligibleForNewTasks() {
4203 return false;
4204 }
4205
Craig Mautnerd163e752014-06-13 17:18:47 -07004206 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004207 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004208 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4209 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4210 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4211 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4212 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004213 }
4214 }
4215
Craig Mautner4a1cb222013-12-04 16:14:06 -08004216 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4217 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004218 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004219 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004220 int mDisplayId;
4221 Display mDisplay;
4222 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004223
Craig Mautner4a1cb222013-12-04 16:14:06 -08004224 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4225 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004226 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004227
Jose Lima4b6c6692014-08-12 17:41:12 -07004228 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004229
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004230 ActivityDisplay() {
4231 }
Craig Mautner4504de52013-12-20 09:06:56 -08004232
Craig Mautner1a70a162014-09-13 12:09:31 -07004233 // After instantiation, check that mDisplay is not null before using this. The alternative
4234 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004235 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004236 final Display display = mDisplayManager.getDisplay(displayId);
4237 if (display == null) {
4238 return;
4239 }
4240 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004241 }
4242
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004243 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004244 mDisplay = display;
4245 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004246 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004247 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004248
4249 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004250 if (DEBUG_STACK) Slog.v(TAG_STACK,
4251 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004252 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004253 }
4254
Craig Mautnere0a38842013-12-16 16:14:02 -08004255 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004256 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004257 + " from displayId=" + mDisplayId);
4258 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004259 }
4260
4261 void getBounds(Point bounds) {
4262 mDisplay.getDisplayInfo(mDisplayInfo);
4263 bounds.x = mDisplayInfo.appWidth;
4264 bounds.y = mDisplayInfo.appHeight;
4265 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004266
Jose Lima4b6c6692014-08-12 17:41:12 -07004267 void setVisibleBehindActivity(ActivityRecord r) {
4268 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004269 }
4270
Jose Lima4b6c6692014-08-12 17:41:12 -07004271 boolean hasVisibleBehindActivity() {
4272 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004273 }
4274
Craig Mautner34b73df2014-01-12 21:11:08 -08004275 @Override
4276 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004277 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4278 }
4279 }
4280
4281 class VirtualActivityDisplay extends ActivityDisplay {
4282 VirtualDisplay mVirtualDisplay;
4283
Craig Mautner6985bad2014-04-21 15:22:06 -07004284 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004285 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004286 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4287 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4288 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4289 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004290
4291 init(mVirtualDisplay.getDisplay());
4292
4293 mWindowManager.handleDisplayAdded(mDisplayId);
4294 }
4295
4296 void setSurface(Surface surface) {
4297 if (mVirtualDisplay != null) {
4298 mVirtualDisplay.setSurface(surface);
4299 }
4300 }
4301
4302 @Override
4303 void detachActivitiesLocked(ActivityStack stack) {
4304 super.detachActivitiesLocked(stack);
4305 if (mVirtualDisplay != null) {
4306 mVirtualDisplay.release();
4307 mVirtualDisplay = null;
4308 }
4309 }
4310
4311 @Override
4312 public String toString() {
4313 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004314 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004315 }
Jose Lima58e66d62014-05-27 20:00:27 -07004316
4317 private boolean isLeanbackOnlyDevice() {
4318 boolean onLeanbackOnly = false;
4319 try {
4320 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4321 PackageManager.FEATURE_LEANBACK_ONLY);
4322 } catch (RemoteException e) {
4323 // noop
4324 }
4325
4326 return onLeanbackOnly;
4327 }
Craig Mautner27084302013-03-25 08:05:25 -07004328}