blob: eb1b1d85d57468931de6635cb30ae833e7cac3b7 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner15df08a2015-04-01 12:17:18 -070020import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070023import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070025import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
26import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner15df08a2015-04-01 12:17:18 -070027import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
Craig Mautner6170f732013-04-02 13:05:23 -070028import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080029import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070030import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070031import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
32import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
33import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070034import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070035import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
36import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
38import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070039
Craig Mautner2420ead2013-04-01 17:13:20 -070040import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080042import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityOptions;
44import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080045import android.app.IActivityContainer;
46import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070047import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070048import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070050import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070051import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070053import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040054import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040055import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070056import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070057import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070061import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.content.pm.ApplicationInfo;
63import android.content.pm.PackageManager;
64import android.content.pm.ResolveInfo;
65import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080066import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080067import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080068import android.hardware.display.DisplayManager;
69import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080070import android.hardware.display.DisplayManagerGlobal;
71import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080072import android.hardware.input.InputManager;
73import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080074import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070075import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070076import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070077import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070078import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070079import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070080import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070081import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070082import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070083import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070084import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070085import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040086import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070087import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070088import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070089import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070090import android.os.WorkSource;
Jason Monk62515be2014-05-21 16:06:19 -040091import android.provider.Settings;
92import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070093import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070094import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070095import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070096import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080097import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070098
Craig Mautner4a1cb222013-12-04 16:14:06 -080099import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800100import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800101import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800102import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800103import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700105import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800106import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700107import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400108import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400109import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800110import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700111import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700112import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113
justinzhang5286d3f2014-05-12 17:06:01 -0400114
Craig Mautner8d341ef2013-03-26 09:03:27 -0700115import java.io.FileDescriptor;
116import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700117import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700119import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700120
Craig Mautner4a1cb222013-12-04 16:14:06 -0800121public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800122 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700123 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
124 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700125 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
126 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
127 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
128 private static final String TAG_STACK = TAG + POSTFIX_STACK;
129 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
130 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
131 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800132
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800133 static final boolean DEBUG = DEBUG_ALL || false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700134 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
135 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700136 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700137 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700138 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700139 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
140 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautnereb0e38a2015-04-02 20:22:54 -0700141 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700142 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700143
Craig Mautner2219a1b2013-03-25 09:44:30 -0700144 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700145
Craig Mautnerf3333272013-04-22 10:55:53 -0700146 /** How long we wait until giving up on the last activity telling us it is idle. */
147 static final int IDLE_TIMEOUT = 10*1000;
148
Craig Mautner0eea92c2013-05-16 13:35:39 -0700149 /** How long we can hold the sleep wake lock before giving up. */
150 static final int SLEEP_TIMEOUT = 5*1000;
151
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700152 // How long we can hold the launch wake lock before giving up.
153 static final int LAUNCH_TIMEOUT = 10*1000;
154
Craig Mautner05d29032013-05-03 13:40:13 -0700155 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
156 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
157 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700158 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700159 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
161 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
162 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700163 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400164 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
165 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700166 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700167 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700168 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169
Craig Mautner4504de52013-12-20 09:06:56 -0800170 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700171
Jason Monk62515be2014-05-21 16:06:19 -0400172 private static final String LOCK_TASK_TAG = "Lock-to-App";
173
justinzhang5286d3f2014-05-12 17:06:01 -0400174 /** Status Bar Service **/
175 private IBinder mToken = new Binder();
176 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400177 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400178
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700179 // For debugging to make sure the caller when acquiring/releasing our
180 // wake lock is the system process.
181 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700182
Craig Mautner27084302013-03-25 08:05:25 -0700183 final ActivityManagerService mService;
184
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800185 private final RecentTasks mRecentTasks;
186
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700187 final ActivityStackSupervisorHandler mHandler;
188
189 /** Short cut */
190 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800191 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700192
Craig Mautner8d341ef2013-03-26 09:03:27 -0700193 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700194 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700195
196 /** Task identifier that activities are currently being started in. Incremented each time a
197 * new task is created. */
198 private int mCurTaskId = 0;
199
Craig Mautner2420ead2013-04-01 17:13:20 -0700200 /** The current user */
201 private int mCurrentUser;
202
Craig Mautnere0a38842013-12-16 16:14:02 -0800203 /** The stack containing the launcher app. Assumed to always be attached to
204 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700205 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700206
Craig Mautnere0a38842013-12-16 16:14:02 -0800207 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700208 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700209
Craig Mautner4a1cb222013-12-04 16:14:06 -0800210 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
211 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800212 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800213 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700214
215 /** List of activities that are waiting for a new activity to become visible before completing
216 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800217 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700218
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700219 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800220 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700221
222 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800223 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700224
Craig Mautnerde4ef022013-04-07 19:01:33 -0700225 /** List of activities that are ready to be stopped, but waiting for the next activity to
226 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800227 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700228
Craig Mautnerf3333272013-04-22 10:55:53 -0700229 /** List of activities that are ready to be finished, but waiting for the previous activity to
230 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800231 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700232
Craig Mautner0eea92c2013-05-16 13:35:39 -0700233 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800234 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700235
Craig Mautnerf3333272013-04-22 10:55:53 -0700236 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800237 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700238
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700239 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800240 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700241
Craig Mautnerde4ef022013-04-07 19:01:33 -0700242 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
243 * is being brought in front of us. */
244 boolean mUserLeaving = false;
245
Craig Mautner0eea92c2013-05-16 13:35:39 -0700246 /** Set when we have taken too long waiting to go to sleep. */
247 boolean mSleepTimeout = false;
248
Jose Lima58e66d62014-05-27 20:00:27 -0700249 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
250 * setWindowManager is called. **/
251 private boolean mLeanbackOnlyDevice;
252
Craig Mautner0eea92c2013-05-16 13:35:39 -0700253 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700254 * We don't want to allow the device to go to sleep while in the process
255 * of launching an activity. This is primarily to allow alarm intent
256 * receivers to launch an activity and get that to run before the device
257 * goes back to sleep.
258 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700259 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700260
261 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700262 * Set when the system is going to sleep, until we have
263 * successfully paused the current activity and released our wake lock.
264 * At that point the system is allowed to actually sleep.
265 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700266 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700267
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700268 /** Stack id of the front stack when user switched, indexed by userId. */
269 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700270
Craig Mautner4504de52013-12-20 09:06:56 -0800271 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800272 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700273 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800274
275 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700276 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800277
Jeff Brownca9bc702014-02-11 14:32:56 -0800278 InputManagerInternal mInputManagerInternal;
279
Craig Mautner15df08a2015-04-01 12:17:18 -0700280 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
281 * may be finished until there is only one entry left. If this is empty the system is not
282 * in lockTask mode. */
283 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000284 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700285 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
286 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000287 */
288 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400289 /**
290 * Notifies the user when entering/exiting lock-task.
291 */
292 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800293
Craig Mautner15df08a2015-04-01 12:17:18 -0700294 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700295
Craig Mautner42d04db2014-11-06 12:13:23 -0800296 /** Used to keep resumeTopActivityLocked() from being entered recursively */
297 boolean inResumeTopActivity;
298
Craig Mautneree36c772014-07-16 14:56:05 -0700299 /**
300 * Description of a request to start a new activity, which has been held
301 * due to app switches being disabled.
302 */
303 static class PendingActivityLaunch {
304 final ActivityRecord r;
305 final ActivityRecord sourceRecord;
306 final int startFlags;
307 final ActivityStack stack;
308
309 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
310 int _startFlags, ActivityStack _stack) {
311 r = _r;
312 sourceRecord = _sourceRecord;
313 startFlags = _startFlags;
314 stack = _stack;
315 }
316 }
317
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800318 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700319 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800320 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700321 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
322 }
323
324 /**
325 * At the time when the constructor runs, the power manager has not yet been
326 * initialized. So we initialize our wakelocks afterwards.
327 */
328 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800329 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700330 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700331 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700332 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700333 }
334
justinzhang5286d3f2014-05-12 17:06:01 -0400335 // This function returns a IStatusBarService. The value is from ServiceManager.
336 // getService and is cached.
337 private IStatusBarService getStatusBarService() {
338 synchronized (mService) {
339 if (mStatusBarService == null) {
340 mStatusBarService = IStatusBarService.Stub.asInterface(
341 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
342 if (mStatusBarService == null) {
343 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
344 }
345 }
346 return mStatusBarService;
347 }
348 }
349
Jason Monk35c62a42014-06-17 10:24:47 -0400350 private IDevicePolicyManager getDevicePolicyManager() {
351 synchronized (mService) {
352 if (mDevicePolicyManager == null) {
353 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
354 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
355 if (mDevicePolicyManager == null) {
356 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
357 }
358 }
359 return mDevicePolicyManager;
360 }
361 }
362
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700363 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800364 synchronized (mService) {
365 mWindowManager = wm;
366
367 mDisplayManager =
368 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
369 mDisplayManager.registerDisplayListener(this, null);
370
371 Display[] displays = mDisplayManager.getDisplays();
372 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
373 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800374 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700375 if (activityDisplay.mDisplay == null) {
376 throw new IllegalStateException("Default Display does not exist");
377 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800378 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 }
380
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700381 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800382 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800383
384 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700385
386 // Initialize this here, now that we can get a valid reference to PackageManager.
387 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800388 }
Craig Mautner27084302013-03-25 08:05:25 -0700389 }
390
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200391 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700392 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200393 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700394 }
395
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700396 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800397 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700398 }
399
Craig Mautnerde4ef022013-04-07 19:01:33 -0700400 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800401 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700402 }
403
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800404 /** Top of all visible stacks is/should always be equal to the focused stack.
405 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
406 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700407 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700408 if (stack == null) {
409 return false;
410 }
411
Craig Mautnerdf88d732014-01-27 09:21:32 -0800412 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
413 if (parent != null) {
414 stack = parent.task.stack;
415 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800416 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700417 }
418
Craig Mautner299f9602015-01-26 09:47:33 -0800419 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800420 moveHomeStack(toFront, reason, null);
421 }
422
423 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800424 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800425 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800426 if (topNdx <= 0) {
427 return;
428 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700429
430 // The home stack should either be at the top or bottom of the stack list.
431 if ((toFront && (stacks.get(topNdx) != mHomeStack))
432 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700433 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700434 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
435 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800436 stacks.remove(mHomeStack);
437 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700438 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800439
440 if (lastFocusedStack != null) {
441 mLastFocusedStack = lastFocusedStack;
442 }
443 mFocusedStack = stacks.get(topNdx);
444
Craig Mautnerde313752015-01-22 14:28:03 -0800445 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
446 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800447 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800448
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800449 if (mService.mBooting || !mService.mBooted) {
450 final ActivityRecord r = topRunningActivityLocked();
451 if (r != null && r.idle) {
452 checkFinishBootingLocked();
453 }
454 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700455 }
456
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700457 /** Returns true if the focus activity was adjusted to the home stack top activity. */
458 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700459 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
460 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700461 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700462 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700463
Craig Mautner84984fa2014-06-19 11:19:20 -0700464 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700465
466 final ActivityRecord top = mHomeStack.topRunningActivityLocked(null);
467 if (top == null) {
468 return false;
469 }
470 mService.setFocusedActivityLocked(top, reason);
471 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700472 }
473
Craig Mautner299f9602015-01-26 09:47:33 -0800474 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700475 if (!mService.mBooting && !mService.mBooted) {
476 // Not ready yet!
477 return false;
478 }
479
Craig Mautner84984fa2014-06-19 11:19:20 -0700480 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
481 mWindowManager.showRecentApps();
482 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700483 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700484
Craig Mautner84984fa2014-06-19 11:19:20 -0700485 if (prev != null) {
486 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
487 }
488
Craig Mautnera8a90e02013-06-28 15:24:50 -0700489 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700490 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800491 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700492 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700493 }
Craig Mautner299f9602015-01-26 09:47:33 -0800494 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700495 }
496
Craig Mautner8d341ef2013-03-26 09:03:27 -0700497 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700498 return anyTaskForIdLocked(id, true);
499 }
500
501 /**
502 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
503 * @param id Id of the task we would like returned.
504 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
505 * restore the task from recents to the active list.
506 */
507 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800508 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800509 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800510 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800511 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
512 ActivityStack stack = stacks.get(stackNdx);
513 TaskRecord task = stack.taskForIdLocked(id);
514 if (task != null) {
515 return task;
516 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700517 }
518 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800519
520 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700521 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800522 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800523 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700524 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800525 return null;
526 }
527
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700528 if (!restoreFromRecents) {
529 return task;
530 }
531
Wale Ogunwale7de05352014-12-12 15:21:33 -0800532 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700533 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
534 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800535 return null;
536 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700537 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800538 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700539 }
540
Craig Mautner6170f732013-04-02 13:05:23 -0700541 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800542 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800543 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800544 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
546 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
547 if (r != null) {
548 return r;
549 }
Craig Mautner6170f732013-04-02 13:05:23 -0700550 }
551 }
552 return null;
553 }
554
Craig Mautneref73ee12014-04-23 11:45:37 -0700555 void setNextTaskId(int taskId) {
556 if (taskId > mCurTaskId) {
557 mCurTaskId = taskId;
558 }
559 }
560
Craig Mautner8d341ef2013-03-26 09:03:27 -0700561 int getNextTaskId() {
562 do {
563 mCurTaskId++;
564 if (mCurTaskId <= 0) {
565 mCurTaskId = 1;
566 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700567 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700568 return mCurTaskId;
569 }
570
Craig Mautnerde4ef022013-04-07 19:01:33 -0700571 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800572 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700573 if (stack == null) {
574 return null;
575 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700576 ActivityRecord resumedActivity = stack.mResumedActivity;
577 if (resumedActivity == null || resumedActivity.app == null) {
578 resumedActivity = stack.mPausingActivity;
579 if (resumedActivity == null || resumedActivity.app == null) {
580 resumedActivity = stack.topRunningActivityLocked(null);
581 }
582 }
583 return resumedActivity;
584 }
585
Dianne Hackbornff072722014-09-24 10:56:28 -0700586 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700587 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800588 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800589 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
590 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
592 final ActivityStack stack = stacks.get(stackNdx);
593 if (!isFrontStack(stack)) {
594 continue;
595 }
596 ActivityRecord hr = stack.topRunningActivityLocked(null);
597 if (hr != null) {
598 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
599 && processName.equals(hr.processName)) {
600 try {
George Mount2c92c972014-03-20 09:38:23 -0700601 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 didSomething = true;
603 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700604 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605 Slog.w(TAG, "Exception in new application when starting activity "
606 + hr.intent.getComponent().flattenToShortString(), e);
607 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700608 }
Craig Mautner20e72272013-04-01 13:45:53 -0700609 }
Craig Mautner20e72272013-04-01 13:45:53 -0700610 }
611 }
612 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700613 if (!didSomething) {
614 ensureActivitiesVisibleLocked(null, 0);
615 }
Craig Mautner20e72272013-04-01 13:45:53 -0700616 return didSomething;
617 }
618
619 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800620 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
621 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800622 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
623 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800624 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 continue;
626 }
627 final ActivityRecord resumedActivity = stack.mResumedActivity;
628 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800629 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
630 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 return false;
632 }
Craig Mautner20e72272013-04-01 13:45:53 -0700633 }
634 }
635 return true;
636 }
637
Craig Mautnerde4ef022013-04-07 19:01:33 -0700638 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
640 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
642 final ActivityStack stack = stacks.get(stackNdx);
643 if (isFrontStack(stack)) {
644 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700645 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800646 return false;
647 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700648 }
649 }
650 }
651 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700652 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800653 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
654 mLastFocusedStack + " to=" + mFocusedStack);
655 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700656 return true;
657 }
658
659 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800660 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800661 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
662 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
664 final ActivityStack stack = stacks.get(stackNdx);
665 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800666 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700667 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800668 return false;
669 }
670 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700672 }
673 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800674 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700675 }
676
Craig Mautner2acc3892013-09-23 10:28:14 -0700677 /**
678 * Pause all activities in either all of the stacks or just the back stacks.
679 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700680 * @return true if any activity was paused as a result of this call.
681 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700682 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700683 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800684 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
685 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
687 final ActivityStack stack = stacks.get(stackNdx);
688 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
689 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
690 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700691 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
692 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800693 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700694 }
695 }
696 return someActivityPaused;
697 }
698
Craig Mautnerde4ef022013-04-07 19:01:33 -0700699 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700700 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800701 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
702 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
704 final ActivityStack stack = stacks.get(stackNdx);
705 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700706 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 if (DEBUG_STATES) {
708 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
709 pausing = false;
710 } else {
711 return false;
712 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700713 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700714 }
715 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700716 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700717 }
718
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700719 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
720 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500721 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
723 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 final ActivityStack stack = stacks.get(stackNdx);
726 if (stack.mResumedActivity != null &&
727 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700728 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800729 }
730 }
731 }
732 }
733
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700734 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700735 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700736 }
737
738 void sendWaitingVisibleReportLocked(ActivityRecord r) {
739 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700740 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700741 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700742 if (w.who == null) {
743 changed = true;
744 w.timeout = false;
745 if (r != null) {
746 w.who = new ComponentName(r.info.packageName, r.info.name);
747 }
748 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
749 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700750 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700751 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700752 if (changed) {
753 mService.notifyAll();
754 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700755 }
756
757 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
758 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700759 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700760 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700761 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700762 if (w.who == null) {
763 changed = true;
764 w.timeout = timeout;
765 if (r != null) {
766 w.who = new ComponentName(r.info.packageName, r.info.name);
767 }
768 w.thisTime = thisTime;
769 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700770 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700771 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700772 if (changed) {
773 mService.notifyAll();
774 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700775 }
776
Craig Mautner29219d92013-04-16 20:19:12 -0700777 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800778 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700779 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
780 if (r != null) {
781 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700782 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700783
Craig Mautner4a1cb222013-12-04 16:14:06 -0800784 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800785 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800786 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
787 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700788 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700789 r = stack.topRunningActivityLocked(null);
790 if (r != null) {
791 return r;
792 }
793 }
794 }
795 return null;
796 }
797
Dianne Hackborn09233282014-04-30 11:33:59 -0700798 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700799 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800800 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
801 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800802 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800803 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800804 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800805 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
806 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700807 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800808 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700809 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700810 }
811 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700812
813 // The lists are already sorted from most recent to oldest. Just pull the most recent off
814 // each list and add it to list. Stop when all lists are empty or maxNum reached.
815 while (maxNum > 0) {
816 long mostRecentActiveTime = Long.MIN_VALUE;
817 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800818 final int numTaskLists = runningTaskLists.size();
819 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
820 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700821 if (!stackTaskList.isEmpty()) {
822 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
823 if (lastActiveTime > mostRecentActiveTime) {
824 mostRecentActiveTime = lastActiveTime;
825 selectedStackList = stackTaskList;
826 }
827 }
828 }
829 if (selectedStackList != null) {
830 list.add(selectedStackList.remove(0));
831 --maxNum;
832 } else {
833 break;
834 }
835 }
Craig Mautner20e72272013-04-01 13:45:53 -0700836 }
837
Craig Mautner23ac33b2013-04-01 16:26:35 -0700838 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700839 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700840 // Collect information about the target of the Intent.
841 ActivityInfo aInfo;
842 try {
843 ResolveInfo rInfo =
844 AppGlobals.getPackageManager().resolveIntent(
845 intent, resolvedType,
846 PackageManager.MATCH_DEFAULT_ONLY
847 | ActivityManagerService.STOCK_PM_FLAGS, userId);
848 aInfo = rInfo != null ? rInfo.activityInfo : null;
849 } catch (RemoteException e) {
850 aInfo = null;
851 }
852
853 if (aInfo != null) {
854 // Store the found target back into the intent, because now that
855 // we have it we never want to do this again. For example, if the
856 // user navigates back to this point in the history, we should
857 // always restart the exact same activity.
858 intent.setComponent(new ComponentName(
859 aInfo.applicationInfo.packageName, aInfo.name));
860
861 // Don't debug things in the system process
862 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
863 if (!aInfo.processName.equals("system")) {
864 mService.setDebugApp(aInfo.processName, true, false);
865 }
866 }
867
868 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
869 if (!aInfo.processName.equals("system")) {
870 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
871 }
872 }
873
Jeff Hao1b012d32014-08-20 10:35:34 -0700874 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700875 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700876 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700877 }
878 }
879 }
880 return aInfo;
881 }
882
Craig Mautner299f9602015-01-26 09:47:33 -0800883 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
884 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700885 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
886 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700887 }
888
Craig Mautner23ac33b2013-04-01 16:26:35 -0700889 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700890 String callingPackage, Intent intent, String resolvedType,
891 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700892 IBinder resultTo, String resultWho, int requestCode, int startFlags,
893 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700894 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700895 // Refuse possible leaked file descriptors
896 if (intent != null && intent.hasFileDescriptors()) {
897 throw new IllegalArgumentException("File descriptors passed in Intent");
898 }
899 boolean componentSpecified = intent.getComponent() != null;
900
901 // Don't modify the client's object!
902 intent = new Intent(intent);
903
904 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700905 ActivityInfo aInfo =
906 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700907
Craig Mautnere0a38842013-12-16 16:14:02 -0800908 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700909 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800910 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700911 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800912 // Cannot start a child activity if the parent is not resumed.
913 return ActivityManager.START_CANCELED;
914 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700915 final int realCallingPid = Binder.getCallingPid();
916 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700917 int callingPid;
918 if (callingUid >= 0) {
919 callingPid = -1;
920 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700921 callingPid = realCallingPid;
922 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700923 } else {
924 callingPid = callingUid = -1;
925 }
926
Craig Mautnere0a38842013-12-16 16:14:02 -0800927 final ActivityStack stack;
928 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800929 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800930 } else {
931 stack = container.mStack;
932 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800933 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700934 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700935 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700936
937 final long origId = Binder.clearCallingIdentity();
938
939 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800940 (aInfo.applicationInfo.privateFlags
941 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700942 // This may be a heavy-weight process! Check to see if we already
943 // have another, different heavy-weight process running.
944 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
945 if (mService.mHeavyWeightProcess != null &&
946 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
947 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700948 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700949 if (caller != null) {
950 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
951 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700952 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700953 } else {
954 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700955 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700956 + intent.toString());
957 ActivityOptions.abort(options);
958 return ActivityManager.START_PERMISSION_DENIED;
959 }
960 }
961
962 IIntentSender target = mService.getIntentSenderLocked(
963 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700964 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700965 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
966 | PendingIntent.FLAG_ONE_SHOT, null);
967
968 Intent newIntent = new Intent();
969 if (requestCode >= 0) {
970 // Caller is requesting a result.
971 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
972 }
973 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
974 new IntentSender(target));
975 if (mService.mHeavyWeightProcess.activities.size() > 0) {
976 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
977 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
978 hist.packageName);
979 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
980 hist.task.taskId);
981 }
982 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
983 aInfo.packageName);
984 newIntent.setFlags(intent.getFlags());
985 newIntent.setClassName("android",
986 HeavyWeightSwitcherActivity.class.getName());
987 intent = newIntent;
988 resolvedType = null;
989 caller = null;
990 callingUid = Binder.getCallingUid();
991 callingPid = Binder.getCallingPid();
992 componentSpecified = true;
993 try {
994 ResolveInfo rInfo =
995 AppGlobals.getPackageManager().resolveIntent(
996 intent, null,
997 PackageManager.MATCH_DEFAULT_ONLY
998 | ActivityManagerService.STOCK_PM_FLAGS, userId);
999 aInfo = rInfo != null ? rInfo.activityInfo : null;
1000 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1001 } catch (RemoteException e) {
1002 aInfo = null;
1003 }
1004 }
1005 }
1006 }
1007
Dianne Hackborn91097de2014-04-04 18:02:06 -07001008 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1009 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001010 requestCode, callingPid, callingUid, callingPackage,
1011 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001012 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001013
Craig Mautner85c11a82014-07-17 12:38:18 -07001014 Binder.restoreCallingIdentity(origId);
1015
Craig Mautnerde4ef022013-04-07 19:01:33 -07001016 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 // If the caller also wants to switch to a new configuration,
1018 // do so now. This allows a clean switch, as we are waiting
1019 // for the current activity to pause (so we will not destroy
1020 // it), and have not yet started the next activity.
1021 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1022 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001023 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001024 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001025 "Updating to new configuration after starting activity.");
1026 mService.updateConfigurationLocked(config, null, false, false);
1027 }
1028
Craig Mautner23ac33b2013-04-01 16:26:35 -07001029 if (outResult != null) {
1030 outResult.result = res;
1031 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001032 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001033 do {
1034 try {
1035 mService.wait();
1036 } catch (InterruptedException e) {
1037 }
1038 } while (!outResult.timeout && outResult.who == null);
1039 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001040 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001041 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001042 outResult.timeout = false;
1043 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1044 outResult.totalTime = 0;
1045 outResult.thisTime = 0;
1046 } else {
1047 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001048 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001049 do {
1050 try {
1051 mService.wait();
1052 } catch (InterruptedException e) {
1053 }
1054 } while (!outResult.timeout && outResult.who == null);
1055 }
1056 }
1057 }
1058
1059 return res;
1060 }
1061 }
1062
1063 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1064 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1065 Bundle options, int userId) {
1066 if (intents == null) {
1067 throw new NullPointerException("intents is null");
1068 }
1069 if (resolvedTypes == null) {
1070 throw new NullPointerException("resolvedTypes is null");
1071 }
1072 if (intents.length != resolvedTypes.length) {
1073 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1074 }
1075
Craig Mautner23ac33b2013-04-01 16:26:35 -07001076
1077 int callingPid;
1078 if (callingUid >= 0) {
1079 callingPid = -1;
1080 } else if (caller == null) {
1081 callingPid = Binder.getCallingPid();
1082 callingUid = Binder.getCallingUid();
1083 } else {
1084 callingPid = callingUid = -1;
1085 }
1086 final long origId = Binder.clearCallingIdentity();
1087 try {
1088 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001089 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001090 for (int i=0; i<intents.length; i++) {
1091 Intent intent = intents[i];
1092 if (intent == null) {
1093 continue;
1094 }
1095
1096 // Refuse possible leaked file descriptors
1097 if (intent != null && intent.hasFileDescriptors()) {
1098 throw new IllegalArgumentException("File descriptors passed in Intent");
1099 }
1100
1101 boolean componentSpecified = intent.getComponent() != null;
1102
1103 // Don't modify the client's object!
1104 intent = new Intent(intent);
1105
1106 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001107 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001108 // TODO: New, check if this is correct
1109 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1110
1111 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001112 (aInfo.applicationInfo.privateFlags
1113 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001114 throw new IllegalArgumentException(
1115 "FLAG_CANT_SAVE_STATE not supported here");
1116 }
1117
1118 Bundle theseOptions;
1119 if (options != null && i == intents.length-1) {
1120 theseOptions = options;
1121 } else {
1122 theseOptions = null;
1123 }
Craig Mautner6170f732013-04-02 13:05:23 -07001124 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001125 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1126 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001127 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001128 if (res < 0) {
1129 return res;
1130 }
1131
1132 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1133 }
1134 }
1135 } finally {
1136 Binder.restoreCallingIdentity(origId);
1137 }
1138
1139 return ActivityManager.START_SUCCESS;
1140 }
1141
Craig Mautner2420ead2013-04-01 17:13:20 -07001142 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001143 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001144 throws RemoteException {
1145
Craig Mautner2568c3a2015-03-26 14:22:34 -07001146 if (andResume) {
1147 r.startFreezingScreenLocked(app, 0);
1148 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001149
Craig Mautner2568c3a2015-03-26 14:22:34 -07001150 // schedule launch ticks to collect information about slow apps.
1151 r.startLaunchTickingLocked();
1152 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001153
1154 // Have the window manager re-evaluate the orientation of
1155 // the screen based on the new activity order. Note that
1156 // as a result of this, it can call back into the activity
1157 // manager with a new orientation. We don't care about that,
1158 // because the activity is not currently running so we are
1159 // just restarting it anyway.
1160 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001161 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001162 mService.mConfiguration,
1163 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1164 mService.updateConfigurationLocked(config, r, false, false);
1165 }
1166
1167 r.app = app;
1168 app.waitingToKill = null;
1169 r.launchCount++;
1170 r.lastLaunchTime = SystemClock.uptimeMillis();
1171
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001172 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001173
1174 int idx = app.activities.indexOf(r);
1175 if (idx < 0) {
1176 app.activities.add(r);
1177 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001178 mService.updateLruProcessLocked(app, true, null);
1179 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001180
Craig Mautner15df08a2015-04-01 12:17:18 -07001181 final TaskRecord task = r.task;
1182 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
1183 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "lockTaskLaunchMode attribute");
1184 }
1185
1186 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001187 try {
1188 if (app.thread == null) {
1189 throw new RemoteException();
1190 }
1191 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001192 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001193 if (andResume) {
1194 results = r.results;
1195 newIntents = r.newIntents;
1196 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001197 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1198 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1199 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001200 if (andResume) {
1201 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1202 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001203 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001204 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001205 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001206 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001207 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 }
1209 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1210 r.sleeping = false;
1211 r.forceNewConfig = false;
1212 mService.showAskCompatModeDialogLocked(r);
1213 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001214 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1216 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1217 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001218 final String profileFile = mService.mProfileFile;
1219 if (profileFile != null) {
1220 ParcelFileDescriptor profileFd = mService.mProfileFd;
1221 if (profileFd != null) {
1222 try {
1223 profileFd = profileFd.dup();
1224 } catch (IOException e) {
1225 if (profileFd != null) {
1226 try {
1227 profileFd.close();
1228 } catch (IOException o) {
1229 }
1230 profileFd = null;
1231 }
1232 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001234
1235 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1236 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001237 }
1238 }
1239 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001240
Craig Mautner2568c3a2015-03-26 14:22:34 -07001241 if (andResume) {
1242 app.hasShownUi = true;
1243 app.pendingUiClean = true;
1244 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001245 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001246 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001247 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001248 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001249 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001250 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001251
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001252 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 // This may be a heavy-weight process! Note that the package
1254 // manager will ensure that only activity can run in the main
1255 // process of the .apk, which is the only thing that will be
1256 // considered heavy-weight.
1257 if (app.processName.equals(app.info.packageName)) {
1258 if (mService.mHeavyWeightProcess != null
1259 && mService.mHeavyWeightProcess != app) {
1260 Slog.w(TAG, "Starting new heavy weight process " + app
1261 + " when already running "
1262 + mService.mHeavyWeightProcess);
1263 }
1264 mService.mHeavyWeightProcess = app;
1265 Message msg = mService.mHandler.obtainMessage(
1266 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1267 msg.obj = r;
1268 mService.mHandler.sendMessage(msg);
1269 }
1270 }
1271
1272 } catch (RemoteException e) {
1273 if (r.launchFailed) {
1274 // This is the second time we failed -- finish activity
1275 // and give up.
1276 Slog.e(TAG, "Second failure launching "
1277 + r.intent.getComponent().flattenToShortString()
1278 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001279 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1281 "2nd-crash", false);
1282 return false;
1283 }
1284
1285 // This is the first time we failed -- restart process and
1286 // retry.
1287 app.activities.remove(r);
1288 throw e;
1289 }
1290
1291 r.launchFailed = false;
1292 if (stack.updateLRUListLocked(r)) {
1293 Slog.w(TAG, "Activity " + r
1294 + " being launched, but already in LRU list");
1295 }
1296
1297 if (andResume) {
1298 // As part of the process of launching, ActivityThread also performs
1299 // a resume.
1300 stack.minimalResumeActivityLocked(r);
1301 } else {
1302 // This activity is not starting in the resumed state... which
1303 // should look like we asked it to pause+stop (but remain visible),
1304 // and it has done so and reported back the current icicle and
1305 // other state.
1306 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1307 + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001308 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001309 r.stopped = true;
1310 }
1311
1312 // Launch the new version setup screen if needed. We do this -after-
1313 // launching the initial activity (that is, home), so that it can have
1314 // a chance to initialize itself while in the background, making the
1315 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001316 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001317 mService.startSetupActivityLocked();
1318 }
1319
Dianne Hackborn465fa392014-09-14 14:21:18 -07001320 // Update any services we are bound to that might care about whether
1321 // their client may have activities.
1322 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1323
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 return true;
1325 }
1326
Craig Mautnere79d42682013-04-01 19:01:53 -07001327 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001328 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001329 // Is this activity's application already running?
1330 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001331 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001332
1333 r.task.stack.setLaunchTime(r);
1334
1335 if (app != null && app.thread != null) {
1336 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001337 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1338 || !"android".equals(r.info.packageName)) {
1339 // Don't add this if it is a platform component that is marked
1340 // to run in multiple processes, because this is actually
1341 // part of the framework so doesn't make sense to track as a
1342 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001343 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1344 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001345 }
George Mount2c92c972014-03-20 09:38:23 -07001346 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001347 return;
1348 } catch (RemoteException e) {
1349 Slog.w(TAG, "Exception when starting activity "
1350 + r.intent.getComponent().flattenToShortString(), e);
1351 }
1352
1353 // If a dead object exception was thrown -- fall through to
1354 // restart the application.
1355 }
1356
1357 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001358 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001359 }
1360
Craig Mautner6170f732013-04-02 13:05:23 -07001361 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001362 Intent intent, String resolvedType, ActivityInfo aInfo,
1363 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1364 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001365 int callingPid, int callingUid, String callingPackage,
1366 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001367 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1368 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001369 int err = ActivityManager.START_SUCCESS;
1370
1371 ProcessRecord callerApp = null;
1372 if (caller != null) {
1373 callerApp = mService.getRecordForAppLocked(caller);
1374 if (callerApp != null) {
1375 callingPid = callerApp.pid;
1376 callingUid = callerApp.info.uid;
1377 } else {
1378 Slog.w(TAG, "Unable to find app for caller " + caller
1379 + " (pid=" + callingPid + ") when starting: "
1380 + intent.toString());
1381 err = ActivityManager.START_PERMISSION_DENIED;
1382 }
1383 }
1384
1385 if (err == ActivityManager.START_SUCCESS) {
1386 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1387 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001388 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001389 + " on display " + (container == null ? (mFocusedStack == null ?
1390 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1391 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1392 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001393 }
1394
1395 ActivityRecord sourceRecord = null;
1396 ActivityRecord resultRecord = null;
1397 if (resultTo != null) {
1398 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001399 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1400 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001401 if (sourceRecord != null) {
1402 if (requestCode >= 0 && !sourceRecord.finishing) {
1403 resultRecord = sourceRecord;
1404 }
1405 }
1406 }
Craig Mautner6170f732013-04-02 13:05:23 -07001407
Dianne Hackborn91097de2014-04-04 18:02:06 -07001408 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001409
Dianne Hackborn95465202014-09-15 16:21:55 -07001410 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001411 // Transfer the result target from the source activity to the new
1412 // one being started, including any failures.
1413 if (requestCode >= 0) {
1414 ActivityOptions.abort(options);
1415 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1416 }
1417 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001418 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1419 resultRecord = null;
1420 }
Craig Mautner6170f732013-04-02 13:05:23 -07001421 resultWho = sourceRecord.resultWho;
1422 requestCode = sourceRecord.requestCode;
1423 sourceRecord.resultTo = null;
1424 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001425 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001426 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001427 if (sourceRecord.launchedFromUid == callingUid) {
1428 // The new activity is being launched from the same uid as the previous
1429 // activity in the flow, and asking to forward its result back to the
1430 // previous. In this case the activity is serving as a trampoline between
1431 // the two, so we also want to update its launchedFromPackage to be the
1432 // same as the previous activity. Note that this is safe, since we know
1433 // these two packages come from the same uid; the caller could just as
1434 // well have supplied that same package name itself. This specifially
1435 // deals with the case of an intent picker/chooser being launched in the app
1436 // flow to redirect to an activity picked by the user, where we want the final
1437 // activity to consider it to have been launched by the previous app activity.
1438 callingPackage = sourceRecord.launchedFromPackage;
1439 }
Craig Mautner6170f732013-04-02 13:05:23 -07001440 }
1441
1442 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1443 // We couldn't find a class that can handle the given Intent.
1444 // That's the end of that!
1445 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1446 }
1447
1448 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1449 // We couldn't find the specific class specified in the Intent.
1450 // Also the end of the line.
1451 err = ActivityManager.START_CLASS_NOT_FOUND;
1452 }
1453
Dianne Hackborn91097de2014-04-04 18:02:06 -07001454 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1455 && sourceRecord.task.voiceSession != null) {
1456 // If this activity is being launched as part of a voice session, we need
1457 // to ensure that it is safe to do so. If the upcoming activity will also
1458 // be part of the voice session, we can only launch it if it has explicitly
1459 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001460 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001461 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1462 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001463 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1464 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001465 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1466 }
1467 } catch (RemoteException e) {
1468 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1469 }
1470 }
1471 }
1472
1473 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1474 // If the caller is starting a new voice session, just make sure the target
1475 // is actually allowing it to run this way.
1476 try {
1477 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1478 intent, resolvedType)) {
1479 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1480 }
1481 } catch (RemoteException e) {
1482 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1483 }
1484 }
1485
louis_changcd5d1982014-08-01 10:09:08 +08001486 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1487
Craig Mautner6170f732013-04-02 13:05:23 -07001488 if (err != ActivityManager.START_SUCCESS) {
1489 if (resultRecord != null) {
1490 resultStack.sendActivityResultLocked(-1,
1491 resultRecord, resultWho, requestCode,
1492 Activity.RESULT_CANCELED, null);
1493 }
Craig Mautner6170f732013-04-02 13:05:23 -07001494 ActivityOptions.abort(options);
1495 return err;
1496 }
1497
1498 final int startAnyPerm = mService.checkPermission(
1499 START_ANY_ACTIVITY, callingPid, callingUid);
1500 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1501 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1502 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1503 if (resultRecord != null) {
1504 resultStack.sendActivityResultLocked(-1,
1505 resultRecord, resultWho, requestCode,
1506 Activity.RESULT_CANCELED, null);
1507 }
Craig Mautner6170f732013-04-02 13:05:23 -07001508 String msg;
1509 if (!aInfo.exported) {
1510 msg = "Permission Denial: starting " + intent.toString()
1511 + " from " + callerApp + " (pid=" + callingPid
1512 + ", uid=" + callingUid + ")"
1513 + " not exported from uid " + aInfo.applicationInfo.uid;
1514 } else {
1515 msg = "Permission Denial: starting " + intent.toString()
1516 + " from " + callerApp + " (pid=" + callingPid
1517 + ", uid=" + callingUid + ")"
1518 + " requires " + aInfo.permission;
1519 }
1520 Slog.w(TAG, msg);
1521 throw new SecurityException(msg);
1522 }
1523
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001524 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001525 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001526
Craig Mautner6170f732013-04-02 13:05:23 -07001527 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001528 try {
1529 // The Intent we give to the watcher has the extra data
1530 // stripped off, since it can contain private information.
1531 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001532 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001533 aInfo.applicationInfo.packageName);
1534 } catch (RemoteException e) {
1535 mService.mController = null;
1536 }
Ben Gruver5e207332013-04-03 17:41:37 -07001537 }
Craig Mautner6170f732013-04-02 13:05:23 -07001538
Ben Gruver5e207332013-04-03 17:41:37 -07001539 if (abort) {
1540 if (resultRecord != null) {
1541 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001542 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001543 }
Ben Gruver5e207332013-04-03 17:41:37 -07001544 // We pretend to the caller that it was really started, but
1545 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001546 ActivityOptions.abort(options);
1547 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001548 }
1549
1550 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001551 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001552 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001553 if (outActivity != null) {
1554 outActivity[0] = r;
1555 }
1556
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001557 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001558 if (voiceSession == null && (stack.mResumedActivity == null
1559 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001560 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1561 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001562 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001563 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001564 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001565 ActivityOptions.abort(options);
1566 return ActivityManager.START_SWITCHES_CANCELED;
1567 }
1568 }
1569
1570 if (mService.mDidAppSwitch) {
1571 // This is the second allowed switch since we stopped switches,
1572 // so now just generally allow switches. Use case: user presses
1573 // home (switches disabled, switch to home, mDidAppSwitch now true);
1574 // user taps a home icon (coming from home so allowed, we hit here
1575 // and now allow anyone to switch again).
1576 mService.mAppSwitchesAllowedTime = 0;
1577 } else {
1578 mService.mDidAppSwitch = true;
1579 }
1580
Craig Mautneree36c772014-07-16 14:56:05 -07001581 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001582
Dianne Hackborn91097de2014-04-04 18:02:06 -07001583 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001584 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001585
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001586 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001587 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001588 // activity start, but we are not actually doing an activity
1589 // switch... just dismiss the keyguard now, because we
1590 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001591 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001592 }
1593 return err;
1594 }
1595
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001596 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001597 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001598
1599 // On leanback only devices we should keep all activities in the same stack.
1600 if (!mLeanbackOnlyDevice &&
1601 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001602
1603 ActivityStack stack;
1604
Wale Ogunwale7d701172015-03-11 15:36:30 -07001605 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001606 stack = task.stack;
1607 if (stack.isOnHomeDisplay()) {
1608 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001609 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1610 "computeStackFocus: Setting " + "focused stack to r=" + r
1611 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001612 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001613 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001614 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001615 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001616 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001617 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001618 }
1619
Craig Mautnere0a38842013-12-16 16:14:02 -08001620 final ActivityContainer container = r.mInitialActivityContainer;
1621 if (container != null) {
1622 // The first time put it on the desired stack, after this put on task stack.
1623 r.mInitialActivityContainer = null;
1624 return container.mStack;
1625 }
1626
Craig Mautner1b4bf852014-05-26 15:06:32 -07001627 if (mFocusedStack != mHomeStack && (!newTask ||
1628 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001629 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001630 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001631 return mFocusedStack;
1632 }
1633
Craig Mautnere0a38842013-12-16 16:14:02 -08001634 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1635 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001636 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001637 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001638 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001639 "computeStackFocus: Setting focused stack=" + stack);
1640 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001641 }
1642 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001643
Craig Mautner4a1cb222013-12-04 16:14:06 -08001644 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001645 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001646 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1647 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001648 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001649 }
1650 return mHomeStack;
1651 }
1652
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001653 boolean setFocusedStack(ActivityRecord r, String reason) {
1654 if (r == null) {
1655 // Not sure what you are trying to do, but it is not going to work...
1656 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001657 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001658 final TaskRecord task = r.task;
1659 if (task == null || task.stack == null) {
1660 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1661 return false;
1662 }
1663 task.stack.moveToFront(reason);
1664 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001665 }
1666
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001667 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001668 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001669 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001670 final Intent intent = r.intent;
1671 final int callingUid = r.launchedFromUid;
1672
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001673 // In some flows in to this function, we retrieve the task record and hold on to it
1674 // without a lock before calling back in to here... so the task at this point may
1675 // not actually be in recents. Check for that, and if it isn't in recents just
1676 // consider it invalid.
1677 if (inTask != null && !inTask.inRecents) {
1678 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1679 inTask = null;
1680 }
1681
Craig Mautnerad400af2014-08-13 16:41:36 -07001682 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001683 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1684 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001685
Craig Mautnera228ae92014-07-09 05:44:55 -07001686 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001687 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001688 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001689 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1690 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1691 "\"singleInstance\" or \"singleTask\"");
1692 launchFlags &=
1693 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1694 } else {
1695 switch (r.info.documentLaunchMode) {
1696 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1697 break;
1698 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1699 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1700 break;
1701 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1702 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1703 break;
1704 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1705 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1706 break;
1707 }
1708 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001709
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001710 final boolean launchTaskBehind = r.mLaunchTaskBehind
1711 && !launchSingleTask && !launchSingleInstance
1712 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001713
Wale Ogunwale7d701172015-03-11 15:36:30 -07001714 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1715 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001716 // For whatever reason this activity is being launched into a new
1717 // task... yet the caller has requested a result back. Well, that
1718 // is pretty messed up, so instead immediately send back a cancel
1719 // and let the new task continue launched as normal without a
1720 // dependency on its originator.
1721 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1722 r.resultTo.task.stack.sendActivityResultLocked(-1,
1723 r.resultTo, r.resultWho, r.requestCode,
1724 Activity.RESULT_CANCELED, null);
1725 r.resultTo = null;
1726 }
1727
1728 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1729 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1730 }
1731
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001732 // If we are actually going to launch in to a new task, there are some cases where
1733 // we further want to do multiple task.
1734 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1735 if (launchTaskBehind
1736 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1737 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1738 }
1739 }
1740
Craig Mautner8849a5e2013-04-02 16:41:03 -07001741 // We'll invoke onUserLeaving before onPause only if the launching
1742 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001743 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001744 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1745 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001746
1747 // If the caller has asked not to resume at this point, we make note
1748 // of this in the record so that we can skip it when trying to find
1749 // the top running activity.
1750 if (!doResume) {
1751 r.delayedResume = true;
1752 }
1753
Craig Mautnera254cd72014-05-25 16:47:39 -07001754 ActivityRecord notTop =
1755 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001756
1757 // If the onlyIfNeeded flag is set, then we can do this if the activity
1758 // being launched is the same as the one making the call... or, as
1759 // a special case, if we do not know the caller then we count the
1760 // current top activity as the caller.
1761 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1762 ActivityRecord checkedCaller = sourceRecord;
1763 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001764 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001765 }
1766 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1767 // Caller is not the same as launcher, so always needed.
1768 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1769 }
1770 }
1771
Craig Mautner8849a5e2013-04-02 16:41:03 -07001772 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001773 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001774
1775 // If the caller is not coming from another activity, but has given us an
1776 // explicit task into which they would like us to launch the new activity,
1777 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001778 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1779 final Intent baseIntent = inTask.getBaseIntent();
1780 final ActivityRecord root = inTask.getRootActivity();
1781 if (baseIntent == null) {
1782 ActivityOptions.abort(options);
1783 throw new IllegalArgumentException("Launching into task without base intent: "
1784 + inTask);
1785 }
1786
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001787 // If this task is empty, then we are adding the first activity -- it
1788 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001789 if (launchSingleInstance || launchSingleTask) {
1790 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1791 ActivityOptions.abort(options);
1792 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1793 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001794 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001795 if (root != null) {
1796 ActivityOptions.abort(options);
1797 throw new IllegalArgumentException("Caller with inTask " + inTask
1798 + " has root " + root + " but target is singleInstance/Task");
1799 }
1800 }
1801
1802 // If task is empty, then adopt the interesting intent launch flags in to the
1803 // activity being started.
1804 if (root == null) {
1805 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1806 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1807 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1808 launchFlags = (launchFlags&~flagsOfInterest)
1809 | (baseIntent.getFlags()&flagsOfInterest);
1810 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001811 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001812 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001813
Dianne Hackborn962d5352014-08-29 16:27:40 -07001814 // If the task is not empty and the caller is asking to start it as the root
1815 // of a new task, then we don't actually want to start this on the task. We
1816 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001817 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001818 addingToTask = false;
1819
1820 } else {
1821 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001822 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001823
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001824 reuseTask = inTask;
1825 } else {
1826 inTask = null;
1827 }
1828
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001829 if (inTask == null) {
1830 if (sourceRecord == null) {
1831 // This activity is not being started from another... in this
1832 // case we -always- start a new task.
1833 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1834 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1835 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1836 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1837 }
1838 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1839 // The original activity who is starting us is running as a single
1840 // instance... this new activity it is starting must go on its
1841 // own task.
1842 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1843 } else if (launchSingleInstance || launchSingleTask) {
1844 // The activity being started is a single instance... it always
1845 // gets launched into its own task.
1846 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1847 }
1848 }
1849
1850 ActivityInfo newTaskInfo = null;
1851 Intent newTaskIntent = null;
1852 ActivityStack sourceStack;
1853 if (sourceRecord != null) {
1854 if (sourceRecord.finishing) {
1855 // If the source is finishing, we can't further count it as our source. This
1856 // is because the task it is associated with may now be empty and on its way out,
1857 // so we don't want to blindly throw it in to that task. Instead we will take
1858 // the NEW_TASK flow and try to find a task for it. But save the task information
1859 // so it can be used when creating the new task.
1860 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1861 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1862 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1863 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1864 newTaskInfo = sourceRecord.info;
1865 newTaskIntent = sourceRecord.task.intent;
1866 }
1867 sourceRecord = null;
1868 sourceStack = null;
1869 } else {
1870 sourceStack = sourceRecord.task.stack;
1871 }
1872 } else {
1873 sourceStack = null;
1874 }
1875
1876 boolean movedHome = false;
1877 ActivityStack targetStack;
1878
1879 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001880 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001881
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001882 // We may want to try to place the new activity in to an existing task. We always
1883 // do this if the target activity is singleTask or singleInstance; we will also do
1884 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1885 // us to still place it in a new task: multi task, always doc mode, or being asked to
1886 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001887 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1888 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001889 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001890 // If bring to front is requested, and no result is requested and we have not
1891 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001892 // we can find a task that was started with this same
1893 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001894 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001895 // See if there is a task to bring to the front. If this is
1896 // a SINGLE_INSTANCE activity, there can be one and only one
1897 // instance of it in the history, and it is always in its own
1898 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001899 ActivityRecord intentActivity = !launchSingleInstance ?
1900 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001901 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001902 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001903 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001904 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001905 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1906 }
Craig Mautner29219d92013-04-16 20:19:12 -07001907 if (r.task == null) {
1908 r.task = intentActivity.task;
1909 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001910 if (intentActivity.task.intent == null) {
1911 // This task was started because of movement of
1912 // the activity based on affinity... now that we
1913 // are actually launching it, we can assign the
1914 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001915 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001916 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001917 targetStack = intentActivity.task.stack;
1918 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001919 // If the target task is not in the front, then we need
1920 // to bring it to the front... except... well, with
1921 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1922 // to have the same behavior as if a new instance was
1923 // being started, which means not bringing it to the front
1924 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001925 final ActivityStack focusStack = getFocusedStack();
1926 ActivityRecord curTop = (focusStack == null)
1927 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001928 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001929 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001930 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001931 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001932 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1933 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001934 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001935 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001936 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1937 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001938 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001939 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1940 options, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001941 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001942 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001943 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1944 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001945 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001946 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001947 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001948 options = null;
1949 }
1950 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001951 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001952 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001953 + " from " + intentActivity);
1954 targetStack.moveToFront("intentActivityFound");
1955 }
1956
Craig Mautner8849a5e2013-04-02 16:41:03 -07001957 // If the caller has requested that the target task be
1958 // reset, then do so.
1959 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1960 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1961 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001962 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001963 // We don't need to start a new activity, and
1964 // the client said not to do anything if that
1965 // is the case, so this is it! And for paranoia, make
1966 // sure we have correctly resumed the top activity.
1967 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001968 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001969
1970 // Make sure to notify Keyguard as well if we are not running an app
1971 // transition later.
1972 if (!movedToFront) {
1973 notifyActivityDrawnForKeyguard();
1974 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001975 } else {
1976 ActivityOptions.abort(options);
1977 }
1978 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1979 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001980 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001981 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001982 // The caller has requested to completely replace any
1983 // existing task with its new activity. Well that should
1984 // not be too hard...
1985 reuseTask = intentActivity.task;
1986 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001987 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001988 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001989 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001990 // In this situation we want to remove all activities
1991 // from the task up to the one being started. In most
1992 // cases this means we are resetting the task to its
1993 // initial state.
1994 ActivityRecord top =
1995 intentActivity.task.performClearTaskLocked(r, launchFlags);
1996 if (top != null) {
1997 if (top.frontOfTask) {
1998 // Activity aliases may mean we use different
1999 // intents for the top activity, so make sure
2000 // the task now has the identity of the new
2001 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002002 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002003 }
2004 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2005 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002006 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002007 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002008 // A special case: we need to start the activity because it is not
2009 // currently running, and the caller has asked to clear the current
2010 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002011 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002012 // Now pretend like this activity is being started by the top of its
2013 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002014 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002015 TaskRecord task = sourceRecord.task;
2016 if (task != null && task.stack == null) {
2017 // Target stack got cleared when we all activities were removed
2018 // above. Go ahead and reset it.
2019 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2020 targetStack.addTask(
2021 task, !launchTaskBehind /* toTop */, false /* moving */);
2022 }
2023
Craig Mautner8849a5e2013-04-02 16:41:03 -07002024 }
2025 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2026 // In this case the top activity on the task is the
2027 // same as the one being launched, so we take that
2028 // as a request to bring the task to the foreground.
2029 // If the top activity in the task is the root
2030 // activity, deliver this new intent to it if it
2031 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002032 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002033 && intentActivity.realActivity.equals(r.realActivity)) {
2034 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2035 intentActivity.task);
2036 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002037 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002038 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002039 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2040 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002041 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2042 // In this case we are launching the root activity
2043 // of the task, but with a different intent. We
2044 // should start a new instance on top.
2045 addingToTask = true;
2046 sourceRecord = intentActivity;
2047 }
2048 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2049 // In this case an activity is being launched in to an
2050 // existing task, without resetting that task. This
2051 // is typically the situation of launching an activity
2052 // from a notification or shortcut. We want to place
2053 // the new activity on top of the current task.
2054 addingToTask = true;
2055 sourceRecord = intentActivity;
2056 } else if (!intentActivity.task.rootWasReset) {
2057 // In this case we are launching in to an existing task
2058 // that has not yet been started from its front door.
2059 // The current task has been brought to the front.
2060 // Ideally, we'd probably like to place this new task
2061 // at the bottom of its stack, but that's a little hard
2062 // to do with the current organization of the code so
2063 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002064 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002065 }
2066 if (!addingToTask && reuseTask == null) {
2067 // We didn't do anything... but it was needed (a.k.a., client
2068 // don't use that intent!) And for paranoia, make
2069 // sure we have correctly resumed the top activity.
2070 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002071 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002072 if (!movedToFront) {
2073 // Make sure to notify Keyguard as well if we are not running an app
2074 // transition later.
2075 notifyActivityDrawnForKeyguard();
2076 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002077 } else {
2078 ActivityOptions.abort(options);
2079 }
2080 return ActivityManager.START_TASK_TO_FRONT;
2081 }
2082 }
2083 }
2084 }
2085
2086 //String uri = r.intent.toURI();
2087 //Intent intent2 = new Intent(uri);
2088 //Slog.i(TAG, "Given intent: " + r.intent);
2089 //Slog.i(TAG, "URI is: " + uri);
2090 //Slog.i(TAG, "To intent: " + intent2);
2091
2092 if (r.packageName != null) {
2093 // If the activity being launched is the same as the one currently
2094 // at the top, then we need to check if it should only be launched
2095 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002096 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002097 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002098 if (top != null && r.resultTo == null) {
2099 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2100 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002101 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002102 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002103 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2104 top.task);
2105 // For paranoia, make sure we have correctly
2106 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002107 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002108 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002109 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002110 }
2111 ActivityOptions.abort(options);
2112 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2113 // We don't need to start a new activity, and
2114 // the client said not to do anything if that
2115 // is the case, so this is it!
2116 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2117 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002118 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002119 return ActivityManager.START_DELIVERED_TO_TOP;
2120 }
2121 }
2122 }
2123 }
2124
2125 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002126 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002127 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2128 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002129 }
2130 ActivityOptions.abort(options);
2131 return ActivityManager.START_CLASS_NOT_FOUND;
2132 }
2133
2134 boolean newTask = false;
2135 boolean keepCurTransition = false;
2136
Craig Mautnerbb742462014-07-07 15:28:55 -07002137 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002138 sourceRecord.task : null;
2139
Craig Mautner8849a5e2013-04-02 16:41:03 -07002140 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002141 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002142 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002143 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002144 targetStack = computeStackFocus(r, newTask);
2145 targetStack.moveToFront("startingNewTask");
2146
Craig Mautner8849a5e2013-04-02 16:41:03 -07002147 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002148 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2149 newTaskInfo != null ? newTaskInfo : r.info,
2150 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002151 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2152 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002153 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2154 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002155 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002156 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002157 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002158 if (isLockTaskModeViolation(r.task)) {
2159 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2160 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2161 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002162 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002163 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002164 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2165 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002166 // Caller wants to appear on home activity, so before starting
2167 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002168 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002169 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002170 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002171 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002172 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002173 if (isLockTaskModeViolation(sourceTask)) {
2174 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2175 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2176 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002177 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002178 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002179 final TaskRecord topTask = targetStack.topTask();
2180 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002181 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2182 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002183 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002184 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002185 // In this case, we are adding the activity to an existing
2186 // task, but the caller has asked to clear that task if the
2187 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002188 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002189 keepCurTransition = true;
2190 if (top != null) {
2191 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002192 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002193 // For paranoia, make sure we have correctly
2194 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002195 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196 if (doResume) {
2197 targetStack.resumeTopActivityLocked(null);
2198 }
2199 ActivityOptions.abort(options);
2200 return ActivityManager.START_DELIVERED_TO_TOP;
2201 }
2202 } else if (!addingToTask &&
2203 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2204 // In this case, we are launching an activity in our own task
2205 // that may already be running somewhere in the history, and
2206 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002207 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002208 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002209 final TaskRecord task = top.task;
2210 task.moveActivityToFrontLocked(top);
2211 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002212 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002213 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002214 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002215 if (doResume) {
2216 targetStack.resumeTopActivityLocked(null);
2217 }
2218 return ActivityManager.START_DELIVERED_TO_TOP;
2219 }
2220 }
2221 // An existing activity is starting this new activity, so we want
2222 // to keep the new one in the same task as the one that is starting
2223 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002224 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002225 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002226 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002227
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002228 } else if (inTask != null) {
2229 // The calling is asking that the new activity be started in an explicit
2230 // task it has provided to us.
2231 if (isLockTaskModeViolation(inTask)) {
2232 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2233 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2234 }
2235 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002236 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002237
2238 // Check whether we should actually launch the new activity in to the task,
2239 // or just reuse the current activity on top.
2240 ActivityRecord top = inTask.getTopActivity();
2241 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2242 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2243 || launchSingleTop || launchSingleTask) {
2244 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2245 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2246 // We don't need to start a new activity, and
2247 // the client said not to do anything if that
2248 // is the case, so this is it!
2249 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2250 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002251 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002252 return ActivityManager.START_DELIVERED_TO_TOP;
2253 }
2254 }
2255
Dianne Hackborn962d5352014-08-29 16:27:40 -07002256 if (!addingToTask) {
2257 // We don't actually want to have this activity added to the task, so just
2258 // stop here but still tell the caller that we consumed the intent.
2259 ActivityOptions.abort(options);
2260 return ActivityManager.START_TASK_TO_FRONT;
2261 }
2262
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002263 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002264 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002265 + " in explicit task " + r.task);
2266
Craig Mautner8849a5e2013-04-02 16:41:03 -07002267 } else {
2268 // This not being started from an existing activity, and not part
2269 // of a new task... just put it in the top task, though these days
2270 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002271 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002272 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002273 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002274 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002275 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002276 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002277 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002278 + " in new guessed " + r.task);
2279 }
2280
2281 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002282 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002283
Craig Mautner76e2a762014-06-24 09:05:43 -07002284 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2285 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2286 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002287 if (newTask) {
2288 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2289 }
2290 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002291 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002292 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002293 if (!launchTaskBehind) {
2294 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002295 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002296 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002297 return ActivityManager.START_SUCCESS;
2298 }
2299
Craig Mautneree36c772014-07-16 14:56:05 -07002300 final void doPendingActivityLaunchesLocked(boolean doResume) {
2301 while (!mPendingActivityLaunches.isEmpty()) {
2302 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002303 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002304 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002305 }
2306 }
2307
Craig Mautner7f13ed32014-07-28 14:00:35 -07002308 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002309 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2310 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002311 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002312 mPendingActivityLaunches.remove(palNdx);
2313 }
2314 }
2315 }
2316
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002317 void setLaunchSource(int uid) {
2318 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2319 }
2320
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002321 void acquireLaunchWakelock() {
2322 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2323 throw new IllegalStateException("Calling must be system uid");
2324 }
2325 mLaunchingActivity.acquire();
2326 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2327 // To be safe, don't allow the wake lock to be held for too long.
2328 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2329 }
2330 }
2331
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002332 /**
2333 * Called when the frontmost task is idle.
2334 * @return the state of mService.mBooting before this was called.
2335 */
2336 private boolean checkFinishBootingLocked() {
2337 final boolean booting = mService.mBooting;
2338 boolean enableScreen = false;
2339 mService.mBooting = false;
2340 if (!mService.mBooted) {
2341 mService.mBooted = true;
2342 enableScreen = true;
2343 }
2344 if (booting || enableScreen) {
2345 mService.postFinishBooting(booting, enableScreen);
2346 }
2347 return booting;
2348 }
2349
Craig Mautnerf3333272013-04-22 10:55:53 -07002350 // Checked.
2351 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2352 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002353 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002354
Craig Mautnerf3333272013-04-22 10:55:53 -07002355 ArrayList<ActivityRecord> stops = null;
2356 ArrayList<ActivityRecord> finishes = null;
2357 ArrayList<UserStartedState> startingUsers = null;
2358 int NS = 0;
2359 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002360 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002361 boolean activityRemoved = false;
2362
Wale Ogunwale7d701172015-03-11 15:36:30 -07002363 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002364 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002365 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2366 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002367 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2368 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002369 if (fromTimeout) {
2370 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002371 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002372
2373 // This is a hack to semi-deal with a race condition
2374 // in the client where it can be constructed with a
2375 // newer configuration from when we asked it to launch.
2376 // We'll update with whatever configuration it now says
2377 // it used to launch.
2378 if (config != null) {
2379 r.configuration = config;
2380 }
2381
2382 // We are now idle. If someone is waiting for a thumbnail from
2383 // us, we can now deliver.
2384 r.idle = true;
2385
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002386 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002387 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002388 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002389 }
2390 }
2391
2392 if (allResumedActivitiesIdle()) {
2393 if (r != null) {
2394 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002395 }
2396
2397 if (mLaunchingActivity.isHeld()) {
2398 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2399 if (VALIDATE_WAKE_LOCK_CALLER &&
2400 Binder.getCallingUid() != Process.myUid()) {
2401 throw new IllegalStateException("Calling must be system uid");
2402 }
2403 mLaunchingActivity.release();
2404 }
2405 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002406 }
2407
2408 // Atomically retrieve all of the other things to do.
2409 stops = processStoppingActivitiesLocked(true);
2410 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002411 if ((NF = mFinishingActivities.size()) > 0) {
2412 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002413 mFinishingActivities.clear();
2414 }
2415
Craig Mautnerf3333272013-04-22 10:55:53 -07002416 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002417 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002418 mStartingUsers.clear();
2419 }
2420
Craig Mautnerf3333272013-04-22 10:55:53 -07002421 // Stop any activities that are scheduled to do so but have been
2422 // waiting for the next one to start.
2423 for (int i = 0; i < NS; i++) {
2424 r = stops.get(i);
2425 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002426 if (stack != null) {
2427 if (r.finishing) {
2428 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2429 } else {
2430 stack.stopActivityLocked(r);
2431 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002432 }
2433 }
2434
2435 // Finish any activities that are scheduled to do so but have been
2436 // waiting for the next one to start.
2437 for (int i = 0; i < NF; i++) {
2438 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002439 final ActivityStack stack = r.task.stack;
2440 if (stack != null) {
2441 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2442 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002443 }
2444
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002445 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002446 // Complete user switch
2447 if (startingUsers != null) {
2448 for (int i = 0; i < startingUsers.size(); i++) {
2449 mService.finishUserSwitch(startingUsers.get(i));
2450 }
2451 }
2452 // Complete starting up of background users
2453 if (mStartingBackgroundUsers.size() > 0) {
2454 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2455 mStartingBackgroundUsers.clear();
2456 for (int i = 0; i < startingUsers.size(); i++) {
2457 mService.finishUserBoot(startingUsers.get(i));
2458 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002459 }
2460 }
2461
2462 mService.trimApplications();
2463 //dump();
2464 //mWindowManager.dump();
2465
Craig Mautnerf3333272013-04-22 10:55:53 -07002466 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002467 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002468 }
2469
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002470 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002471 }
2472
Craig Mautner8e569572013-10-11 17:36:59 -07002473 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002474 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002475 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2476 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002477 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2478 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2479 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002480 }
Craig Mautner19091252013-10-05 00:03:53 -07002481 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002482 }
2483
2484 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002485 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2486 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002487 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2488 stacks.get(stackNdx).closeSystemDialogsLocked();
2489 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002490 }
2491 }
2492
Craig Mautner93529a42013-10-04 15:03:13 -07002493 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002494 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002495 }
2496
Craig Mautner8d341ef2013-03-26 09:03:27 -07002497 /**
2498 * @return true if some activity was finished (or would have finished if doit were true).
2499 */
2500 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2501 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002502 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2503 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002504 final int numStacks = stacks.size();
2505 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2506 final ActivityStack stack = stacks.get(stackNdx);
2507 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2508 didSomething = true;
2509 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002510 }
2511 }
2512 return didSomething;
2513 }
2514
Dianne Hackborna413dc02013-07-12 12:02:55 -07002515 void updatePreviousProcessLocked(ActivityRecord r) {
2516 // Now that this process has stopped, we may want to consider
2517 // it to be the previous app to try to keep around in case
2518 // the user wants to return to it.
2519
2520 // First, found out what is currently the foreground app, so that
2521 // we don't blow away the previous app if this activity is being
2522 // hosted by the process that is actually still the foreground.
2523 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002524 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2525 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002526 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2527 final ActivityStack stack = stacks.get(stackNdx);
2528 if (isFrontStack(stack)) {
2529 if (stack.mResumedActivity != null) {
2530 fgApp = stack.mResumedActivity.app;
2531 } else if (stack.mPausingActivity != null) {
2532 fgApp = stack.mPausingActivity.app;
2533 }
2534 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002535 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002536 }
2537 }
2538
2539 // Now set this one as the previous process, only if that really
2540 // makes sense to.
2541 if (r.app != null && fgApp != null && r.app != fgApp
2542 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002543 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002544 mService.mPreviousProcess = r.app;
2545 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2546 }
2547 }
2548
Craig Mautner05d29032013-05-03 13:40:13 -07002549 boolean resumeTopActivitiesLocked() {
2550 return resumeTopActivitiesLocked(null, null, null);
2551 }
2552
2553 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2554 Bundle targetOptions) {
2555 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002556 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002557 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002558 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002559 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002560 if (isFrontStack(targetStack)) {
2561 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2562 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002563
Craig Mautnere0a38842013-12-16 16:14:02 -08002564 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2565 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002566 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2567 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002568 if (stack == targetStack) {
2569 // Already started above.
2570 continue;
2571 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002572 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002573 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002574 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002575 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002576 }
Craig Mautner05d29032013-05-03 13:40:13 -07002577 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002578 }
2579
Todd Kennedy539db512014-12-15 09:57:55 -08002580 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002581 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2582 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002583 final int numStacks = stacks.size();
2584 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2585 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002586 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002587 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002588 }
2589 }
2590
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002591 void finishVoiceTask(IVoiceInteractionSession session) {
2592 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2593 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2594 final int numStacks = stacks.size();
2595 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2596 final ActivityStack stack = stacks.get(stackNdx);
2597 stack.finishVoiceTask(session);
2598 }
2599 }
2600 }
2601
Craig Mautner299f9602015-01-26 09:47:33 -08002602 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002603 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2604 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002605 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002606 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2607 // Caller wants the home activity moved with it. To accomplish this,
2608 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002609 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002610 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002611 if (task.stack == null) {
2612 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2613 + task + " to front. Stack is null");
2614 return;
2615 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002616 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002617 if (DEBUG_STACK) Slog.d(TAG_STACK,
2618 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002619 }
2620
Craig Mautner967212c2013-04-13 21:10:58 -07002621 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002622 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2623 if (activityContainer != null) {
2624 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002625 }
2626 return null;
2627 }
2628
Craig Mautner967212c2013-04-13 21:10:58 -07002629 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002630 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002631 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2632 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002633 }
2634 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002635 }
2636
Craig Mautner4a1cb222013-12-04 16:14:06 -08002637 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002638 ActivityRecord homeActivity = getHomeActivity();
2639 if (homeActivity != null) {
2640 return homeActivity.appToken;
2641 }
2642 return null;
2643 }
2644
2645 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002646 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2647 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2648 final TaskRecord task = tasks.get(taskNdx);
2649 if (task.isHomeTask()) {
2650 final ArrayList<ActivityRecord> activities = task.mActivities;
2651 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2652 final ActivityRecord r = activities.get(activityNdx);
2653 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002654 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002655 }
2656 }
2657 }
2658 }
2659 return null;
2660 }
2661
Todd Kennedyca4d8422015-01-15 15:19:22 -08002662 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002663 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002664 ActivityContainer activityContainer =
2665 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002666 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002667 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002668 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002669 return activityContainer;
2670 }
2671
Craig Mautner34b73df2014-01-12 21:11:08 -08002672 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002673 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2674 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2675 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002676 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2677 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002678 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002679 }
2680 }
2681
Craig Mautner95da1082014-02-24 17:54:35 -08002682 void deleteActivityContainer(IActivityContainer container) {
2683 ActivityContainer activityContainer = (ActivityContainer)container;
2684 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002685 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2686 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002687 final int stackId = activityContainer.mStackId;
2688 mActivityContainers.remove(stackId);
2689 mWindowManager.removeStack(stackId);
2690 }
2691 }
2692
Wale Ogunwale60454db2015-01-23 16:05:07 -08002693 void resizeStackLocked(int stackId, Rect bounds) {
2694 final ActivityStack stack = getStack(stackId);
2695 if (stack == null) {
2696 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2697 return;
2698 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002699
2700 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002701 if (r != null && !r.task.mResizeable) {
2702 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002703 return;
2704 }
2705
Wale Ogunwale60454db2015-01-23 16:05:07 -08002706 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2707 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002708 if (r != null) {
2709 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2710 // And we need to make sure at this point that all other activities
2711 // are made visible with the correct configuration.
2712 ensureActivitiesVisibleLocked(r, 0);
2713 if (!updated) {
2714 resumeTopActivitiesLocked(stack, null, null);
2715 }
2716 }
2717 }
2718 }
2719
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002720 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2721 * current task stack if it only has one entry, moving the task to a stack that matches the
2722 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2723 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2724 task.mResizeable = true;
2725 final ActivityStack currentStack = task.stack;
2726 if (currentStack.isHomeStack()) {
2727 // Can't move task off the home stack. Sorry!
2728 return;
2729 }
2730
2731 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2732 if (matchingStackId != -1) {
2733 // There is already a stack with the right bounds!
2734 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2735 // Nothing to do here. Already in the right stack...
2736 return;
2737 }
2738 // Move task to stack with matching bounds.
2739 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2740 return;
2741 }
2742
2743 if (currentStack != null && currentStack.numTasks() == 1) {
2744 // Just resize the current stack since this is the task in it.
2745 resizeStackLocked(currentStack.mStackId, bounds);
2746 return;
2747 }
2748
2749 // Create new stack and move the task to it.
2750 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2751 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2752 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2753
2754 if (newStack == null) {
2755 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2756 return;
2757 }
2758 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2759 resizeStackLocked(newStack.mStackId, bounds);
2760 }
2761
Todd Kennedy4900bf92015-01-16 16:05:14 -08002762 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002763 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2764 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002765 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002766 }
2767
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002768 ActivityContainer activityContainer = new ActivityContainer(stackId);
2769 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002770 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002771 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002772 }
2773
2774 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002775 while (true) {
2776 if (++mLastStackId <= HOME_STACK_ID) {
2777 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002778 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002779 if (getStack(mLastStackId) == null) {
2780 break;
2781 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002782 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002783 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002784 }
2785
Wale Ogunwale7de05352014-12-12 15:21:33 -08002786 private boolean restoreRecentTaskLocked(TaskRecord task) {
2787 ActivityStack stack = null;
2788 // Determine stack to restore task to.
2789 if (mLeanbackOnlyDevice) {
2790 // There is only one stack for lean back devices.
2791 stack = mHomeStack;
2792 } else {
2793 // Look for the top stack on the home display that isn't the home stack.
2794 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2795 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2796 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002797 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002798 stack = tmpStack;
2799 break;
2800 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002801 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002802 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002803
2804 if (stack == null) {
2805 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2806 // before an application stack is created...Go ahead and create one on the default
2807 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002808 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002809 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002810 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002811 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2812 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002813 }
2814
2815 if (stack == null) {
2816 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002817 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2818 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002819 return false;
2820 }
2821
2822 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002823 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2824 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002825 final ArrayList<ActivityRecord> activities = task.mActivities;
2826 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2827 final ActivityRecord r = activities.get(activityNdx);
2828 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2829 r.info.screenOrientation, r.fullscreen,
2830 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2831 r.userId, r.info.configChanges, task.voiceSession != null,
2832 r.mLaunchTaskBehind);
2833 }
2834 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002835 }
2836
Craig Mautner299f9602015-01-26 09:47:33 -08002837 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002838 final TaskRecord task = anyTaskForIdLocked(taskId);
2839 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002840 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002841 return;
2842 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002843 final ActivityStack stack = getStack(stackId);
2844 if (stack == null) {
2845 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2846 return;
2847 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002848 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2849 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07002850 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002851 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002852 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002853 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002854 }
2855
Craig Mautnerac6f8432013-07-17 13:24:59 -07002856 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002857 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002858 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2859 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002860 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2861 final ActivityStack stack = stacks.get(stackNdx);
2862 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002863 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002864 continue;
2865 }
2866 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002867 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2868 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002869 continue;
2870 }
2871 final ActivityRecord ar = stack.findTaskLocked(r);
2872 if (ar != null) {
2873 return ar;
2874 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002875 }
2876 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002877 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002878 return null;
2879 }
2880
2881 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002882 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2883 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002884 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2885 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2886 if (ar != null) {
2887 return ar;
2888 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002889 }
2890 }
2891 return null;
2892 }
2893
Craig Mautner8d341ef2013-03-26 09:03:27 -07002894 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002895 scheduleSleepTimeout();
2896 if (!mGoingToSleep.isHeld()) {
2897 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002898 if (mLaunchingActivity.isHeld()) {
2899 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2900 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002901 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002902 mLaunchingActivity.release();
2903 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002904 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002905 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002906 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 }
2908
2909 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002910 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002911
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002912 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002913 final long endTime = System.currentTimeMillis() + timeout;
2914 while (true) {
2915 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002916 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2917 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002918 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2919 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2920 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002921 }
2922 if (cantShutdown) {
2923 long timeRemaining = endTime - System.currentTimeMillis();
2924 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002925 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002926 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002927 } catch (InterruptedException e) {
2928 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002929 } else {
2930 Slog.w(TAG, "Activity manager shutdown timed out");
2931 timedout = true;
2932 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002933 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002934 } else {
2935 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002936 }
2937 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002938
2939 // Force checkReadyForSleep to complete.
2940 mSleepTimeout = true;
2941 checkReadyForSleepLocked();
2942
Craig Mautner8d341ef2013-03-26 09:03:27 -07002943 return timedout;
2944 }
2945
2946 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002947 removeSleepTimeouts();
2948 if (mGoingToSleep.isHeld()) {
2949 mGoingToSleep.release();
2950 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002951 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2952 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002953 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2954 final ActivityStack stack = stacks.get(stackNdx);
2955 stack.awakeFromSleepingLocked();
2956 if (isFrontStack(stack)) {
2957 resumeTopActivitiesLocked();
2958 }
Craig Mautner5314a402013-09-26 12:40:16 -07002959 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002960 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002961 mGoingToSleepActivities.clear();
2962 }
2963
2964 void activitySleptLocked(ActivityRecord r) {
2965 mGoingToSleepActivities.remove(r);
2966 checkReadyForSleepLocked();
2967 }
2968
2969 void checkReadyForSleepLocked() {
2970 if (!mService.isSleepingOrShuttingDown()) {
2971 // Do not care.
2972 return;
2973 }
2974
2975 if (!mSleepTimeout) {
2976 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002977 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2978 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002979 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2980 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2981 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002982 }
2983
2984 if (mStoppingActivities.size() > 0) {
2985 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002986 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002987 + mStoppingActivities.size() + " activities");
2988 scheduleIdleLocked();
2989 dontSleep = true;
2990 }
2991
2992 if (mGoingToSleepActivities.size() > 0) {
2993 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002994 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002995 + mGoingToSleepActivities.size() + " activities");
2996 dontSleep = true;
2997 }
2998
2999 if (dontSleep) {
3000 return;
3001 }
3002 }
3003
Craig Mautnere0a38842013-12-16 16:14:02 -08003004 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3005 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003006 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3007 stacks.get(stackNdx).goToSleep();
3008 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003009 }
3010
3011 removeSleepTimeouts();
3012
3013 if (mGoingToSleep.isHeld()) {
3014 mGoingToSleep.release();
3015 }
3016 if (mService.mShuttingDown) {
3017 mService.notifyAll();
3018 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003019 }
3020
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003021 boolean reportResumedActivityLocked(ActivityRecord r) {
3022 final ActivityStack stack = r.task.stack;
3023 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003024 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003025 }
3026 if (allResumedActivitiesComplete()) {
3027 ensureActivitiesVisibleLocked(null, 0);
3028 mWindowManager.executeAppTransition();
3029 return true;
3030 }
3031 return false;
3032 }
3033
Craig Mautner8d341ef2013-03-26 09:03:27 -07003034 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003035 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3036 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003037 final int numStacks = stacks.size();
3038 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3039 final ActivityStack stack = stacks.get(stackNdx);
3040 stack.handleAppCrashLocked(app);
3041 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003042 }
3043 }
3044
Jose Lima4b6c6692014-08-12 17:41:12 -07003045 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003046 final ActivityStack stack = r.task.stack;
3047 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003048 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3049 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003050 return false;
3051 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003052 final boolean isVisible = stack.hasVisibleBehindActivity();
3053 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3054 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003055
3056 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003057 if (top == null || top == r || (visible == isVisible)) {
3058 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3059 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003060 return true;
3061 }
3062
3063 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003064 if (visible && top.fullscreen) {
3065 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003066 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003067 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003068 " top.app.thread=" + top.app.thread);
3069 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003070 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3071 // Only the activity set as currently visible behind should actively reset its
3072 // visible behind state.
3073 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3074 + visible + " stack.getVisibleBehindActivity()=" +
3075 stack.getVisibleBehindActivity() + " r=" + r);
3076 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003077 }
3078
Jose Lima4b6c6692014-08-12 17:41:12 -07003079 stack.setVisibleBehindActivity(visible ? r : null);
3080 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003081 // Make the activity immediately above r opaque.
3082 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3083 if (next != null) {
3084 mService.convertFromTranslucent(next.appToken);
3085 }
3086 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003087 if (top.app != null && top.app.thread != null) {
3088 // Notify the top app of the change.
3089 try {
3090 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3091 } catch (RemoteException e) {
3092 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003093 }
3094 return true;
3095 }
3096
Craig Mautnerbb742462014-07-07 15:28:55 -07003097 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3098 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3099 r.mLaunchTaskBehind = false;
3100 final TaskRecord task = r.task;
3101 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003102 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003103 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003104 mWindowManager.setAppVisibility(r.appToken, false);
3105 }
3106
3107 void scheduleLaunchTaskBehindComplete(IBinder token) {
3108 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3109 }
3110
Craig Mautnerde4ef022013-04-07 19:01:33 -07003111 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003112 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003113 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3114 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003115 final int topStackNdx = stacks.size() - 1;
3116 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3117 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003118 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003119 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003120 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003121 }
3122
3123 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003124 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3125 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003126 final int numStacks = stacks.size();
3127 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3128 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003129 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003130 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003131 }
3132 }
3133
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003134 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3135 // Examine all activities currently running in the process.
3136 TaskRecord firstTask = null;
3137 // Tasks is non-null only if two or more tasks are found.
3138 ArraySet<TaskRecord> tasks = null;
3139 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3140 for (int i=0; i<app.activities.size(); i++) {
3141 ActivityRecord r = app.activities.get(i);
3142 // First, if we find an activity that is in the process of being destroyed,
3143 // then we just aren't going to do anything for now; we want things to settle
3144 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003145 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003146 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3147 return;
3148 }
3149 // Don't consider any activies that are currently not in a state where they
3150 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003151 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3152 || r.state == PAUSED || r.state == STOPPING) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003153 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3154 continue;
3155 }
3156 if (r.task != null) {
3157 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3158 + " from " + r);
3159 if (firstTask == null) {
3160 firstTask = r.task;
3161 } else if (firstTask != r.task) {
3162 if (tasks == null) {
3163 tasks = new ArraySet<>();
3164 tasks.add(firstTask);
3165 }
3166 tasks.add(r.task);
3167 }
3168 }
3169 }
3170 if (tasks == null) {
3171 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3172 return;
3173 }
3174 // If we have activities in multiple tasks that are in a position to be destroyed,
3175 // let's iterate through the tasks and release the oldest one.
3176 final int numDisplays = mActivityDisplays.size();
3177 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3178 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3179 // Step through all stacks starting from behind, to hit the oldest things first.
3180 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3181 final ActivityStack stack = stacks.get(stackNdx);
3182 // Try to release activities in this stack; if we manage to, we are done.
3183 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3184 return;
3185 }
3186 }
3187 }
3188 }
3189
Craig Mautner8d341ef2013-03-26 09:03:27 -07003190 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003191 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003192 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003193 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003194
Craig Mautner858d8a62013-04-23 17:08:34 -07003195 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003196 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3197 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003198 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003199 final ActivityStack stack = stacks.get(stackNdx);
3200 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003201 TaskRecord task = stack.topTask();
3202 if (task != null) {
3203 mWindowManager.moveTaskToTop(task.taskId);
3204 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003205 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003206 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003207
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003208 ActivityStack stack = getStack(restoreStackId);
3209 if (stack == null) {
3210 stack = mHomeStack;
3211 }
3212 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003213 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003214 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003215 TaskRecord task = stack.topTask();
3216 if (task != null) {
3217 mWindowManager.moveTaskToTop(task.taskId);
3218 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003219 } else {
3220 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003221 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003222 }
Craig Mautner93529a42013-10-04 15:03:13 -07003223 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003224 }
3225
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003226 /**
3227 * Add background users to send boot completed events to.
3228 * @param userId The user being started in the background
3229 * @param uss The state object for the user.
3230 */
3231 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3232 mStartingBackgroundUsers.add(uss);
3233 }
3234
Craig Mautnerde4ef022013-04-07 19:01:33 -07003235 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003236 ArrayList<ActivityRecord> stops = null;
3237
3238 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003239 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3240 ActivityRecord s = mStoppingActivities.get(activityNdx);
3241 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003242 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003243 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3244 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003245 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003246 if (s.finishing) {
3247 // If this activity is finishing, it is sitting on top of
3248 // everyone else but we now know it is no longer needed...
3249 // so get rid of it. Otherwise, we need to go through the
3250 // normal flow and hide it once we determine that it is
3251 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003252 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003253 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003254 }
3255 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003256 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003257 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003258 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003259 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003260 }
3261 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003262 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003263 }
3264 }
3265
3266 return stops;
3267 }
3268
Craig Mautnercf910b02013-04-23 11:23:27 -07003269 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003270 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3271 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3272 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3273 final ActivityStack stack = stacks.get(stackNdx);
3274 final ActivityRecord r = stack.topRunningActivityLocked(null);
3275 final ActivityState state = r == null ? DESTROYED : r.state;
3276 if (isFrontStack(stack)) {
3277 if (r == null) Slog.e(TAG,
3278 "validateTop...: null top activity, stack=" + stack);
3279 else {
3280 final ActivityRecord pausing = stack.mPausingActivity;
3281 if (pausing != null && pausing == r) Slog.e(TAG,
3282 "validateTop...: top stack has pausing activity r=" + r
3283 + " state=" + state);
3284 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3285 "validateTop...: activity in front not resumed r=" + r
3286 + " state=" + state);
3287 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003288 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003289 final ActivityRecord resumed = stack.mResumedActivity;
3290 if (resumed != null && resumed == r) Slog.e(TAG,
3291 "validateTop...: back stack has resumed activity r=" + r
3292 + " state=" + state);
3293 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3294 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003295 }
3296 }
3297 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003298 }
3299
Craig Mautner27084302013-03-25 08:05:25 -07003300 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003301 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003302 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003303 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3304 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3305 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003306 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner15df08a2015-04-01 12:17:18 -07003307 pw.print(prefix); pw.println("mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003308 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003309
Craig Mautner20e72272013-04-01 13:45:53 -07003310 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003311 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003312 }
3313
Dianne Hackborn390517b2013-05-30 15:03:32 -07003314 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3315 boolean needSep, String prefix) {
3316 if (activity != null) {
3317 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3318 if (needSep) {
3319 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003320 }
3321 pw.print(prefix);
3322 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003323 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003324 }
3325 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003326 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003327 }
3328
Craig Mautner8d341ef2013-03-26 09:03:27 -07003329 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3330 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003331 boolean printed = false;
3332 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003333 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3334 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003335 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003336 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003337 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003338 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003339 final ActivityStack stack = stacks.get(stackNdx);
3340 StringBuilder stackHeader = new StringBuilder(128);
3341 stackHeader.append(" Stack #");
3342 stackHeader.append(stack.mStackId);
3343 stackHeader.append(":");
3344 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3345 needSep, stackHeader.toString());
3346 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3347 !dumpAll, false, dumpPackage, true,
3348 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003349
Craig Mautner4a1cb222013-12-04 16:14:06 -08003350 needSep = printed;
3351 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3352 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003353 if (pr) {
3354 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003355 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003356 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003357 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3358 " mResumedActivity: ");
3359 if (pr) {
3360 printed = true;
3361 needSep = false;
3362 }
3363 if (dumpAll) {
3364 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3365 " mLastPausedActivity: ");
3366 if (pr) {
3367 printed = true;
3368 needSep = true;
3369 }
3370 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3371 needSep, " mLastNoHistoryActivity: ");
3372 }
3373 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003374 }
3375 }
3376
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003377 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3378 false, dumpPackage, true, " Activities waiting to finish:", null);
3379 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3380 false, dumpPackage, true, " Activities waiting to stop:", null);
3381 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3382 false, dumpPackage, true, " Activities waiting for another to become visible:",
3383 null);
3384 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3385 false, dumpPackage, true, " Activities waiting to sleep:", null);
3386 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3387 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003388
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003389 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003390 }
3391
Dianne Hackborn390517b2013-05-30 15:03:32 -07003392 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003393 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003394 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003395 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003396 String innerPrefix = null;
3397 String[] args = null;
3398 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003399 for (int i=list.size()-1; i>=0; i--) {
3400 final ActivityRecord r = list.get(i);
3401 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3402 continue;
3403 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003404 if (innerPrefix == null) {
3405 innerPrefix = prefix + " ";
3406 args = new String[0];
3407 }
3408 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003409 final boolean full = !brief && (complete || !r.isInHistory());
3410 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003411 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003412 needNL = false;
3413 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003414 if (header1 != null) {
3415 pw.println(header1);
3416 header1 = null;
3417 }
3418 if (header2 != null) {
3419 pw.println(header2);
3420 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003421 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003422 if (lastTask != r.task) {
3423 lastTask = r.task;
3424 pw.print(prefix);
3425 pw.print(full ? "* " : " ");
3426 pw.println(lastTask);
3427 if (full) {
3428 lastTask.dump(pw, prefix + " ");
3429 } else if (complete) {
3430 // Complete + brief == give a summary. Isn't that obvious?!?
3431 if (lastTask.intent != null) {
3432 pw.print(prefix); pw.print(" ");
3433 pw.println(lastTask.intent.toInsecureStringWithClip());
3434 }
3435 }
3436 }
3437 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3438 pw.print(" #"); pw.print(i); pw.print(": ");
3439 pw.println(r);
3440 if (full) {
3441 r.dump(pw, innerPrefix);
3442 } else if (complete) {
3443 // Complete + brief == give a summary. Isn't that obvious?!?
3444 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3445 if (r.app != null) {
3446 pw.print(innerPrefix); pw.println(r.app);
3447 }
3448 }
3449 if (client && r.app != null && r.app.thread != null) {
3450 // flush anything that is already in the PrintWriter since the thread is going
3451 // to write to the file descriptor directly
3452 pw.flush();
3453 try {
3454 TransferPipe tp = new TransferPipe();
3455 try {
3456 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3457 r.appToken, innerPrefix, args);
3458 // Short timeout, since blocking here can
3459 // deadlock with the application.
3460 tp.go(fd, 2000);
3461 } finally {
3462 tp.kill();
3463 }
3464 } catch (IOException e) {
3465 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3466 } catch (RemoteException e) {
3467 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3468 }
3469 needNL = true;
3470 }
3471 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003472 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003473 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003474
Craig Mautnerf3333272013-04-22 10:55:53 -07003475 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003476 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003477 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3478 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003479 }
3480
3481 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003482 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003483 }
3484
3485 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003486 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003487 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3488 }
3489
Craig Mautner05d29032013-05-03 13:40:13 -07003490 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003491 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3492 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3493 }
Craig Mautner05d29032013-05-03 13:40:13 -07003494 }
3495
Craig Mautner0eea92c2013-05-16 13:35:39 -07003496 void removeSleepTimeouts() {
3497 mSleepTimeout = false;
3498 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3499 }
3500
3501 final void scheduleSleepTimeout() {
3502 removeSleepTimeouts();
3503 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3504 }
3505
Craig Mautner4a1cb222013-12-04 16:14:06 -08003506 @Override
3507 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003508 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003509 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3510 }
3511
3512 @Override
3513 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003514 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003515 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3516 }
3517
3518 @Override
3519 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003520 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003521 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3522 }
3523
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003524 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003525 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003526 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003527 newDisplay = mActivityDisplays.get(displayId) == null;
3528 if (newDisplay) {
3529 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003530 if (activityDisplay.mDisplay == null) {
3531 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3532 return;
3533 }
Craig Mautner4504de52013-12-20 09:06:56 -08003534 mActivityDisplays.put(displayId, activityDisplay);
3535 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003536 }
Craig Mautner4504de52013-12-20 09:06:56 -08003537 if (newDisplay) {
3538 mWindowManager.onDisplayAdded(displayId);
3539 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003540 }
3541
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003542 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003543 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003544 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3545 if (activityDisplay != null) {
3546 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003547 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003548 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003549 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003550 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003551 }
3552 }
3553 mWindowManager.onDisplayRemoved(displayId);
3554 }
3555
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003556 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003557 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003558 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3559 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003560 // TODO: Update the bounds.
3561 }
3562 }
3563 mWindowManager.onDisplayChanged(displayId);
3564 }
3565
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003566 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003567 StackInfo info = new StackInfo();
3568 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3569 info.displayId = Display.DEFAULT_DISPLAY;
3570 info.stackId = stack.mStackId;
3571
3572 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3573 final int numTasks = tasks.size();
3574 int[] taskIds = new int[numTasks];
3575 String[] taskNames = new String[numTasks];
3576 for (int i = 0; i < numTasks; ++i) {
3577 final TaskRecord task = tasks.get(i);
3578 taskIds[i] = task.taskId;
3579 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3580 : task.realActivity != null ? task.realActivity.flattenToString()
3581 : task.getTopActivity() != null ? task.getTopActivity().packageName
3582 : "unknown";
3583 }
3584 info.taskIds = taskIds;
3585 info.taskNames = taskNames;
3586 return info;
3587 }
3588
3589 StackInfo getStackInfoLocked(int stackId) {
3590 ActivityStack stack = getStack(stackId);
3591 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003592 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003593 }
3594 return null;
3595 }
3596
3597 ArrayList<StackInfo> getAllStackInfosLocked() {
3598 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003599 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3600 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003601 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003602 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003603 }
3604 }
3605 return list;
3606 }
3607
Craig Mautner15df08a2015-04-01 12:17:18 -07003608 TaskRecord getLockedTaskLocked() {
3609 final int top = mLockTaskModeTasks.size() - 1;
3610 if (top >= 0) {
3611 return mLockTaskModeTasks.get(top);
3612 }
3613 return null;
3614 }
3615
3616 boolean isLockedTask(TaskRecord task) {
3617 return mLockTaskModeTasks.contains(task);
3618 }
3619
3620 boolean isLastLockedTask(TaskRecord task) {
3621 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3622 }
3623
3624 void removeLockedTaskLocked(final TaskRecord task) {
3625 if (mLockTaskModeTasks.remove(task) && mLockTaskModeTasks.isEmpty()) {
3626 // Last one.
3627 final Message lockTaskMsg = Message.obtain();
3628 lockTaskMsg.arg1 = task.userId;
3629 lockTaskMsg.what = LOCK_TASK_END_MSG;
3630 mHandler.sendMessage(lockTaskMsg);
3631 }
3632 }
3633
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003634 void showLockTaskToast() {
3635 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003636 }
3637
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003638 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3639 if (mLockTaskModeTasks.contains(task)) {
3640 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3641 }
3642 }
3643
Benjamin Franz43261142015-02-11 15:59:44 +00003644 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003645 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003646 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003647 final TaskRecord lockedTask = getLockedTaskLocked();
3648 if (lockedTask != null) {
3649 removeLockedTaskLocked(lockedTask);
3650 if (!mLockTaskModeTasks.isEmpty()) {
3651 // There are locked tasks remaining, can only finish this task, not unlock it.
3652 lockedTask.performClearTaskLocked();
3653 resumeTopActivitiesLocked();
3654 return;
3655 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003656 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003657 return;
3658 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003659
3660 // Should have already been checked, but do it again.
3661 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003662 return;
3663 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003664 if (isLockTaskModeViolation(task)) {
3665 Slog.e(TAG, "setLockTaskMode: Attempt to start an unauthorized lock task.");
3666 return;
3667 }
3668
3669 if (mLockTaskModeTasks.isEmpty()) {
3670 // First locktask.
3671 final Message lockTaskMsg = Message.obtain();
3672 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3673 lockTaskMsg.arg1 = task.userId;
3674 lockTaskMsg.what = LOCK_TASK_START_MSG;
3675 lockTaskMsg.arg2 = lockTaskModeState;
3676 mHandler.sendMessage(lockTaskMsg);
3677 }
3678 // Add it or move it to the top.
3679 mLockTaskModeTasks.remove(task);
3680 mLockTaskModeTasks.add(task);
3681
3682 if (task.mLockTaskUid == -1) {
3683 task.mLockTaskUid = task.mCallingUid;
3684 }
Craig Mautner299f9602015-01-26 09:47:33 -08003685 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003686 resumeTopActivitiesLocked();
3687 }
3688
3689 boolean isLockTaskModeViolation(TaskRecord task) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003690 if (getLockedTaskLocked() == task) {
3691 return false;
3692 }
3693 final int lockTaskAuth = task.mLockTaskAuth;
3694 switch (lockTaskAuth) {
3695 case LOCK_TASK_AUTH_DONT_LOCK:
3696 return !mLockTaskModeTasks.isEmpty();
3697 case LOCK_TASK_AUTH_LAUNCHABLE:
3698 case LOCK_TASK_AUTH_WHITELISTED:
3699 return false;
3700 case LOCK_TASK_AUTH_PINNABLE:
3701 // Pinnable tasks can't be launched on top of locktask tasks.
3702 return !mLockTaskModeTasks.isEmpty();
3703 default:
3704 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3705 return true;
3706 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003707 }
3708
Craig Mautner15df08a2015-04-01 12:17:18 -07003709 void onLockTaskPackagesUpdatedLocked() {
3710 boolean didSomething = false;
3711 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3712 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
3713 if (lockedTask.mLockTaskMode != LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED) {
3714 continue;
3715 }
3716 final boolean wasLaunchable = lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE;
3717 lockedTask.setLockTaskAuth();
3718 if (wasLaunchable && lockedTask.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE) {
3719 // Lost whitelisting authorization. End it now.
3720 removeLockedTaskLocked(lockedTask);
3721 lockedTask.performClearTaskLocked();
3722 didSomething = true;
3723 }
3724 }
3725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3726 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3728 final ActivityStack stack = stacks.get(stackNdx);
3729 stack.onLockTaskPackagesUpdatedLocked();
3730 }
3731 }
3732 if (didSomething) {
3733 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003734 }
3735 }
3736
Benjamin Franz43261142015-02-11 15:59:44 +00003737 int getLockTaskModeState() {
3738 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003739 }
3740
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003741 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003742
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003743 public ActivityStackSupervisorHandler(Looper looper) {
3744 super(looper);
3745 }
3746
Craig Mautnerf3333272013-04-22 10:55:53 -07003747 void activityIdleInternal(ActivityRecord r) {
3748 synchronized (mService) {
3749 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3750 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003751 }
3752
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003753 @Override
3754 public void handleMessage(Message msg) {
3755 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003756 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003757 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003758 if (mService.mDidDexOpt) {
3759 mService.mDidDexOpt = false;
3760 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3761 nmsg.obj = msg.obj;
3762 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3763 return;
3764 }
3765 // We don't at this point know if the activity is fullscreen,
3766 // so we need to be conservative and assume it isn't.
3767 activityIdleInternal((ActivityRecord)msg.obj);
3768 } break;
3769 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003770 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003771 activityIdleInternal((ActivityRecord)msg.obj);
3772 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003773 case RESUME_TOP_ACTIVITY_MSG: {
3774 synchronized (mService) {
3775 resumeTopActivitiesLocked();
3776 }
3777 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003778 case SLEEP_TIMEOUT_MSG: {
3779 synchronized (mService) {
3780 if (mService.isSleepingOrShuttingDown()) {
3781 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3782 mSleepTimeout = true;
3783 checkReadyForSleepLocked();
3784 }
3785 }
3786 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003787 case LAUNCH_TIMEOUT_MSG: {
3788 if (mService.mDidDexOpt) {
3789 mService.mDidDexOpt = false;
3790 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3791 return;
3792 }
3793 synchronized (mService) {
3794 if (mLaunchingActivity.isHeld()) {
3795 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3796 if (VALIDATE_WAKE_LOCK_CALLER
3797 && Binder.getCallingUid() != Process.myUid()) {
3798 throw new IllegalStateException("Calling must be system uid");
3799 }
3800 mLaunchingActivity.release();
3801 }
3802 }
3803 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003804 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003805 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806 } break;
3807 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003808 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003809 } break;
3810 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003811 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003812 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003813 case CONTAINER_CALLBACK_VISIBILITY: {
3814 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003815 final IActivityContainerCallback callback = container.mCallback;
3816 if (callback != null) {
3817 try {
3818 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3819 } catch (RemoteException e) {
3820 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003821 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003822 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003823 case LOCK_TASK_START_MSG: {
3824 // When lock task starts, we disable the status bars.
3825 try {
Jason Monk62515be2014-05-21 16:06:19 -04003826 if (mLockTaskNotify == null) {
3827 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003828 }
Jason Monk62515be2014-05-21 16:06:19 -04003829 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003830 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003831 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003832 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003833 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003834 flags = StatusBarManager.DISABLE_MASK
3835 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003836 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003837 flags = StatusBarManager.DISABLE_MASK
3838 & (~StatusBarManager.DISABLE_BACK)
3839 & (~StatusBarManager.DISABLE_HOME)
3840 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003841 }
3842 getStatusBarService().disable(flags, mToken,
3843 mService.mContext.getPackageName());
3844 }
3845 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003846 if (getDevicePolicyManager() != null) {
3847 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003848 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003849 }
justinzhang5286d3f2014-05-12 17:06:01 -04003850 } catch (RemoteException ex) {
3851 throw new RuntimeException(ex);
3852 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003853 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003854 case LOCK_TASK_END_MSG: {
3855 // When lock task ends, we enable the status bars.
3856 try {
Jason Monk62515be2014-05-21 16:06:19 -04003857 if (getStatusBarService() != null) {
3858 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3859 mService.mContext.getPackageName());
3860 }
3861 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003862 if (getDevicePolicyManager() != null) {
3863 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3864 msg.arg1);
3865 }
Jason Monk62515be2014-05-21 16:06:19 -04003866 if (mLockTaskNotify == null) {
3867 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3868 }
3869 mLockTaskNotify.show(false);
3870 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003871 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003872 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003873 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003874 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003875 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003876 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003877 new LockPatternUtils(mService.mContext)
3878 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003879 }
3880 } catch (SettingNotFoundException e) {
3881 // No setting, don't lock.
3882 }
justinzhang5286d3f2014-05-12 17:06:01 -04003883 } catch (RemoteException ex) {
3884 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003885 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003886 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003887 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003888 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003889 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3890 if (mLockTaskNotify == null) {
3891 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3892 }
3893 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3894 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003895 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3896 final ActivityContainer container = (ActivityContainer) msg.obj;
3897 final IActivityContainerCallback callback = container.mCallback;
3898 if (callback != null) {
3899 try {
3900 callback.onAllActivitiesComplete(container.asBinder());
3901 } catch (RemoteException e) {
3902 }
3903 }
3904 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003905 case LAUNCH_TASK_BEHIND_COMPLETE: {
3906 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003907 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003908 if (r != null) {
3909 handleLaunchTaskBehindCompleteLocked(r);
3910 }
3911 }
3912 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003913 }
3914 }
3915 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003916
Ying Wangb081a592014-04-22 15:20:16 -07003917 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003918 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003919 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003920 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003921 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003922 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003923 ActivityRecord mParentActivity = null;
3924 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003925
Craig Mautnere3a00d72014-04-16 08:31:19 -07003926 boolean mVisible = true;
3927
Craig Mautner4a1cb222013-12-04 16:14:06 -08003928 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003929 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003930
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003931 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3932 final static int CONTAINER_STATE_NO_SURFACE = 1;
3933 final static int CONTAINER_STATE_FINISHING = 2;
3934 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3935
3936 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003937 synchronized (mService) {
3938 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003939 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003940 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003941 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003942 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003943 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003944
Craig Mautnere0a38842013-12-16 16:14:02 -08003945 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003946 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08003947 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003948 mActivityDisplay = activityDisplay;
3949 mStack.mDisplayId = activityDisplay.mDisplayId;
3950 mStack.mStacks = activityDisplay.mStacks;
3951
3952 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003953 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003954 }
3955
3956 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003957 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003958 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003959 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3960 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003961 return;
3962 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003963 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003964 }
3965 }
3966
3967 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003968 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003969 synchronized (mService) {
3970 if (mActivityDisplay != null) {
3971 return mActivityDisplay.mDisplayId;
3972 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003973 }
3974 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003975 }
3976
Jeff Brownca9bc702014-02-11 14:32:56 -08003977 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003978 public int getStackId() {
3979 synchronized (mService) {
3980 return mStackId;
3981 }
3982 }
3983
3984 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003985 public boolean injectEvent(InputEvent event) {
3986 final long origId = Binder.clearCallingIdentity();
3987 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003988 synchronized (mService) {
3989 if (mActivityDisplay != null) {
3990 return mInputManagerInternal.injectInputEvent(event,
3991 mActivityDisplay.mDisplayId,
3992 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3993 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003994 }
3995 return false;
3996 } finally {
3997 Binder.restoreCallingIdentity(origId);
3998 }
3999 }
4000
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004001 @Override
4002 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004003 synchronized (mService) {
4004 if (mContainerState == CONTAINER_STATE_FINISHING) {
4005 return;
4006 }
4007 mContainerState = CONTAINER_STATE_FINISHING;
4008
Craig Mautnerd163e752014-06-13 17:18:47 -07004009 long origId = Binder.clearCallingIdentity();
4010 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004011 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004012 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004013 } finally {
4014 Binder.restoreCallingIdentity(origId);
4015 }
4016 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004017 }
4018
Craig Mautner60257702014-09-17 15:02:33 -07004019 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004020 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004021 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004022 if (mActivityDisplay != null) {
4023 mActivityDisplay.detachActivitiesLocked(mStack);
4024 mActivityDisplay = null;
4025 mStack.mDisplayId = -1;
4026 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004027 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004028 }
4029 }
4030
4031 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004032 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004033 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004034 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004035 Binder.getCallingUid(), mCurrentUser, false,
4036 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004037
Craig Mautnere0a38842013-12-16 16:14:02 -08004038 // TODO: Switch to user app stacks here.
4039 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004040 final Uri data = intent.getData();
4041 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4042 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004043 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004044 checkEmbeddedAllowedInner(userId, intent, mimeType);
4045
4046 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4047 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4048 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004049 }
4050
4051 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004052 public final int startActivityIntentSender(IIntentSender intentSender)
4053 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004054 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4055
4056 if (!(intentSender instanceof PendingIntentRecord)) {
4057 throw new IllegalArgumentException("Bad PendingIntent object");
4058 }
4059
Craig Mautnerb9168362015-02-26 20:40:19 -08004060 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004061 Binder.getCallingUid(), mCurrentUser, false,
4062 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004063
4064 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4065 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4066 pendingIntent.key.requestResolvedType);
4067
4068 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4069 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4070 }
4071
4072 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004073 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004074 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004075 throw new SecurityException(
4076 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4077 }
4078 }
4079
Craig Mautnerdf88d732014-01-27 09:21:32 -08004080 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004081 public IBinder asBinder() {
4082 return this;
4083 }
4084
Craig Mautner4504de52013-12-20 09:06:56 -08004085 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004086 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004087 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004088 }
4089
Craig Mautner4a1cb222013-12-04 16:14:06 -08004090 ActivityStackSupervisor getOuter() {
4091 return ActivityStackSupervisor.this;
4092 }
4093
Craig Mautnerd163e752014-06-13 17:18:47 -07004094 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004095 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004096 }
4097
4098 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004099 synchronized (mService) {
4100 if (mActivityDisplay != null) {
4101 mActivityDisplay.getBounds(outBounds);
4102 } else {
4103 outBounds.set(0, 0);
4104 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004105 }
4106 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004107
Craig Mautner6985bad2014-04-21 15:22:06 -07004108 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004109 void setVisible(boolean visible) {
4110 if (mVisible != visible) {
4111 mVisible = visible;
4112 if (mCallback != null) {
4113 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4114 0 /* unused */, this).sendToTarget();
4115 }
4116 }
4117 }
4118
Craig Mautner6985bad2014-04-21 15:22:06 -07004119 void setDrawn() {
4120 }
4121
Craig Mautner1b4bf852014-05-26 15:06:32 -07004122 // You can always start a new task on a regular ActivityStack.
4123 boolean isEligibleForNewTasks() {
4124 return true;
4125 }
4126
Craig Mautnerd163e752014-06-13 17:18:47 -07004127 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004128 detachLocked();
4129 deleteActivityContainer(this);
4130 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004131 }
4132
Craig Mautner34b73df2014-01-12 21:11:08 -08004133 @Override
4134 public String toString() {
4135 return mIdString + (mActivityDisplay == null ? "N" : "A");
4136 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004137 }
4138
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004139 private class VirtualActivityContainer extends ActivityContainer {
4140 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004141 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004142
4143 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4144 super(getNextStackId());
4145 mParentActivity = parent;
4146 mCallback = callback;
4147 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004148 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004149 }
4150
4151 @Override
4152 public void setSurface(Surface surface, int width, int height, int density) {
4153 super.setSurface(surface, width, height, density);
4154
4155 synchronized (mService) {
4156 final long origId = Binder.clearCallingIdentity();
4157 try {
4158 setSurfaceLocked(surface, width, height, density);
4159 } finally {
4160 Binder.restoreCallingIdentity(origId);
4161 }
4162 }
4163 }
4164
4165 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4166 if (mContainerState == CONTAINER_STATE_FINISHING) {
4167 return;
4168 }
4169 VirtualActivityDisplay virtualActivityDisplay =
4170 (VirtualActivityDisplay) mActivityDisplay;
4171 if (virtualActivityDisplay == null) {
4172 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004173 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004174 mActivityDisplay = virtualActivityDisplay;
4175 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4176 attachToDisplayLocked(virtualActivityDisplay);
4177 }
4178
4179 if (mSurface != null) {
4180 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004181 }
4182
Craig Mautner6985bad2014-04-21 15:22:06 -07004183 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004184 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004185 mStack.resumeTopActivityLocked(null);
4186 } else {
4187 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004188 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004189 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004190 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004191 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004192 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004193
Craig Mautnerd163e752014-06-13 17:18:47 -07004194 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004195
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004196 if (DEBUG_STACK) Slog.d(TAG_STACK,
4197 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004198 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004199
Craig Mautner6985bad2014-04-21 15:22:06 -07004200 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004201 boolean isAttachedLocked() {
4202 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004203 }
4204
4205 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004206 void setDrawn() {
4207 synchronized (mService) {
4208 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004209 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004210 }
4211 }
4212
Craig Mautner1b4bf852014-05-26 15:06:32 -07004213 // Never start a new task on an ActivityView if it isn't explicitly specified.
4214 @Override
4215 boolean isEligibleForNewTasks() {
4216 return false;
4217 }
4218
Craig Mautnerd163e752014-06-13 17:18:47 -07004219 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004220 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004221 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4222 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4223 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4224 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4225 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004226 }
4227 }
4228
Craig Mautner4a1cb222013-12-04 16:14:06 -08004229 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4230 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004231 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004232 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004233 int mDisplayId;
4234 Display mDisplay;
4235 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004236
Craig Mautner4a1cb222013-12-04 16:14:06 -08004237 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4238 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004239 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004240
Jose Lima4b6c6692014-08-12 17:41:12 -07004241 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004242
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004243 ActivityDisplay() {
4244 }
Craig Mautner4504de52013-12-20 09:06:56 -08004245
Craig Mautner1a70a162014-09-13 12:09:31 -07004246 // After instantiation, check that mDisplay is not null before using this. The alternative
4247 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004248 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004249 final Display display = mDisplayManager.getDisplay(displayId);
4250 if (display == null) {
4251 return;
4252 }
4253 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004254 }
4255
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004256 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004257 mDisplay = display;
4258 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004259 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004260 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004261
4262 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004263 if (DEBUG_STACK) Slog.v(TAG_STACK,
4264 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004265 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004266 }
4267
Craig Mautnere0a38842013-12-16 16:14:02 -08004268 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004269 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004270 + " from displayId=" + mDisplayId);
4271 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004272 }
4273
4274 void getBounds(Point bounds) {
4275 mDisplay.getDisplayInfo(mDisplayInfo);
4276 bounds.x = mDisplayInfo.appWidth;
4277 bounds.y = mDisplayInfo.appHeight;
4278 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004279
Jose Lima4b6c6692014-08-12 17:41:12 -07004280 void setVisibleBehindActivity(ActivityRecord r) {
4281 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004282 }
4283
Jose Lima4b6c6692014-08-12 17:41:12 -07004284 boolean hasVisibleBehindActivity() {
4285 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004286 }
4287
Craig Mautner34b73df2014-01-12 21:11:08 -08004288 @Override
4289 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004290 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4291 }
4292 }
4293
4294 class VirtualActivityDisplay extends ActivityDisplay {
4295 VirtualDisplay mVirtualDisplay;
4296
Craig Mautner6985bad2014-04-21 15:22:06 -07004297 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004298 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004299 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4300 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4301 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4302 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004303
4304 init(mVirtualDisplay.getDisplay());
4305
4306 mWindowManager.handleDisplayAdded(mDisplayId);
4307 }
4308
4309 void setSurface(Surface surface) {
4310 if (mVirtualDisplay != null) {
4311 mVirtualDisplay.setSurface(surface);
4312 }
4313 }
4314
4315 @Override
4316 void detachActivitiesLocked(ActivityStack stack) {
4317 super.detachActivitiesLocked(stack);
4318 if (mVirtualDisplay != null) {
4319 mVirtualDisplay.release();
4320 mVirtualDisplay = null;
4321 }
4322 }
4323
4324 @Override
4325 public String toString() {
4326 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004327 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004328 }
Jose Lima58e66d62014-05-27 20:00:27 -07004329
4330 private boolean isLeanbackOnlyDevice() {
4331 boolean onLeanbackOnly = false;
4332 try {
4333 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4334 PackageManager.FEATURE_LEANBACK_ONLY);
4335 } catch (RemoteException e) {
4336 // noop
4337 }
4338
4339 return onLeanbackOnly;
4340 }
Craig Mautner27084302013-03-25 08:05:25 -07004341}