blob: c4dc591fcbbc26f0e6977bff8d07a9b4f5f470d0 [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 Mautner23ac33b2013-04-01 16:26:35 -070026import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070027import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Wale Ogunwale7de05352014-12-12 15:21:33 -080029import static com.android.server.am.ActivityManagerService.DEBUG_RECENTS;
Craig Mautner6170f732013-04-02 13:05:23 -070030import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070031import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070032import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070033import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
34import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070035import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070036import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
37import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
38import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070039
Craig Mautner2420ead2013-04-01 17:13:20 -070040import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080042import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityOptions;
44import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080045import android.app.IActivityContainer;
46import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070047import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070048import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070050import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070051import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070053import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040054import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040055import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070056import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070057import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070061import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.content.pm.ApplicationInfo;
63import android.content.pm.PackageManager;
64import android.content.pm.ResolveInfo;
65import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080066import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080067import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080068import android.hardware.display.DisplayManager;
69import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080070import android.hardware.display.DisplayManagerGlobal;
71import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080072import android.hardware.input.InputManager;
73import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080074import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070075import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070076import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070077import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070078import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070079import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070080import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070081import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070082import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070083import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070084import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070085import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040086import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070087import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070088import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070089import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070090import android.os.WorkSource;
Jason Monk62515be2014-05-21 16:06:19 -040091import android.provider.Settings;
92import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070093import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070094import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070095import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070096import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080097import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070098
Craig Mautner4a1cb222013-12-04 16:14:06 -080099import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800100import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800101import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800102import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800103import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700105import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800106import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700107import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400108import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400109import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800110import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700111import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700112import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113
justinzhang5286d3f2014-05-12 17:06:01 -0400114
Craig Mautner8d341ef2013-03-26 09:03:27 -0700115import java.io.FileDescriptor;
116import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700117import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700119import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700120
Craig Mautner4a1cb222013-12-04 16:14:06 -0800121public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800122 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
123
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800124 static final boolean DEBUG = DEBUG_ALL || false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700125 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
126 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700127 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700128 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700129 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700130 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
131 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner30bd3bb2014-11-13 00:55:53 +0000132 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700133 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700134
Craig Mautner2219a1b2013-03-25 09:44:30 -0700135 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700136
Craig Mautnerf3333272013-04-22 10:55:53 -0700137 /** How long we wait until giving up on the last activity telling us it is idle. */
138 static final int IDLE_TIMEOUT = 10*1000;
139
Craig Mautner0eea92c2013-05-16 13:35:39 -0700140 /** How long we can hold the sleep wake lock before giving up. */
141 static final int SLEEP_TIMEOUT = 5*1000;
142
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700143 // How long we can hold the launch wake lock before giving up.
144 static final int LAUNCH_TIMEOUT = 10*1000;
145
Craig Mautner05d29032013-05-03 13:40:13 -0700146 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
147 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
148 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700149 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700150 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800151 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
152 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
153 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700154 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400155 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
156 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700157 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700158 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700159 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160
Craig Mautner4504de52013-12-20 09:06:56 -0800161 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700162
Jason Monk62515be2014-05-21 16:06:19 -0400163 private static final String LOCK_TASK_TAG = "Lock-to-App";
164
justinzhang5286d3f2014-05-12 17:06:01 -0400165 /** Status Bar Service **/
166 private IBinder mToken = new Binder();
167 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400168 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400169
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700170 // For debugging to make sure the caller when acquiring/releasing our
171 // wake lock is the system process.
172 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700173
Craig Mautner27084302013-03-25 08:05:25 -0700174 final ActivityManagerService mService;
175
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800176 private final RecentTasks mRecentTasks;
177
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700178 final ActivityStackSupervisorHandler mHandler;
179
180 /** Short cut */
181 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800182 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700183
Craig Mautner8d341ef2013-03-26 09:03:27 -0700184 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700185 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700186
187 /** Task identifier that activities are currently being started in. Incremented each time a
188 * new task is created. */
189 private int mCurTaskId = 0;
190
Craig Mautner2420ead2013-04-01 17:13:20 -0700191 /** The current user */
192 private int mCurrentUser;
193
Craig Mautnere0a38842013-12-16 16:14:02 -0800194 /** The stack containing the launcher app. Assumed to always be attached to
195 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700196 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700197
Craig Mautnere0a38842013-12-16 16:14:02 -0800198 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700199 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700200
Craig Mautner4a1cb222013-12-04 16:14:06 -0800201 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
202 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800203 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800204 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700205
206 /** List of activities that are waiting for a new activity to become visible before completing
207 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800208 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700209
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700210 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800211 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700212
213 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800214 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700215
Craig Mautnerde4ef022013-04-07 19:01:33 -0700216 /** List of activities that are ready to be stopped, but waiting for the next activity to
217 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800218 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700219
Craig Mautnerf3333272013-04-22 10:55:53 -0700220 /** List of activities that are ready to be finished, but waiting for the previous activity to
221 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800222 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700223
Craig Mautner0eea92c2013-05-16 13:35:39 -0700224 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800225 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700226
Craig Mautnerf3333272013-04-22 10:55:53 -0700227 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800228 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700229
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700230 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800231 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700232
Craig Mautnerde4ef022013-04-07 19:01:33 -0700233 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
234 * is being brought in front of us. */
235 boolean mUserLeaving = false;
236
Craig Mautner0eea92c2013-05-16 13:35:39 -0700237 /** Set when we have taken too long waiting to go to sleep. */
238 boolean mSleepTimeout = false;
239
Jose Lima58e66d62014-05-27 20:00:27 -0700240 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
241 * setWindowManager is called. **/
242 private boolean mLeanbackOnlyDevice;
243
Craig Mautner0eea92c2013-05-16 13:35:39 -0700244 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700245 * We don't want to allow the device to go to sleep while in the process
246 * of launching an activity. This is primarily to allow alarm intent
247 * receivers to launch an activity and get that to run before the device
248 * goes back to sleep.
249 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700250 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700251
252 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700253 * Set when the system is going to sleep, until we have
254 * successfully paused the current activity and released our wake lock.
255 * At that point the system is allowed to actually sleep.
256 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700257 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700258
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700259 /** Stack id of the front stack when user switched, indexed by userId. */
260 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700261
Craig Mautner4504de52013-12-20 09:06:56 -0800262 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800263 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700264 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800265
266 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700267 private final SparseArray<ActivityDisplay> mActivityDisplays =
268 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800269
Jeff Brownca9bc702014-02-11 14:32:56 -0800270 InputManagerInternal mInputManagerInternal;
271
Craig Mautneraea74a52014-03-08 14:23:10 -0800272 /** If non-null then the task specified remains in front and no other tasks may be started
273 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400274 TaskRecord mLockTaskModeTask;
Benjamin Franz43261142015-02-11 15:59:44 +0000275 /** Store the current lock task mode. Possible values:
276 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActicityManager#LOCK_TASK_MODE_LOCKED},
277 * {@link ActicityManager#LOCK_TASK_MODE_PINNED}
278 */
279 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400280 /**
281 * Notifies the user when entering/exiting lock-task.
282 */
283 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800284
Craig Mautneree36c772014-07-16 14:56:05 -0700285 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
286 = new ArrayList<PendingActivityLaunch>();
287
Craig Mautner42d04db2014-11-06 12:13:23 -0800288 /** Used to keep resumeTopActivityLocked() from being entered recursively */
289 boolean inResumeTopActivity;
290
Craig Mautneree36c772014-07-16 14:56:05 -0700291 /**
292 * Description of a request to start a new activity, which has been held
293 * due to app switches being disabled.
294 */
295 static class PendingActivityLaunch {
296 final ActivityRecord r;
297 final ActivityRecord sourceRecord;
298 final int startFlags;
299 final ActivityStack stack;
300
301 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
302 int _startFlags, ActivityStack _stack) {
303 r = _r;
304 sourceRecord = _sourceRecord;
305 startFlags = _startFlags;
306 stack = _stack;
307 }
308 }
309
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800310 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700311 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800312 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700313 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
314 }
315
316 /**
317 * At the time when the constructor runs, the power manager has not yet been
318 * initialized. So we initialize our wakelocks afterwards.
319 */
320 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800321 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700322 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700323 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700324 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700325 }
326
justinzhang5286d3f2014-05-12 17:06:01 -0400327 // This function returns a IStatusBarService. The value is from ServiceManager.
328 // getService and is cached.
329 private IStatusBarService getStatusBarService() {
330 synchronized (mService) {
331 if (mStatusBarService == null) {
332 mStatusBarService = IStatusBarService.Stub.asInterface(
333 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
334 if (mStatusBarService == null) {
335 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
336 }
337 }
338 return mStatusBarService;
339 }
340 }
341
Jason Monk35c62a42014-06-17 10:24:47 -0400342 private IDevicePolicyManager getDevicePolicyManager() {
343 synchronized (mService) {
344 if (mDevicePolicyManager == null) {
345 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
346 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
347 if (mDevicePolicyManager == null) {
348 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
349 }
350 }
351 return mDevicePolicyManager;
352 }
353 }
354
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700355 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356 synchronized (mService) {
357 mWindowManager = wm;
358
359 mDisplayManager =
360 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
361 mDisplayManager.registerDisplayListener(this, null);
362
363 Display[] displays = mDisplayManager.getDisplays();
364 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
365 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800366 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700367 if (activityDisplay.mDisplay == null) {
368 throw new IllegalStateException("Default Display does not exist");
369 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800370 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800371 }
372
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700373 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800374 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800375
376 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700377
378 // Initialize this here, now that we can get a valid reference to PackageManager.
379 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380 }
Craig Mautner27084302013-03-25 08:05:25 -0700381 }
382
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200383 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700384 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200385 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700386 }
387
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700388 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800389 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700390 }
391
Craig Mautnerde4ef022013-04-07 19:01:33 -0700392 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800393 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700394 }
395
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800396 /** Top of all visible stacks is/should always be equal to the focused stack.
397 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
398 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700399 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700400 if (stack == null) {
401 return false;
402 }
403
Craig Mautnerdf88d732014-01-27 09:21:32 -0800404 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
405 if (parent != null) {
406 stack = parent.task.stack;
407 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800408 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700409 }
410
Craig Mautner299f9602015-01-26 09:47:33 -0800411 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800412 moveHomeStack(toFront, reason, null);
413 }
414
415 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800416 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800417 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800418 if (topNdx <= 0) {
419 return;
420 }
421 ActivityStack topStack = stacks.get(topNdx);
422 final boolean homeInFront = topStack == mHomeStack;
423 if (homeInFront != toFront) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800424 stacks.remove(mHomeStack);
425 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800426 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
427 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700428 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800429
430 if (lastFocusedStack != null) {
431 mLastFocusedStack = lastFocusedStack;
432 }
433 mFocusedStack = stacks.get(topNdx);
434
Craig Mautnerde313752015-01-22 14:28:03 -0800435 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
436 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800437 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800438
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800439 if (mService.mBooting || !mService.mBooted) {
440 final ActivityRecord r = topRunningActivityLocked();
441 if (r != null && r.idle) {
442 checkFinishBootingLocked();
443 }
444 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700445 }
446
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700447 /** Returns true if the focus activity was adjusted to the home stack top activity. */
448 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700449 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
450 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700451 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700452 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700453
Craig Mautner84984fa2014-06-19 11:19:20 -0700454 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700455
456 final ActivityRecord top = mHomeStack.topRunningActivityLocked(null);
457 if (top == null) {
458 return false;
459 }
460 mService.setFocusedActivityLocked(top, reason);
461 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700462 }
463
Craig Mautner299f9602015-01-26 09:47:33 -0800464 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700465 if (!mService.mBooting && !mService.mBooted) {
466 // Not ready yet!
467 return false;
468 }
469
Craig Mautner84984fa2014-06-19 11:19:20 -0700470 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
471 mWindowManager.showRecentApps();
472 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700473 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700474
Craig Mautner84984fa2014-06-19 11:19:20 -0700475 if (prev != null) {
476 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
477 }
478
Craig Mautnera8a90e02013-06-28 15:24:50 -0700479 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700480 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800481 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700482 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700483 }
Craig Mautner299f9602015-01-26 09:47:33 -0800484 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700485 }
486
Craig Mautner8d341ef2013-03-26 09:03:27 -0700487 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700488 return anyTaskForIdLocked(id, true);
489 }
490
491 /**
492 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
493 * @param id Id of the task we would like returned.
494 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
495 * restore the task from recents to the active list.
496 */
497 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800498 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800499 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800500 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800501 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
502 ActivityStack stack = stacks.get(stackNdx);
503 TaskRecord task = stack.taskForIdLocked(id);
504 if (task != null) {
505 return task;
506 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700507 }
508 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800509
510 // Don't give up! Look in recents.
511 if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800512 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800513 if (task == null) {
514 if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents");
515 return null;
516 }
517
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700518 if (!restoreFromRecents) {
519 return task;
520 }
521
Wale Ogunwale7de05352014-12-12 15:21:33 -0800522 if (!restoreRecentTaskLocked(task)) {
523 if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents");
524 return null;
525 }
526 if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents");
527 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700528 }
529
Craig Mautner6170f732013-04-02 13:05:23 -0700530 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800531 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800532 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800533 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800534 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
535 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
536 if (r != null) {
537 return r;
538 }
Craig Mautner6170f732013-04-02 13:05:23 -0700539 }
540 }
541 return null;
542 }
543
Craig Mautneref73ee12014-04-23 11:45:37 -0700544 void setNextTaskId(int taskId) {
545 if (taskId > mCurTaskId) {
546 mCurTaskId = taskId;
547 }
548 }
549
Craig Mautner8d341ef2013-03-26 09:03:27 -0700550 int getNextTaskId() {
551 do {
552 mCurTaskId++;
553 if (mCurTaskId <= 0) {
554 mCurTaskId = 1;
555 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700556 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700557 return mCurTaskId;
558 }
559
Craig Mautnerde4ef022013-04-07 19:01:33 -0700560 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800561 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700562 if (stack == null) {
563 return null;
564 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700565 ActivityRecord resumedActivity = stack.mResumedActivity;
566 if (resumedActivity == null || resumedActivity.app == null) {
567 resumedActivity = stack.mPausingActivity;
568 if (resumedActivity == null || resumedActivity.app == null) {
569 resumedActivity = stack.topRunningActivityLocked(null);
570 }
571 }
572 return resumedActivity;
573 }
574
Dianne Hackbornff072722014-09-24 10:56:28 -0700575 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700576 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800577 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800578 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
579 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800580 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
581 final ActivityStack stack = stacks.get(stackNdx);
582 if (!isFrontStack(stack)) {
583 continue;
584 }
585 ActivityRecord hr = stack.topRunningActivityLocked(null);
586 if (hr != null) {
587 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
588 && processName.equals(hr.processName)) {
589 try {
George Mount2c92c972014-03-20 09:38:23 -0700590 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 didSomething = true;
592 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700593 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 Slog.w(TAG, "Exception in new application when starting activity "
595 + hr.intent.getComponent().flattenToShortString(), e);
596 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700597 }
Craig Mautner20e72272013-04-01 13:45:53 -0700598 }
Craig Mautner20e72272013-04-01 13:45:53 -0700599 }
600 }
601 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700602 if (!didSomething) {
603 ensureActivitiesVisibleLocked(null, 0);
604 }
Craig Mautner20e72272013-04-01 13:45:53 -0700605 return didSomething;
606 }
607
608 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800609 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
610 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800611 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
612 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800613 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800614 continue;
615 }
616 final ActivityRecord resumedActivity = stack.mResumedActivity;
617 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800618 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
619 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800620 return false;
621 }
Craig Mautner20e72272013-04-01 13:45:53 -0700622 }
623 }
624 return true;
625 }
626
Craig Mautnerde4ef022013-04-07 19:01:33 -0700627 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800628 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
629 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800630 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
631 final ActivityStack stack = stacks.get(stackNdx);
632 if (isFrontStack(stack)) {
633 final ActivityRecord r = stack.mResumedActivity;
634 if (r != null && r.state != ActivityState.RESUMED) {
635 return false;
636 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700637 }
638 }
639 }
640 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 if (DEBUG_STACK) Slog.d(TAG,
642 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
643 mLastFocusedStack + " to=" + mFocusedStack);
644 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700645 return true;
646 }
647
648 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800649 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
650 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800651 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
652 final ActivityStack stack = stacks.get(stackNdx);
653 final ActivityRecord r = stack.mResumedActivity;
Craig Mautner8c14c152015-01-15 17:32:07 -0800654 if (r != null && (!r.nowVisible || mWaitingVisibleActivities.contains(r))) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 return false;
656 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700657 }
658 }
659 return true;
660 }
661
Craig Mautner2acc3892013-09-23 10:28:14 -0700662 /**
663 * Pause all activities in either all of the stacks or just the back stacks.
664 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700665 * @return true if any activity was paused as a result of this call.
666 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700667 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700668 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800669 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
670 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
672 final ActivityStack stack = stacks.get(stackNdx);
673 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
674 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
675 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700676 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
677 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700679 }
680 }
681 return someActivityPaused;
682 }
683
Craig Mautnerde4ef022013-04-07 19:01:33 -0700684 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700685 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800686 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
687 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800688 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
689 final ActivityStack stack = stacks.get(stackNdx);
690 final ActivityRecord r = stack.mPausingActivity;
691 if (r != null && r.state != ActivityState.PAUSED
692 && r.state != ActivityState.STOPPED
693 && r.state != ActivityState.STOPPING) {
694 if (DEBUG_STATES) {
695 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
696 pausing = false;
697 } else {
698 return false;
699 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700700 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700701 }
702 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700703 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700704 }
705
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700706 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
707 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500708 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800709 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
710 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
711 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
712 final ActivityStack stack = stacks.get(stackNdx);
713 if (stack.mResumedActivity != null &&
714 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700715 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800716 }
717 }
718 }
719 }
720
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700721 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700722 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700723 }
724
725 void sendWaitingVisibleReportLocked(ActivityRecord r) {
726 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700727 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700728 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700729 if (w.who == null) {
730 changed = true;
731 w.timeout = false;
732 if (r != null) {
733 w.who = new ComponentName(r.info.packageName, r.info.name);
734 }
735 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
736 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700737 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700738 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700739 if (changed) {
740 mService.notifyAll();
741 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700742 }
743
744 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
745 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700746 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700747 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700748 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700749 if (w.who == null) {
750 changed = true;
751 w.timeout = timeout;
752 if (r != null) {
753 w.who = new ComponentName(r.info.packageName, r.info.name);
754 }
755 w.thisTime = thisTime;
756 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700757 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700758 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700759 if (changed) {
760 mService.notifyAll();
761 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700762 }
763
Craig Mautner29219d92013-04-16 20:19:12 -0700764 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800765 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700766 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
767 if (r != null) {
768 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700769 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700770
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800772 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
774 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700775 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700776 r = stack.topRunningActivityLocked(null);
777 if (r != null) {
778 return r;
779 }
780 }
781 }
782 return null;
783 }
784
Dianne Hackborn09233282014-04-30 11:33:59 -0700785 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700786 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800787 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
788 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800789 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800791 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800792 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
793 final ActivityStack stack = stacks.get(stackNdx);
794 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
795 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700796 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700797 }
798 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700799
800 // The lists are already sorted from most recent to oldest. Just pull the most recent off
801 // each list and add it to list. Stop when all lists are empty or maxNum reached.
802 while (maxNum > 0) {
803 long mostRecentActiveTime = Long.MIN_VALUE;
804 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800805 final int numTaskLists = runningTaskLists.size();
806 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
807 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700808 if (!stackTaskList.isEmpty()) {
809 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
810 if (lastActiveTime > mostRecentActiveTime) {
811 mostRecentActiveTime = lastActiveTime;
812 selectedStackList = stackTaskList;
813 }
814 }
815 }
816 if (selectedStackList != null) {
817 list.add(selectedStackList.remove(0));
818 --maxNum;
819 } else {
820 break;
821 }
822 }
Craig Mautner20e72272013-04-01 13:45:53 -0700823 }
824
Craig Mautner23ac33b2013-04-01 16:26:35 -0700825 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700826 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700827 // Collect information about the target of the Intent.
828 ActivityInfo aInfo;
829 try {
830 ResolveInfo rInfo =
831 AppGlobals.getPackageManager().resolveIntent(
832 intent, resolvedType,
833 PackageManager.MATCH_DEFAULT_ONLY
834 | ActivityManagerService.STOCK_PM_FLAGS, userId);
835 aInfo = rInfo != null ? rInfo.activityInfo : null;
836 } catch (RemoteException e) {
837 aInfo = null;
838 }
839
840 if (aInfo != null) {
841 // Store the found target back into the intent, because now that
842 // we have it we never want to do this again. For example, if the
843 // user navigates back to this point in the history, we should
844 // always restart the exact same activity.
845 intent.setComponent(new ComponentName(
846 aInfo.applicationInfo.packageName, aInfo.name));
847
848 // Don't debug things in the system process
849 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
850 if (!aInfo.processName.equals("system")) {
851 mService.setDebugApp(aInfo.processName, true, false);
852 }
853 }
854
855 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
856 if (!aInfo.processName.equals("system")) {
857 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
858 }
859 }
860
Jeff Hao1b012d32014-08-20 10:35:34 -0700861 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700862 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700863 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700864 }
865 }
866 }
867 return aInfo;
868 }
869
Craig Mautner299f9602015-01-26 09:47:33 -0800870 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
871 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700872 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
873 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700874 }
875
Craig Mautner23ac33b2013-04-01 16:26:35 -0700876 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700877 String callingPackage, Intent intent, String resolvedType,
878 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700879 IBinder resultTo, String resultWho, int requestCode, int startFlags,
880 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700881 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700882 // Refuse possible leaked file descriptors
883 if (intent != null && intent.hasFileDescriptors()) {
884 throw new IllegalArgumentException("File descriptors passed in Intent");
885 }
886 boolean componentSpecified = intent.getComponent() != null;
887
888 // Don't modify the client's object!
889 intent = new Intent(intent);
890
891 // Collect information about the target of the Intent.
892 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700893 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700894
Craig Mautnere0a38842013-12-16 16:14:02 -0800895 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700896 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800897 if (container != null && container.mParentActivity != null &&
898 container.mParentActivity.state != ActivityState.RESUMED) {
899 // Cannot start a child activity if the parent is not resumed.
900 return ActivityManager.START_CANCELED;
901 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700902 final int realCallingPid = Binder.getCallingPid();
903 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700904 int callingPid;
905 if (callingUid >= 0) {
906 callingPid = -1;
907 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700908 callingPid = realCallingPid;
909 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700910 } else {
911 callingPid = callingUid = -1;
912 }
913
Craig Mautnere0a38842013-12-16 16:14:02 -0800914 final ActivityStack stack;
915 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800916 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800917 } else {
918 stack = container.mStack;
919 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800920 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700921 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700922 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700923
924 final long origId = Binder.clearCallingIdentity();
925
926 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800927 (aInfo.applicationInfo.privateFlags
928 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700929 // This may be a heavy-weight process! Check to see if we already
930 // have another, different heavy-weight process running.
931 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
932 if (mService.mHeavyWeightProcess != null &&
933 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
934 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700935 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700936 if (caller != null) {
937 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
938 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700939 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700940 } else {
941 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700942 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700943 + intent.toString());
944 ActivityOptions.abort(options);
945 return ActivityManager.START_PERMISSION_DENIED;
946 }
947 }
948
949 IIntentSender target = mService.getIntentSenderLocked(
950 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700951 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700952 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
953 | PendingIntent.FLAG_ONE_SHOT, null);
954
955 Intent newIntent = new Intent();
956 if (requestCode >= 0) {
957 // Caller is requesting a result.
958 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
959 }
960 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
961 new IntentSender(target));
962 if (mService.mHeavyWeightProcess.activities.size() > 0) {
963 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
964 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
965 hist.packageName);
966 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
967 hist.task.taskId);
968 }
969 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
970 aInfo.packageName);
971 newIntent.setFlags(intent.getFlags());
972 newIntent.setClassName("android",
973 HeavyWeightSwitcherActivity.class.getName());
974 intent = newIntent;
975 resolvedType = null;
976 caller = null;
977 callingUid = Binder.getCallingUid();
978 callingPid = Binder.getCallingPid();
979 componentSpecified = true;
980 try {
981 ResolveInfo rInfo =
982 AppGlobals.getPackageManager().resolveIntent(
983 intent, null,
984 PackageManager.MATCH_DEFAULT_ONLY
985 | ActivityManagerService.STOCK_PM_FLAGS, userId);
986 aInfo = rInfo != null ? rInfo.activityInfo : null;
987 aInfo = mService.getActivityInfoForUser(aInfo, userId);
988 } catch (RemoteException e) {
989 aInfo = null;
990 }
991 }
992 }
993 }
994
Dianne Hackborn91097de2014-04-04 18:02:06 -0700995 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
996 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700997 requestCode, callingPid, callingUid, callingPackage,
998 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700999 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001000
Craig Mautner85c11a82014-07-17 12:38:18 -07001001 Binder.restoreCallingIdentity(origId);
1002
Craig Mautnerde4ef022013-04-07 19:01:33 -07001003 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001004 // If the caller also wants to switch to a new configuration,
1005 // do so now. This allows a clean switch, as we are waiting
1006 // for the current activity to pause (so we will not destroy
1007 // it), and have not yet started the next activity.
1008 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1009 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001010 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001011 if (DEBUG_CONFIGURATION) Slog.v(TAG,
1012 "Updating to new configuration after starting activity.");
1013 mService.updateConfigurationLocked(config, null, false, false);
1014 }
1015
Craig Mautner23ac33b2013-04-01 16:26:35 -07001016 if (outResult != null) {
1017 outResult.result = res;
1018 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001019 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001020 do {
1021 try {
1022 mService.wait();
1023 } catch (InterruptedException e) {
1024 }
1025 } while (!outResult.timeout && outResult.who == null);
1026 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001027 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001028 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001029 outResult.timeout = false;
1030 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1031 outResult.totalTime = 0;
1032 outResult.thisTime = 0;
1033 } else {
1034 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001035 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001036 do {
1037 try {
1038 mService.wait();
1039 } catch (InterruptedException e) {
1040 }
1041 } while (!outResult.timeout && outResult.who == null);
1042 }
1043 }
1044 }
1045
1046 return res;
1047 }
1048 }
1049
1050 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1051 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1052 Bundle options, int userId) {
1053 if (intents == null) {
1054 throw new NullPointerException("intents is null");
1055 }
1056 if (resolvedTypes == null) {
1057 throw new NullPointerException("resolvedTypes is null");
1058 }
1059 if (intents.length != resolvedTypes.length) {
1060 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1061 }
1062
Craig Mautner23ac33b2013-04-01 16:26:35 -07001063
1064 int callingPid;
1065 if (callingUid >= 0) {
1066 callingPid = -1;
1067 } else if (caller == null) {
1068 callingPid = Binder.getCallingPid();
1069 callingUid = Binder.getCallingUid();
1070 } else {
1071 callingPid = callingUid = -1;
1072 }
1073 final long origId = Binder.clearCallingIdentity();
1074 try {
1075 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001076 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001077 for (int i=0; i<intents.length; i++) {
1078 Intent intent = intents[i];
1079 if (intent == null) {
1080 continue;
1081 }
1082
1083 // Refuse possible leaked file descriptors
1084 if (intent != null && intent.hasFileDescriptors()) {
1085 throw new IllegalArgumentException("File descriptors passed in Intent");
1086 }
1087
1088 boolean componentSpecified = intent.getComponent() != null;
1089
1090 // Don't modify the client's object!
1091 intent = new Intent(intent);
1092
1093 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001094 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095 // TODO: New, check if this is correct
1096 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1097
1098 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001099 (aInfo.applicationInfo.privateFlags
1100 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001101 throw new IllegalArgumentException(
1102 "FLAG_CANT_SAVE_STATE not supported here");
1103 }
1104
1105 Bundle theseOptions;
1106 if (options != null && i == intents.length-1) {
1107 theseOptions = options;
1108 } else {
1109 theseOptions = null;
1110 }
Craig Mautner6170f732013-04-02 13:05:23 -07001111 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001112 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1113 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001114 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001115 if (res < 0) {
1116 return res;
1117 }
1118
1119 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1120 }
1121 }
1122 } finally {
1123 Binder.restoreCallingIdentity(origId);
1124 }
1125
1126 return ActivityManager.START_SUCCESS;
1127 }
1128
Craig Mautner2420ead2013-04-01 17:13:20 -07001129 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001130 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001131 throws RemoteException {
1132
1133 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001134 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001135 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001136
1137 // schedule launch ticks to collect information about slow apps.
1138 r.startLaunchTickingLocked();
1139
1140 // Have the window manager re-evaluate the orientation of
1141 // the screen based on the new activity order. Note that
1142 // as a result of this, it can call back into the activity
1143 // manager with a new orientation. We don't care about that,
1144 // because the activity is not currently running so we are
1145 // just restarting it anyway.
1146 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001147 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001148 mService.mConfiguration,
1149 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1150 mService.updateConfigurationLocked(config, r, false, false);
1151 }
1152
1153 r.app = app;
1154 app.waitingToKill = null;
1155 r.launchCount++;
1156 r.lastLaunchTime = SystemClock.uptimeMillis();
1157
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001158 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001159
1160 int idx = app.activities.indexOf(r);
1161 if (idx < 0) {
1162 app.activities.add(r);
1163 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001164 mService.updateLruProcessLocked(app, true, null);
1165 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001166
1167 final ActivityStack stack = r.task.stack;
1168 try {
1169 if (app.thread == null) {
1170 throw new RemoteException();
1171 }
1172 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001173 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001174 if (andResume) {
1175 results = r.results;
1176 newIntents = r.newIntents;
1177 }
1178 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1179 + " icicle=" + r.icicle
1180 + " with results=" + results + " newIntents=" + newIntents
1181 + " andResume=" + andResume);
1182 if (andResume) {
1183 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1184 r.userId, System.identityHashCode(r),
1185 r.task.taskId, r.shortComponentName);
1186 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001187 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001188 // Home process is the root process of the task.
1189 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001190 }
1191 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1192 r.sleeping = false;
1193 r.forceNewConfig = false;
1194 mService.showAskCompatModeDialogLocked(r);
1195 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1196 String profileFile = null;
1197 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001198 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1199 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1200 mService.mProfileProc = app;
1201 profileFile = mService.mProfileFile;
1202 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001203 }
1204 }
1205 app.hasShownUi = true;
1206 app.pendingUiClean = true;
1207 if (profileFd != null) {
1208 try {
1209 profileFd = profileFd.dup();
1210 } catch (IOException e) {
1211 if (profileFd != null) {
1212 try {
1213 profileFd.close();
1214 } catch (IOException o) {
1215 }
1216 profileFd = null;
1217 }
1218 }
1219 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001220
Jeff Hao1b012d32014-08-20 10:35:34 -07001221 ProfilerInfo profilerInfo = profileFile != null
1222 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1223 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001224 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001225 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001226 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001227 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1228 r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1229 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001230
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001231 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001232 // This may be a heavy-weight process! Note that the package
1233 // manager will ensure that only activity can run in the main
1234 // process of the .apk, which is the only thing that will be
1235 // considered heavy-weight.
1236 if (app.processName.equals(app.info.packageName)) {
1237 if (mService.mHeavyWeightProcess != null
1238 && mService.mHeavyWeightProcess != app) {
1239 Slog.w(TAG, "Starting new heavy weight process " + app
1240 + " when already running "
1241 + mService.mHeavyWeightProcess);
1242 }
1243 mService.mHeavyWeightProcess = app;
1244 Message msg = mService.mHandler.obtainMessage(
1245 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1246 msg.obj = r;
1247 mService.mHandler.sendMessage(msg);
1248 }
1249 }
1250
1251 } catch (RemoteException e) {
1252 if (r.launchFailed) {
1253 // This is the second time we failed -- finish activity
1254 // and give up.
1255 Slog.e(TAG, "Second failure launching "
1256 + r.intent.getComponent().flattenToShortString()
1257 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001258 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001259 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1260 "2nd-crash", false);
1261 return false;
1262 }
1263
1264 // This is the first time we failed -- restart process and
1265 // retry.
1266 app.activities.remove(r);
1267 throw e;
1268 }
1269
1270 r.launchFailed = false;
1271 if (stack.updateLRUListLocked(r)) {
1272 Slog.w(TAG, "Activity " + r
1273 + " being launched, but already in LRU list");
1274 }
1275
1276 if (andResume) {
1277 // As part of the process of launching, ActivityThread also performs
1278 // a resume.
1279 stack.minimalResumeActivityLocked(r);
1280 } else {
1281 // This activity is not starting in the resumed state... which
1282 // should look like we asked it to pause+stop (but remain visible),
1283 // and it has done so and reported back the current icicle and
1284 // other state.
1285 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1286 + " (starting in stopped state)");
1287 r.state = ActivityState.STOPPED;
1288 r.stopped = true;
1289 }
1290
1291 // Launch the new version setup screen if needed. We do this -after-
1292 // launching the initial activity (that is, home), so that it can have
1293 // a chance to initialize itself while in the background, making the
1294 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001295 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 mService.startSetupActivityLocked();
1297 }
1298
Dianne Hackborn465fa392014-09-14 14:21:18 -07001299 // Update any services we are bound to that might care about whether
1300 // their client may have activities.
1301 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1302
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 return true;
1304 }
1305
Craig Mautnere79d42682013-04-01 19:01:53 -07001306 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001307 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001308 // Is this activity's application already running?
1309 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001310 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001311
1312 r.task.stack.setLaunchTime(r);
1313
1314 if (app != null && app.thread != null) {
1315 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001316 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1317 || !"android".equals(r.info.packageName)) {
1318 // Don't add this if it is a platform component that is marked
1319 // to run in multiple processes, because this is actually
1320 // part of the framework so doesn't make sense to track as a
1321 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001322 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1323 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001324 }
George Mount2c92c972014-03-20 09:38:23 -07001325 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001326 return;
1327 } catch (RemoteException e) {
1328 Slog.w(TAG, "Exception when starting activity "
1329 + r.intent.getComponent().flattenToShortString(), e);
1330 }
1331
1332 // If a dead object exception was thrown -- fall through to
1333 // restart the application.
1334 }
1335
1336 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001337 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001338 }
1339
Craig Mautner6170f732013-04-02 13:05:23 -07001340 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001341 Intent intent, String resolvedType, ActivityInfo aInfo,
1342 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1343 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001344 int callingPid, int callingUid, String callingPackage,
1345 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001346 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1347 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001348 int err = ActivityManager.START_SUCCESS;
1349
1350 ProcessRecord callerApp = null;
1351 if (caller != null) {
1352 callerApp = mService.getRecordForAppLocked(caller);
1353 if (callerApp != null) {
1354 callingPid = callerApp.pid;
1355 callingUid = callerApp.info.uid;
1356 } else {
1357 Slog.w(TAG, "Unable to find app for caller " + caller
1358 + " (pid=" + callingPid + ") when starting: "
1359 + intent.toString());
1360 err = ActivityManager.START_PERMISSION_DENIED;
1361 }
1362 }
1363
1364 if (err == ActivityManager.START_SUCCESS) {
1365 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1366 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001367 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001368 + " on display " + (container == null ? (mFocusedStack == null ?
1369 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1370 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1371 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001372 }
1373
1374 ActivityRecord sourceRecord = null;
1375 ActivityRecord resultRecord = null;
1376 if (resultTo != null) {
1377 sourceRecord = isInAnyStackLocked(resultTo);
1378 if (DEBUG_RESULTS) Slog.v(
1379 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1380 if (sourceRecord != null) {
1381 if (requestCode >= 0 && !sourceRecord.finishing) {
1382 resultRecord = sourceRecord;
1383 }
1384 }
1385 }
Craig Mautner6170f732013-04-02 13:05:23 -07001386
Dianne Hackborn91097de2014-04-04 18:02:06 -07001387 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001388
Dianne Hackborn95465202014-09-15 16:21:55 -07001389 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001390 // Transfer the result target from the source activity to the new
1391 // one being started, including any failures.
1392 if (requestCode >= 0) {
1393 ActivityOptions.abort(options);
1394 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1395 }
1396 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001397 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1398 resultRecord = null;
1399 }
Craig Mautner6170f732013-04-02 13:05:23 -07001400 resultWho = sourceRecord.resultWho;
1401 requestCode = sourceRecord.requestCode;
1402 sourceRecord.resultTo = null;
1403 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001404 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001405 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001406 if (sourceRecord.launchedFromUid == callingUid) {
1407 // The new activity is being launched from the same uid as the previous
1408 // activity in the flow, and asking to forward its result back to the
1409 // previous. In this case the activity is serving as a trampoline between
1410 // the two, so we also want to update its launchedFromPackage to be the
1411 // same as the previous activity. Note that this is safe, since we know
1412 // these two packages come from the same uid; the caller could just as
1413 // well have supplied that same package name itself. This specifially
1414 // deals with the case of an intent picker/chooser being launched in the app
1415 // flow to redirect to an activity picked by the user, where we want the final
1416 // activity to consider it to have been launched by the previous app activity.
1417 callingPackage = sourceRecord.launchedFromPackage;
1418 }
Craig Mautner6170f732013-04-02 13:05:23 -07001419 }
1420
1421 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1422 // We couldn't find a class that can handle the given Intent.
1423 // That's the end of that!
1424 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1425 }
1426
1427 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1428 // We couldn't find the specific class specified in the Intent.
1429 // Also the end of the line.
1430 err = ActivityManager.START_CLASS_NOT_FOUND;
1431 }
1432
Dianne Hackborn91097de2014-04-04 18:02:06 -07001433 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1434 && sourceRecord.task.voiceSession != null) {
1435 // If this activity is being launched as part of a voice session, we need
1436 // to ensure that it is safe to do so. If the upcoming activity will also
1437 // be part of the voice session, we can only launch it if it has explicitly
1438 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001439 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001440 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1441 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001442 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1443 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001444 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1445 }
1446 } catch (RemoteException e) {
1447 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1448 }
1449 }
1450 }
1451
1452 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1453 // If the caller is starting a new voice session, just make sure the target
1454 // is actually allowing it to run this way.
1455 try {
1456 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1457 intent, resolvedType)) {
1458 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1459 }
1460 } catch (RemoteException e) {
1461 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1462 }
1463 }
1464
louis_changcd5d1982014-08-01 10:09:08 +08001465 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1466
Craig Mautner6170f732013-04-02 13:05:23 -07001467 if (err != ActivityManager.START_SUCCESS) {
1468 if (resultRecord != null) {
1469 resultStack.sendActivityResultLocked(-1,
1470 resultRecord, resultWho, requestCode,
1471 Activity.RESULT_CANCELED, null);
1472 }
Craig Mautner6170f732013-04-02 13:05:23 -07001473 ActivityOptions.abort(options);
1474 return err;
1475 }
1476
1477 final int startAnyPerm = mService.checkPermission(
1478 START_ANY_ACTIVITY, callingPid, callingUid);
1479 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1480 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1481 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1482 if (resultRecord != null) {
1483 resultStack.sendActivityResultLocked(-1,
1484 resultRecord, resultWho, requestCode,
1485 Activity.RESULT_CANCELED, null);
1486 }
Craig Mautner6170f732013-04-02 13:05:23 -07001487 String msg;
1488 if (!aInfo.exported) {
1489 msg = "Permission Denial: starting " + intent.toString()
1490 + " from " + callerApp + " (pid=" + callingPid
1491 + ", uid=" + callingUid + ")"
1492 + " not exported from uid " + aInfo.applicationInfo.uid;
1493 } else {
1494 msg = "Permission Denial: starting " + intent.toString()
1495 + " from " + callerApp + " (pid=" + callingPid
1496 + ", uid=" + callingUid + ")"
1497 + " requires " + aInfo.permission;
1498 }
1499 Slog.w(TAG, msg);
1500 throw new SecurityException(msg);
1501 }
1502
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001503 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001504 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001505
Craig Mautner6170f732013-04-02 13:05:23 -07001506 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001507 try {
1508 // The Intent we give to the watcher has the extra data
1509 // stripped off, since it can contain private information.
1510 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001511 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001512 aInfo.applicationInfo.packageName);
1513 } catch (RemoteException e) {
1514 mService.mController = null;
1515 }
Ben Gruver5e207332013-04-03 17:41:37 -07001516 }
Craig Mautner6170f732013-04-02 13:05:23 -07001517
Ben Gruver5e207332013-04-03 17:41:37 -07001518 if (abort) {
1519 if (resultRecord != null) {
1520 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001521 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001522 }
Ben Gruver5e207332013-04-03 17:41:37 -07001523 // We pretend to the caller that it was really started, but
1524 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001525 ActivityOptions.abort(options);
1526 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001527 }
1528
1529 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001530 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001531 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001532 if (outActivity != null) {
1533 outActivity[0] = r;
1534 }
1535
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001536 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001537 if (voiceSession == null && (stack.mResumedActivity == null
1538 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001539 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1540 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001541 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001542 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001543 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001544 ActivityOptions.abort(options);
1545 return ActivityManager.START_SWITCHES_CANCELED;
1546 }
1547 }
1548
1549 if (mService.mDidAppSwitch) {
1550 // This is the second allowed switch since we stopped switches,
1551 // so now just generally allow switches. Use case: user presses
1552 // home (switches disabled, switch to home, mDidAppSwitch now true);
1553 // user taps a home icon (coming from home so allowed, we hit here
1554 // and now allow anyone to switch again).
1555 mService.mAppSwitchesAllowedTime = 0;
1556 } else {
1557 mService.mDidAppSwitch = true;
1558 }
1559
Craig Mautneree36c772014-07-16 14:56:05 -07001560 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001561
Dianne Hackborn91097de2014-04-04 18:02:06 -07001562 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001563 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001564
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001565 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001566 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001567 // activity start, but we are not actually doing an activity
1568 // switch... just dismiss the keyguard now, because we
1569 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001570 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001571 }
1572 return err;
1573 }
1574
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001575 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001576 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001577
1578 // On leanback only devices we should keep all activities in the same stack.
1579 if (!mLeanbackOnlyDevice &&
1580 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001581
1582 ActivityStack stack;
1583
Wale Ogunwale7d701172015-03-11 15:36:30 -07001584 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001585 stack = task.stack;
1586 if (stack.isOnHomeDisplay()) {
1587 if (mFocusedStack != stack) {
1588 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "computeStackFocus: Setting " +
Craig Mautnere0a38842013-12-16 16:14:02 -08001589 "focused stack to r=" + r + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001590 } else {
1591 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
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())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001607 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
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()) {
1616 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
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);
1624 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "computeStackFocus: New stack r=" + r +
1625 " stackId=" + stack.mStackId);
1626 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 Mautner5f2bb4c2015-03-12 16:10:27 -07003955 public final int startActivityIntentSender(IIntentSender intentSender)
3956 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003957 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3958
3959 if (!(intentSender instanceof PendingIntentRecord)) {
3960 throw new IllegalArgumentException("Bad PendingIntent object");
3961 }
3962
Craig Mautnerb9168362015-02-26 20:40:19 -08003963 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003964 Binder.getCallingUid(), mCurrentUser, false,
3965 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003966
3967 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3968 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3969 pendingIntent.key.requestResolvedType);
3970
3971 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3972 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3973 }
3974
3975 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003976 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003977 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003978 throw new SecurityException(
3979 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3980 }
3981 }
3982
Craig Mautnerdf88d732014-01-27 09:21:32 -08003983 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003984 public IBinder asBinder() {
3985 return this;
3986 }
3987
Craig Mautner4504de52013-12-20 09:06:56 -08003988 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003989 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003990 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003991 }
3992
Craig Mautner4a1cb222013-12-04 16:14:06 -08003993 ActivityStackSupervisor getOuter() {
3994 return ActivityStackSupervisor.this;
3995 }
3996
Craig Mautnerd163e752014-06-13 17:18:47 -07003997 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003998 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003999 }
4000
4001 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004002 synchronized (mService) {
4003 if (mActivityDisplay != null) {
4004 mActivityDisplay.getBounds(outBounds);
4005 } else {
4006 outBounds.set(0, 0);
4007 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004008 }
4009 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004010
Craig Mautner6985bad2014-04-21 15:22:06 -07004011 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004012 void setVisible(boolean visible) {
4013 if (mVisible != visible) {
4014 mVisible = visible;
4015 if (mCallback != null) {
4016 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4017 0 /* unused */, this).sendToTarget();
4018 }
4019 }
4020 }
4021
Craig Mautner6985bad2014-04-21 15:22:06 -07004022 void setDrawn() {
4023 }
4024
Craig Mautner1b4bf852014-05-26 15:06:32 -07004025 // You can always start a new task on a regular ActivityStack.
4026 boolean isEligibleForNewTasks() {
4027 return true;
4028 }
4029
Craig Mautnerd163e752014-06-13 17:18:47 -07004030 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004031 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
4032 detachLocked();
4033 deleteActivityContainer(this);
4034 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004035 }
4036
Craig Mautner34b73df2014-01-12 21:11:08 -08004037 @Override
4038 public String toString() {
4039 return mIdString + (mActivityDisplay == null ? "N" : "A");
4040 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004041 }
4042
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004043 private class VirtualActivityContainer extends ActivityContainer {
4044 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004045 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004046
4047 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4048 super(getNextStackId());
4049 mParentActivity = parent;
4050 mCallback = callback;
4051 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004052 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004053 }
4054
4055 @Override
4056 public void setSurface(Surface surface, int width, int height, int density) {
4057 super.setSurface(surface, width, height, density);
4058
4059 synchronized (mService) {
4060 final long origId = Binder.clearCallingIdentity();
4061 try {
4062 setSurfaceLocked(surface, width, height, density);
4063 } finally {
4064 Binder.restoreCallingIdentity(origId);
4065 }
4066 }
4067 }
4068
4069 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4070 if (mContainerState == CONTAINER_STATE_FINISHING) {
4071 return;
4072 }
4073 VirtualActivityDisplay virtualActivityDisplay =
4074 (VirtualActivityDisplay) mActivityDisplay;
4075 if (virtualActivityDisplay == null) {
4076 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004077 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004078 mActivityDisplay = virtualActivityDisplay;
4079 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4080 attachToDisplayLocked(virtualActivityDisplay);
4081 }
4082
4083 if (mSurface != null) {
4084 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004085 }
4086
Craig Mautner6985bad2014-04-21 15:22:06 -07004087 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004088 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004089 mStack.resumeTopActivityLocked(null);
4090 } else {
4091 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004092 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004093 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004094 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004095 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004096 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004097
Craig Mautnerd163e752014-06-13 17:18:47 -07004098 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004099
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004100 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
4101 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004102 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004103
Craig Mautner6985bad2014-04-21 15:22:06 -07004104 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004105 boolean isAttachedLocked() {
4106 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004107 }
4108
4109 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004110 void setDrawn() {
4111 synchronized (mService) {
4112 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004113 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004114 }
4115 }
4116
Craig Mautner1b4bf852014-05-26 15:06:32 -07004117 // Never start a new task on an ActivityView if it isn't explicitly specified.
4118 @Override
4119 boolean isEligibleForNewTasks() {
4120 return false;
4121 }
4122
Craig Mautnerd163e752014-06-13 17:18:47 -07004123 private void setSurfaceIfReadyLocked() {
4124 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004125 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4126 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4127 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4128 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4129 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004130 }
4131 }
4132
Craig Mautner4a1cb222013-12-04 16:14:06 -08004133 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4134 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004135 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004136 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004137 int mDisplayId;
4138 Display mDisplay;
4139 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004140
Craig Mautner4a1cb222013-12-04 16:14:06 -08004141 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4142 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004143 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004144
Jose Lima4b6c6692014-08-12 17:41:12 -07004145 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004146
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004147 ActivityDisplay() {
4148 }
Craig Mautner4504de52013-12-20 09:06:56 -08004149
Craig Mautner1a70a162014-09-13 12:09:31 -07004150 // After instantiation, check that mDisplay is not null before using this. The alternative
4151 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004152 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004153 final Display display = mDisplayManager.getDisplay(displayId);
4154 if (display == null) {
4155 return;
4156 }
4157 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004158 }
4159
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004160 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004161 mDisplay = display;
4162 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004163 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004164 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004165
4166 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004167 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
4168 + mDisplayId);
4169 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004170 }
4171
Craig Mautnere0a38842013-12-16 16:14:02 -08004172 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004173 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004174 + " from displayId=" + mDisplayId);
4175 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004176 }
4177
4178 void getBounds(Point bounds) {
4179 mDisplay.getDisplayInfo(mDisplayInfo);
4180 bounds.x = mDisplayInfo.appWidth;
4181 bounds.y = mDisplayInfo.appHeight;
4182 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004183
Jose Lima4b6c6692014-08-12 17:41:12 -07004184 void setVisibleBehindActivity(ActivityRecord r) {
4185 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004186 }
4187
Jose Lima4b6c6692014-08-12 17:41:12 -07004188 boolean hasVisibleBehindActivity() {
4189 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004190 }
4191
Craig Mautner34b73df2014-01-12 21:11:08 -08004192 @Override
4193 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004194 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4195 }
4196 }
4197
4198 class VirtualActivityDisplay extends ActivityDisplay {
4199 VirtualDisplay mVirtualDisplay;
4200
Craig Mautner6985bad2014-04-21 15:22:06 -07004201 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004202 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004203 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4204 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4205 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4206 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004207
4208 init(mVirtualDisplay.getDisplay());
4209
4210 mWindowManager.handleDisplayAdded(mDisplayId);
4211 }
4212
4213 void setSurface(Surface surface) {
4214 if (mVirtualDisplay != null) {
4215 mVirtualDisplay.setSurface(surface);
4216 }
4217 }
4218
4219 @Override
4220 void detachActivitiesLocked(ActivityStack stack) {
4221 super.detachActivitiesLocked(stack);
4222 if (mVirtualDisplay != null) {
4223 mVirtualDisplay.release();
4224 mVirtualDisplay = null;
4225 }
4226 }
4227
4228 @Override
4229 public String toString() {
4230 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004231 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004232 }
Jose Lima58e66d62014-05-27 20:00:27 -07004233
4234 private boolean isLeanbackOnlyDevice() {
4235 boolean onLeanbackOnly = false;
4236 try {
4237 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4238 PackageManager.FEATURE_LEANBACK_ONLY);
4239 } catch (RemoteException e) {
4240 // noop
4241 }
4242
4243 return onLeanbackOnly;
4244 }
Craig Mautner27084302013-03-25 08:05:25 -07004245}