blob: 4bab4fd3800476b533aa0ce77009b2275920485a [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;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070020import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070022import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
23import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070024import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080025import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Wale Ogunwale7de05352014-12-12 15:21:33 -080027import static com.android.server.am.ActivityManagerService.DEBUG_RECENTS;
Craig Mautner6170f732013-04-02 13:05:23 -070028import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070029import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070030import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070031import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
32import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070033import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070034import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
35import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070037
Craig Mautner2420ead2013-04-01 17:13:20 -070038import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080040import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityOptions;
42import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import android.app.IActivityContainer;
44import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070048import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070049import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070051import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040052import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040053import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070055import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070056import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070057import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.pm.ApplicationInfo;
61import android.content.pm.PackageManager;
62import android.content.pm.ResolveInfo;
63import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080064import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080065import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080066import android.hardware.display.DisplayManager;
67import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080068import android.hardware.display.DisplayManagerGlobal;
69import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080070import android.hardware.input.InputManager;
71import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080072import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070073import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070074import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070075import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070076import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070078import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070079import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070081import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070082import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070083import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040084import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070085import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070086import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070087import android.os.WorkSource;
Jason Monk62515be2014-05-21 16:06:19 -040088import android.provider.Settings;
89import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070090import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070091import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070092import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070093import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080094import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070095
Craig Mautner4a1cb222013-12-04 16:14:06 -080096import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080097import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080098import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080099import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800100import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700101import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700102import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800103import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700104import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400105import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400106import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800107import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700108import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700109import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110
justinzhang5286d3f2014-05-12 17:06:01 -0400111
Craig Mautner8d341ef2013-03-26 09:03:27 -0700112import java.io.FileDescriptor;
113import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700114import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700115import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700116import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700117
Craig Mautner4a1cb222013-12-04 16:14:06 -0800118public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800119 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700120 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
121 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800122
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800123 static final boolean DEBUG = DEBUG_ALL || false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700124 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
125 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700126 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700127 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700128 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700129 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
130 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner30bd3bb2014-11-13 00:55:53 +0000131 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700132 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700133
Craig Mautner2219a1b2013-03-25 09:44:30 -0700134 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700135
Craig Mautnerf3333272013-04-22 10:55:53 -0700136 /** How long we wait until giving up on the last activity telling us it is idle. */
137 static final int IDLE_TIMEOUT = 10*1000;
138
Craig Mautner0eea92c2013-05-16 13:35:39 -0700139 /** How long we can hold the sleep wake lock before giving up. */
140 static final int SLEEP_TIMEOUT = 5*1000;
141
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700142 // How long we can hold the launch wake lock before giving up.
143 static final int LAUNCH_TIMEOUT = 10*1000;
144
Craig Mautner05d29032013-05-03 13:40:13 -0700145 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
146 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
147 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700148 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700149 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800150 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
151 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
152 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700153 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400154 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
155 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700156 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700157 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700158 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800159
Craig Mautner4504de52013-12-20 09:06:56 -0800160 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700161
Jason Monk62515be2014-05-21 16:06:19 -0400162 private static final String LOCK_TASK_TAG = "Lock-to-App";
163
justinzhang5286d3f2014-05-12 17:06:01 -0400164 /** Status Bar Service **/
165 private IBinder mToken = new Binder();
166 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400167 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400168
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700169 // For debugging to make sure the caller when acquiring/releasing our
170 // wake lock is the system process.
171 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700172
Craig Mautner27084302013-03-25 08:05:25 -0700173 final ActivityManagerService mService;
174
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800175 private final RecentTasks mRecentTasks;
176
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700177 final ActivityStackSupervisorHandler mHandler;
178
179 /** Short cut */
180 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800181 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700182
Craig Mautner8d341ef2013-03-26 09:03:27 -0700183 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700184 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700185
186 /** Task identifier that activities are currently being started in. Incremented each time a
187 * new task is created. */
188 private int mCurTaskId = 0;
189
Craig Mautner2420ead2013-04-01 17:13:20 -0700190 /** The current user */
191 private int mCurrentUser;
192
Craig Mautnere0a38842013-12-16 16:14:02 -0800193 /** The stack containing the launcher app. Assumed to always be attached to
194 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700195 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700196
Craig Mautnere0a38842013-12-16 16:14:02 -0800197 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700198 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700199
Craig Mautner4a1cb222013-12-04 16:14:06 -0800200 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
201 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800202 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800203 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700204
205 /** List of activities that are waiting for a new activity to become visible before completing
206 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800207 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700208
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700209 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800210 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700211
212 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800213 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700214
Craig Mautnerde4ef022013-04-07 19:01:33 -0700215 /** List of activities that are ready to be stopped, but waiting for the next activity to
216 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800217 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700218
Craig Mautnerf3333272013-04-22 10:55:53 -0700219 /** List of activities that are ready to be finished, but waiting for the previous activity to
220 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800221 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700222
Craig Mautner0eea92c2013-05-16 13:35:39 -0700223 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800224 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700225
Craig Mautnerf3333272013-04-22 10:55:53 -0700226 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800227 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700228
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700229 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800230 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700231
Craig Mautnerde4ef022013-04-07 19:01:33 -0700232 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
233 * is being brought in front of us. */
234 boolean mUserLeaving = false;
235
Craig Mautner0eea92c2013-05-16 13:35:39 -0700236 /** Set when we have taken too long waiting to go to sleep. */
237 boolean mSleepTimeout = false;
238
Jose Lima58e66d62014-05-27 20:00:27 -0700239 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
240 * setWindowManager is called. **/
241 private boolean mLeanbackOnlyDevice;
242
Craig Mautner0eea92c2013-05-16 13:35:39 -0700243 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700244 * We don't want to allow the device to go to sleep while in the process
245 * of launching an activity. This is primarily to allow alarm intent
246 * receivers to launch an activity and get that to run before the device
247 * goes back to sleep.
248 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700249 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700250
251 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700252 * Set when the system is going to sleep, until we have
253 * successfully paused the current activity and released our wake lock.
254 * At that point the system is allowed to actually sleep.
255 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700256 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700257
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700258 /** Stack id of the front stack when user switched, indexed by userId. */
259 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700260
Craig Mautner4504de52013-12-20 09:06:56 -0800261 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800262 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700263 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800264
265 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700266 private final SparseArray<ActivityDisplay> mActivityDisplays =
267 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800268
Jeff Brownca9bc702014-02-11 14:32:56 -0800269 InputManagerInternal mInputManagerInternal;
270
Craig Mautneraea74a52014-03-08 14:23:10 -0800271 /** If non-null then the task specified remains in front and no other tasks may be started
272 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400273 TaskRecord mLockTaskModeTask;
Benjamin Franz43261142015-02-11 15:59:44 +0000274 /** Store the current lock task mode. Possible values:
275 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActicityManager#LOCK_TASK_MODE_LOCKED},
276 * {@link ActicityManager#LOCK_TASK_MODE_PINNED}
277 */
278 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400279 /**
280 * Notifies the user when entering/exiting lock-task.
281 */
282 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800283
Craig Mautneree36c772014-07-16 14:56:05 -0700284 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
285 = new ArrayList<PendingActivityLaunch>();
286
Craig Mautner42d04db2014-11-06 12:13:23 -0800287 /** Used to keep resumeTopActivityLocked() from being entered recursively */
288 boolean inResumeTopActivity;
289
Craig Mautneree36c772014-07-16 14:56:05 -0700290 /**
291 * Description of a request to start a new activity, which has been held
292 * due to app switches being disabled.
293 */
294 static class PendingActivityLaunch {
295 final ActivityRecord r;
296 final ActivityRecord sourceRecord;
297 final int startFlags;
298 final ActivityStack stack;
299
300 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
301 int _startFlags, ActivityStack _stack) {
302 r = _r;
303 sourceRecord = _sourceRecord;
304 startFlags = _startFlags;
305 stack = _stack;
306 }
307 }
308
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800309 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700310 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800311 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700312 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
313 }
314
315 /**
316 * At the time when the constructor runs, the power manager has not yet been
317 * initialized. So we initialize our wakelocks afterwards.
318 */
319 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700321 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700322 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700323 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700324 }
325
justinzhang5286d3f2014-05-12 17:06:01 -0400326 // This function returns a IStatusBarService. The value is from ServiceManager.
327 // getService and is cached.
328 private IStatusBarService getStatusBarService() {
329 synchronized (mService) {
330 if (mStatusBarService == null) {
331 mStatusBarService = IStatusBarService.Stub.asInterface(
332 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
333 if (mStatusBarService == null) {
334 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
335 }
336 }
337 return mStatusBarService;
338 }
339 }
340
Jason Monk35c62a42014-06-17 10:24:47 -0400341 private IDevicePolicyManager getDevicePolicyManager() {
342 synchronized (mService) {
343 if (mDevicePolicyManager == null) {
344 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
345 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
346 if (mDevicePolicyManager == null) {
347 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
348 }
349 }
350 return mDevicePolicyManager;
351 }
352 }
353
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700354 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800355 synchronized (mService) {
356 mWindowManager = wm;
357
358 mDisplayManager =
359 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
360 mDisplayManager.registerDisplayListener(this, null);
361
362 Display[] displays = mDisplayManager.getDisplays();
363 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
364 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800365 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700366 if (activityDisplay.mDisplay == null) {
367 throw new IllegalStateException("Default Display does not exist");
368 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800369 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800370 }
371
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700372 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800373 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800374
375 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700376
377 // Initialize this here, now that we can get a valid reference to PackageManager.
378 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 }
Craig Mautner27084302013-03-25 08:05:25 -0700380 }
381
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200382 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700383 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200384 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700385 }
386
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700387 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800388 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700389 }
390
Craig Mautnerde4ef022013-04-07 19:01:33 -0700391 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800392 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700393 }
394
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800395 /** Top of all visible stacks is/should always be equal to the focused stack.
396 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
397 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700398 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700399 if (stack == null) {
400 return false;
401 }
402
Craig Mautnerdf88d732014-01-27 09:21:32 -0800403 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
404 if (parent != null) {
405 stack = parent.task.stack;
406 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800407 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700408 }
409
Craig Mautner299f9602015-01-26 09:47:33 -0800410 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800411 moveHomeStack(toFront, reason, null);
412 }
413
414 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800415 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800416 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800417 if (topNdx <= 0) {
418 return;
419 }
420 ActivityStack topStack = stacks.get(topNdx);
421 final boolean homeInFront = topStack == mHomeStack;
422 if (homeInFront != toFront) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800423 stacks.remove(mHomeStack);
424 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800425 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
426 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700427 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800428
429 if (lastFocusedStack != null) {
430 mLastFocusedStack = lastFocusedStack;
431 }
432 mFocusedStack = stacks.get(topNdx);
433
Craig Mautnerde313752015-01-22 14:28:03 -0800434 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
435 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800436 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800437
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800438 if (mService.mBooting || !mService.mBooted) {
439 final ActivityRecord r = topRunningActivityLocked();
440 if (r != null && r.idle) {
441 checkFinishBootingLocked();
442 }
443 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700444 }
445
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700446 /** Returns true if the focus activity was adjusted to the home stack top activity. */
447 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700448 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
449 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700450 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700451 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700452
Craig Mautner84984fa2014-06-19 11:19:20 -0700453 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700454
455 final ActivityRecord top = mHomeStack.topRunningActivityLocked(null);
456 if (top == null) {
457 return false;
458 }
459 mService.setFocusedActivityLocked(top, reason);
460 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700461 }
462
Craig Mautner299f9602015-01-26 09:47:33 -0800463 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700464 if (!mService.mBooting && !mService.mBooted) {
465 // Not ready yet!
466 return false;
467 }
468
Craig Mautner84984fa2014-06-19 11:19:20 -0700469 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
470 mWindowManager.showRecentApps();
471 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700472 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700473
Craig Mautner84984fa2014-06-19 11:19:20 -0700474 if (prev != null) {
475 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
476 }
477
Craig Mautnera8a90e02013-06-28 15:24:50 -0700478 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700479 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800480 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700481 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700482 }
Craig Mautner299f9602015-01-26 09:47:33 -0800483 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700484 }
485
Craig Mautner8d341ef2013-03-26 09:03:27 -0700486 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700487 return anyTaskForIdLocked(id, true);
488 }
489
490 /**
491 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
492 * @param id Id of the task we would like returned.
493 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
494 * restore the task from recents to the active list.
495 */
496 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800497 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800498 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800499 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800500 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
501 ActivityStack stack = stacks.get(stackNdx);
502 TaskRecord task = stack.taskForIdLocked(id);
503 if (task != null) {
504 return task;
505 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700506 }
507 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800508
509 // Don't give up! Look in recents.
510 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800511 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800512 if (task == null) {
513 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
514 return null;
515 }
516
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700517 if (!restoreFromRecents) {
518 return task;
519 }
520
Wale Ogunwale7de05352014-12-12 15:21:33 -0800521 if (!restoreRecentTaskLocked(task)) {
522 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
523 return null;
524 }
525 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
526 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700527 }
528
Craig Mautner6170f732013-04-02 13:05:23 -0700529 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800530 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800531 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800532 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800533 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
534 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
535 if (r != null) {
536 return r;
537 }
Craig Mautner6170f732013-04-02 13:05:23 -0700538 }
539 }
540 return null;
541 }
542
Craig Mautneref73ee12014-04-23 11:45:37 -0700543 void setNextTaskId(int taskId) {
544 if (taskId > mCurTaskId) {
545 mCurTaskId = taskId;
546 }
547 }
548
Craig Mautner8d341ef2013-03-26 09:03:27 -0700549 int getNextTaskId() {
550 do {
551 mCurTaskId++;
552 if (mCurTaskId <= 0) {
553 mCurTaskId = 1;
554 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700555 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700556 return mCurTaskId;
557 }
558
Craig Mautnerde4ef022013-04-07 19:01:33 -0700559 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800560 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700561 if (stack == null) {
562 return null;
563 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700564 ActivityRecord resumedActivity = stack.mResumedActivity;
565 if (resumedActivity == null || resumedActivity.app == null) {
566 resumedActivity = stack.mPausingActivity;
567 if (resumedActivity == null || resumedActivity.app == null) {
568 resumedActivity = stack.topRunningActivityLocked(null);
569 }
570 }
571 return resumedActivity;
572 }
573
Dianne Hackbornff072722014-09-24 10:56:28 -0700574 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700575 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800576 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800577 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
578 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800579 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
580 final ActivityStack stack = stacks.get(stackNdx);
581 if (!isFrontStack(stack)) {
582 continue;
583 }
584 ActivityRecord hr = stack.topRunningActivityLocked(null);
585 if (hr != null) {
586 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
587 && processName.equals(hr.processName)) {
588 try {
George Mount2c92c972014-03-20 09:38:23 -0700589 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590 didSomething = true;
591 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700592 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800593 Slog.w(TAG, "Exception in new application when starting activity "
594 + hr.intent.getComponent().flattenToShortString(), e);
595 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700596 }
Craig Mautner20e72272013-04-01 13:45:53 -0700597 }
Craig Mautner20e72272013-04-01 13:45:53 -0700598 }
599 }
600 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700601 if (!didSomething) {
602 ensureActivitiesVisibleLocked(null, 0);
603 }
Craig Mautner20e72272013-04-01 13:45:53 -0700604 return didSomething;
605 }
606
607 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800608 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
609 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
611 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800612 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613 continue;
614 }
615 final ActivityRecord resumedActivity = stack.mResumedActivity;
616 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800617 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
618 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800619 return false;
620 }
Craig Mautner20e72272013-04-01 13:45:53 -0700621 }
622 }
623 return true;
624 }
625
Craig Mautnerde4ef022013-04-07 19:01:33 -0700626 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800627 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
628 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
630 final ActivityStack stack = stacks.get(stackNdx);
631 if (isFrontStack(stack)) {
632 final ActivityRecord r = stack.mResumedActivity;
633 if (r != null && r.state != ActivityState.RESUMED) {
634 return false;
635 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700636 }
637 }
638 }
639 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800640 if (DEBUG_STACK) Slog.d(TAG,
641 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
642 mLastFocusedStack + " to=" + mFocusedStack);
643 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700644 return true;
645 }
646
647 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800648 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
649 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
651 final ActivityStack stack = stacks.get(stackNdx);
652 final ActivityRecord r = stack.mResumedActivity;
Craig Mautner8c14c152015-01-15 17:32:07 -0800653 if (r != null && (!r.nowVisible || mWaitingVisibleActivities.contains(r))) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800654 return false;
655 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700656 }
657 }
658 return true;
659 }
660
Craig Mautner2acc3892013-09-23 10:28:14 -0700661 /**
662 * Pause all activities in either all of the stacks or just the back stacks.
663 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700664 * @return true if any activity was paused as a result of this call.
665 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700666 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700667 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800668 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
669 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800670 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
671 final ActivityStack stack = stacks.get(stackNdx);
672 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
673 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
674 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700675 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
676 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700678 }
679 }
680 return someActivityPaused;
681 }
682
Craig Mautnerde4ef022013-04-07 19:01:33 -0700683 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700684 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800685 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
686 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
688 final ActivityStack stack = stacks.get(stackNdx);
689 final ActivityRecord r = stack.mPausingActivity;
690 if (r != null && r.state != ActivityState.PAUSED
691 && r.state != ActivityState.STOPPED
692 && r.state != ActivityState.STOPPING) {
693 if (DEBUG_STATES) {
694 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
695 pausing = false;
696 } else {
697 return false;
698 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700699 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700700 }
701 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700702 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700703 }
704
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700705 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
706 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500707 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800708 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
709 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
710 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
711 final ActivityStack stack = stacks.get(stackNdx);
712 if (stack.mResumedActivity != null &&
713 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700714 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800715 }
716 }
717 }
718 }
719
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700720 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700721 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700722 }
723
724 void sendWaitingVisibleReportLocked(ActivityRecord r) {
725 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700726 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700727 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700728 if (w.who == null) {
729 changed = true;
730 w.timeout = false;
731 if (r != null) {
732 w.who = new ComponentName(r.info.packageName, r.info.name);
733 }
734 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
735 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700736 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700737 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700738 if (changed) {
739 mService.notifyAll();
740 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700741 }
742
743 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
744 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700745 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700746 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700747 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700748 if (w.who == null) {
749 changed = true;
750 w.timeout = timeout;
751 if (r != null) {
752 w.who = new ComponentName(r.info.packageName, r.info.name);
753 }
754 w.thisTime = thisTime;
755 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700756 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700757 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700758 if (changed) {
759 mService.notifyAll();
760 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700761 }
762
Craig Mautner29219d92013-04-16 20:19:12 -0700763 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800764 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700765 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
766 if (r != null) {
767 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700768 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700769
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800771 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800772 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
773 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700774 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700775 r = stack.topRunningActivityLocked(null);
776 if (r != null) {
777 return r;
778 }
779 }
780 }
781 return null;
782 }
783
Dianne Hackborn09233282014-04-30 11:33:59 -0700784 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700785 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800786 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
787 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800788 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800789 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800790 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800791 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
792 final ActivityStack stack = stacks.get(stackNdx);
793 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
794 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700795 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700796 }
797 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700798
799 // The lists are already sorted from most recent to oldest. Just pull the most recent off
800 // each list and add it to list. Stop when all lists are empty or maxNum reached.
801 while (maxNum > 0) {
802 long mostRecentActiveTime = Long.MIN_VALUE;
803 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800804 final int numTaskLists = runningTaskLists.size();
805 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
806 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700807 if (!stackTaskList.isEmpty()) {
808 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
809 if (lastActiveTime > mostRecentActiveTime) {
810 mostRecentActiveTime = lastActiveTime;
811 selectedStackList = stackTaskList;
812 }
813 }
814 }
815 if (selectedStackList != null) {
816 list.add(selectedStackList.remove(0));
817 --maxNum;
818 } else {
819 break;
820 }
821 }
Craig Mautner20e72272013-04-01 13:45:53 -0700822 }
823
Craig Mautner23ac33b2013-04-01 16:26:35 -0700824 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700825 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700826 // Collect information about the target of the Intent.
827 ActivityInfo aInfo;
828 try {
829 ResolveInfo rInfo =
830 AppGlobals.getPackageManager().resolveIntent(
831 intent, resolvedType,
832 PackageManager.MATCH_DEFAULT_ONLY
833 | ActivityManagerService.STOCK_PM_FLAGS, userId);
834 aInfo = rInfo != null ? rInfo.activityInfo : null;
835 } catch (RemoteException e) {
836 aInfo = null;
837 }
838
839 if (aInfo != null) {
840 // Store the found target back into the intent, because now that
841 // we have it we never want to do this again. For example, if the
842 // user navigates back to this point in the history, we should
843 // always restart the exact same activity.
844 intent.setComponent(new ComponentName(
845 aInfo.applicationInfo.packageName, aInfo.name));
846
847 // Don't debug things in the system process
848 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
849 if (!aInfo.processName.equals("system")) {
850 mService.setDebugApp(aInfo.processName, true, false);
851 }
852 }
853
854 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
855 if (!aInfo.processName.equals("system")) {
856 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
857 }
858 }
859
Jeff Hao1b012d32014-08-20 10:35:34 -0700860 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700861 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700862 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700863 }
864 }
865 }
866 return aInfo;
867 }
868
Craig Mautner299f9602015-01-26 09:47:33 -0800869 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
870 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700871 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
872 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700873 }
874
Craig Mautner23ac33b2013-04-01 16:26:35 -0700875 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700876 String callingPackage, Intent intent, String resolvedType,
877 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700878 IBinder resultTo, String resultWho, int requestCode, int startFlags,
879 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700880 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700881 // Refuse possible leaked file descriptors
882 if (intent != null && intent.hasFileDescriptors()) {
883 throw new IllegalArgumentException("File descriptors passed in Intent");
884 }
885 boolean componentSpecified = intent.getComponent() != null;
886
887 // Don't modify the client's object!
888 intent = new Intent(intent);
889
890 // Collect information about the target of the Intent.
891 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700892 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700893
Craig Mautnere0a38842013-12-16 16:14:02 -0800894 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700895 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800896 if (container != null && container.mParentActivity != null &&
897 container.mParentActivity.state != ActivityState.RESUMED) {
898 // Cannot start a child activity if the parent is not resumed.
899 return ActivityManager.START_CANCELED;
900 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700901 final int realCallingPid = Binder.getCallingPid();
902 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700903 int callingPid;
904 if (callingUid >= 0) {
905 callingPid = -1;
906 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700907 callingPid = realCallingPid;
908 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700909 } else {
910 callingPid = callingUid = -1;
911 }
912
Craig Mautnere0a38842013-12-16 16:14:02 -0800913 final ActivityStack stack;
914 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800915 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800916 } else {
917 stack = container.mStack;
918 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800919 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700920 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700921 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922
923 final long origId = Binder.clearCallingIdentity();
924
925 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800926 (aInfo.applicationInfo.privateFlags
927 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700928 // This may be a heavy-weight process! Check to see if we already
929 // have another, different heavy-weight process running.
930 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
931 if (mService.mHeavyWeightProcess != null &&
932 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
933 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700934 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700935 if (caller != null) {
936 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
937 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700938 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700939 } else {
940 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700941 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700942 + intent.toString());
943 ActivityOptions.abort(options);
944 return ActivityManager.START_PERMISSION_DENIED;
945 }
946 }
947
948 IIntentSender target = mService.getIntentSenderLocked(
949 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700950 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700951 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
952 | PendingIntent.FLAG_ONE_SHOT, null);
953
954 Intent newIntent = new Intent();
955 if (requestCode >= 0) {
956 // Caller is requesting a result.
957 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
958 }
959 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
960 new IntentSender(target));
961 if (mService.mHeavyWeightProcess.activities.size() > 0) {
962 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
963 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
964 hist.packageName);
965 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
966 hist.task.taskId);
967 }
968 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
969 aInfo.packageName);
970 newIntent.setFlags(intent.getFlags());
971 newIntent.setClassName("android",
972 HeavyWeightSwitcherActivity.class.getName());
973 intent = newIntent;
974 resolvedType = null;
975 caller = null;
976 callingUid = Binder.getCallingUid();
977 callingPid = Binder.getCallingPid();
978 componentSpecified = true;
979 try {
980 ResolveInfo rInfo =
981 AppGlobals.getPackageManager().resolveIntent(
982 intent, null,
983 PackageManager.MATCH_DEFAULT_ONLY
984 | ActivityManagerService.STOCK_PM_FLAGS, userId);
985 aInfo = rInfo != null ? rInfo.activityInfo : null;
986 aInfo = mService.getActivityInfoForUser(aInfo, userId);
987 } catch (RemoteException e) {
988 aInfo = null;
989 }
990 }
991 }
992 }
993
Dianne Hackborn91097de2014-04-04 18:02:06 -0700994 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
995 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700996 requestCode, callingPid, callingUid, callingPackage,
997 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700998 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700999
Craig Mautner85c11a82014-07-17 12:38:18 -07001000 Binder.restoreCallingIdentity(origId);
1001
Craig Mautnerde4ef022013-04-07 19:01:33 -07001002 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001003 // If the caller also wants to switch to a new configuration,
1004 // do so now. This allows a clean switch, as we are waiting
1005 // for the current activity to pause (so we will not destroy
1006 // it), and have not yet started the next activity.
1007 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1008 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001009 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001010 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001011 "Updating to new configuration after starting activity.");
1012 mService.updateConfigurationLocked(config, null, false, false);
1013 }
1014
Craig Mautner23ac33b2013-04-01 16:26:35 -07001015 if (outResult != null) {
1016 outResult.result = res;
1017 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001018 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001019 do {
1020 try {
1021 mService.wait();
1022 } catch (InterruptedException e) {
1023 }
1024 } while (!outResult.timeout && outResult.who == null);
1025 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001026 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001027 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001028 outResult.timeout = false;
1029 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1030 outResult.totalTime = 0;
1031 outResult.thisTime = 0;
1032 } else {
1033 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001034 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001035 do {
1036 try {
1037 mService.wait();
1038 } catch (InterruptedException e) {
1039 }
1040 } while (!outResult.timeout && outResult.who == null);
1041 }
1042 }
1043 }
1044
1045 return res;
1046 }
1047 }
1048
1049 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1050 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1051 Bundle options, int userId) {
1052 if (intents == null) {
1053 throw new NullPointerException("intents is null");
1054 }
1055 if (resolvedTypes == null) {
1056 throw new NullPointerException("resolvedTypes is null");
1057 }
1058 if (intents.length != resolvedTypes.length) {
1059 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1060 }
1061
Craig Mautner23ac33b2013-04-01 16:26:35 -07001062
1063 int callingPid;
1064 if (callingUid >= 0) {
1065 callingPid = -1;
1066 } else if (caller == null) {
1067 callingPid = Binder.getCallingPid();
1068 callingUid = Binder.getCallingUid();
1069 } else {
1070 callingPid = callingUid = -1;
1071 }
1072 final long origId = Binder.clearCallingIdentity();
1073 try {
1074 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001075 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001076 for (int i=0; i<intents.length; i++) {
1077 Intent intent = intents[i];
1078 if (intent == null) {
1079 continue;
1080 }
1081
1082 // Refuse possible leaked file descriptors
1083 if (intent != null && intent.hasFileDescriptors()) {
1084 throw new IllegalArgumentException("File descriptors passed in Intent");
1085 }
1086
1087 boolean componentSpecified = intent.getComponent() != null;
1088
1089 // Don't modify the client's object!
1090 intent = new Intent(intent);
1091
1092 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001093 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001094 // TODO: New, check if this is correct
1095 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1096
1097 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001098 (aInfo.applicationInfo.privateFlags
1099 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001100 throw new IllegalArgumentException(
1101 "FLAG_CANT_SAVE_STATE not supported here");
1102 }
1103
1104 Bundle theseOptions;
1105 if (options != null && i == intents.length-1) {
1106 theseOptions = options;
1107 } else {
1108 theseOptions = null;
1109 }
Craig Mautner6170f732013-04-02 13:05:23 -07001110 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001111 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1112 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001113 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001114 if (res < 0) {
1115 return res;
1116 }
1117
1118 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1119 }
1120 }
1121 } finally {
1122 Binder.restoreCallingIdentity(origId);
1123 }
1124
1125 return ActivityManager.START_SUCCESS;
1126 }
1127
Craig Mautner2420ead2013-04-01 17:13:20 -07001128 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001129 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001130 throws RemoteException {
1131
1132 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001133 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001134 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001135
1136 // schedule launch ticks to collect information about slow apps.
1137 r.startLaunchTickingLocked();
1138
1139 // Have the window manager re-evaluate the orientation of
1140 // the screen based on the new activity order. Note that
1141 // as a result of this, it can call back into the activity
1142 // manager with a new orientation. We don't care about that,
1143 // because the activity is not currently running so we are
1144 // just restarting it anyway.
1145 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001146 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001147 mService.mConfiguration,
1148 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1149 mService.updateConfigurationLocked(config, r, false, false);
1150 }
1151
1152 r.app = app;
1153 app.waitingToKill = null;
1154 r.launchCount++;
1155 r.lastLaunchTime = SystemClock.uptimeMillis();
1156
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001157 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001158
1159 int idx = app.activities.indexOf(r);
1160 if (idx < 0) {
1161 app.activities.add(r);
1162 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001163 mService.updateLruProcessLocked(app, true, null);
1164 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001165
1166 final ActivityStack stack = r.task.stack;
1167 try {
1168 if (app.thread == null) {
1169 throw new RemoteException();
1170 }
1171 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001172 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001173 if (andResume) {
1174 results = r.results;
1175 newIntents = r.newIntents;
1176 }
1177 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1178 + " icicle=" + r.icicle
1179 + " with results=" + results + " newIntents=" + newIntents
1180 + " andResume=" + andResume);
1181 if (andResume) {
1182 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1183 r.userId, System.identityHashCode(r),
1184 r.task.taskId, r.shortComponentName);
1185 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001186 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001187 // Home process is the root process of the task.
1188 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001189 }
1190 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1191 r.sleeping = false;
1192 r.forceNewConfig = false;
1193 mService.showAskCompatModeDialogLocked(r);
1194 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1195 String profileFile = null;
1196 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001197 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1198 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1199 mService.mProfileProc = app;
1200 profileFile = mService.mProfileFile;
1201 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 }
1203 }
1204 app.hasShownUi = true;
1205 app.pendingUiClean = true;
1206 if (profileFd != null) {
1207 try {
1208 profileFd = profileFd.dup();
1209 } catch (IOException e) {
1210 if (profileFd != null) {
1211 try {
1212 profileFd.close();
1213 } catch (IOException o) {
1214 }
1215 profileFd = null;
1216 }
1217 }
1218 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001219
Jeff Hao1b012d32014-08-20 10:35:34 -07001220 ProfilerInfo profilerInfo = profileFile != null
1221 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1222 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001223 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001224 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001225 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001226 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1227 r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1228 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001229
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001230 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001231 // This may be a heavy-weight process! Note that the package
1232 // manager will ensure that only activity can run in the main
1233 // process of the .apk, which is the only thing that will be
1234 // considered heavy-weight.
1235 if (app.processName.equals(app.info.packageName)) {
1236 if (mService.mHeavyWeightProcess != null
1237 && mService.mHeavyWeightProcess != app) {
1238 Slog.w(TAG, "Starting new heavy weight process " + app
1239 + " when already running "
1240 + mService.mHeavyWeightProcess);
1241 }
1242 mService.mHeavyWeightProcess = app;
1243 Message msg = mService.mHandler.obtainMessage(
1244 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1245 msg.obj = r;
1246 mService.mHandler.sendMessage(msg);
1247 }
1248 }
1249
1250 } catch (RemoteException e) {
1251 if (r.launchFailed) {
1252 // This is the second time we failed -- finish activity
1253 // and give up.
1254 Slog.e(TAG, "Second failure launching "
1255 + r.intent.getComponent().flattenToShortString()
1256 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001257 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001258 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1259 "2nd-crash", false);
1260 return false;
1261 }
1262
1263 // This is the first time we failed -- restart process and
1264 // retry.
1265 app.activities.remove(r);
1266 throw e;
1267 }
1268
1269 r.launchFailed = false;
1270 if (stack.updateLRUListLocked(r)) {
1271 Slog.w(TAG, "Activity " + r
1272 + " being launched, but already in LRU list");
1273 }
1274
1275 if (andResume) {
1276 // As part of the process of launching, ActivityThread also performs
1277 // a resume.
1278 stack.minimalResumeActivityLocked(r);
1279 } else {
1280 // This activity is not starting in the resumed state... which
1281 // should look like we asked it to pause+stop (but remain visible),
1282 // and it has done so and reported back the current icicle and
1283 // other state.
1284 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1285 + " (starting in stopped state)");
1286 r.state = ActivityState.STOPPED;
1287 r.stopped = true;
1288 }
1289
1290 // Launch the new version setup screen if needed. We do this -after-
1291 // launching the initial activity (that is, home), so that it can have
1292 // a chance to initialize itself while in the background, making the
1293 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001294 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 mService.startSetupActivityLocked();
1296 }
1297
Dianne Hackborn465fa392014-09-14 14:21:18 -07001298 // Update any services we are bound to that might care about whether
1299 // their client may have activities.
1300 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1301
Craig Mautner2420ead2013-04-01 17:13:20 -07001302 return true;
1303 }
1304
Craig Mautnere79d42682013-04-01 19:01:53 -07001305 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001306 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001307 // Is this activity's application already running?
1308 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001309 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001310
1311 r.task.stack.setLaunchTime(r);
1312
1313 if (app != null && app.thread != null) {
1314 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001315 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1316 || !"android".equals(r.info.packageName)) {
1317 // Don't add this if it is a platform component that is marked
1318 // to run in multiple processes, because this is actually
1319 // part of the framework so doesn't make sense to track as a
1320 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001321 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1322 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001323 }
George Mount2c92c972014-03-20 09:38:23 -07001324 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001325 return;
1326 } catch (RemoteException e) {
1327 Slog.w(TAG, "Exception when starting activity "
1328 + r.intent.getComponent().flattenToShortString(), e);
1329 }
1330
1331 // If a dead object exception was thrown -- fall through to
1332 // restart the application.
1333 }
1334
1335 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001336 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001337 }
1338
Craig Mautner6170f732013-04-02 13:05:23 -07001339 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001340 Intent intent, String resolvedType, ActivityInfo aInfo,
1341 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1342 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001343 int callingPid, int callingUid, String callingPackage,
1344 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001345 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1346 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001347 int err = ActivityManager.START_SUCCESS;
1348
1349 ProcessRecord callerApp = null;
1350 if (caller != null) {
1351 callerApp = mService.getRecordForAppLocked(caller);
1352 if (callerApp != null) {
1353 callingPid = callerApp.pid;
1354 callingUid = callerApp.info.uid;
1355 } else {
1356 Slog.w(TAG, "Unable to find app for caller " + caller
1357 + " (pid=" + callingPid + ") when starting: "
1358 + intent.toString());
1359 err = ActivityManager.START_PERMISSION_DENIED;
1360 }
1361 }
1362
1363 if (err == ActivityManager.START_SUCCESS) {
1364 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1365 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001366 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001367 + " on display " + (container == null ? (mFocusedStack == null ?
1368 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1369 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1370 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001371 }
1372
1373 ActivityRecord sourceRecord = null;
1374 ActivityRecord resultRecord = null;
1375 if (resultTo != null) {
1376 sourceRecord = isInAnyStackLocked(resultTo);
1377 if (DEBUG_RESULTS) Slog.v(
1378 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1379 if (sourceRecord != null) {
1380 if (requestCode >= 0 && !sourceRecord.finishing) {
1381 resultRecord = sourceRecord;
1382 }
1383 }
1384 }
Craig Mautner6170f732013-04-02 13:05:23 -07001385
Dianne Hackborn91097de2014-04-04 18:02:06 -07001386 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001387
Dianne Hackborn95465202014-09-15 16:21:55 -07001388 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001389 // Transfer the result target from the source activity to the new
1390 // one being started, including any failures.
1391 if (requestCode >= 0) {
1392 ActivityOptions.abort(options);
1393 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1394 }
1395 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001396 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1397 resultRecord = null;
1398 }
Craig Mautner6170f732013-04-02 13:05:23 -07001399 resultWho = sourceRecord.resultWho;
1400 requestCode = sourceRecord.requestCode;
1401 sourceRecord.resultTo = null;
1402 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001403 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001404 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001405 if (sourceRecord.launchedFromUid == callingUid) {
1406 // The new activity is being launched from the same uid as the previous
1407 // activity in the flow, and asking to forward its result back to the
1408 // previous. In this case the activity is serving as a trampoline between
1409 // the two, so we also want to update its launchedFromPackage to be the
1410 // same as the previous activity. Note that this is safe, since we know
1411 // these two packages come from the same uid; the caller could just as
1412 // well have supplied that same package name itself. This specifially
1413 // deals with the case of an intent picker/chooser being launched in the app
1414 // flow to redirect to an activity picked by the user, where we want the final
1415 // activity to consider it to have been launched by the previous app activity.
1416 callingPackage = sourceRecord.launchedFromPackage;
1417 }
Craig Mautner6170f732013-04-02 13:05:23 -07001418 }
1419
1420 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1421 // We couldn't find a class that can handle the given Intent.
1422 // That's the end of that!
1423 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1424 }
1425
1426 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1427 // We couldn't find the specific class specified in the Intent.
1428 // Also the end of the line.
1429 err = ActivityManager.START_CLASS_NOT_FOUND;
1430 }
1431
Dianne Hackborn91097de2014-04-04 18:02:06 -07001432 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1433 && sourceRecord.task.voiceSession != null) {
1434 // If this activity is being launched as part of a voice session, we need
1435 // to ensure that it is safe to do so. If the upcoming activity will also
1436 // be part of the voice session, we can only launch it if it has explicitly
1437 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001438 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001439 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1440 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001441 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1442 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001443 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1444 }
1445 } catch (RemoteException e) {
1446 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1447 }
1448 }
1449 }
1450
1451 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1452 // If the caller is starting a new voice session, just make sure the target
1453 // is actually allowing it to run this way.
1454 try {
1455 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1456 intent, resolvedType)) {
1457 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1458 }
1459 } catch (RemoteException e) {
1460 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1461 }
1462 }
1463
louis_changcd5d1982014-08-01 10:09:08 +08001464 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1465
Craig Mautner6170f732013-04-02 13:05:23 -07001466 if (err != ActivityManager.START_SUCCESS) {
1467 if (resultRecord != null) {
1468 resultStack.sendActivityResultLocked(-1,
1469 resultRecord, resultWho, requestCode,
1470 Activity.RESULT_CANCELED, null);
1471 }
Craig Mautner6170f732013-04-02 13:05:23 -07001472 ActivityOptions.abort(options);
1473 return err;
1474 }
1475
1476 final int startAnyPerm = mService.checkPermission(
1477 START_ANY_ACTIVITY, callingPid, callingUid);
1478 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1479 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1480 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1481 if (resultRecord != null) {
1482 resultStack.sendActivityResultLocked(-1,
1483 resultRecord, resultWho, requestCode,
1484 Activity.RESULT_CANCELED, null);
1485 }
Craig Mautner6170f732013-04-02 13:05:23 -07001486 String msg;
1487 if (!aInfo.exported) {
1488 msg = "Permission Denial: starting " + intent.toString()
1489 + " from " + callerApp + " (pid=" + callingPid
1490 + ", uid=" + callingUid + ")"
1491 + " not exported from uid " + aInfo.applicationInfo.uid;
1492 } else {
1493 msg = "Permission Denial: starting " + intent.toString()
1494 + " from " + callerApp + " (pid=" + callingPid
1495 + ", uid=" + callingUid + ")"
1496 + " requires " + aInfo.permission;
1497 }
1498 Slog.w(TAG, msg);
1499 throw new SecurityException(msg);
1500 }
1501
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001502 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001503 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001504
Craig Mautner6170f732013-04-02 13:05:23 -07001505 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001506 try {
1507 // The Intent we give to the watcher has the extra data
1508 // stripped off, since it can contain private information.
1509 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001510 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001511 aInfo.applicationInfo.packageName);
1512 } catch (RemoteException e) {
1513 mService.mController = null;
1514 }
Ben Gruver5e207332013-04-03 17:41:37 -07001515 }
Craig Mautner6170f732013-04-02 13:05:23 -07001516
Ben Gruver5e207332013-04-03 17:41:37 -07001517 if (abort) {
1518 if (resultRecord != null) {
1519 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001520 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001521 }
Ben Gruver5e207332013-04-03 17:41:37 -07001522 // We pretend to the caller that it was really started, but
1523 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001524 ActivityOptions.abort(options);
1525 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001526 }
1527
1528 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001529 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001530 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001531 if (outActivity != null) {
1532 outActivity[0] = r;
1533 }
1534
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001535 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001536 if (voiceSession == null && (stack.mResumedActivity == null
1537 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001538 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1539 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001540 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001541 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001542 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001543 ActivityOptions.abort(options);
1544 return ActivityManager.START_SWITCHES_CANCELED;
1545 }
1546 }
1547
1548 if (mService.mDidAppSwitch) {
1549 // This is the second allowed switch since we stopped switches,
1550 // so now just generally allow switches. Use case: user presses
1551 // home (switches disabled, switch to home, mDidAppSwitch now true);
1552 // user taps a home icon (coming from home so allowed, we hit here
1553 // and now allow anyone to switch again).
1554 mService.mAppSwitchesAllowedTime = 0;
1555 } else {
1556 mService.mDidAppSwitch = true;
1557 }
1558
Craig Mautneree36c772014-07-16 14:56:05 -07001559 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001560
Dianne Hackborn91097de2014-04-04 18:02:06 -07001561 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001562 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001563
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001564 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001565 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001566 // activity start, but we are not actually doing an activity
1567 // switch... just dismiss the keyguard now, because we
1568 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001569 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001570 }
1571 return err;
1572 }
1573
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001574 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001575 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001576
1577 // On leanback only devices we should keep all activities in the same stack.
1578 if (!mLeanbackOnlyDevice &&
1579 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001580
1581 ActivityStack stack;
1582
Wale Ogunwale7d701172015-03-11 15:36:30 -07001583 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001584 stack = task.stack;
1585 if (stack.isOnHomeDisplay()) {
1586 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001587 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1588 "computeStackFocus: Setting " + "focused stack to r=" + r
1589 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001590 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001591 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001592 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001593 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001594 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001595 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001596 }
1597
Craig Mautnere0a38842013-12-16 16:14:02 -08001598 final ActivityContainer container = r.mInitialActivityContainer;
1599 if (container != null) {
1600 // The first time put it on the desired stack, after this put on task stack.
1601 r.mInitialActivityContainer = null;
1602 return container.mStack;
1603 }
1604
Craig Mautner1b4bf852014-05-26 15:06:32 -07001605 if (mFocusedStack != mHomeStack && (!newTask ||
1606 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001607 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001608 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001609 return mFocusedStack;
1610 }
1611
Craig Mautnere0a38842013-12-16 16:14:02 -08001612 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1613 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001614 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001615 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001616 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001617 "computeStackFocus: Setting focused stack=" + stack);
1618 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001619 }
1620 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001621
Craig Mautner4a1cb222013-12-04 16:14:06 -08001622 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001623 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001624 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1625 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001626 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001627 }
1628 return mHomeStack;
1629 }
1630
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001631 boolean setFocusedStack(ActivityRecord r, String reason) {
1632 if (r == null) {
1633 // Not sure what you are trying to do, but it is not going to work...
1634 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001635 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001636 final TaskRecord task = r.task;
1637 if (task == null || task.stack == null) {
1638 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1639 return false;
1640 }
1641 task.stack.moveToFront(reason);
1642 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001643 }
1644
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001645 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001646 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001647 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001648 final Intent intent = r.intent;
1649 final int callingUid = r.launchedFromUid;
1650
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001651 // In some flows in to this function, we retrieve the task record and hold on to it
1652 // without a lock before calling back in to here... so the task at this point may
1653 // not actually be in recents. Check for that, and if it isn't in recents just
1654 // consider it invalid.
1655 if (inTask != null && !inTask.inRecents) {
1656 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1657 inTask = null;
1658 }
1659
Craig Mautnerad400af2014-08-13 16:41:36 -07001660 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001661 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1662 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001663
Craig Mautnera228ae92014-07-09 05:44:55 -07001664 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001665 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001666 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001667 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1668 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1669 "\"singleInstance\" or \"singleTask\"");
1670 launchFlags &=
1671 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1672 } else {
1673 switch (r.info.documentLaunchMode) {
1674 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1675 break;
1676 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1677 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1678 break;
1679 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1680 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1681 break;
1682 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1683 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1684 break;
1685 }
1686 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001687
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001688 final boolean launchTaskBehind = r.mLaunchTaskBehind
1689 && !launchSingleTask && !launchSingleInstance
1690 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001691
Wale Ogunwale7d701172015-03-11 15:36:30 -07001692 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1693 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001694 // For whatever reason this activity is being launched into a new
1695 // task... yet the caller has requested a result back. Well, that
1696 // is pretty messed up, so instead immediately send back a cancel
1697 // and let the new task continue launched as normal without a
1698 // dependency on its originator.
1699 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1700 r.resultTo.task.stack.sendActivityResultLocked(-1,
1701 r.resultTo, r.resultWho, r.requestCode,
1702 Activity.RESULT_CANCELED, null);
1703 r.resultTo = null;
1704 }
1705
1706 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1707 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1708 }
1709
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001710 // If we are actually going to launch in to a new task, there are some cases where
1711 // we further want to do multiple task.
1712 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1713 if (launchTaskBehind
1714 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1715 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1716 }
1717 }
1718
Craig Mautner8849a5e2013-04-02 16:41:03 -07001719 // We'll invoke onUserLeaving before onPause only if the launching
1720 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001721 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001722 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001723
1724 // If the caller has asked not to resume at this point, we make note
1725 // of this in the record so that we can skip it when trying to find
1726 // the top running activity.
1727 if (!doResume) {
1728 r.delayedResume = true;
1729 }
1730
Craig Mautnera254cd72014-05-25 16:47:39 -07001731 ActivityRecord notTop =
1732 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001733
1734 // If the onlyIfNeeded flag is set, then we can do this if the activity
1735 // being launched is the same as the one making the call... or, as
1736 // a special case, if we do not know the caller then we count the
1737 // current top activity as the caller.
1738 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1739 ActivityRecord checkedCaller = sourceRecord;
1740 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001741 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001742 }
1743 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1744 // Caller is not the same as launcher, so always needed.
1745 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1746 }
1747 }
1748
Craig Mautner8849a5e2013-04-02 16:41:03 -07001749 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001750 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001751
1752 // If the caller is not coming from another activity, but has given us an
1753 // explicit task into which they would like us to launch the new activity,
1754 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001755 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1756 final Intent baseIntent = inTask.getBaseIntent();
1757 final ActivityRecord root = inTask.getRootActivity();
1758 if (baseIntent == null) {
1759 ActivityOptions.abort(options);
1760 throw new IllegalArgumentException("Launching into task without base intent: "
1761 + inTask);
1762 }
1763
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001764 // If this task is empty, then we are adding the first activity -- it
1765 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001766 if (launchSingleInstance || launchSingleTask) {
1767 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1768 ActivityOptions.abort(options);
1769 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1770 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001771 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001772 if (root != null) {
1773 ActivityOptions.abort(options);
1774 throw new IllegalArgumentException("Caller with inTask " + inTask
1775 + " has root " + root + " but target is singleInstance/Task");
1776 }
1777 }
1778
1779 // If task is empty, then adopt the interesting intent launch flags in to the
1780 // activity being started.
1781 if (root == null) {
1782 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1783 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1784 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1785 launchFlags = (launchFlags&~flagsOfInterest)
1786 | (baseIntent.getFlags()&flagsOfInterest);
1787 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001788 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001789 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001790
Dianne Hackborn962d5352014-08-29 16:27:40 -07001791 // If the task is not empty and the caller is asking to start it as the root
1792 // of a new task, then we don't actually want to start this on the task. We
1793 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001794 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001795 addingToTask = false;
1796
1797 } else {
1798 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001799 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001800
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001801 reuseTask = inTask;
1802 } else {
1803 inTask = null;
1804 }
1805
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001806 if (inTask == null) {
1807 if (sourceRecord == null) {
1808 // This activity is not being started from another... in this
1809 // case we -always- start a new task.
1810 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1811 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1812 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1813 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1814 }
1815 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1816 // The original activity who is starting us is running as a single
1817 // instance... this new activity it is starting must go on its
1818 // own task.
1819 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1820 } else if (launchSingleInstance || launchSingleTask) {
1821 // The activity being started is a single instance... it always
1822 // gets launched into its own task.
1823 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1824 }
1825 }
1826
1827 ActivityInfo newTaskInfo = null;
1828 Intent newTaskIntent = null;
1829 ActivityStack sourceStack;
1830 if (sourceRecord != null) {
1831 if (sourceRecord.finishing) {
1832 // If the source is finishing, we can't further count it as our source. This
1833 // is because the task it is associated with may now be empty and on its way out,
1834 // so we don't want to blindly throw it in to that task. Instead we will take
1835 // the NEW_TASK flow and try to find a task for it. But save the task information
1836 // so it can be used when creating the new task.
1837 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1838 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1839 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1840 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1841 newTaskInfo = sourceRecord.info;
1842 newTaskIntent = sourceRecord.task.intent;
1843 }
1844 sourceRecord = null;
1845 sourceStack = null;
1846 } else {
1847 sourceStack = sourceRecord.task.stack;
1848 }
1849 } else {
1850 sourceStack = null;
1851 }
1852
1853 boolean movedHome = false;
1854 ActivityStack targetStack;
1855
1856 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001857 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001858
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001859 // We may want to try to place the new activity in to an existing task. We always
1860 // do this if the target activity is singleTask or singleInstance; we will also do
1861 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1862 // us to still place it in a new task: multi task, always doc mode, or being asked to
1863 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001864 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1865 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001866 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001867 // If bring to front is requested, and no result is requested and we have not
1868 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001869 // we can find a task that was started with this same
1870 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001871 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001872 // See if there is a task to bring to the front. If this is
1873 // a SINGLE_INSTANCE activity, there can be one and only one
1874 // instance of it in the history, and it is always in its own
1875 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001876 ActivityRecord intentActivity = !launchSingleInstance ?
1877 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001878 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001879 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001880 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001881 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001882 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1883 }
Craig Mautner29219d92013-04-16 20:19:12 -07001884 if (r.task == null) {
1885 r.task = intentActivity.task;
1886 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001887 if (intentActivity.task.intent == null) {
1888 // This task was started because of movement of
1889 // the activity based on affinity... now that we
1890 // are actually launching it, we can assign the
1891 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001892 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001893 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001894 targetStack = intentActivity.task.stack;
1895 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001896 // If the target task is not in the front, then we need
1897 // to bring it to the front... except... well, with
1898 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1899 // to have the same behavior as if a new instance was
1900 // being started, which means not bringing it to the front
1901 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001902 final ActivityStack focusStack = getFocusedStack();
1903 ActivityRecord curTop = (focusStack == null)
1904 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001905 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001906 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001907 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001908 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001909 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1910 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001911 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001912 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001913 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1914 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001915 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001916 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1917 options, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001918 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001919 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001920 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1921 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001922 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001923 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001924 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001925 options = null;
1926 }
1927 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001928 if (!movedToFront) {
1929 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1930 + " from " + intentActivity);
1931 targetStack.moveToFront("intentActivityFound");
1932 }
1933
Craig Mautner8849a5e2013-04-02 16:41:03 -07001934 // If the caller has requested that the target task be
1935 // reset, then do so.
1936 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1937 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1938 }
1939 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1940 // We don't need to start a new activity, and
1941 // the client said not to do anything if that
1942 // is the case, so this is it! And for paranoia, make
1943 // sure we have correctly resumed the top activity.
1944 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001945 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001946
1947 // Make sure to notify Keyguard as well if we are not running an app
1948 // transition later.
1949 if (!movedToFront) {
1950 notifyActivityDrawnForKeyguard();
1951 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001952 } else {
1953 ActivityOptions.abort(options);
1954 }
1955 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1956 }
1957 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001958 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1959 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001960 // The caller has requested to completely replace any
1961 // existing task with its new activity. Well that should
1962 // not be too hard...
1963 reuseTask = intentActivity.task;
1964 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001965 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001966 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001967 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001968 // In this situation we want to remove all activities
1969 // from the task up to the one being started. In most
1970 // cases this means we are resetting the task to its
1971 // initial state.
1972 ActivityRecord top =
1973 intentActivity.task.performClearTaskLocked(r, launchFlags);
1974 if (top != null) {
1975 if (top.frontOfTask) {
1976 // Activity aliases may mean we use different
1977 // intents for the top activity, so make sure
1978 // the task now has the identity of the new
1979 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001980 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001981 }
1982 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1983 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001984 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001985 } else {
1986 // A special case: we need to
1987 // start the activity because it is not currently
1988 // running, and the caller has asked to clear the
1989 // current task to have this activity at the top.
1990 addingToTask = true;
1991 // Now pretend like this activity is being started
1992 // by the top of its task, so it is put in the
1993 // right place.
1994 sourceRecord = intentActivity;
1995 }
1996 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1997 // In this case the top activity on the task is the
1998 // same as the one being launched, so we take that
1999 // as a request to bring the task to the foreground.
2000 // If the top activity in the task is the root
2001 // activity, deliver this new intent to it if it
2002 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002003 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002004 && intentActivity.realActivity.equals(r.realActivity)) {
2005 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2006 intentActivity.task);
2007 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002008 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002009 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002010 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2011 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002012 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2013 // In this case we are launching the root activity
2014 // of the task, but with a different intent. We
2015 // should start a new instance on top.
2016 addingToTask = true;
2017 sourceRecord = intentActivity;
2018 }
2019 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2020 // In this case an activity is being launched in to an
2021 // existing task, without resetting that task. This
2022 // is typically the situation of launching an activity
2023 // from a notification or shortcut. We want to place
2024 // the new activity on top of the current task.
2025 addingToTask = true;
2026 sourceRecord = intentActivity;
2027 } else if (!intentActivity.task.rootWasReset) {
2028 // In this case we are launching in to an existing task
2029 // that has not yet been started from its front door.
2030 // The current task has been brought to the front.
2031 // Ideally, we'd probably like to place this new task
2032 // at the bottom of its stack, but that's a little hard
2033 // to do with the current organization of the code so
2034 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002035 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002036 }
2037 if (!addingToTask && reuseTask == null) {
2038 // We didn't do anything... but it was needed (a.k.a., client
2039 // don't use that intent!) And for paranoia, make
2040 // sure we have correctly resumed the top activity.
2041 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002042 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002043 if (!movedToFront) {
2044 // Make sure to notify Keyguard as well if we are not running an app
2045 // transition later.
2046 notifyActivityDrawnForKeyguard();
2047 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002048 } else {
2049 ActivityOptions.abort(options);
2050 }
2051 return ActivityManager.START_TASK_TO_FRONT;
2052 }
2053 }
2054 }
2055 }
2056
2057 //String uri = r.intent.toURI();
2058 //Intent intent2 = new Intent(uri);
2059 //Slog.i(TAG, "Given intent: " + r.intent);
2060 //Slog.i(TAG, "URI is: " + uri);
2061 //Slog.i(TAG, "To intent: " + intent2);
2062
2063 if (r.packageName != null) {
2064 // If the activity being launched is the same as the one currently
2065 // at the top, then we need to check if it should only be launched
2066 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002067 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002068 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002069 if (top != null && r.resultTo == null) {
2070 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2071 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002072 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002073 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002074 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2075 top.task);
2076 // For paranoia, make sure we have correctly
2077 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002078 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002079 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002080 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002081 }
2082 ActivityOptions.abort(options);
2083 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2084 // We don't need to start a new activity, and
2085 // the client said not to do anything if that
2086 // is the case, so this is it!
2087 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2088 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002089 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002090 return ActivityManager.START_DELIVERED_TO_TOP;
2091 }
2092 }
2093 }
2094 }
2095
2096 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002097 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002098 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2099 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002100 }
2101 ActivityOptions.abort(options);
2102 return ActivityManager.START_CLASS_NOT_FOUND;
2103 }
2104
2105 boolean newTask = false;
2106 boolean keepCurTransition = false;
2107
Craig Mautnerbb742462014-07-07 15:28:55 -07002108 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002109 sourceRecord.task : null;
2110
Craig Mautner8849a5e2013-04-02 16:41:03 -07002111 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002112 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002113 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002114 if (isLockTaskModeViolation(reuseTask)) {
2115 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2116 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2117 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002118 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002119 targetStack = computeStackFocus(r, newTask);
2120 targetStack.moveToFront("startingNewTask");
2121
Craig Mautner8849a5e2013-04-02 16:41:03 -07002122 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002123 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2124 newTaskInfo != null ? newTaskInfo : r.info,
2125 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002126 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2127 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002128 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2129 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002130 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002131 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002132 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002133 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002134 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002135 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2136 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002137 // Caller wants to appear on home activity, so before starting
2138 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002139 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002140 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002141 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002142 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002143 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002144 if (isLockTaskModeViolation(sourceTask)) {
2145 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2146 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2147 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002148 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002149 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002150 final TaskRecord topTask = targetStack.topTask();
2151 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002152 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2153 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002154 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002155 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002156 // In this case, we are adding the activity to an existing
2157 // task, but the caller has asked to clear that task if the
2158 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002159 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002160 keepCurTransition = true;
2161 if (top != null) {
2162 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002163 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002164 // For paranoia, make sure we have correctly
2165 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002166 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002167 if (doResume) {
2168 targetStack.resumeTopActivityLocked(null);
2169 }
2170 ActivityOptions.abort(options);
2171 return ActivityManager.START_DELIVERED_TO_TOP;
2172 }
2173 } else if (!addingToTask &&
2174 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2175 // In this case, we are launching an activity in our own task
2176 // that may already be running somewhere in the history, and
2177 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002178 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002179 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002180 final TaskRecord task = top.task;
2181 task.moveActivityToFrontLocked(top);
2182 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002183 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002184 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002185 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002186 if (doResume) {
2187 targetStack.resumeTopActivityLocked(null);
2188 }
2189 return ActivityManager.START_DELIVERED_TO_TOP;
2190 }
2191 }
2192 // An existing activity is starting this new activity, so we want
2193 // to keep the new one in the same task as the one that is starting
2194 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002195 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002197 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002198
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002199 } else if (inTask != null) {
2200 // The calling is asking that the new activity be started in an explicit
2201 // task it has provided to us.
2202 if (isLockTaskModeViolation(inTask)) {
2203 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2204 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2205 }
2206 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002207 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002208
2209 // Check whether we should actually launch the new activity in to the task,
2210 // or just reuse the current activity on top.
2211 ActivityRecord top = inTask.getTopActivity();
2212 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2213 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2214 || launchSingleTop || launchSingleTask) {
2215 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2216 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2217 // We don't need to start a new activity, and
2218 // the client said not to do anything if that
2219 // is the case, so this is it!
2220 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2221 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002222 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002223 return ActivityManager.START_DELIVERED_TO_TOP;
2224 }
2225 }
2226
Dianne Hackborn962d5352014-08-29 16:27:40 -07002227 if (!addingToTask) {
2228 // We don't actually want to have this activity added to the task, so just
2229 // stop here but still tell the caller that we consumed the intent.
2230 ActivityOptions.abort(options);
2231 return ActivityManager.START_TASK_TO_FRONT;
2232 }
2233
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002234 r.setTask(inTask, null);
2235 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2236 + " in explicit task " + r.task);
2237
Craig Mautner8849a5e2013-04-02 16:41:03 -07002238 } else {
2239 // This not being started from an existing activity, and not part
2240 // of a new task... just put it in the top task, though these days
2241 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002242 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002243 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002244 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002245 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002246 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002247 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002248 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2249 + " in new guessed " + r.task);
2250 }
2251
2252 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002253 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002254
Craig Mautner76e2a762014-06-24 09:05:43 -07002255 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2256 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2257 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002258 if (newTask) {
2259 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2260 }
2261 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002262 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002263 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002264 if (!launchTaskBehind) {
2265 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002266 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002267 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002268 return ActivityManager.START_SUCCESS;
2269 }
2270
Craig Mautneree36c772014-07-16 14:56:05 -07002271 final void doPendingActivityLaunchesLocked(boolean doResume) {
2272 while (!mPendingActivityLaunches.isEmpty()) {
2273 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002274 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002275 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002276 }
2277 }
2278
Craig Mautner7f13ed32014-07-28 14:00:35 -07002279 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002280 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2281 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002282 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002283 mPendingActivityLaunches.remove(palNdx);
2284 }
2285 }
2286 }
2287
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002288 void setLaunchSource(int uid) {
2289 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2290 }
2291
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002292 void acquireLaunchWakelock() {
2293 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2294 throw new IllegalStateException("Calling must be system uid");
2295 }
2296 mLaunchingActivity.acquire();
2297 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2298 // To be safe, don't allow the wake lock to be held for too long.
2299 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2300 }
2301 }
2302
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002303 /**
2304 * Called when the frontmost task is idle.
2305 * @return the state of mService.mBooting before this was called.
2306 */
2307 private boolean checkFinishBootingLocked() {
2308 final boolean booting = mService.mBooting;
2309 boolean enableScreen = false;
2310 mService.mBooting = false;
2311 if (!mService.mBooted) {
2312 mService.mBooted = true;
2313 enableScreen = true;
2314 }
2315 if (booting || enableScreen) {
2316 mService.postFinishBooting(booting, enableScreen);
2317 }
2318 return booting;
2319 }
2320
Craig Mautnerf3333272013-04-22 10:55:53 -07002321 // Checked.
2322 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2323 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002324 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002325
Craig Mautnerf3333272013-04-22 10:55:53 -07002326 ArrayList<ActivityRecord> stops = null;
2327 ArrayList<ActivityRecord> finishes = null;
2328 ArrayList<UserStartedState> startingUsers = null;
2329 int NS = 0;
2330 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002331 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002332 boolean activityRemoved = false;
2333
Wale Ogunwale7d701172015-03-11 15:36:30 -07002334 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002335 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002336 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2337 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002338 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2339 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002340 if (fromTimeout) {
2341 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002342 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002343
2344 // This is a hack to semi-deal with a race condition
2345 // in the client where it can be constructed with a
2346 // newer configuration from when we asked it to launch.
2347 // We'll update with whatever configuration it now says
2348 // it used to launch.
2349 if (config != null) {
2350 r.configuration = config;
2351 }
2352
2353 // We are now idle. If someone is waiting for a thumbnail from
2354 // us, we can now deliver.
2355 r.idle = true;
2356
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002357 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002358 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002359 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002360 }
2361 }
2362
2363 if (allResumedActivitiesIdle()) {
2364 if (r != null) {
2365 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002366 }
2367
2368 if (mLaunchingActivity.isHeld()) {
2369 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2370 if (VALIDATE_WAKE_LOCK_CALLER &&
2371 Binder.getCallingUid() != Process.myUid()) {
2372 throw new IllegalStateException("Calling must be system uid");
2373 }
2374 mLaunchingActivity.release();
2375 }
2376 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002377 }
2378
2379 // Atomically retrieve all of the other things to do.
2380 stops = processStoppingActivitiesLocked(true);
2381 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002382 if ((NF = mFinishingActivities.size()) > 0) {
2383 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002384 mFinishingActivities.clear();
2385 }
2386
Craig Mautnerf3333272013-04-22 10:55:53 -07002387 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002388 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002389 mStartingUsers.clear();
2390 }
2391
Craig Mautnerf3333272013-04-22 10:55:53 -07002392 // Stop any activities that are scheduled to do so but have been
2393 // waiting for the next one to start.
2394 for (int i = 0; i < NS; i++) {
2395 r = stops.get(i);
2396 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002397 if (stack != null) {
2398 if (r.finishing) {
2399 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2400 } else {
2401 stack.stopActivityLocked(r);
2402 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002403 }
2404 }
2405
2406 // Finish any activities that are scheduled to do so but have been
2407 // waiting for the next one to start.
2408 for (int i = 0; i < NF; i++) {
2409 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002410 final ActivityStack stack = r.task.stack;
2411 if (stack != null) {
2412 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2413 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002414 }
2415
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002416 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002417 // Complete user switch
2418 if (startingUsers != null) {
2419 for (int i = 0; i < startingUsers.size(); i++) {
2420 mService.finishUserSwitch(startingUsers.get(i));
2421 }
2422 }
2423 // Complete starting up of background users
2424 if (mStartingBackgroundUsers.size() > 0) {
2425 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2426 mStartingBackgroundUsers.clear();
2427 for (int i = 0; i < startingUsers.size(); i++) {
2428 mService.finishUserBoot(startingUsers.get(i));
2429 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002430 }
2431 }
2432
2433 mService.trimApplications();
2434 //dump();
2435 //mWindowManager.dump();
2436
Craig Mautnerf3333272013-04-22 10:55:53 -07002437 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002438 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002439 }
2440
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002441 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002442 }
2443
Craig Mautner8e569572013-10-11 17:36:59 -07002444 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002445 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002446 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2447 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002448 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2449 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2450 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002451 }
Craig Mautner19091252013-10-05 00:03:53 -07002452 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002453 }
2454
2455 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002456 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2457 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002458 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2459 stacks.get(stackNdx).closeSystemDialogsLocked();
2460 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002461 }
2462 }
2463
Craig Mautner93529a42013-10-04 15:03:13 -07002464 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002465 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002466 }
2467
Craig Mautner8d341ef2013-03-26 09:03:27 -07002468 /**
2469 * @return true if some activity was finished (or would have finished if doit were true).
2470 */
2471 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2472 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002473 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2474 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002475 final int numStacks = stacks.size();
2476 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2477 final ActivityStack stack = stacks.get(stackNdx);
2478 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2479 didSomething = true;
2480 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002481 }
2482 }
2483 return didSomething;
2484 }
2485
Dianne Hackborna413dc02013-07-12 12:02:55 -07002486 void updatePreviousProcessLocked(ActivityRecord r) {
2487 // Now that this process has stopped, we may want to consider
2488 // it to be the previous app to try to keep around in case
2489 // the user wants to return to it.
2490
2491 // First, found out what is currently the foreground app, so that
2492 // we don't blow away the previous app if this activity is being
2493 // hosted by the process that is actually still the foreground.
2494 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002495 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2496 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002497 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2498 final ActivityStack stack = stacks.get(stackNdx);
2499 if (isFrontStack(stack)) {
2500 if (stack.mResumedActivity != null) {
2501 fgApp = stack.mResumedActivity.app;
2502 } else if (stack.mPausingActivity != null) {
2503 fgApp = stack.mPausingActivity.app;
2504 }
2505 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002506 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002507 }
2508 }
2509
2510 // Now set this one as the previous process, only if that really
2511 // makes sense to.
2512 if (r.app != null && fgApp != null && r.app != fgApp
2513 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002514 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002515 mService.mPreviousProcess = r.app;
2516 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2517 }
2518 }
2519
Craig Mautner05d29032013-05-03 13:40:13 -07002520 boolean resumeTopActivitiesLocked() {
2521 return resumeTopActivitiesLocked(null, null, null);
2522 }
2523
2524 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2525 Bundle targetOptions) {
2526 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002527 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002528 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002529 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002530 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002531 if (isFrontStack(targetStack)) {
2532 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2533 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002534
Craig Mautnere0a38842013-12-16 16:14:02 -08002535 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2536 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002537 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2538 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002539 if (stack == targetStack) {
2540 // Already started above.
2541 continue;
2542 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002543 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002544 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002545 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002546 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002547 }
Craig Mautner05d29032013-05-03 13:40:13 -07002548 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002549 }
2550
Todd Kennedy539db512014-12-15 09:57:55 -08002551 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002552 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2553 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002554 final int numStacks = stacks.size();
2555 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2556 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002557 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002558 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002559 }
2560 }
2561
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002562 void finishVoiceTask(IVoiceInteractionSession session) {
2563 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2564 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2565 final int numStacks = stacks.size();
2566 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2567 final ActivityStack stack = stacks.get(stackNdx);
2568 stack.finishVoiceTask(session);
2569 }
2570 }
2571 }
2572
Craig Mautner299f9602015-01-26 09:47:33 -08002573 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002574 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2575 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002576 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002577 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2578 // Caller wants the home activity moved with it. To accomplish this,
2579 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002580 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002581 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002582 if (task.stack == null) {
2583 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2584 + task + " to front. Stack is null");
2585 return;
2586 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002587 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08002588 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2589 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002590 }
2591
Craig Mautner967212c2013-04-13 21:10:58 -07002592 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002593 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2594 if (activityContainer != null) {
2595 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002596 }
2597 return null;
2598 }
2599
Craig Mautner967212c2013-04-13 21:10:58 -07002600 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002601 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002602 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2603 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002604 }
2605 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002606 }
2607
Craig Mautner4a1cb222013-12-04 16:14:06 -08002608 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002609 ActivityRecord homeActivity = getHomeActivity();
2610 if (homeActivity != null) {
2611 return homeActivity.appToken;
2612 }
2613 return null;
2614 }
2615
2616 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002617 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2618 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2619 final TaskRecord task = tasks.get(taskNdx);
2620 if (task.isHomeTask()) {
2621 final ArrayList<ActivityRecord> activities = task.mActivities;
2622 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2623 final ActivityRecord r = activities.get(activityNdx);
2624 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002625 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002626 }
2627 }
2628 }
2629 }
2630 return null;
2631 }
2632
Todd Kennedyca4d8422015-01-15 15:19:22 -08002633 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002634 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002635 ActivityContainer activityContainer =
2636 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002637 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002638 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002639 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002640 return activityContainer;
2641 }
2642
Craig Mautner34b73df2014-01-12 21:11:08 -08002643 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002644 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2645 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2646 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002647 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2648 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002649 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002650 }
2651 }
2652
Craig Mautner95da1082014-02-24 17:54:35 -08002653 void deleteActivityContainer(IActivityContainer container) {
2654 ActivityContainer activityContainer = (ActivityContainer)container;
2655 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002656 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2657 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002658 final int stackId = activityContainer.mStackId;
2659 mActivityContainers.remove(stackId);
2660 mWindowManager.removeStack(stackId);
2661 }
2662 }
2663
Wale Ogunwale60454db2015-01-23 16:05:07 -08002664 void resizeStackLocked(int stackId, Rect bounds) {
2665 final ActivityStack stack = getStack(stackId);
2666 if (stack == null) {
2667 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2668 return;
2669 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002670
2671 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002672 if (r != null && !r.task.mResizeable) {
2673 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002674 return;
2675 }
2676
Wale Ogunwale60454db2015-01-23 16:05:07 -08002677 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2678 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002679 if (r != null) {
2680 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2681 // And we need to make sure at this point that all other activities
2682 // are made visible with the correct configuration.
2683 ensureActivitiesVisibleLocked(r, 0);
2684 if (!updated) {
2685 resumeTopActivitiesLocked(stack, null, null);
2686 }
2687 }
2688 }
2689 }
2690
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002691 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2692 * current task stack if it only has one entry, moving the task to a stack that matches the
2693 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2694 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2695 task.mResizeable = true;
2696 final ActivityStack currentStack = task.stack;
2697 if (currentStack.isHomeStack()) {
2698 // Can't move task off the home stack. Sorry!
2699 return;
2700 }
2701
2702 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2703 if (matchingStackId != -1) {
2704 // There is already a stack with the right bounds!
2705 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2706 // Nothing to do here. Already in the right stack...
2707 return;
2708 }
2709 // Move task to stack with matching bounds.
2710 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2711 return;
2712 }
2713
2714 if (currentStack != null && currentStack.numTasks() == 1) {
2715 // Just resize the current stack since this is the task in it.
2716 resizeStackLocked(currentStack.mStackId, bounds);
2717 return;
2718 }
2719
2720 // Create new stack and move the task to it.
2721 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2722 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2723 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2724
2725 if (newStack == null) {
2726 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2727 return;
2728 }
2729 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2730 resizeStackLocked(newStack.mStackId, bounds);
2731 }
2732
Todd Kennedy4900bf92015-01-16 16:05:14 -08002733 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002734 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2735 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002736 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002737 }
2738
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002739 ActivityContainer activityContainer = new ActivityContainer(stackId);
2740 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002741 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002742 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002743 }
2744
2745 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002746 while (true) {
2747 if (++mLastStackId <= HOME_STACK_ID) {
2748 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002749 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002750 if (getStack(mLastStackId) == null) {
2751 break;
2752 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002753 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002754 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002755 }
2756
Wale Ogunwale7de05352014-12-12 15:21:33 -08002757 private boolean restoreRecentTaskLocked(TaskRecord task) {
2758 ActivityStack stack = null;
2759 // Determine stack to restore task to.
2760 if (mLeanbackOnlyDevice) {
2761 // There is only one stack for lean back devices.
2762 stack = mHomeStack;
2763 } else {
2764 // Look for the top stack on the home display that isn't the home stack.
2765 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2766 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2767 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002768 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002769 stack = tmpStack;
2770 break;
2771 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002772 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002773 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002774
2775 if (stack == null) {
2776 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2777 // before an application stack is created...Go ahead and create one on the default
2778 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002779 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002780 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002781 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002782 if (DEBUG_RECENTS)
2783 Slog.v(TAG, "Created stack=" + stack + " for recents restoration.");
2784 }
2785
2786 if (stack == null) {
2787 // What does this mean??? Not sure how we would get here...
2788 if (DEBUG_RECENTS)
2789 Slog.v(TAG, "Unable to find/create stack to restore recent task=" + task);
2790 return false;
2791 }
2792
2793 stack.addTask(task, false, false);
2794 if (DEBUG_RECENTS)
2795 Slog.v(TAG, "Added restored task=" + task + " to stack=" + stack);
2796 final ArrayList<ActivityRecord> activities = task.mActivities;
2797 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2798 final ActivityRecord r = activities.get(activityNdx);
2799 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2800 r.info.screenOrientation, r.fullscreen,
2801 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2802 r.userId, r.info.configChanges, task.voiceSession != null,
2803 r.mLaunchTaskBehind);
2804 }
2805 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002806 }
2807
Craig Mautner299f9602015-01-26 09:47:33 -08002808 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002809 final TaskRecord task = anyTaskForIdLocked(taskId);
2810 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002811 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002812 return;
2813 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002814 final ActivityStack stack = getStack(stackId);
2815 if (stack == null) {
2816 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2817 return;
2818 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002819 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2820 if (task.stack != null) {
2821 task.stack.removeTask(task, "moveTaskToStack", false);
2822 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002823 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002824 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002825 }
2826
Craig Mautnerac6f8432013-07-17 13:24:59 -07002827 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002828 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002829 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2830 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002831 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2832 final ActivityStack stack = stacks.get(stackNdx);
2833 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002834 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2835 continue;
2836 }
2837 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2838 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2839 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002840 continue;
2841 }
2842 final ActivityRecord ar = stack.findTaskLocked(r);
2843 if (ar != null) {
2844 return ar;
2845 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002846 }
2847 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002848 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002849 return null;
2850 }
2851
2852 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002853 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2854 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002855 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2856 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2857 if (ar != null) {
2858 return ar;
2859 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002860 }
2861 }
2862 return null;
2863 }
2864
Craig Mautner8d341ef2013-03-26 09:03:27 -07002865 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002866 scheduleSleepTimeout();
2867 if (!mGoingToSleep.isHeld()) {
2868 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002869 if (mLaunchingActivity.isHeld()) {
2870 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2871 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002872 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002873 mLaunchingActivity.release();
2874 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002875 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002876 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002877 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002878 }
2879
2880 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002881 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002882
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002883 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002884 final long endTime = System.currentTimeMillis() + timeout;
2885 while (true) {
2886 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002887 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2888 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002889 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2890 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2891 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002892 }
2893 if (cantShutdown) {
2894 long timeRemaining = endTime - System.currentTimeMillis();
2895 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002896 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002897 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002898 } catch (InterruptedException e) {
2899 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002900 } else {
2901 Slog.w(TAG, "Activity manager shutdown timed out");
2902 timedout = true;
2903 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002904 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002905 } else {
2906 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 }
2908 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002909
2910 // Force checkReadyForSleep to complete.
2911 mSleepTimeout = true;
2912 checkReadyForSleepLocked();
2913
Craig Mautner8d341ef2013-03-26 09:03:27 -07002914 return timedout;
2915 }
2916
2917 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002918 removeSleepTimeouts();
2919 if (mGoingToSleep.isHeld()) {
2920 mGoingToSleep.release();
2921 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002922 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2923 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002924 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2925 final ActivityStack stack = stacks.get(stackNdx);
2926 stack.awakeFromSleepingLocked();
2927 if (isFrontStack(stack)) {
2928 resumeTopActivitiesLocked();
2929 }
Craig Mautner5314a402013-09-26 12:40:16 -07002930 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002931 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002932 mGoingToSleepActivities.clear();
2933 }
2934
2935 void activitySleptLocked(ActivityRecord r) {
2936 mGoingToSleepActivities.remove(r);
2937 checkReadyForSleepLocked();
2938 }
2939
2940 void checkReadyForSleepLocked() {
2941 if (!mService.isSleepingOrShuttingDown()) {
2942 // Do not care.
2943 return;
2944 }
2945
2946 if (!mSleepTimeout) {
2947 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002948 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2949 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002950 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2951 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2952 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002953 }
2954
2955 if (mStoppingActivities.size() > 0) {
2956 // Still need to tell some activities to stop; can't sleep yet.
2957 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2958 + mStoppingActivities.size() + " activities");
2959 scheduleIdleLocked();
2960 dontSleep = true;
2961 }
2962
2963 if (mGoingToSleepActivities.size() > 0) {
2964 // Still need to tell some activities to sleep; can't sleep yet.
2965 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2966 + mGoingToSleepActivities.size() + " activities");
2967 dontSleep = true;
2968 }
2969
2970 if (dontSleep) {
2971 return;
2972 }
2973 }
2974
Craig Mautnere0a38842013-12-16 16:14:02 -08002975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2976 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002977 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2978 stacks.get(stackNdx).goToSleep();
2979 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002980 }
2981
2982 removeSleepTimeouts();
2983
2984 if (mGoingToSleep.isHeld()) {
2985 mGoingToSleep.release();
2986 }
2987 if (mService.mShuttingDown) {
2988 mService.notifyAll();
2989 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002990 }
2991
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002992 boolean reportResumedActivityLocked(ActivityRecord r) {
2993 final ActivityStack stack = r.task.stack;
2994 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002995 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002996 }
2997 if (allResumedActivitiesComplete()) {
2998 ensureActivitiesVisibleLocked(null, 0);
2999 mWindowManager.executeAppTransition();
3000 return true;
3001 }
3002 return false;
3003 }
3004
Craig Mautner8d341ef2013-03-26 09:03:27 -07003005 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003006 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3007 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003008 final int numStacks = stacks.size();
3009 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3010 final ActivityStack stack = stacks.get(stackNdx);
3011 stack.handleAppCrashLocked(app);
3012 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003013 }
3014 }
3015
Jose Lima4b6c6692014-08-12 17:41:12 -07003016 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003017 final ActivityStack stack = r.task.stack;
3018 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003019 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3020 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003021 return false;
3022 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003023 final boolean isVisible = stack.hasVisibleBehindActivity();
3024 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3025 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003026
3027 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003028 if (top == null || top == r || (visible == isVisible)) {
3029 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3030 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003031 return true;
3032 }
3033
3034 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003035 if (visible && top.fullscreen) {
3036 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003037 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003038 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003039 " top.app.thread=" + top.app.thread);
3040 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003041 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3042 // Only the activity set as currently visible behind should actively reset its
3043 // visible behind state.
3044 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3045 + visible + " stack.getVisibleBehindActivity()=" +
3046 stack.getVisibleBehindActivity() + " r=" + r);
3047 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003048 }
3049
Jose Lima4b6c6692014-08-12 17:41:12 -07003050 stack.setVisibleBehindActivity(visible ? r : null);
3051 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003052 // Make the activity immediately above r opaque.
3053 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3054 if (next != null) {
3055 mService.convertFromTranslucent(next.appToken);
3056 }
3057 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003058 if (top.app != null && top.app.thread != null) {
3059 // Notify the top app of the change.
3060 try {
3061 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3062 } catch (RemoteException e) {
3063 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003064 }
3065 return true;
3066 }
3067
Craig Mautnerbb742462014-07-07 15:28:55 -07003068 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3069 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3070 r.mLaunchTaskBehind = false;
3071 final TaskRecord task = r.task;
3072 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003073 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003074 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003075 mWindowManager.setAppVisibility(r.appToken, false);
3076 }
3077
3078 void scheduleLaunchTaskBehindComplete(IBinder token) {
3079 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3080 }
3081
Craig Mautnerde4ef022013-04-07 19:01:33 -07003082 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003083 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003084 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3085 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003086 final int topStackNdx = stacks.size() - 1;
3087 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3088 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003089 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003090 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003091 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003092 }
3093
3094 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003095 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3096 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003097 final int numStacks = stacks.size();
3098 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3099 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003100 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003101 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003102 }
3103 }
3104
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003105 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3106 // Examine all activities currently running in the process.
3107 TaskRecord firstTask = null;
3108 // Tasks is non-null only if two or more tasks are found.
3109 ArraySet<TaskRecord> tasks = null;
3110 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3111 for (int i=0; i<app.activities.size(); i++) {
3112 ActivityRecord r = app.activities.get(i);
3113 // First, if we find an activity that is in the process of being destroyed,
3114 // then we just aren't going to do anything for now; we want things to settle
3115 // down before we try to prune more activities.
3116 if (r.finishing || r.state == ActivityState.DESTROYING
3117 || r.state == ActivityState.DESTROYED) {
3118 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3119 return;
3120 }
3121 // Don't consider any activies that are currently not in a state where they
3122 // can be destroyed.
3123 if (r.visible || !r.stopped || !r.haveState
3124 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
3125 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
3126 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3127 continue;
3128 }
3129 if (r.task != null) {
3130 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3131 + " from " + r);
3132 if (firstTask == null) {
3133 firstTask = r.task;
3134 } else if (firstTask != r.task) {
3135 if (tasks == null) {
3136 tasks = new ArraySet<>();
3137 tasks.add(firstTask);
3138 }
3139 tasks.add(r.task);
3140 }
3141 }
3142 }
3143 if (tasks == null) {
3144 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3145 return;
3146 }
3147 // If we have activities in multiple tasks that are in a position to be destroyed,
3148 // let's iterate through the tasks and release the oldest one.
3149 final int numDisplays = mActivityDisplays.size();
3150 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3151 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3152 // Step through all stacks starting from behind, to hit the oldest things first.
3153 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3154 final ActivityStack stack = stacks.get(stackNdx);
3155 // Try to release activities in this stack; if we manage to, we are done.
3156 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3157 return;
3158 }
3159 }
3160 }
3161 }
3162
Craig Mautner8d341ef2013-03-26 09:03:27 -07003163 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003164 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003165 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003166 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003167
Craig Mautner858d8a62013-04-23 17:08:34 -07003168 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003169 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3170 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003171 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003172 final ActivityStack stack = stacks.get(stackNdx);
3173 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003174 TaskRecord task = stack.topTask();
3175 if (task != null) {
3176 mWindowManager.moveTaskToTop(task.taskId);
3177 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003178 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003179 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003180
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003181 ActivityStack stack = getStack(restoreStackId);
3182 if (stack == null) {
3183 stack = mHomeStack;
3184 }
3185 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003186 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003187 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003188 TaskRecord task = stack.topTask();
3189 if (task != null) {
3190 mWindowManager.moveTaskToTop(task.taskId);
3191 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003192 } else {
3193 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003194 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003195 }
Craig Mautner93529a42013-10-04 15:03:13 -07003196 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003197 }
3198
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003199 /**
3200 * Add background users to send boot completed events to.
3201 * @param userId The user being started in the background
3202 * @param uss The state object for the user.
3203 */
3204 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3205 mStartingBackgroundUsers.add(uss);
3206 }
3207
Craig Mautnerde4ef022013-04-07 19:01:33 -07003208 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003209 ArrayList<ActivityRecord> stops = null;
3210
3211 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003212 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3213 ActivityRecord s = mStoppingActivities.get(activityNdx);
3214 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003215 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003216 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3217 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003218 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003219 if (s.finishing) {
3220 // If this activity is finishing, it is sitting on top of
3221 // everyone else but we now know it is no longer needed...
3222 // so get rid of it. Otherwise, we need to go through the
3223 // normal flow and hide it once we determine that it is
3224 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003225 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003226 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003227 }
3228 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003229 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003230 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003231 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003232 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003233 }
3234 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003235 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003236 }
3237 }
3238
3239 return stops;
3240 }
3241
Craig Mautnercf910b02013-04-23 11:23:27 -07003242 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003243 // FIXME
3244/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3245 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003246 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003247 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003248 if (isFrontStack(stack)) {
3249 if (r == null) {
3250 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3251 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003252 final ActivityRecord pausing = stack.mPausingActivity;
3253 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003254 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003255 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003256 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003257 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003258 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003259 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003260 }
3261 }
3262 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003263 final ActivityRecord resumed = stack.mResumedActivity;
3264 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003265 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003266 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003267 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003268 if (r != null && (state == ActivityState.INITIALIZING
3269 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003270 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003271 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003272 }
3273 }
3274 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003275*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003276 }
3277
Craig Mautner27084302013-03-25 08:05:25 -07003278 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003279 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003280 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003281 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3282 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3283 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003284 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003285 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003286
Craig Mautner20e72272013-04-01 13:45:53 -07003287 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003288 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003289 }
3290
Dianne Hackborn390517b2013-05-30 15:03:32 -07003291 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3292 boolean needSep, String prefix) {
3293 if (activity != null) {
3294 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3295 if (needSep) {
3296 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003297 }
3298 pw.print(prefix);
3299 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003300 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003301 }
3302 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003303 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003304 }
3305
Craig Mautner8d341ef2013-03-26 09:03:27 -07003306 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3307 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003308 boolean printed = false;
3309 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003310 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3311 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003312 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003313 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003314 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003315 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003316 final ActivityStack stack = stacks.get(stackNdx);
3317 StringBuilder stackHeader = new StringBuilder(128);
3318 stackHeader.append(" Stack #");
3319 stackHeader.append(stack.mStackId);
3320 stackHeader.append(":");
3321 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3322 needSep, stackHeader.toString());
3323 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3324 !dumpAll, false, dumpPackage, true,
3325 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003326
Craig Mautner4a1cb222013-12-04 16:14:06 -08003327 needSep = printed;
3328 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3329 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003330 if (pr) {
3331 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003332 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003333 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003334 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3335 " mResumedActivity: ");
3336 if (pr) {
3337 printed = true;
3338 needSep = false;
3339 }
3340 if (dumpAll) {
3341 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3342 " mLastPausedActivity: ");
3343 if (pr) {
3344 printed = true;
3345 needSep = true;
3346 }
3347 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3348 needSep, " mLastNoHistoryActivity: ");
3349 }
3350 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003351 }
3352 }
3353
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003354 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3355 false, dumpPackage, true, " Activities waiting to finish:", null);
3356 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3357 false, dumpPackage, true, " Activities waiting to stop:", null);
3358 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3359 false, dumpPackage, true, " Activities waiting for another to become visible:",
3360 null);
3361 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3362 false, dumpPackage, true, " Activities waiting to sleep:", null);
3363 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3364 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003365
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003366 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003367 }
3368
Dianne Hackborn390517b2013-05-30 15:03:32 -07003369 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003370 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003371 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003372 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003373 String innerPrefix = null;
3374 String[] args = null;
3375 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003376 for (int i=list.size()-1; i>=0; i--) {
3377 final ActivityRecord r = list.get(i);
3378 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3379 continue;
3380 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003381 if (innerPrefix == null) {
3382 innerPrefix = prefix + " ";
3383 args = new String[0];
3384 }
3385 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003386 final boolean full = !brief && (complete || !r.isInHistory());
3387 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003388 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003389 needNL = false;
3390 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003391 if (header1 != null) {
3392 pw.println(header1);
3393 header1 = null;
3394 }
3395 if (header2 != null) {
3396 pw.println(header2);
3397 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003398 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003399 if (lastTask != r.task) {
3400 lastTask = r.task;
3401 pw.print(prefix);
3402 pw.print(full ? "* " : " ");
3403 pw.println(lastTask);
3404 if (full) {
3405 lastTask.dump(pw, prefix + " ");
3406 } else if (complete) {
3407 // Complete + brief == give a summary. Isn't that obvious?!?
3408 if (lastTask.intent != null) {
3409 pw.print(prefix); pw.print(" ");
3410 pw.println(lastTask.intent.toInsecureStringWithClip());
3411 }
3412 }
3413 }
3414 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3415 pw.print(" #"); pw.print(i); pw.print(": ");
3416 pw.println(r);
3417 if (full) {
3418 r.dump(pw, innerPrefix);
3419 } else if (complete) {
3420 // Complete + brief == give a summary. Isn't that obvious?!?
3421 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3422 if (r.app != null) {
3423 pw.print(innerPrefix); pw.println(r.app);
3424 }
3425 }
3426 if (client && r.app != null && r.app.thread != null) {
3427 // flush anything that is already in the PrintWriter since the thread is going
3428 // to write to the file descriptor directly
3429 pw.flush();
3430 try {
3431 TransferPipe tp = new TransferPipe();
3432 try {
3433 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3434 r.appToken, innerPrefix, args);
3435 // Short timeout, since blocking here can
3436 // deadlock with the application.
3437 tp.go(fd, 2000);
3438 } finally {
3439 tp.kill();
3440 }
3441 } catch (IOException e) {
3442 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3443 } catch (RemoteException e) {
3444 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3445 }
3446 needNL = true;
3447 }
3448 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003449 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003450 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003451
Craig Mautnerf3333272013-04-22 10:55:53 -07003452 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003453 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003454 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3455 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003456 }
3457
3458 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003459 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003460 }
3461
3462 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003463 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003464 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3465 }
3466
Craig Mautner05d29032013-05-03 13:40:13 -07003467 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003468 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3469 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3470 }
Craig Mautner05d29032013-05-03 13:40:13 -07003471 }
3472
Craig Mautner0eea92c2013-05-16 13:35:39 -07003473 void removeSleepTimeouts() {
3474 mSleepTimeout = false;
3475 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3476 }
3477
3478 final void scheduleSleepTimeout() {
3479 removeSleepTimeouts();
3480 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3481 }
3482
Craig Mautner4a1cb222013-12-04 16:14:06 -08003483 @Override
3484 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003485 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003486 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3487 }
3488
3489 @Override
3490 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003491 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003492 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3493 }
3494
3495 @Override
3496 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003497 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003498 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3499 }
3500
3501 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003502 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003503 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003504 newDisplay = mActivityDisplays.get(displayId) == null;
3505 if (newDisplay) {
3506 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003507 if (activityDisplay.mDisplay == null) {
3508 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3509 return;
3510 }
Craig Mautner4504de52013-12-20 09:06:56 -08003511 mActivityDisplays.put(displayId, activityDisplay);
3512 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003513 }
Craig Mautner4504de52013-12-20 09:06:56 -08003514 if (newDisplay) {
3515 mWindowManager.onDisplayAdded(displayId);
3516 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003517 }
3518
3519 public void handleDisplayRemovedLocked(int displayId) {
3520 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003521 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3522 if (activityDisplay != null) {
3523 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003524 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003525 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003526 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003527 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003528 }
3529 }
3530 mWindowManager.onDisplayRemoved(displayId);
3531 }
3532
3533 public void handleDisplayChangedLocked(int displayId) {
3534 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003535 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3536 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003537 // TODO: Update the bounds.
3538 }
3539 }
3540 mWindowManager.onDisplayChanged(displayId);
3541 }
3542
3543 StackInfo getStackInfo(ActivityStack stack) {
3544 StackInfo info = new StackInfo();
3545 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3546 info.displayId = Display.DEFAULT_DISPLAY;
3547 info.stackId = stack.mStackId;
3548
3549 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3550 final int numTasks = tasks.size();
3551 int[] taskIds = new int[numTasks];
3552 String[] taskNames = new String[numTasks];
3553 for (int i = 0; i < numTasks; ++i) {
3554 final TaskRecord task = tasks.get(i);
3555 taskIds[i] = task.taskId;
3556 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3557 : task.realActivity != null ? task.realActivity.flattenToString()
3558 : task.getTopActivity() != null ? task.getTopActivity().packageName
3559 : "unknown";
3560 }
3561 info.taskIds = taskIds;
3562 info.taskNames = taskNames;
3563 return info;
3564 }
3565
3566 StackInfo getStackInfoLocked(int stackId) {
3567 ActivityStack stack = getStack(stackId);
3568 if (stack != null) {
3569 return getStackInfo(stack);
3570 }
3571 return null;
3572 }
3573
3574 ArrayList<StackInfo> getAllStackInfosLocked() {
3575 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003576 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3577 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003578 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3579 list.add(getStackInfo(stacks.get(ndx)));
3580 }
3581 }
3582 return list;
3583 }
3584
Jason Monka8f569c2014-07-07 12:15:21 -04003585 void showLockTaskToast() {
Benjamin Franz43261142015-02-11 15:59:44 +00003586 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003587 }
3588
Benjamin Franz43261142015-02-11 15:59:44 +00003589 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003590 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003591 // Take out of lock task mode if necessary
3592 if (mLockTaskModeTask != null) {
3593 final Message lockTaskMsg = Message.obtain();
3594 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3595 lockTaskMsg.what = LOCK_TASK_END_MSG;
3596 mLockTaskModeTask = null;
3597 mHandler.sendMessage(lockTaskMsg);
3598 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003599 return;
3600 }
3601 if (isLockTaskModeViolation(task)) {
3602 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3603 return;
3604 }
3605 mLockTaskModeTask = task;
Craig Mautner299f9602015-01-26 09:47:33 -08003606 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003607 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003608
3609 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003610 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3611 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003612 lockTaskMsg.what = LOCK_TASK_START_MSG;
Benjamin Franz43261142015-02-11 15:59:44 +00003613 lockTaskMsg.arg2 = lockTaskModeState;
justinzhang5286d3f2014-05-12 17:06:01 -04003614 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003615 }
3616
3617 boolean isLockTaskModeViolation(TaskRecord task) {
3618 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3619 }
3620
3621 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3622 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003623 final Message lockTaskMsg = Message.obtain();
3624 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3625 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003626 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003627 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003628 }
3629 }
3630
Benjamin Franz43261142015-02-11 15:59:44 +00003631 int getLockTaskModeState() {
3632 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003633 }
3634
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003635 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003636
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003637 public ActivityStackSupervisorHandler(Looper looper) {
3638 super(looper);
3639 }
3640
Craig Mautnerf3333272013-04-22 10:55:53 -07003641 void activityIdleInternal(ActivityRecord r) {
3642 synchronized (mService) {
3643 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3644 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003645 }
3646
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003647 @Override
3648 public void handleMessage(Message msg) {
3649 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003650 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003651 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003652 if (mService.mDidDexOpt) {
3653 mService.mDidDexOpt = false;
3654 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3655 nmsg.obj = msg.obj;
3656 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3657 return;
3658 }
3659 // We don't at this point know if the activity is fullscreen,
3660 // so we need to be conservative and assume it isn't.
3661 activityIdleInternal((ActivityRecord)msg.obj);
3662 } break;
3663 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003664 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003665 activityIdleInternal((ActivityRecord)msg.obj);
3666 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003667 case RESUME_TOP_ACTIVITY_MSG: {
3668 synchronized (mService) {
3669 resumeTopActivitiesLocked();
3670 }
3671 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003672 case SLEEP_TIMEOUT_MSG: {
3673 synchronized (mService) {
3674 if (mService.isSleepingOrShuttingDown()) {
3675 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3676 mSleepTimeout = true;
3677 checkReadyForSleepLocked();
3678 }
3679 }
3680 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003681 case LAUNCH_TIMEOUT_MSG: {
3682 if (mService.mDidDexOpt) {
3683 mService.mDidDexOpt = false;
3684 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3685 return;
3686 }
3687 synchronized (mService) {
3688 if (mLaunchingActivity.isHeld()) {
3689 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3690 if (VALIDATE_WAKE_LOCK_CALLER
3691 && Binder.getCallingUid() != Process.myUid()) {
3692 throw new IllegalStateException("Calling must be system uid");
3693 }
3694 mLaunchingActivity.release();
3695 }
3696 }
3697 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003698 case HANDLE_DISPLAY_ADDED: {
3699 handleDisplayAddedLocked(msg.arg1);
3700 } break;
3701 case HANDLE_DISPLAY_CHANGED: {
3702 handleDisplayChangedLocked(msg.arg1);
3703 } break;
3704 case HANDLE_DISPLAY_REMOVED: {
3705 handleDisplayRemovedLocked(msg.arg1);
3706 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003707 case CONTAINER_CALLBACK_VISIBILITY: {
3708 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003709 final IActivityContainerCallback callback = container.mCallback;
3710 if (callback != null) {
3711 try {
3712 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3713 } catch (RemoteException e) {
3714 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003715 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003716 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003717 case LOCK_TASK_START_MSG: {
3718 // When lock task starts, we disable the status bars.
3719 try {
Jason Monk62515be2014-05-21 16:06:19 -04003720 if (mLockTaskNotify == null) {
3721 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003722 }
Jason Monk62515be2014-05-21 16:06:19 -04003723 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003724 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003725 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003726 int flags = 0;
3727 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_LOCKED) {
3728 flags = StatusBarManager.DISABLE_MASK
3729 & (~StatusBarManager.DISABLE_BACK);
3730 } else if (mLockTaskModeState ==
3731 ActivityManager.LOCK_TASK_MODE_PINNED) {
3732 flags = StatusBarManager.DISABLE_MASK
3733 & (~StatusBarManager.DISABLE_BACK)
3734 & (~StatusBarManager.DISABLE_HOME)
3735 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003736 }
3737 getStatusBarService().disable(flags, mToken,
3738 mService.mContext.getPackageName());
3739 }
3740 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003741 if (getDevicePolicyManager() != null) {
3742 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003743 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003744 }
justinzhang5286d3f2014-05-12 17:06:01 -04003745 } catch (RemoteException ex) {
3746 throw new RuntimeException(ex);
3747 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003748 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003749 case LOCK_TASK_END_MSG: {
3750 // When lock task ends, we enable the status bars.
3751 try {
Jason Monk62515be2014-05-21 16:06:19 -04003752 if (getStatusBarService() != null) {
3753 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3754 mService.mContext.getPackageName());
3755 }
3756 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003757 if (getDevicePolicyManager() != null) {
3758 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3759 msg.arg1);
3760 }
Jason Monk62515be2014-05-21 16:06:19 -04003761 if (mLockTaskNotify == null) {
3762 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3763 }
3764 mLockTaskNotify.show(false);
3765 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003766 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003767 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003768 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Benjamin Franz43261142015-02-11 15:59:44 +00003769 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_PINNED &&
3770 shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003771 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003772 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003773 new LockPatternUtils(mService.mContext)
3774 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003775 }
3776 } catch (SettingNotFoundException e) {
3777 // No setting, don't lock.
3778 }
justinzhang5286d3f2014-05-12 17:06:01 -04003779 } catch (RemoteException ex) {
3780 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003781 } finally {
3782 mLockTaskModeState = ActivityManager.LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003783 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003784 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003785 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3786 final ActivityContainer container = (ActivityContainer) msg.obj;
3787 final IActivityContainerCallback callback = container.mCallback;
3788 if (callback != null) {
3789 try {
3790 callback.onAllActivitiesComplete(container.asBinder());
3791 } catch (RemoteException e) {
3792 }
3793 }
3794 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003795 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3796 synchronized (mService) {
3797 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3798 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003799 final ActivityContainer container = (ActivityContainer) msg.obj;
3800 container.mStack.finishAllActivitiesLocked(true);
3801 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003802 }
3803 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003804 case LAUNCH_TASK_BEHIND_COMPLETE: {
3805 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003806 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003807 if (r != null) {
3808 handleLaunchTaskBehindCompleteLocked(r);
3809 }
3810 }
3811 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003812 }
3813 }
3814 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003815
Ying Wangb081a592014-04-22 15:20:16 -07003816 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003817 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003818 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003819 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003820 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003821 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003822 ActivityRecord mParentActivity = null;
3823 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003824
Craig Mautnere3a00d72014-04-16 08:31:19 -07003825 boolean mVisible = true;
3826
Craig Mautner4a1cb222013-12-04 16:14:06 -08003827 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003828 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003829
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003830 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3831 final static int CONTAINER_STATE_NO_SURFACE = 1;
3832 final static int CONTAINER_STATE_FINISHING = 2;
3833 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3834
3835 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003836 synchronized (mService) {
3837 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003838 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003839 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003840 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003842 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003843
Craig Mautnere0a38842013-12-16 16:14:02 -08003844 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003845 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3846 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003847 mActivityDisplay = activityDisplay;
3848 mStack.mDisplayId = activityDisplay.mDisplayId;
3849 mStack.mStacks = activityDisplay.mStacks;
3850
3851 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003852 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003853 }
3854
3855 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003856 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003857 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003858 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3859 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003860 return;
3861 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003862 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003863 }
3864 }
3865
3866 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003867 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003868 synchronized (mService) {
3869 if (mActivityDisplay != null) {
3870 return mActivityDisplay.mDisplayId;
3871 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003872 }
3873 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003874 }
3875
Jeff Brownca9bc702014-02-11 14:32:56 -08003876 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003877 public int getStackId() {
3878 synchronized (mService) {
3879 return mStackId;
3880 }
3881 }
3882
3883 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003884 public boolean injectEvent(InputEvent event) {
3885 final long origId = Binder.clearCallingIdentity();
3886 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003887 synchronized (mService) {
3888 if (mActivityDisplay != null) {
3889 return mInputManagerInternal.injectInputEvent(event,
3890 mActivityDisplay.mDisplayId,
3891 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3892 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003893 }
3894 return false;
3895 } finally {
3896 Binder.restoreCallingIdentity(origId);
3897 }
3898 }
3899
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003900 @Override
3901 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003902 synchronized (mService) {
3903 if (mContainerState == CONTAINER_STATE_FINISHING) {
3904 return;
3905 }
3906 mContainerState = CONTAINER_STATE_FINISHING;
3907
3908 final Message msg =
3909 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003910 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003911
3912 long origId = Binder.clearCallingIdentity();
3913 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003914 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003915 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003916 } finally {
3917 Binder.restoreCallingIdentity(origId);
3918 }
3919 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003920 }
3921
Craig Mautner60257702014-09-17 15:02:33 -07003922 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003923 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3924 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003925 if (mActivityDisplay != null) {
3926 mActivityDisplay.detachActivitiesLocked(mStack);
3927 mActivityDisplay = null;
3928 mStack.mDisplayId = -1;
3929 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003930 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003931 }
3932 }
3933
3934 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003935 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003936 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08003937 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003938 Binder.getCallingUid(), mCurrentUser, false,
3939 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003940
Craig Mautnere0a38842013-12-16 16:14:02 -08003941 // TODO: Switch to user app stacks here.
3942 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08003943 final Uri data = intent.getData();
3944 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
3945 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08003946 }
Craig Mautnerb9168362015-02-26 20:40:19 -08003947 checkEmbeddedAllowedInner(userId, intent, mimeType);
3948
3949 intent.addFlags(FORCE_NEW_TASK_FLAGS);
3950 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
3951 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003952 }
3953
3954 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003955 public final int startActivityIntentSender(IIntentSender intentSender) {
3956 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3957
3958 if (!(intentSender instanceof PendingIntentRecord)) {
3959 throw new IllegalArgumentException("Bad PendingIntent object");
3960 }
3961
Craig Mautnerb9168362015-02-26 20:40:19 -08003962 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003963 Binder.getCallingUid(), mCurrentUser, false,
3964 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003965
3966 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3967 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3968 pendingIntent.key.requestResolvedType);
3969
3970 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3971 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3972 }
3973
3974 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003975 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003976 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003977 throw new SecurityException(
3978 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3979 }
3980 }
3981
Craig Mautnerdf88d732014-01-27 09:21:32 -08003982 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003983 public IBinder asBinder() {
3984 return this;
3985 }
3986
Craig Mautner4504de52013-12-20 09:06:56 -08003987 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003988 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003989 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003990 }
3991
Craig Mautner4a1cb222013-12-04 16:14:06 -08003992 ActivityStackSupervisor getOuter() {
3993 return ActivityStackSupervisor.this;
3994 }
3995
Craig Mautnerd163e752014-06-13 17:18:47 -07003996 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003997 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003998 }
3999
4000 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004001 synchronized (mService) {
4002 if (mActivityDisplay != null) {
4003 mActivityDisplay.getBounds(outBounds);
4004 } else {
4005 outBounds.set(0, 0);
4006 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 }
4008 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004009
Craig Mautner6985bad2014-04-21 15:22:06 -07004010 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004011 void setVisible(boolean visible) {
4012 if (mVisible != visible) {
4013 mVisible = visible;
4014 if (mCallback != null) {
4015 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4016 0 /* unused */, this).sendToTarget();
4017 }
4018 }
4019 }
4020
Craig Mautner6985bad2014-04-21 15:22:06 -07004021 void setDrawn() {
4022 }
4023
Craig Mautner1b4bf852014-05-26 15:06:32 -07004024 // You can always start a new task on a regular ActivityStack.
4025 boolean isEligibleForNewTasks() {
4026 return true;
4027 }
4028
Craig Mautnerd163e752014-06-13 17:18:47 -07004029 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004030 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
4031 detachLocked();
4032 deleteActivityContainer(this);
4033 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004034 }
4035
Craig Mautner34b73df2014-01-12 21:11:08 -08004036 @Override
4037 public String toString() {
4038 return mIdString + (mActivityDisplay == null ? "N" : "A");
4039 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004040 }
4041
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004042 private class VirtualActivityContainer extends ActivityContainer {
4043 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004044 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004045
4046 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4047 super(getNextStackId());
4048 mParentActivity = parent;
4049 mCallback = callback;
4050 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004051 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004052 }
4053
4054 @Override
4055 public void setSurface(Surface surface, int width, int height, int density) {
4056 super.setSurface(surface, width, height, density);
4057
4058 synchronized (mService) {
4059 final long origId = Binder.clearCallingIdentity();
4060 try {
4061 setSurfaceLocked(surface, width, height, density);
4062 } finally {
4063 Binder.restoreCallingIdentity(origId);
4064 }
4065 }
4066 }
4067
4068 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4069 if (mContainerState == CONTAINER_STATE_FINISHING) {
4070 return;
4071 }
4072 VirtualActivityDisplay virtualActivityDisplay =
4073 (VirtualActivityDisplay) mActivityDisplay;
4074 if (virtualActivityDisplay == null) {
4075 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004076 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004077 mActivityDisplay = virtualActivityDisplay;
4078 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4079 attachToDisplayLocked(virtualActivityDisplay);
4080 }
4081
4082 if (mSurface != null) {
4083 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004084 }
4085
Craig Mautner6985bad2014-04-21 15:22:06 -07004086 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004087 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004088 mStack.resumeTopActivityLocked(null);
4089 } else {
4090 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004091 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004092 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004093 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004094 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004095 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004096
Craig Mautnerd163e752014-06-13 17:18:47 -07004097 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004098
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004099 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
4100 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004101 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004102
Craig Mautner6985bad2014-04-21 15:22:06 -07004103 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004104 boolean isAttachedLocked() {
4105 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004106 }
4107
4108 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004109 void setDrawn() {
4110 synchronized (mService) {
4111 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004112 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004113 }
4114 }
4115
Craig Mautner1b4bf852014-05-26 15:06:32 -07004116 // Never start a new task on an ActivityView if it isn't explicitly specified.
4117 @Override
4118 boolean isEligibleForNewTasks() {
4119 return false;
4120 }
4121
Craig Mautnerd163e752014-06-13 17:18:47 -07004122 private void setSurfaceIfReadyLocked() {
4123 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004124 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4125 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4126 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4127 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4128 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004129 }
4130 }
4131
Craig Mautner4a1cb222013-12-04 16:14:06 -08004132 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4133 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004134 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004135 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004136 int mDisplayId;
4137 Display mDisplay;
4138 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004139
Craig Mautner4a1cb222013-12-04 16:14:06 -08004140 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4141 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004142 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004143
Jose Lima4b6c6692014-08-12 17:41:12 -07004144 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004145
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004146 ActivityDisplay() {
4147 }
Craig Mautner4504de52013-12-20 09:06:56 -08004148
Craig Mautner1a70a162014-09-13 12:09:31 -07004149 // After instantiation, check that mDisplay is not null before using this. The alternative
4150 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004151 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004152 final Display display = mDisplayManager.getDisplay(displayId);
4153 if (display == null) {
4154 return;
4155 }
4156 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004157 }
4158
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004159 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004160 mDisplay = display;
4161 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004162 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004163 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004164
4165 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004166 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4167 + mDisplayId);
4168 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004169 }
4170
Craig Mautnere0a38842013-12-16 16:14:02 -08004171 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004172 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004173 + " from displayId=" + mDisplayId);
4174 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004175 }
4176
4177 void getBounds(Point bounds) {
4178 mDisplay.getDisplayInfo(mDisplayInfo);
4179 bounds.x = mDisplayInfo.appWidth;
4180 bounds.y = mDisplayInfo.appHeight;
4181 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004182
Jose Lima4b6c6692014-08-12 17:41:12 -07004183 void setVisibleBehindActivity(ActivityRecord r) {
4184 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004185 }
4186
Jose Lima4b6c6692014-08-12 17:41:12 -07004187 boolean hasVisibleBehindActivity() {
4188 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004189 }
4190
Craig Mautner34b73df2014-01-12 21:11:08 -08004191 @Override
4192 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004193 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4194 }
4195 }
4196
4197 class VirtualActivityDisplay extends ActivityDisplay {
4198 VirtualDisplay mVirtualDisplay;
4199
Craig Mautner6985bad2014-04-21 15:22:06 -07004200 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004201 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004202 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4203 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4204 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4205 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004206
4207 init(mVirtualDisplay.getDisplay());
4208
4209 mWindowManager.handleDisplayAdded(mDisplayId);
4210 }
4211
4212 void setSurface(Surface surface) {
4213 if (mVirtualDisplay != null) {
4214 mVirtualDisplay.setSurface(surface);
4215 }
4216 }
4217
4218 @Override
4219 void detachActivitiesLocked(ActivityStack stack) {
4220 super.detachActivitiesLocked(stack);
4221 if (mVirtualDisplay != null) {
4222 mVirtualDisplay.release();
4223 mVirtualDisplay = null;
4224 }
4225 }
4226
4227 @Override
4228 public String toString() {
4229 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004230 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004231 }
Jose Lima58e66d62014-05-27 20:00:27 -07004232
4233 private boolean isLeanbackOnlyDevice() {
4234 boolean onLeanbackOnly = false;
4235 try {
4236 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4237 PackageManager.FEATURE_LEANBACK_ONLY);
4238 } catch (RemoteException e) {
4239 // noop
4240 }
4241
4242 return onLeanbackOnly;
4243 }
Craig Mautner27084302013-03-25 08:05:25 -07004244}