blob: cb5ba8e7bda4f2732f82bd9071b0b3f02dc9c15d [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner15df08a2015-04-01 12:17:18 -070020import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070023import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070025import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
26import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070027import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner15df08a2015-04-01 12:17:18 -070028import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
Craig Mautner6170f732013-04-02 13:05:23 -070029import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080030import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070031import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070032import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
33import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070035import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
38import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
39import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070040
Craig Mautner2420ead2013-04-01 17:13:20 -070041import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080043import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.ActivityOptions;
45import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080046import android.app.IActivityContainer;
47import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070048import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070051import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070052import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070054import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040055import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040056import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070062import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.content.pm.ApplicationInfo;
64import android.content.pm.PackageManager;
65import android.content.pm.ResolveInfo;
66import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080067import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080068import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080069import android.hardware.display.DisplayManager;
70import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080071import android.hardware.display.DisplayManagerGlobal;
72import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080073import android.hardware.input.InputManager;
74import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080075import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070077import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070078import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070079import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070081import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070082import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070083import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070084import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070085import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070086import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040087import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070088import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070089import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070090import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070091import android.os.WorkSource;
Jason Monk62515be2014-05-21 16:06:19 -040092import android.provider.Settings;
93import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070094import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070095import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070096import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070097import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080098import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070099
Craig Mautner4a1cb222013-12-04 16:14:06 -0800100import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800101import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800103import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800104import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700105import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700106import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800107import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700108import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400109import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400110import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800111import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700112import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700113import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114
justinzhang5286d3f2014-05-12 17:06:01 -0400115
Craig Mautner8d341ef2013-03-26 09:03:27 -0700116import java.io.FileDescriptor;
117import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700118import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700119import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700120import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700121import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700122
Craig Mautner4a1cb222013-12-04 16:14:06 -0800123public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800124 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700125 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
126 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700127 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
128 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
129 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
130 private static final String TAG_STACK = TAG + POSTFIX_STACK;
131 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
132 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
133 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800134
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800135 static final boolean DEBUG = DEBUG_ALL || false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700136 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
137 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700138 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700139 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700140 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700141 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
142 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautnereb0e38a2015-04-02 20:22:54 -0700143 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700144 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700145
Craig Mautner2219a1b2013-03-25 09:44:30 -0700146 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700147
Craig Mautnerf3333272013-04-22 10:55:53 -0700148 /** How long we wait until giving up on the last activity telling us it is idle. */
149 static final int IDLE_TIMEOUT = 10*1000;
150
Craig Mautner0eea92c2013-05-16 13:35:39 -0700151 /** How long we can hold the sleep wake lock before giving up. */
152 static final int SLEEP_TIMEOUT = 5*1000;
153
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700154 // How long we can hold the launch wake lock before giving up.
155 static final int LAUNCH_TIMEOUT = 10*1000;
156
Craig Mautner05d29032013-05-03 13:40:13 -0700157 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
158 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
159 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700160 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700161 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800162 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
163 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
164 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700165 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400166 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
167 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700168 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700169 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700170 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800171
Craig Mautner4504de52013-12-20 09:06:56 -0800172 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700173
Jason Monk62515be2014-05-21 16:06:19 -0400174 private static final String LOCK_TASK_TAG = "Lock-to-App";
175
justinzhang5286d3f2014-05-12 17:06:01 -0400176 /** Status Bar Service **/
177 private IBinder mToken = new Binder();
178 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400179 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400180
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700181 // For debugging to make sure the caller when acquiring/releasing our
182 // wake lock is the system process.
183 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700184
Craig Mautner27084302013-03-25 08:05:25 -0700185 final ActivityManagerService mService;
186
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800187 private final RecentTasks mRecentTasks;
188
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700189 final ActivityStackSupervisorHandler mHandler;
190
191 /** Short cut */
192 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800193 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700194
Craig Mautner8d341ef2013-03-26 09:03:27 -0700195 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700196 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700197
198 /** Task identifier that activities are currently being started in. Incremented each time a
199 * new task is created. */
200 private int mCurTaskId = 0;
201
Craig Mautner2420ead2013-04-01 17:13:20 -0700202 /** The current user */
203 private int mCurrentUser;
204
Craig Mautnere0a38842013-12-16 16:14:02 -0800205 /** The stack containing the launcher app. Assumed to always be attached to
206 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700207 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700208
Craig Mautnere0a38842013-12-16 16:14:02 -0800209 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700210 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700211
Craig Mautner4a1cb222013-12-04 16:14:06 -0800212 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
213 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800214 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800215 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700216
217 /** List of activities that are waiting for a new activity to become visible before completing
218 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800219 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700220
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700221 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800222 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700223
224 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800225 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700226
Craig Mautnerde4ef022013-04-07 19:01:33 -0700227 /** List of activities that are ready to be stopped, but waiting for the next activity to
228 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800229 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700230
Craig Mautnerf3333272013-04-22 10:55:53 -0700231 /** List of activities that are ready to be finished, but waiting for the previous activity to
232 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800233 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700234
Craig Mautner0eea92c2013-05-16 13:35:39 -0700235 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800236 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700237
Craig Mautnerf3333272013-04-22 10:55:53 -0700238 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800239 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700240
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700241 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800242 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700243
Craig Mautnerde4ef022013-04-07 19:01:33 -0700244 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
245 * is being brought in front of us. */
246 boolean mUserLeaving = false;
247
Craig Mautner0eea92c2013-05-16 13:35:39 -0700248 /** Set when we have taken too long waiting to go to sleep. */
249 boolean mSleepTimeout = false;
250
Jose Lima58e66d62014-05-27 20:00:27 -0700251 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
252 * setWindowManager is called. **/
253 private boolean mLeanbackOnlyDevice;
254
Craig Mautner0eea92c2013-05-16 13:35:39 -0700255 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700256 * We don't want to allow the device to go to sleep while in the process
257 * of launching an activity. This is primarily to allow alarm intent
258 * receivers to launch an activity and get that to run before the device
259 * goes back to sleep.
260 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700261 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700262
263 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700264 * Set when the system is going to sleep, until we have
265 * successfully paused the current activity and released our wake lock.
266 * At that point the system is allowed to actually sleep.
267 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700268 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700269
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700270 /** Stack id of the front stack when user switched, indexed by userId. */
271 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700272
Craig Mautner4504de52013-12-20 09:06:56 -0800273 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800274 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700275 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800276
277 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700278 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800279
Jeff Brownca9bc702014-02-11 14:32:56 -0800280 InputManagerInternal mInputManagerInternal;
281
Craig Mautner15df08a2015-04-01 12:17:18 -0700282 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
283 * may be finished until there is only one entry left. If this is empty the system is not
284 * in lockTask mode. */
285 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000286 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700287 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
288 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000289 */
290 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400291 /**
292 * Notifies the user when entering/exiting lock-task.
293 */
294 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800295
Craig Mautner15df08a2015-04-01 12:17:18 -0700296 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700297
Craig Mautner42d04db2014-11-06 12:13:23 -0800298 /** Used to keep resumeTopActivityLocked() from being entered recursively */
299 boolean inResumeTopActivity;
300
Craig Mautneree36c772014-07-16 14:56:05 -0700301 /**
302 * Description of a request to start a new activity, which has been held
303 * due to app switches being disabled.
304 */
305 static class PendingActivityLaunch {
306 final ActivityRecord r;
307 final ActivityRecord sourceRecord;
308 final int startFlags;
309 final ActivityStack stack;
310
311 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
312 int _startFlags, ActivityStack _stack) {
313 r = _r;
314 sourceRecord = _sourceRecord;
315 startFlags = _startFlags;
316 stack = _stack;
317 }
318 }
319
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800320 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700321 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800322 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700323 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
324 }
325
326 /**
327 * At the time when the constructor runs, the power manager has not yet been
328 * initialized. So we initialize our wakelocks afterwards.
329 */
330 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800331 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700332 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700333 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700334 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700335 }
336
justinzhang5286d3f2014-05-12 17:06:01 -0400337 // This function returns a IStatusBarService. The value is from ServiceManager.
338 // getService and is cached.
339 private IStatusBarService getStatusBarService() {
340 synchronized (mService) {
341 if (mStatusBarService == null) {
342 mStatusBarService = IStatusBarService.Stub.asInterface(
343 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
344 if (mStatusBarService == null) {
345 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
346 }
347 }
348 return mStatusBarService;
349 }
350 }
351
Jason Monk35c62a42014-06-17 10:24:47 -0400352 private IDevicePolicyManager getDevicePolicyManager() {
353 synchronized (mService) {
354 if (mDevicePolicyManager == null) {
355 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
356 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
357 if (mDevicePolicyManager == null) {
358 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
359 }
360 }
361 return mDevicePolicyManager;
362 }
363 }
364
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700365 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800366 synchronized (mService) {
367 mWindowManager = wm;
368
369 mDisplayManager =
370 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
371 mDisplayManager.registerDisplayListener(this, null);
372
373 Display[] displays = mDisplayManager.getDisplays();
374 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
375 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800376 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700377 if (activityDisplay.mDisplay == null) {
378 throw new IllegalStateException("Default Display does not exist");
379 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800380 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381 }
382
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700383 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800384 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800385
386 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700387
388 // Initialize this here, now that we can get a valid reference to PackageManager.
389 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800390 }
Craig Mautner27084302013-03-25 08:05:25 -0700391 }
392
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200393 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700394 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200395 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700396 }
397
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700398 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800399 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700400 }
401
Craig Mautnerde4ef022013-04-07 19:01:33 -0700402 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800403 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700404 }
405
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800406 /** Top of all visible stacks is/should always be equal to the focused stack.
407 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
408 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700409 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700410 if (stack == null) {
411 return false;
412 }
413
Craig Mautnerdf88d732014-01-27 09:21:32 -0800414 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
415 if (parent != null) {
416 stack = parent.task.stack;
417 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800418 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700419 }
420
Craig Mautner299f9602015-01-26 09:47:33 -0800421 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800422 moveHomeStack(toFront, reason, null);
423 }
424
425 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800426 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800427 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800428 if (topNdx <= 0) {
429 return;
430 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700431
432 // The home stack should either be at the top or bottom of the stack list.
433 if ((toFront && (stacks.get(topNdx) != mHomeStack))
434 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700435 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700436 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
437 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800438 stacks.remove(mHomeStack);
439 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700440 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800441
442 if (lastFocusedStack != null) {
443 mLastFocusedStack = lastFocusedStack;
444 }
445 mFocusedStack = stacks.get(topNdx);
446
Craig Mautnerde313752015-01-22 14:28:03 -0800447 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
448 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800449 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800450
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800451 if (mService.mBooting || !mService.mBooted) {
452 final ActivityRecord r = topRunningActivityLocked();
453 if (r != null && r.idle) {
454 checkFinishBootingLocked();
455 }
456 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700457 }
458
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700459 /** Returns true if the focus activity was adjusted to the home stack top activity. */
460 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700461 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
462 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700463 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700464 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700465
Craig Mautner84984fa2014-06-19 11:19:20 -0700466 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700467
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700468 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700469 if (top == null) {
470 return false;
471 }
472 mService.setFocusedActivityLocked(top, reason);
473 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700474 }
475
Craig Mautner299f9602015-01-26 09:47:33 -0800476 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700477 if (!mService.mBooting && !mService.mBooted) {
478 // Not ready yet!
479 return false;
480 }
481
Craig Mautner84984fa2014-06-19 11:19:20 -0700482 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
483 mWindowManager.showRecentApps();
484 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700485 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700486
Craig Mautner84984fa2014-06-19 11:19:20 -0700487 if (prev != null) {
488 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
489 }
490
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700491 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
492 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700493 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800494 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700495 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700496 }
Craig Mautner299f9602015-01-26 09:47:33 -0800497 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700498 }
499
Craig Mautner8d341ef2013-03-26 09:03:27 -0700500 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700501 return anyTaskForIdLocked(id, true);
502 }
503
504 /**
505 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
506 * @param id Id of the task we would like returned.
507 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
508 * restore the task from recents to the active list.
509 */
510 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800511 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800512 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800513 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800514 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
515 ActivityStack stack = stacks.get(stackNdx);
516 TaskRecord task = stack.taskForIdLocked(id);
517 if (task != null) {
518 return task;
519 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700520 }
521 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800522
523 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700524 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800525 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800526 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700527 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800528 return null;
529 }
530
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700531 if (!restoreFromRecents) {
532 return task;
533 }
534
Wale Ogunwale7de05352014-12-12 15:21:33 -0800535 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700536 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
537 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800538 return null;
539 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700540 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800541 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700542 }
543
Craig Mautner6170f732013-04-02 13:05:23 -0700544 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800545 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800546 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800547 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800548 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
549 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
550 if (r != null) {
551 return r;
552 }
Craig Mautner6170f732013-04-02 13:05:23 -0700553 }
554 }
555 return null;
556 }
557
Craig Mautneref73ee12014-04-23 11:45:37 -0700558 void setNextTaskId(int taskId) {
559 if (taskId > mCurTaskId) {
560 mCurTaskId = taskId;
561 }
562 }
563
Craig Mautner8d341ef2013-03-26 09:03:27 -0700564 int getNextTaskId() {
565 do {
566 mCurTaskId++;
567 if (mCurTaskId <= 0) {
568 mCurTaskId = 1;
569 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700570 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700571 return mCurTaskId;
572 }
573
Craig Mautnerde4ef022013-04-07 19:01:33 -0700574 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800575 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700576 if (stack == null) {
577 return null;
578 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700579 ActivityRecord resumedActivity = stack.mResumedActivity;
580 if (resumedActivity == null || resumedActivity.app == null) {
581 resumedActivity = stack.mPausingActivity;
582 if (resumedActivity == null || resumedActivity.app == null) {
583 resumedActivity = stack.topRunningActivityLocked(null);
584 }
585 }
586 return resumedActivity;
587 }
588
Dianne Hackbornff072722014-09-24 10:56:28 -0700589 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700590 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800592 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
593 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
595 final ActivityStack stack = stacks.get(stackNdx);
596 if (!isFrontStack(stack)) {
597 continue;
598 }
599 ActivityRecord hr = stack.topRunningActivityLocked(null);
600 if (hr != null) {
601 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
602 && processName.equals(hr.processName)) {
603 try {
George Mount2c92c972014-03-20 09:38:23 -0700604 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605 didSomething = true;
606 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700607 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800608 Slog.w(TAG, "Exception in new application when starting activity "
609 + hr.intent.getComponent().flattenToShortString(), e);
610 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700611 }
Craig Mautner20e72272013-04-01 13:45:53 -0700612 }
Craig Mautner20e72272013-04-01 13:45:53 -0700613 }
614 }
615 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700616 if (!didSomething) {
617 ensureActivitiesVisibleLocked(null, 0);
618 }
Craig Mautner20e72272013-04-01 13:45:53 -0700619 return didSomething;
620 }
621
622 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800623 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
624 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
626 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800627 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800628 continue;
629 }
630 final ActivityRecord resumedActivity = stack.mResumedActivity;
631 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800632 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
633 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 return false;
635 }
Craig Mautner20e72272013-04-01 13:45:53 -0700636 }
637 }
638 return true;
639 }
640
Craig Mautnerde4ef022013-04-07 19:01:33 -0700641 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800642 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
643 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800644 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
645 final ActivityStack stack = stacks.get(stackNdx);
646 if (isFrontStack(stack)) {
647 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700648 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800649 return false;
650 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700651 }
652 }
653 }
654 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700655 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800656 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
657 mLastFocusedStack + " to=" + mFocusedStack);
658 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700659 return true;
660 }
661
662 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800663 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800664 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
665 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800666 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
667 final ActivityStack stack = stacks.get(stackNdx);
668 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800669 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700670 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800671 return false;
672 }
673 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700675 }
676 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800677 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700678 }
679
Craig Mautner2acc3892013-09-23 10:28:14 -0700680 /**
681 * Pause all activities in either all of the stacks or just the back stacks.
682 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700683 * @return true if any activity was paused as a result of this call.
684 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700685 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700686 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800687 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
688 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800689 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
690 final ActivityStack stack = stacks.get(stackNdx);
691 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
692 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
693 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700694 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
695 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800696 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700697 }
698 }
699 return someActivityPaused;
700 }
701
Craig Mautnerde4ef022013-04-07 19:01:33 -0700702 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700703 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800704 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
705 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
707 final ActivityStack stack = stacks.get(stackNdx);
708 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700709 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800710 if (DEBUG_STATES) {
711 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
712 pausing = false;
713 } else {
714 return false;
715 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700716 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700717 }
718 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700719 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700720 }
721
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700722 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
723 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500724 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
726 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
728 final ActivityStack stack = stacks.get(stackNdx);
729 if (stack.mResumedActivity != null &&
730 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700731 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800732 }
733 }
734 }
735 }
736
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700737 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700738 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700739 }
740
741 void sendWaitingVisibleReportLocked(ActivityRecord r) {
742 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700743 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700744 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700745 if (w.who == null) {
746 changed = true;
747 w.timeout = false;
748 if (r != null) {
749 w.who = new ComponentName(r.info.packageName, r.info.name);
750 }
751 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
752 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700753 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700754 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700755 if (changed) {
756 mService.notifyAll();
757 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700758 }
759
760 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
761 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700762 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700763 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700764 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700765 if (w.who == null) {
766 changed = true;
767 w.timeout = timeout;
768 if (r != null) {
769 w.who = new ComponentName(r.info.packageName, r.info.name);
770 }
771 w.thisTime = thisTime;
772 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700773 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700774 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700775 if (changed) {
776 mService.notifyAll();
777 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700778 }
779
Craig Mautner29219d92013-04-16 20:19:12 -0700780 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800781 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700782 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
783 if (r != null) {
784 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700785 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700786
Craig Mautner4a1cb222013-12-04 16:14:06 -0800787 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800788 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800789 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
790 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700791 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700792 r = stack.topRunningActivityLocked(null);
793 if (r != null) {
794 return r;
795 }
796 }
797 }
798 return null;
799 }
800
Dianne Hackborn09233282014-04-30 11:33:59 -0700801 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700802 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800803 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
804 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800805 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800806 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800807 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800808 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
809 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700810 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800811 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700812 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700813 }
814 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700815
816 // The lists are already sorted from most recent to oldest. Just pull the most recent off
817 // each list and add it to list. Stop when all lists are empty or maxNum reached.
818 while (maxNum > 0) {
819 long mostRecentActiveTime = Long.MIN_VALUE;
820 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800821 final int numTaskLists = runningTaskLists.size();
822 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
823 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700824 if (!stackTaskList.isEmpty()) {
825 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
826 if (lastActiveTime > mostRecentActiveTime) {
827 mostRecentActiveTime = lastActiveTime;
828 selectedStackList = stackTaskList;
829 }
830 }
831 }
832 if (selectedStackList != null) {
833 list.add(selectedStackList.remove(0));
834 --maxNum;
835 } else {
836 break;
837 }
838 }
Craig Mautner20e72272013-04-01 13:45:53 -0700839 }
840
Craig Mautner23ac33b2013-04-01 16:26:35 -0700841 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700842 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700843 // Collect information about the target of the Intent.
844 ActivityInfo aInfo;
845 try {
846 ResolveInfo rInfo =
847 AppGlobals.getPackageManager().resolveIntent(
848 intent, resolvedType,
849 PackageManager.MATCH_DEFAULT_ONLY
850 | ActivityManagerService.STOCK_PM_FLAGS, userId);
851 aInfo = rInfo != null ? rInfo.activityInfo : null;
852 } catch (RemoteException e) {
853 aInfo = null;
854 }
855
856 if (aInfo != null) {
857 // Store the found target back into the intent, because now that
858 // we have it we never want to do this again. For example, if the
859 // user navigates back to this point in the history, we should
860 // always restart the exact same activity.
861 intent.setComponent(new ComponentName(
862 aInfo.applicationInfo.packageName, aInfo.name));
863
864 // Don't debug things in the system process
865 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
866 if (!aInfo.processName.equals("system")) {
867 mService.setDebugApp(aInfo.processName, true, false);
868 }
869 }
870
871 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
872 if (!aInfo.processName.equals("system")) {
873 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
874 }
875 }
876
Jeff Hao1b012d32014-08-20 10:35:34 -0700877 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700878 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700879 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700880 }
881 }
882 }
883 return aInfo;
884 }
885
Craig Mautner299f9602015-01-26 09:47:33 -0800886 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
887 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700888 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
889 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700890 }
891
Craig Mautner23ac33b2013-04-01 16:26:35 -0700892 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700893 String callingPackage, Intent intent, String resolvedType,
894 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700895 IBinder resultTo, String resultWho, int requestCode, int startFlags,
896 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700897 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700898 // Refuse possible leaked file descriptors
899 if (intent != null && intent.hasFileDescriptors()) {
900 throw new IllegalArgumentException("File descriptors passed in Intent");
901 }
902 boolean componentSpecified = intent.getComponent() != null;
903
904 // Don't modify the client's object!
905 intent = new Intent(intent);
906
907 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700908 ActivityInfo aInfo =
909 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700910
Craig Mautnere0a38842013-12-16 16:14:02 -0800911 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700912 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800913 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700914 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800915 // Cannot start a child activity if the parent is not resumed.
916 return ActivityManager.START_CANCELED;
917 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700918 final int realCallingPid = Binder.getCallingPid();
919 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700920 int callingPid;
921 if (callingUid >= 0) {
922 callingPid = -1;
923 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700924 callingPid = realCallingPid;
925 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926 } else {
927 callingPid = callingUid = -1;
928 }
929
Craig Mautnere0a38842013-12-16 16:14:02 -0800930 final ActivityStack stack;
931 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800932 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800933 } else {
934 stack = container.mStack;
935 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800936 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700937 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700938 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700939
940 final long origId = Binder.clearCallingIdentity();
941
942 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800943 (aInfo.applicationInfo.privateFlags
944 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700945 // This may be a heavy-weight process! Check to see if we already
946 // have another, different heavy-weight process running.
947 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
948 if (mService.mHeavyWeightProcess != null &&
949 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
950 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700951 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700952 if (caller != null) {
953 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
954 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700955 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700956 } else {
957 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700958 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700959 + intent.toString());
960 ActivityOptions.abort(options);
961 return ActivityManager.START_PERMISSION_DENIED;
962 }
963 }
964
965 IIntentSender target = mService.getIntentSenderLocked(
966 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700967 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700968 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
969 | PendingIntent.FLAG_ONE_SHOT, null);
970
971 Intent newIntent = new Intent();
972 if (requestCode >= 0) {
973 // Caller is requesting a result.
974 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
975 }
976 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
977 new IntentSender(target));
978 if (mService.mHeavyWeightProcess.activities.size() > 0) {
979 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
980 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
981 hist.packageName);
982 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
983 hist.task.taskId);
984 }
985 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
986 aInfo.packageName);
987 newIntent.setFlags(intent.getFlags());
988 newIntent.setClassName("android",
989 HeavyWeightSwitcherActivity.class.getName());
990 intent = newIntent;
991 resolvedType = null;
992 caller = null;
993 callingUid = Binder.getCallingUid();
994 callingPid = Binder.getCallingPid();
995 componentSpecified = true;
996 try {
997 ResolveInfo rInfo =
998 AppGlobals.getPackageManager().resolveIntent(
999 intent, null,
1000 PackageManager.MATCH_DEFAULT_ONLY
1001 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1002 aInfo = rInfo != null ? rInfo.activityInfo : null;
1003 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1004 } catch (RemoteException e) {
1005 aInfo = null;
1006 }
1007 }
1008 }
1009 }
1010
Dianne Hackborn91097de2014-04-04 18:02:06 -07001011 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1012 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001013 requestCode, callingPid, callingUid, callingPackage,
1014 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001015 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001016
Craig Mautner85c11a82014-07-17 12:38:18 -07001017 Binder.restoreCallingIdentity(origId);
1018
Craig Mautnerde4ef022013-04-07 19:01:33 -07001019 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001020 // If the caller also wants to switch to a new configuration,
1021 // do so now. This allows a clean switch, as we are waiting
1022 // for the current activity to pause (so we will not destroy
1023 // it), and have not yet started the next activity.
1024 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1025 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001026 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001027 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001028 "Updating to new configuration after starting activity.");
1029 mService.updateConfigurationLocked(config, null, false, false);
1030 }
1031
Craig Mautner23ac33b2013-04-01 16:26:35 -07001032 if (outResult != null) {
1033 outResult.result = res;
1034 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001035 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001036 do {
1037 try {
1038 mService.wait();
1039 } catch (InterruptedException e) {
1040 }
1041 } while (!outResult.timeout && outResult.who == null);
1042 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001043 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001044 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001045 outResult.timeout = false;
1046 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1047 outResult.totalTime = 0;
1048 outResult.thisTime = 0;
1049 } else {
1050 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001051 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001052 do {
1053 try {
1054 mService.wait();
1055 } catch (InterruptedException e) {
1056 }
1057 } while (!outResult.timeout && outResult.who == null);
1058 }
1059 }
1060 }
1061
1062 return res;
1063 }
1064 }
1065
1066 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1067 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1068 Bundle options, int userId) {
1069 if (intents == null) {
1070 throw new NullPointerException("intents is null");
1071 }
1072 if (resolvedTypes == null) {
1073 throw new NullPointerException("resolvedTypes is null");
1074 }
1075 if (intents.length != resolvedTypes.length) {
1076 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1077 }
1078
Craig Mautner23ac33b2013-04-01 16:26:35 -07001079
1080 int callingPid;
1081 if (callingUid >= 0) {
1082 callingPid = -1;
1083 } else if (caller == null) {
1084 callingPid = Binder.getCallingPid();
1085 callingUid = Binder.getCallingUid();
1086 } else {
1087 callingPid = callingUid = -1;
1088 }
1089 final long origId = Binder.clearCallingIdentity();
1090 try {
1091 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001092 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001093 for (int i=0; i<intents.length; i++) {
1094 Intent intent = intents[i];
1095 if (intent == null) {
1096 continue;
1097 }
1098
1099 // Refuse possible leaked file descriptors
1100 if (intent != null && intent.hasFileDescriptors()) {
1101 throw new IllegalArgumentException("File descriptors passed in Intent");
1102 }
1103
1104 boolean componentSpecified = intent.getComponent() != null;
1105
1106 // Don't modify the client's object!
1107 intent = new Intent(intent);
1108
1109 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001110 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001111 // TODO: New, check if this is correct
1112 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1113
1114 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001115 (aInfo.applicationInfo.privateFlags
1116 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001117 throw new IllegalArgumentException(
1118 "FLAG_CANT_SAVE_STATE not supported here");
1119 }
1120
1121 Bundle theseOptions;
1122 if (options != null && i == intents.length-1) {
1123 theseOptions = options;
1124 } else {
1125 theseOptions = null;
1126 }
Craig Mautner6170f732013-04-02 13:05:23 -07001127 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001128 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1129 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001130 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001131 if (res < 0) {
1132 return res;
1133 }
1134
1135 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1136 }
1137 }
1138 } finally {
1139 Binder.restoreCallingIdentity(origId);
1140 }
1141
1142 return ActivityManager.START_SUCCESS;
1143 }
1144
Craig Mautner2420ead2013-04-01 17:13:20 -07001145 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001146 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001147 throws RemoteException {
1148
Craig Mautner2568c3a2015-03-26 14:22:34 -07001149 if (andResume) {
1150 r.startFreezingScreenLocked(app, 0);
1151 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001152
Craig Mautner2568c3a2015-03-26 14:22:34 -07001153 // schedule launch ticks to collect information about slow apps.
1154 r.startLaunchTickingLocked();
1155 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001156
1157 // Have the window manager re-evaluate the orientation of
1158 // the screen based on the new activity order. Note that
1159 // as a result of this, it can call back into the activity
1160 // manager with a new orientation. We don't care about that,
1161 // because the activity is not currently running so we are
1162 // just restarting it anyway.
1163 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001164 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001165 mService.mConfiguration,
1166 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1167 mService.updateConfigurationLocked(config, r, false, false);
1168 }
1169
1170 r.app = app;
1171 app.waitingToKill = null;
1172 r.launchCount++;
1173 r.lastLaunchTime = SystemClock.uptimeMillis();
1174
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001175 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001176
1177 int idx = app.activities.indexOf(r);
1178 if (idx < 0) {
1179 app.activities.add(r);
1180 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001181 mService.updateLruProcessLocked(app, true, null);
1182 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001183
Craig Mautner15df08a2015-04-01 12:17:18 -07001184 final TaskRecord task = r.task;
1185 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
1186 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "lockTaskLaunchMode attribute");
1187 }
1188
1189 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001190 try {
1191 if (app.thread == null) {
1192 throw new RemoteException();
1193 }
1194 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001195 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001196 if (andResume) {
1197 results = r.results;
1198 newIntents = r.newIntents;
1199 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001200 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1201 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1202 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001203 if (andResume) {
1204 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1205 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001206 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001207 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001208 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001209 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001210 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001211 }
1212 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1213 r.sleeping = false;
1214 r.forceNewConfig = false;
1215 mService.showAskCompatModeDialogLocked(r);
1216 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001217 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001218 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1219 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1220 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001221 final String profileFile = mService.mProfileFile;
1222 if (profileFile != null) {
1223 ParcelFileDescriptor profileFd = mService.mProfileFd;
1224 if (profileFd != null) {
1225 try {
1226 profileFd = profileFd.dup();
1227 } catch (IOException e) {
1228 if (profileFd != null) {
1229 try {
1230 profileFd.close();
1231 } catch (IOException o) {
1232 }
1233 profileFd = null;
1234 }
1235 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001236 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001237
1238 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1239 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 }
1241 }
1242 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001243
Craig Mautner2568c3a2015-03-26 14:22:34 -07001244 if (andResume) {
1245 app.hasShownUi = true;
1246 app.pendingUiClean = true;
1247 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001248 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001250 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001251 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001252 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001253 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001254
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001255 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001256 // This may be a heavy-weight process! Note that the package
1257 // manager will ensure that only activity can run in the main
1258 // process of the .apk, which is the only thing that will be
1259 // considered heavy-weight.
1260 if (app.processName.equals(app.info.packageName)) {
1261 if (mService.mHeavyWeightProcess != null
1262 && mService.mHeavyWeightProcess != app) {
1263 Slog.w(TAG, "Starting new heavy weight process " + app
1264 + " when already running "
1265 + mService.mHeavyWeightProcess);
1266 }
1267 mService.mHeavyWeightProcess = app;
1268 Message msg = mService.mHandler.obtainMessage(
1269 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1270 msg.obj = r;
1271 mService.mHandler.sendMessage(msg);
1272 }
1273 }
1274
1275 } catch (RemoteException e) {
1276 if (r.launchFailed) {
1277 // This is the second time we failed -- finish activity
1278 // and give up.
1279 Slog.e(TAG, "Second failure launching "
1280 + r.intent.getComponent().flattenToShortString()
1281 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001282 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001283 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1284 "2nd-crash", false);
1285 return false;
1286 }
1287
1288 // This is the first time we failed -- restart process and
1289 // retry.
1290 app.activities.remove(r);
1291 throw e;
1292 }
1293
1294 r.launchFailed = false;
1295 if (stack.updateLRUListLocked(r)) {
1296 Slog.w(TAG, "Activity " + r
1297 + " being launched, but already in LRU list");
1298 }
1299
1300 if (andResume) {
1301 // As part of the process of launching, ActivityThread also performs
1302 // a resume.
1303 stack.minimalResumeActivityLocked(r);
1304 } else {
1305 // This activity is not starting in the resumed state... which
1306 // should look like we asked it to pause+stop (but remain visible),
1307 // and it has done so and reported back the current icicle and
1308 // other state.
1309 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1310 + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001311 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001312 r.stopped = true;
1313 }
1314
1315 // Launch the new version setup screen if needed. We do this -after-
1316 // launching the initial activity (that is, home), so that it can have
1317 // a chance to initialize itself while in the background, making the
1318 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001319 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001320 mService.startSetupActivityLocked();
1321 }
1322
Dianne Hackborn465fa392014-09-14 14:21:18 -07001323 // Update any services we are bound to that might care about whether
1324 // their client may have activities.
1325 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1326
Craig Mautner2420ead2013-04-01 17:13:20 -07001327 return true;
1328 }
1329
Craig Mautnere79d42682013-04-01 19:01:53 -07001330 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001331 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001332 // Is this activity's application already running?
1333 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001334 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001335
1336 r.task.stack.setLaunchTime(r);
1337
1338 if (app != null && app.thread != null) {
1339 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001340 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1341 || !"android".equals(r.info.packageName)) {
1342 // Don't add this if it is a platform component that is marked
1343 // to run in multiple processes, because this is actually
1344 // part of the framework so doesn't make sense to track as a
1345 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001346 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1347 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001348 }
George Mount2c92c972014-03-20 09:38:23 -07001349 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001350 return;
1351 } catch (RemoteException e) {
1352 Slog.w(TAG, "Exception when starting activity "
1353 + r.intent.getComponent().flattenToShortString(), e);
1354 }
1355
1356 // If a dead object exception was thrown -- fall through to
1357 // restart the application.
1358 }
1359
1360 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001361 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001362 }
1363
Craig Mautner6170f732013-04-02 13:05:23 -07001364 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001365 Intent intent, String resolvedType, ActivityInfo aInfo,
1366 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1367 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001368 int callingPid, int callingUid, String callingPackage,
1369 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001370 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1371 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001372 int err = ActivityManager.START_SUCCESS;
1373
1374 ProcessRecord callerApp = null;
1375 if (caller != null) {
1376 callerApp = mService.getRecordForAppLocked(caller);
1377 if (callerApp != null) {
1378 callingPid = callerApp.pid;
1379 callingUid = callerApp.info.uid;
1380 } else {
1381 Slog.w(TAG, "Unable to find app for caller " + caller
1382 + " (pid=" + callingPid + ") when starting: "
1383 + intent.toString());
1384 err = ActivityManager.START_PERMISSION_DENIED;
1385 }
1386 }
1387
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001388 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1389
Craig Mautner6170f732013-04-02 13:05:23 -07001390 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001391 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001392 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001393 + " on display " + (container == null ? (mFocusedStack == null ?
1394 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1395 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1396 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001397 }
1398
1399 ActivityRecord sourceRecord = null;
1400 ActivityRecord resultRecord = null;
1401 if (resultTo != null) {
1402 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001403 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1404 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001405 if (sourceRecord != null) {
1406 if (requestCode >= 0 && !sourceRecord.finishing) {
1407 resultRecord = sourceRecord;
1408 }
1409 }
1410 }
Craig Mautner6170f732013-04-02 13:05:23 -07001411
Dianne Hackborn91097de2014-04-04 18:02:06 -07001412 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001413
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001414 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001415 // Transfer the result target from the source activity to the new
1416 // one being started, including any failures.
1417 if (requestCode >= 0) {
1418 ActivityOptions.abort(options);
1419 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1420 }
1421 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001422 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1423 resultRecord = null;
1424 }
Craig Mautner6170f732013-04-02 13:05:23 -07001425 resultWho = sourceRecord.resultWho;
1426 requestCode = sourceRecord.requestCode;
1427 sourceRecord.resultTo = null;
1428 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001429 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001430 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001431 if (sourceRecord.launchedFromUid == callingUid) {
1432 // The new activity is being launched from the same uid as the previous
1433 // activity in the flow, and asking to forward its result back to the
1434 // previous. In this case the activity is serving as a trampoline between
1435 // the two, so we also want to update its launchedFromPackage to be the
1436 // same as the previous activity. Note that this is safe, since we know
1437 // these two packages come from the same uid; the caller could just as
1438 // well have supplied that same package name itself. This specifially
1439 // deals with the case of an intent picker/chooser being launched in the app
1440 // flow to redirect to an activity picked by the user, where we want the final
1441 // activity to consider it to have been launched by the previous app activity.
1442 callingPackage = sourceRecord.launchedFromPackage;
1443 }
Craig Mautner6170f732013-04-02 13:05:23 -07001444 }
1445
1446 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1447 // We couldn't find a class that can handle the given Intent.
1448 // That's the end of that!
1449 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1450 }
1451
1452 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1453 // We couldn't find the specific class specified in the Intent.
1454 // Also the end of the line.
1455 err = ActivityManager.START_CLASS_NOT_FOUND;
1456 }
1457
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001458 if (err == ActivityManager.START_SUCCESS
1459 && !isCurrentProfileLocked(userId)
1460 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1461 // Trying to launch a background activity that doesn't show for all users.
1462 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1463 }
1464
Dianne Hackborn91097de2014-04-04 18:02:06 -07001465 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1466 && sourceRecord.task.voiceSession != null) {
1467 // If this activity is being launched as part of a voice session, we need
1468 // to ensure that it is safe to do so. If the upcoming activity will also
1469 // be part of the voice session, we can only launch it if it has explicitly
1470 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001471 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001472 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1473 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001474 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1475 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001476 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1477 }
1478 } catch (RemoteException e) {
1479 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1480 }
1481 }
1482 }
1483
1484 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1485 // If the caller is starting a new voice session, just make sure the target
1486 // is actually allowing it to run this way.
1487 try {
1488 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1489 intent, resolvedType)) {
1490 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1491 }
1492 } catch (RemoteException e) {
1493 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1494 }
1495 }
1496
louis_changcd5d1982014-08-01 10:09:08 +08001497 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1498
Craig Mautner6170f732013-04-02 13:05:23 -07001499 if (err != ActivityManager.START_SUCCESS) {
1500 if (resultRecord != null) {
1501 resultStack.sendActivityResultLocked(-1,
1502 resultRecord, resultWho, requestCode,
1503 Activity.RESULT_CANCELED, null);
1504 }
Craig Mautner6170f732013-04-02 13:05:23 -07001505 ActivityOptions.abort(options);
1506 return err;
1507 }
1508
1509 final int startAnyPerm = mService.checkPermission(
1510 START_ANY_ACTIVITY, callingPid, callingUid);
1511 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1512 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1513 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1514 if (resultRecord != null) {
1515 resultStack.sendActivityResultLocked(-1,
1516 resultRecord, resultWho, requestCode,
1517 Activity.RESULT_CANCELED, null);
1518 }
Craig Mautner6170f732013-04-02 13:05:23 -07001519 String msg;
1520 if (!aInfo.exported) {
1521 msg = "Permission Denial: starting " + intent.toString()
1522 + " from " + callerApp + " (pid=" + callingPid
1523 + ", uid=" + callingUid + ")"
1524 + " not exported from uid " + aInfo.applicationInfo.uid;
1525 } else {
1526 msg = "Permission Denial: starting " + intent.toString()
1527 + " from " + callerApp + " (pid=" + callingPid
1528 + ", uid=" + callingUid + ")"
1529 + " requires " + aInfo.permission;
1530 }
1531 Slog.w(TAG, msg);
1532 throw new SecurityException(msg);
1533 }
1534
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001535 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001536 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001537
Craig Mautner6170f732013-04-02 13:05:23 -07001538 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001539 try {
1540 // The Intent we give to the watcher has the extra data
1541 // stripped off, since it can contain private information.
1542 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001543 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001544 aInfo.applicationInfo.packageName);
1545 } catch (RemoteException e) {
1546 mService.mController = null;
1547 }
Ben Gruver5e207332013-04-03 17:41:37 -07001548 }
Craig Mautner6170f732013-04-02 13:05:23 -07001549
Ben Gruver5e207332013-04-03 17:41:37 -07001550 if (abort) {
1551 if (resultRecord != null) {
1552 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001553 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001554 }
Ben Gruver5e207332013-04-03 17:41:37 -07001555 // We pretend to the caller that it was really started, but
1556 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001557 ActivityOptions.abort(options);
1558 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001559 }
1560
1561 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001562 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001563 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001564 if (outActivity != null) {
1565 outActivity[0] = r;
1566 }
1567
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001568 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001569 if (voiceSession == null && (stack.mResumedActivity == null
1570 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001571 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1572 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001573 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001574 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001575 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001576 ActivityOptions.abort(options);
1577 return ActivityManager.START_SWITCHES_CANCELED;
1578 }
1579 }
1580
1581 if (mService.mDidAppSwitch) {
1582 // This is the second allowed switch since we stopped switches,
1583 // so now just generally allow switches. Use case: user presses
1584 // home (switches disabled, switch to home, mDidAppSwitch now true);
1585 // user taps a home icon (coming from home so allowed, we hit here
1586 // and now allow anyone to switch again).
1587 mService.mAppSwitchesAllowedTime = 0;
1588 } else {
1589 mService.mDidAppSwitch = true;
1590 }
1591
Craig Mautneree36c772014-07-16 14:56:05 -07001592 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001593
Dianne Hackborn91097de2014-04-04 18:02:06 -07001594 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001595 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001596
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001597 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001598 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001599 // activity start, but we are not actually doing an activity
1600 // switch... just dismiss the keyguard now, because we
1601 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001602 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001603 }
1604 return err;
1605 }
1606
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001607 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001608 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001609
1610 // On leanback only devices we should keep all activities in the same stack.
1611 if (!mLeanbackOnlyDevice &&
1612 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001613
1614 ActivityStack stack;
1615
Wale Ogunwale7d701172015-03-11 15:36:30 -07001616 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001617 stack = task.stack;
1618 if (stack.isOnHomeDisplay()) {
1619 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001620 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1621 "computeStackFocus: Setting " + "focused stack to r=" + r
1622 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001623 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001624 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001625 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001626 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001627 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001628 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001629 }
1630
Craig Mautnere0a38842013-12-16 16:14:02 -08001631 final ActivityContainer container = r.mInitialActivityContainer;
1632 if (container != null) {
1633 // The first time put it on the desired stack, after this put on task stack.
1634 r.mInitialActivityContainer = null;
1635 return container.mStack;
1636 }
1637
Craig Mautner1b4bf852014-05-26 15:06:32 -07001638 if (mFocusedStack != mHomeStack && (!newTask ||
1639 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001640 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001641 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001642 return mFocusedStack;
1643 }
1644
Craig Mautnere0a38842013-12-16 16:14:02 -08001645 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1646 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001647 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001648 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001649 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001650 "computeStackFocus: Setting focused stack=" + stack);
1651 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001652 }
1653 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001654
Craig Mautner4a1cb222013-12-04 16:14:06 -08001655 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001656 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001657 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1658 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001659 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001660 }
1661 return mHomeStack;
1662 }
1663
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001664 boolean setFocusedStack(ActivityRecord r, String reason) {
1665 if (r == null) {
1666 // Not sure what you are trying to do, but it is not going to work...
1667 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001668 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001669 final TaskRecord task = r.task;
1670 if (task == null || task.stack == null) {
1671 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1672 return false;
1673 }
1674 task.stack.moveToFront(reason);
1675 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001676 }
1677
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001678 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001679 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001680 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001681 final Intent intent = r.intent;
1682 final int callingUid = r.launchedFromUid;
1683
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001684 // In some flows in to this function, we retrieve the task record and hold on to it
1685 // without a lock before calling back in to here... so the task at this point may
1686 // not actually be in recents. Check for that, and if it isn't in recents just
1687 // consider it invalid.
1688 if (inTask != null && !inTask.inRecents) {
1689 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1690 inTask = null;
1691 }
1692
Craig Mautnerad400af2014-08-13 16:41:36 -07001693 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001694 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1695 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001696
Craig Mautnera228ae92014-07-09 05:44:55 -07001697 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001698 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001699 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001700 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1701 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1702 "\"singleInstance\" or \"singleTask\"");
1703 launchFlags &=
1704 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1705 } else {
1706 switch (r.info.documentLaunchMode) {
1707 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1708 break;
1709 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1710 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1711 break;
1712 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1713 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1714 break;
1715 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1716 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1717 break;
1718 }
1719 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001720
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001721 final boolean launchTaskBehind = r.mLaunchTaskBehind
1722 && !launchSingleTask && !launchSingleInstance
1723 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001724
Wale Ogunwale7d701172015-03-11 15:36:30 -07001725 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1726 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001727 // For whatever reason this activity is being launched into a new
1728 // task... yet the caller has requested a result back. Well, that
1729 // is pretty messed up, so instead immediately send back a cancel
1730 // and let the new task continue launched as normal without a
1731 // dependency on its originator.
1732 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1733 r.resultTo.task.stack.sendActivityResultLocked(-1,
1734 r.resultTo, r.resultWho, r.requestCode,
1735 Activity.RESULT_CANCELED, null);
1736 r.resultTo = null;
1737 }
1738
1739 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1740 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1741 }
1742
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001743 // If we are actually going to launch in to a new task, there are some cases where
1744 // we further want to do multiple task.
1745 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1746 if (launchTaskBehind
1747 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1748 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1749 }
1750 }
1751
Craig Mautner8849a5e2013-04-02 16:41:03 -07001752 // We'll invoke onUserLeaving before onPause only if the launching
1753 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001754 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001755 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1756 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001757
1758 // If the caller has asked not to resume at this point, we make note
1759 // of this in the record so that we can skip it when trying to find
1760 // the top running activity.
1761 if (!doResume) {
1762 r.delayedResume = true;
1763 }
1764
Craig Mautnera254cd72014-05-25 16:47:39 -07001765 ActivityRecord notTop =
1766 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001767
1768 // If the onlyIfNeeded flag is set, then we can do this if the activity
1769 // being launched is the same as the one making the call... or, as
1770 // a special case, if we do not know the caller then we count the
1771 // current top activity as the caller.
1772 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1773 ActivityRecord checkedCaller = sourceRecord;
1774 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001775 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001776 }
1777 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1778 // Caller is not the same as launcher, so always needed.
1779 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1780 }
1781 }
1782
Craig Mautner8849a5e2013-04-02 16:41:03 -07001783 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001784 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001785
1786 // If the caller is not coming from another activity, but has given us an
1787 // explicit task into which they would like us to launch the new activity,
1788 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001789 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1790 final Intent baseIntent = inTask.getBaseIntent();
1791 final ActivityRecord root = inTask.getRootActivity();
1792 if (baseIntent == null) {
1793 ActivityOptions.abort(options);
1794 throw new IllegalArgumentException("Launching into task without base intent: "
1795 + inTask);
1796 }
1797
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001798 // If this task is empty, then we are adding the first activity -- it
1799 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001800 if (launchSingleInstance || launchSingleTask) {
1801 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1802 ActivityOptions.abort(options);
1803 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1804 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001805 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001806 if (root != null) {
1807 ActivityOptions.abort(options);
1808 throw new IllegalArgumentException("Caller with inTask " + inTask
1809 + " has root " + root + " but target is singleInstance/Task");
1810 }
1811 }
1812
1813 // If task is empty, then adopt the interesting intent launch flags in to the
1814 // activity being started.
1815 if (root == null) {
1816 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1817 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1818 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1819 launchFlags = (launchFlags&~flagsOfInterest)
1820 | (baseIntent.getFlags()&flagsOfInterest);
1821 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001822 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001823 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001824
Dianne Hackborn962d5352014-08-29 16:27:40 -07001825 // If the task is not empty and the caller is asking to start it as the root
1826 // of a new task, then we don't actually want to start this on the task. We
1827 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001828 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001829 addingToTask = false;
1830
1831 } else {
1832 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001833 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001834
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001835 reuseTask = inTask;
1836 } else {
1837 inTask = null;
1838 }
1839
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001840 if (inTask == null) {
1841 if (sourceRecord == null) {
1842 // This activity is not being started from another... in this
1843 // case we -always- start a new task.
1844 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1845 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1846 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1847 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1848 }
1849 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1850 // The original activity who is starting us is running as a single
1851 // instance... this new activity it is starting must go on its
1852 // own task.
1853 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1854 } else if (launchSingleInstance || launchSingleTask) {
1855 // The activity being started is a single instance... it always
1856 // gets launched into its own task.
1857 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1858 }
1859 }
1860
1861 ActivityInfo newTaskInfo = null;
1862 Intent newTaskIntent = null;
1863 ActivityStack sourceStack;
1864 if (sourceRecord != null) {
1865 if (sourceRecord.finishing) {
1866 // If the source is finishing, we can't further count it as our source. This
1867 // is because the task it is associated with may now be empty and on its way out,
1868 // so we don't want to blindly throw it in to that task. Instead we will take
1869 // the NEW_TASK flow and try to find a task for it. But save the task information
1870 // so it can be used when creating the new task.
1871 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1872 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1873 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1874 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1875 newTaskInfo = sourceRecord.info;
1876 newTaskIntent = sourceRecord.task.intent;
1877 }
1878 sourceRecord = null;
1879 sourceStack = null;
1880 } else {
1881 sourceStack = sourceRecord.task.stack;
1882 }
1883 } else {
1884 sourceStack = null;
1885 }
1886
1887 boolean movedHome = false;
1888 ActivityStack targetStack;
1889
1890 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001891 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001892
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001893 // We may want to try to place the new activity in to an existing task. We always
1894 // do this if the target activity is singleTask or singleInstance; we will also do
1895 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1896 // us to still place it in a new task: multi task, always doc mode, or being asked to
1897 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001898 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1899 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001900 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001901 // If bring to front is requested, and no result is requested and we have not
1902 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001903 // we can find a task that was started with this same
1904 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001905 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001906 // See if there is a task to bring to the front. If this is
1907 // a SINGLE_INSTANCE activity, there can be one and only one
1908 // instance of it in the history, and it is always in its own
1909 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001910 ActivityRecord intentActivity = !launchSingleInstance ?
1911 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001912 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001913 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001914 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001915 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001916 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1917 }
Craig Mautner29219d92013-04-16 20:19:12 -07001918 if (r.task == null) {
1919 r.task = intentActivity.task;
1920 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001921 if (intentActivity.task.intent == null) {
1922 // This task was started because of movement of
1923 // the activity based on affinity... now that we
1924 // are actually launching it, we can assign the
1925 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001926 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001927 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001928 targetStack = intentActivity.task.stack;
1929 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001930 // If the target task is not in the front, then we need
1931 // to bring it to the front... except... well, with
1932 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1933 // to have the same behavior as if a new instance was
1934 // being started, which means not bringing it to the front
1935 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001936 final ActivityStack focusStack = getFocusedStack();
1937 ActivityRecord curTop = (focusStack == null)
1938 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001939 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001940 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001941 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001942 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001943 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1944 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001945 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001946 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001947 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1948 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001949 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001950 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1951 options, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001952 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001953 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001954 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1955 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001956 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001957 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001958 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001959 options = null;
1960 }
1961 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001962 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001963 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001964 + " from " + intentActivity);
1965 targetStack.moveToFront("intentActivityFound");
1966 }
1967
Craig Mautner8849a5e2013-04-02 16:41:03 -07001968 // If the caller has requested that the target task be
1969 // reset, then do so.
1970 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1971 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1972 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001973 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001974 // We don't need to start a new activity, and
1975 // the client said not to do anything if that
1976 // is the case, so this is it! And for paranoia, make
1977 // sure we have correctly resumed the top activity.
1978 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001979 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001980
1981 // Make sure to notify Keyguard as well if we are not running an app
1982 // transition later.
1983 if (!movedToFront) {
1984 notifyActivityDrawnForKeyguard();
1985 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001986 } else {
1987 ActivityOptions.abort(options);
1988 }
1989 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1990 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001991 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001992 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001993 // The caller has requested to completely replace any
1994 // existing task with its new activity. Well that should
1995 // not be too hard...
1996 reuseTask = intentActivity.task;
1997 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001998 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001999 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002000 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002001 // In this situation we want to remove all activities
2002 // from the task up to the one being started. In most
2003 // cases this means we are resetting the task to its
2004 // initial state.
2005 ActivityRecord top =
2006 intentActivity.task.performClearTaskLocked(r, launchFlags);
2007 if (top != null) {
2008 if (top.frontOfTask) {
2009 // Activity aliases may mean we use different
2010 // intents for the top activity, so make sure
2011 // the task now has the identity of the new
2012 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002013 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002014 }
2015 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2016 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002017 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002018 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002019 // A special case: we need to start the activity because it is not
2020 // currently running, and the caller has asked to clear the current
2021 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002022 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002023 // Now pretend like this activity is being started by the top of its
2024 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002025 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002026 TaskRecord task = sourceRecord.task;
2027 if (task != null && task.stack == null) {
2028 // Target stack got cleared when we all activities were removed
2029 // above. Go ahead and reset it.
2030 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2031 targetStack.addTask(
2032 task, !launchTaskBehind /* toTop */, false /* moving */);
2033 }
2034
Craig Mautner8849a5e2013-04-02 16:41:03 -07002035 }
2036 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2037 // In this case the top activity on the task is the
2038 // same as the one being launched, so we take that
2039 // as a request to bring the task to the foreground.
2040 // If the top activity in the task is the root
2041 // activity, deliver this new intent to it if it
2042 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002043 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002044 && intentActivity.realActivity.equals(r.realActivity)) {
2045 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2046 intentActivity.task);
2047 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002048 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002049 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002050 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2051 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002052 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2053 // In this case we are launching the root activity
2054 // of the task, but with a different intent. We
2055 // should start a new instance on top.
2056 addingToTask = true;
2057 sourceRecord = intentActivity;
2058 }
2059 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2060 // In this case an activity is being launched in to an
2061 // existing task, without resetting that task. This
2062 // is typically the situation of launching an activity
2063 // from a notification or shortcut. We want to place
2064 // the new activity on top of the current task.
2065 addingToTask = true;
2066 sourceRecord = intentActivity;
2067 } else if (!intentActivity.task.rootWasReset) {
2068 // In this case we are launching in to an existing task
2069 // that has not yet been started from its front door.
2070 // The current task has been brought to the front.
2071 // Ideally, we'd probably like to place this new task
2072 // at the bottom of its stack, but that's a little hard
2073 // to do with the current organization of the code so
2074 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002075 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002076 }
2077 if (!addingToTask && reuseTask == null) {
2078 // We didn't do anything... but it was needed (a.k.a., client
2079 // don't use that intent!) And for paranoia, make
2080 // sure we have correctly resumed the top activity.
2081 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002082 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002083 if (!movedToFront) {
2084 // Make sure to notify Keyguard as well if we are not running an app
2085 // transition later.
2086 notifyActivityDrawnForKeyguard();
2087 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002088 } else {
2089 ActivityOptions.abort(options);
2090 }
2091 return ActivityManager.START_TASK_TO_FRONT;
2092 }
2093 }
2094 }
2095 }
2096
2097 //String uri = r.intent.toURI();
2098 //Intent intent2 = new Intent(uri);
2099 //Slog.i(TAG, "Given intent: " + r.intent);
2100 //Slog.i(TAG, "URI is: " + uri);
2101 //Slog.i(TAG, "To intent: " + intent2);
2102
2103 if (r.packageName != null) {
2104 // If the activity being launched is the same as the one currently
2105 // at the top, then we need to check if it should only be launched
2106 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002107 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002108 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002109 if (top != null && r.resultTo == null) {
2110 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2111 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002112 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002113 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002114 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2115 top.task);
2116 // For paranoia, make sure we have correctly
2117 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002118 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002119 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002120 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 }
2122 ActivityOptions.abort(options);
2123 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2124 // We don't need to start a new activity, and
2125 // the client said not to do anything if that
2126 // is the case, so this is it!
2127 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2128 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002129 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002130 return ActivityManager.START_DELIVERED_TO_TOP;
2131 }
2132 }
2133 }
2134 }
2135
2136 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002137 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002138 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2139 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002140 }
2141 ActivityOptions.abort(options);
2142 return ActivityManager.START_CLASS_NOT_FOUND;
2143 }
2144
2145 boolean newTask = false;
2146 boolean keepCurTransition = false;
2147
Craig Mautnerbb742462014-07-07 15:28:55 -07002148 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002149 sourceRecord.task : null;
2150
Craig Mautner8849a5e2013-04-02 16:41:03 -07002151 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002152 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002153 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002154 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002155 targetStack = computeStackFocus(r, newTask);
2156 targetStack.moveToFront("startingNewTask");
2157
Craig Mautner8849a5e2013-04-02 16:41:03 -07002158 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002159 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2160 newTaskInfo != null ? newTaskInfo : r.info,
2161 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002162 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2163 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002164 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2165 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002166 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002167 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002168 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002169 if (isLockTaskModeViolation(r.task)) {
2170 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2171 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2172 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002173 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002174 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002175 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2176 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002177 // Caller wants to appear on home activity, so before starting
2178 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002179 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002180 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002181 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002182 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002183 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002184 if (isLockTaskModeViolation(sourceTask)) {
2185 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2186 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2187 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002188 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002189 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002190 final TaskRecord topTask = targetStack.topTask();
2191 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002192 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2193 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002194 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002195 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196 // In this case, we are adding the activity to an existing
2197 // task, but the caller has asked to clear that task if the
2198 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002199 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002200 keepCurTransition = true;
2201 if (top != null) {
2202 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002203 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002204 // For paranoia, make sure we have correctly
2205 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002206 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002207 if (doResume) {
2208 targetStack.resumeTopActivityLocked(null);
2209 }
2210 ActivityOptions.abort(options);
2211 return ActivityManager.START_DELIVERED_TO_TOP;
2212 }
2213 } else if (!addingToTask &&
2214 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2215 // In this case, we are launching an activity in our own task
2216 // that may already be running somewhere in the history, and
2217 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002218 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002219 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002220 final TaskRecord task = top.task;
2221 task.moveActivityToFrontLocked(top);
2222 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002223 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002224 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002225 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002226 if (doResume) {
2227 targetStack.resumeTopActivityLocked(null);
2228 }
2229 return ActivityManager.START_DELIVERED_TO_TOP;
2230 }
2231 }
2232 // An existing activity is starting this new activity, so we want
2233 // to keep the new one in the same task as the one that is starting
2234 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002235 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002236 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002237 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002238
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002239 } else if (inTask != null) {
2240 // The calling is asking that the new activity be started in an explicit
2241 // task it has provided to us.
2242 if (isLockTaskModeViolation(inTask)) {
2243 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2244 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2245 }
2246 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002247 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002248
2249 // Check whether we should actually launch the new activity in to the task,
2250 // or just reuse the current activity on top.
2251 ActivityRecord top = inTask.getTopActivity();
2252 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2253 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2254 || launchSingleTop || launchSingleTask) {
2255 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2256 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2257 // We don't need to start a new activity, and
2258 // the client said not to do anything if that
2259 // is the case, so this is it!
2260 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2261 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002262 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002263 return ActivityManager.START_DELIVERED_TO_TOP;
2264 }
2265 }
2266
Dianne Hackborn962d5352014-08-29 16:27:40 -07002267 if (!addingToTask) {
2268 // We don't actually want to have this activity added to the task, so just
2269 // stop here but still tell the caller that we consumed the intent.
2270 ActivityOptions.abort(options);
2271 return ActivityManager.START_TASK_TO_FRONT;
2272 }
2273
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002274 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002275 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002276 + " in explicit task " + r.task);
2277
Craig Mautner8849a5e2013-04-02 16:41:03 -07002278 } else {
2279 // This not being started from an existing activity, and not part
2280 // of a new task... just put it in the top task, though these days
2281 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002282 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002283 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002284 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002285 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002286 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002287 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002288 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002289 + " in new guessed " + r.task);
2290 }
2291
2292 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002293 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002294
Craig Mautner76e2a762014-06-24 09:05:43 -07002295 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2296 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2297 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002298 if (newTask) {
2299 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2300 }
2301 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002302 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002303 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002304 if (!launchTaskBehind) {
2305 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002306 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002307 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002308 return ActivityManager.START_SUCCESS;
2309 }
2310
Craig Mautneree36c772014-07-16 14:56:05 -07002311 final void doPendingActivityLaunchesLocked(boolean doResume) {
2312 while (!mPendingActivityLaunches.isEmpty()) {
2313 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002314 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002315 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002316 }
2317 }
2318
Craig Mautner7f13ed32014-07-28 14:00:35 -07002319 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002320 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2321 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002322 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002323 mPendingActivityLaunches.remove(palNdx);
2324 }
2325 }
2326 }
2327
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002328 void setLaunchSource(int uid) {
2329 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2330 }
2331
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002332 void acquireLaunchWakelock() {
2333 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2334 throw new IllegalStateException("Calling must be system uid");
2335 }
2336 mLaunchingActivity.acquire();
2337 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2338 // To be safe, don't allow the wake lock to be held for too long.
2339 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2340 }
2341 }
2342
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002343 /**
2344 * Called when the frontmost task is idle.
2345 * @return the state of mService.mBooting before this was called.
2346 */
2347 private boolean checkFinishBootingLocked() {
2348 final boolean booting = mService.mBooting;
2349 boolean enableScreen = false;
2350 mService.mBooting = false;
2351 if (!mService.mBooted) {
2352 mService.mBooted = true;
2353 enableScreen = true;
2354 }
2355 if (booting || enableScreen) {
2356 mService.postFinishBooting(booting, enableScreen);
2357 }
2358 return booting;
2359 }
2360
Craig Mautnerf3333272013-04-22 10:55:53 -07002361 // Checked.
2362 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2363 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002364 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002365
Craig Mautnerf3333272013-04-22 10:55:53 -07002366 ArrayList<ActivityRecord> stops = null;
2367 ArrayList<ActivityRecord> finishes = null;
2368 ArrayList<UserStartedState> startingUsers = null;
2369 int NS = 0;
2370 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002371 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002372 boolean activityRemoved = false;
2373
Wale Ogunwale7d701172015-03-11 15:36:30 -07002374 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002375 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002376 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2377 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002378 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2379 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002380 if (fromTimeout) {
2381 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002382 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002383
2384 // This is a hack to semi-deal with a race condition
2385 // in the client where it can be constructed with a
2386 // newer configuration from when we asked it to launch.
2387 // We'll update with whatever configuration it now says
2388 // it used to launch.
2389 if (config != null) {
2390 r.configuration = config;
2391 }
2392
2393 // We are now idle. If someone is waiting for a thumbnail from
2394 // us, we can now deliver.
2395 r.idle = true;
2396
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002397 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002398 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002399 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002400 }
2401 }
2402
2403 if (allResumedActivitiesIdle()) {
2404 if (r != null) {
2405 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002406 }
2407
2408 if (mLaunchingActivity.isHeld()) {
2409 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2410 if (VALIDATE_WAKE_LOCK_CALLER &&
2411 Binder.getCallingUid() != Process.myUid()) {
2412 throw new IllegalStateException("Calling must be system uid");
2413 }
2414 mLaunchingActivity.release();
2415 }
2416 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002417 }
2418
2419 // Atomically retrieve all of the other things to do.
2420 stops = processStoppingActivitiesLocked(true);
2421 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002422 if ((NF = mFinishingActivities.size()) > 0) {
2423 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002424 mFinishingActivities.clear();
2425 }
2426
Craig Mautnerf3333272013-04-22 10:55:53 -07002427 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002428 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002429 mStartingUsers.clear();
2430 }
2431
Craig Mautnerf3333272013-04-22 10:55:53 -07002432 // Stop any activities that are scheduled to do so but have been
2433 // waiting for the next one to start.
2434 for (int i = 0; i < NS; i++) {
2435 r = stops.get(i);
2436 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002437 if (stack != null) {
2438 if (r.finishing) {
2439 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2440 } else {
2441 stack.stopActivityLocked(r);
2442 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002443 }
2444 }
2445
2446 // Finish any activities that are scheduled to do so but have been
2447 // waiting for the next one to start.
2448 for (int i = 0; i < NF; i++) {
2449 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002450 final ActivityStack stack = r.task.stack;
2451 if (stack != null) {
2452 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2453 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002454 }
2455
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002456 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002457 // Complete user switch
2458 if (startingUsers != null) {
2459 for (int i = 0; i < startingUsers.size(); i++) {
2460 mService.finishUserSwitch(startingUsers.get(i));
2461 }
2462 }
2463 // Complete starting up of background users
2464 if (mStartingBackgroundUsers.size() > 0) {
2465 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2466 mStartingBackgroundUsers.clear();
2467 for (int i = 0; i < startingUsers.size(); i++) {
2468 mService.finishUserBoot(startingUsers.get(i));
2469 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002470 }
2471 }
2472
2473 mService.trimApplications();
2474 //dump();
2475 //mWindowManager.dump();
2476
Craig Mautnerf3333272013-04-22 10:55:53 -07002477 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002478 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002479 }
2480
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002481 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002482 }
2483
Craig Mautner8e569572013-10-11 17:36:59 -07002484 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002485 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002486 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2487 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002488 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2489 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2490 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002491 }
Craig Mautner19091252013-10-05 00:03:53 -07002492 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002493 }
2494
2495 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002496 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2497 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002498 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2499 stacks.get(stackNdx).closeSystemDialogsLocked();
2500 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002501 }
2502 }
2503
Craig Mautner93529a42013-10-04 15:03:13 -07002504 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002505 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002506 }
2507
Craig Mautner8d341ef2013-03-26 09:03:27 -07002508 /**
2509 * @return true if some activity was finished (or would have finished if doit were true).
2510 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002511 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2512 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002513 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002514 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2515 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002516 final int numStacks = stacks.size();
2517 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2518 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002519 if (stack.finishDisabledPackageActivitiesLocked(
2520 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002521 didSomething = true;
2522 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002523 }
2524 }
2525 return didSomething;
2526 }
2527
Dianne Hackborna413dc02013-07-12 12:02:55 -07002528 void updatePreviousProcessLocked(ActivityRecord r) {
2529 // Now that this process has stopped, we may want to consider
2530 // it to be the previous app to try to keep around in case
2531 // the user wants to return to it.
2532
2533 // First, found out what is currently the foreground app, so that
2534 // we don't blow away the previous app if this activity is being
2535 // hosted by the process that is actually still the foreground.
2536 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002537 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2538 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002539 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2540 final ActivityStack stack = stacks.get(stackNdx);
2541 if (isFrontStack(stack)) {
2542 if (stack.mResumedActivity != null) {
2543 fgApp = stack.mResumedActivity.app;
2544 } else if (stack.mPausingActivity != null) {
2545 fgApp = stack.mPausingActivity.app;
2546 }
2547 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002548 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002549 }
2550 }
2551
2552 // Now set this one as the previous process, only if that really
2553 // makes sense to.
2554 if (r.app != null && fgApp != null && r.app != fgApp
2555 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002556 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002557 mService.mPreviousProcess = r.app;
2558 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2559 }
2560 }
2561
Craig Mautner05d29032013-05-03 13:40:13 -07002562 boolean resumeTopActivitiesLocked() {
2563 return resumeTopActivitiesLocked(null, null, null);
2564 }
2565
2566 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2567 Bundle targetOptions) {
2568 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002569 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002570 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002571 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002572 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002573 if (isFrontStack(targetStack)) {
2574 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2575 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002576
Craig Mautnere0a38842013-12-16 16:14:02 -08002577 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2578 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002579 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2580 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002581 if (stack == targetStack) {
2582 // Already started above.
2583 continue;
2584 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002585 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002586 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002587 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002588 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002589 }
Craig Mautner05d29032013-05-03 13:40:13 -07002590 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002591 }
2592
Todd Kennedy539db512014-12-15 09:57:55 -08002593 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002594 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2595 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002596 final int numStacks = stacks.size();
2597 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2598 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002599 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002600 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002601 }
2602 }
2603
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002604 void finishVoiceTask(IVoiceInteractionSession session) {
2605 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2606 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2607 final int numStacks = stacks.size();
2608 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2609 final ActivityStack stack = stacks.get(stackNdx);
2610 stack.finishVoiceTask(session);
2611 }
2612 }
2613 }
2614
Craig Mautner299f9602015-01-26 09:47:33 -08002615 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002616 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2617 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002618 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002619 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2620 // Caller wants the home activity moved with it. To accomplish this,
2621 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002622 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002623 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002624 if (task.stack == null) {
2625 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2626 + task + " to front. Stack is null");
2627 return;
2628 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002629 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002630 if (DEBUG_STACK) Slog.d(TAG_STACK,
2631 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002632 }
2633
Craig Mautner967212c2013-04-13 21:10:58 -07002634 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002635 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2636 if (activityContainer != null) {
2637 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002638 }
2639 return null;
2640 }
2641
Craig Mautner967212c2013-04-13 21:10:58 -07002642 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002643 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002644 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2645 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002646 }
2647 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002648 }
2649
Craig Mautner4a1cb222013-12-04 16:14:06 -08002650 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002651 ActivityRecord homeActivity = getHomeActivity();
2652 if (homeActivity != null) {
2653 return homeActivity.appToken;
2654 }
2655 return null;
2656 }
2657
2658 ActivityRecord getHomeActivity() {
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002659 return getHomeActivityForUser(UserHandle.USER_ALL);
2660 }
2661
2662 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002663 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2664 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2665 final TaskRecord task = tasks.get(taskNdx);
2666 if (task.isHomeTask()) {
2667 final ArrayList<ActivityRecord> activities = task.mActivities;
2668 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2669 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002670 if (r.isHomeActivity()
2671 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002672 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002673 }
2674 }
2675 }
2676 }
2677 return null;
2678 }
2679
Todd Kennedyca4d8422015-01-15 15:19:22 -08002680 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002681 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002682 ActivityContainer activityContainer =
2683 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002684 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002685 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002686 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002687 return activityContainer;
2688 }
2689
Craig Mautner34b73df2014-01-12 21:11:08 -08002690 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002691 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2692 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2693 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002694 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2695 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002696 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002697 }
2698 }
2699
Craig Mautner95da1082014-02-24 17:54:35 -08002700 void deleteActivityContainer(IActivityContainer container) {
2701 ActivityContainer activityContainer = (ActivityContainer)container;
2702 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002703 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2704 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002705 final int stackId = activityContainer.mStackId;
2706 mActivityContainers.remove(stackId);
2707 mWindowManager.removeStack(stackId);
2708 }
2709 }
2710
Wale Ogunwale60454db2015-01-23 16:05:07 -08002711 void resizeStackLocked(int stackId, Rect bounds) {
2712 final ActivityStack stack = getStack(stackId);
2713 if (stack == null) {
2714 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2715 return;
2716 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002717
2718 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002719 if (r != null && !r.task.mResizeable) {
2720 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002721 return;
2722 }
2723
Wale Ogunwale60454db2015-01-23 16:05:07 -08002724 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2725 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002726 if (r != null) {
2727 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2728 // And we need to make sure at this point that all other activities
2729 // are made visible with the correct configuration.
2730 ensureActivitiesVisibleLocked(r, 0);
2731 if (!updated) {
2732 resumeTopActivitiesLocked(stack, null, null);
2733 }
2734 }
2735 }
2736 }
2737
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002738 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2739 * current task stack if it only has one entry, moving the task to a stack that matches the
2740 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2741 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2742 task.mResizeable = true;
2743 final ActivityStack currentStack = task.stack;
2744 if (currentStack.isHomeStack()) {
2745 // Can't move task off the home stack. Sorry!
2746 return;
2747 }
2748
2749 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2750 if (matchingStackId != -1) {
2751 // There is already a stack with the right bounds!
2752 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2753 // Nothing to do here. Already in the right stack...
2754 return;
2755 }
2756 // Move task to stack with matching bounds.
2757 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2758 return;
2759 }
2760
2761 if (currentStack != null && currentStack.numTasks() == 1) {
2762 // Just resize the current stack since this is the task in it.
2763 resizeStackLocked(currentStack.mStackId, bounds);
2764 return;
2765 }
2766
2767 // Create new stack and move the task to it.
2768 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2769 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2770 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2771
2772 if (newStack == null) {
2773 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2774 return;
2775 }
2776 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2777 resizeStackLocked(newStack.mStackId, bounds);
2778 }
2779
Todd Kennedy4900bf92015-01-16 16:05:14 -08002780 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002781 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2782 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002783 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002784 }
2785
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002786 ActivityContainer activityContainer = new ActivityContainer(stackId);
2787 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002788 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002789 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002790 }
2791
2792 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002793 while (true) {
2794 if (++mLastStackId <= HOME_STACK_ID) {
2795 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002796 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002797 if (getStack(mLastStackId) == null) {
2798 break;
2799 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002800 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002801 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002802 }
2803
Wale Ogunwale7de05352014-12-12 15:21:33 -08002804 private boolean restoreRecentTaskLocked(TaskRecord task) {
2805 ActivityStack stack = null;
2806 // Determine stack to restore task to.
2807 if (mLeanbackOnlyDevice) {
2808 // There is only one stack for lean back devices.
2809 stack = mHomeStack;
2810 } else {
2811 // Look for the top stack on the home display that isn't the home stack.
2812 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2813 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2814 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002815 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002816 stack = tmpStack;
2817 break;
2818 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002819 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002820 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002821
2822 if (stack == null) {
2823 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2824 // before an application stack is created...Go ahead and create one on the default
2825 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002826 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002827 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002828 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002829 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2830 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002831 }
2832
2833 if (stack == null) {
2834 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002835 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2836 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002837 return false;
2838 }
2839
2840 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002841 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2842 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002843 final ArrayList<ActivityRecord> activities = task.mActivities;
2844 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2845 final ActivityRecord r = activities.get(activityNdx);
2846 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2847 r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002848 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Wale Ogunwale7de05352014-12-12 15:21:33 -08002849 r.userId, r.info.configChanges, task.voiceSession != null,
2850 r.mLaunchTaskBehind);
2851 }
2852 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002853 }
2854
Craig Mautner299f9602015-01-26 09:47:33 -08002855 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002856 final TaskRecord task = anyTaskForIdLocked(taskId);
2857 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002858 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002859 return;
2860 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002861 final ActivityStack stack = getStack(stackId);
2862 if (stack == null) {
2863 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2864 return;
2865 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002866 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2867 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07002868 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002869 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002870 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002871 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002872 }
2873
Craig Mautnerac6f8432013-07-17 13:24:59 -07002874 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002875 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002876 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2877 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002878 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2879 final ActivityStack stack = stacks.get(stackNdx);
2880 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002881 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002882 continue;
2883 }
2884 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002885 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2886 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002887 continue;
2888 }
2889 final ActivityRecord ar = stack.findTaskLocked(r);
2890 if (ar != null) {
2891 return ar;
2892 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002893 }
2894 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002895 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002896 return null;
2897 }
2898
2899 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002900 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2901 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002902 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2903 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2904 if (ar != null) {
2905 return ar;
2906 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002907 }
2908 }
2909 return null;
2910 }
2911
Craig Mautner8d341ef2013-03-26 09:03:27 -07002912 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002913 scheduleSleepTimeout();
2914 if (!mGoingToSleep.isHeld()) {
2915 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002916 if (mLaunchingActivity.isHeld()) {
2917 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2918 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002919 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002920 mLaunchingActivity.release();
2921 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002922 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002923 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002924 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002925 }
2926
2927 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002928 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002929
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002930 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002931 final long endTime = System.currentTimeMillis() + timeout;
2932 while (true) {
2933 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002934 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2935 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002936 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2937 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2938 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002939 }
2940 if (cantShutdown) {
2941 long timeRemaining = endTime - System.currentTimeMillis();
2942 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002943 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002944 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002945 } catch (InterruptedException e) {
2946 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002947 } else {
2948 Slog.w(TAG, "Activity manager shutdown timed out");
2949 timedout = true;
2950 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002951 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002952 } else {
2953 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002954 }
2955 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002956
2957 // Force checkReadyForSleep to complete.
2958 mSleepTimeout = true;
2959 checkReadyForSleepLocked();
2960
Craig Mautner8d341ef2013-03-26 09:03:27 -07002961 return timedout;
2962 }
2963
2964 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002965 removeSleepTimeouts();
2966 if (mGoingToSleep.isHeld()) {
2967 mGoingToSleep.release();
2968 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002969 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2970 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002971 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2972 final ActivityStack stack = stacks.get(stackNdx);
2973 stack.awakeFromSleepingLocked();
2974 if (isFrontStack(stack)) {
2975 resumeTopActivitiesLocked();
2976 }
Craig Mautner5314a402013-09-26 12:40:16 -07002977 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002978 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002979 mGoingToSleepActivities.clear();
2980 }
2981
2982 void activitySleptLocked(ActivityRecord r) {
2983 mGoingToSleepActivities.remove(r);
2984 checkReadyForSleepLocked();
2985 }
2986
2987 void checkReadyForSleepLocked() {
2988 if (!mService.isSleepingOrShuttingDown()) {
2989 // Do not care.
2990 return;
2991 }
2992
2993 if (!mSleepTimeout) {
2994 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002995 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2996 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002997 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2998 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2999 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003000 }
3001
3002 if (mStoppingActivities.size() > 0) {
3003 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003004 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003005 + mStoppingActivities.size() + " activities");
3006 scheduleIdleLocked();
3007 dontSleep = true;
3008 }
3009
3010 if (mGoingToSleepActivities.size() > 0) {
3011 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003012 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003013 + mGoingToSleepActivities.size() + " activities");
3014 dontSleep = true;
3015 }
3016
3017 if (dontSleep) {
3018 return;
3019 }
3020 }
3021
Craig Mautnere0a38842013-12-16 16:14:02 -08003022 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3023 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003024 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3025 stacks.get(stackNdx).goToSleep();
3026 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003027 }
3028
3029 removeSleepTimeouts();
3030
3031 if (mGoingToSleep.isHeld()) {
3032 mGoingToSleep.release();
3033 }
3034 if (mService.mShuttingDown) {
3035 mService.notifyAll();
3036 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003037 }
3038
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003039 boolean reportResumedActivityLocked(ActivityRecord r) {
3040 final ActivityStack stack = r.task.stack;
3041 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003042 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003043 }
3044 if (allResumedActivitiesComplete()) {
3045 ensureActivitiesVisibleLocked(null, 0);
3046 mWindowManager.executeAppTransition();
3047 return true;
3048 }
3049 return false;
3050 }
3051
Craig Mautner8d341ef2013-03-26 09:03:27 -07003052 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003053 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3054 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003055 final int numStacks = stacks.size();
3056 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3057 final ActivityStack stack = stacks.get(stackNdx);
3058 stack.handleAppCrashLocked(app);
3059 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003060 }
3061 }
3062
Jose Lima4b6c6692014-08-12 17:41:12 -07003063 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003064 final ActivityStack stack = r.task.stack;
3065 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003066 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3067 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003068 return false;
3069 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003070 final boolean isVisible = stack.hasVisibleBehindActivity();
3071 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3072 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003073
3074 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003075 if (top == null || top == r || (visible == isVisible)) {
3076 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3077 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003078 return true;
3079 }
3080
3081 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003082 if (visible && top.fullscreen) {
3083 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003084 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003085 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003086 " top.app.thread=" + top.app.thread);
3087 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003088 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3089 // Only the activity set as currently visible behind should actively reset its
3090 // visible behind state.
3091 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3092 + visible + " stack.getVisibleBehindActivity()=" +
3093 stack.getVisibleBehindActivity() + " r=" + r);
3094 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003095 }
3096
Jose Lima4b6c6692014-08-12 17:41:12 -07003097 stack.setVisibleBehindActivity(visible ? r : null);
3098 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003099 // Make the activity immediately above r opaque.
3100 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3101 if (next != null) {
3102 mService.convertFromTranslucent(next.appToken);
3103 }
3104 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003105 if (top.app != null && top.app.thread != null) {
3106 // Notify the top app of the change.
3107 try {
3108 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3109 } catch (RemoteException e) {
3110 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003111 }
3112 return true;
3113 }
3114
Craig Mautnerbb742462014-07-07 15:28:55 -07003115 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3116 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3117 r.mLaunchTaskBehind = false;
3118 final TaskRecord task = r.task;
3119 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003120 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003121 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003122 mWindowManager.setAppVisibility(r.appToken, false);
3123 }
3124
3125 void scheduleLaunchTaskBehindComplete(IBinder token) {
3126 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3127 }
3128
Craig Mautnerde4ef022013-04-07 19:01:33 -07003129 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003130 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003131 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3132 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003133 final int topStackNdx = stacks.size() - 1;
3134 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3135 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003136 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003137 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003138 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003139 }
3140
3141 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003142 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3143 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003144 final int numStacks = stacks.size();
3145 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3146 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003147 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003148 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003149 }
3150 }
3151
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003152 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3153 // Examine all activities currently running in the process.
3154 TaskRecord firstTask = null;
3155 // Tasks is non-null only if two or more tasks are found.
3156 ArraySet<TaskRecord> tasks = null;
3157 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3158 for (int i=0; i<app.activities.size(); i++) {
3159 ActivityRecord r = app.activities.get(i);
3160 // First, if we find an activity that is in the process of being destroyed,
3161 // then we just aren't going to do anything for now; we want things to settle
3162 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003163 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003164 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3165 return;
3166 }
3167 // Don't consider any activies that are currently not in a state where they
3168 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003169 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3170 || r.state == PAUSED || r.state == STOPPING) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003171 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3172 continue;
3173 }
3174 if (r.task != null) {
3175 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3176 + " from " + r);
3177 if (firstTask == null) {
3178 firstTask = r.task;
3179 } else if (firstTask != r.task) {
3180 if (tasks == null) {
3181 tasks = new ArraySet<>();
3182 tasks.add(firstTask);
3183 }
3184 tasks.add(r.task);
3185 }
3186 }
3187 }
3188 if (tasks == null) {
3189 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3190 return;
3191 }
3192 // If we have activities in multiple tasks that are in a position to be destroyed,
3193 // let's iterate through the tasks and release the oldest one.
3194 final int numDisplays = mActivityDisplays.size();
3195 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3196 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3197 // Step through all stacks starting from behind, to hit the oldest things first.
3198 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3199 final ActivityStack stack = stacks.get(stackNdx);
3200 // Try to release activities in this stack; if we manage to, we are done.
3201 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3202 return;
3203 }
3204 }
3205 }
3206 }
3207
Craig Mautner8d341ef2013-03-26 09:03:27 -07003208 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003209 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003210 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003211 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003212
Craig Mautner858d8a62013-04-23 17:08:34 -07003213 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003214 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3215 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003216 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003217 final ActivityStack stack = stacks.get(stackNdx);
3218 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003219 TaskRecord task = stack.topTask();
3220 if (task != null) {
3221 mWindowManager.moveTaskToTop(task.taskId);
3222 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003223 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003224 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003225
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003226 ActivityStack stack = getStack(restoreStackId);
3227 if (stack == null) {
3228 stack = mHomeStack;
3229 }
3230 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003231 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003232 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003233 TaskRecord task = stack.topTask();
3234 if (task != null) {
3235 mWindowManager.moveTaskToTop(task.taskId);
3236 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003237 } else {
3238 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003239 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003240 }
Craig Mautner93529a42013-10-04 15:03:13 -07003241 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003242 }
3243
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003244 /**
3245 * Add background users to send boot completed events to.
3246 * @param userId The user being started in the background
3247 * @param uss The state object for the user.
3248 */
3249 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3250 mStartingBackgroundUsers.add(uss);
3251 }
3252
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003253 /** Checks whether the userid is a profile of the current user. */
3254 boolean isCurrentProfileLocked(int userId) {
3255 if (userId == mCurrentUser) return true;
3256 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3257 if (mService.mCurrentProfileIds[i] == userId) return true;
3258 }
3259 return false;
3260 }
3261
Craig Mautnerde4ef022013-04-07 19:01:33 -07003262 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003263 ArrayList<ActivityRecord> stops = null;
3264
3265 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003266 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3267 ActivityRecord s = mStoppingActivities.get(activityNdx);
3268 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003269 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003270 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3271 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003272 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003273 if (s.finishing) {
3274 // If this activity is finishing, it is sitting on top of
3275 // everyone else but we now know it is no longer needed...
3276 // so get rid of it. Otherwise, we need to go through the
3277 // normal flow and hide it once we determine that it is
3278 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003279 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003280 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003281 }
3282 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003283 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003284 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003285 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003286 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003287 }
3288 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003289 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003290 }
3291 }
3292
3293 return stops;
3294 }
3295
Craig Mautnercf910b02013-04-23 11:23:27 -07003296 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003297 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3298 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3299 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3300 final ActivityStack stack = stacks.get(stackNdx);
3301 final ActivityRecord r = stack.topRunningActivityLocked(null);
3302 final ActivityState state = r == null ? DESTROYED : r.state;
3303 if (isFrontStack(stack)) {
3304 if (r == null) Slog.e(TAG,
3305 "validateTop...: null top activity, stack=" + stack);
3306 else {
3307 final ActivityRecord pausing = stack.mPausingActivity;
3308 if (pausing != null && pausing == r) Slog.e(TAG,
3309 "validateTop...: top stack has pausing activity r=" + r
3310 + " state=" + state);
3311 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3312 "validateTop...: activity in front not resumed r=" + r
3313 + " state=" + state);
3314 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003315 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003316 final ActivityRecord resumed = stack.mResumedActivity;
3317 if (resumed != null && resumed == r) Slog.e(TAG,
3318 "validateTop...: back stack has resumed activity r=" + r
3319 + " state=" + state);
3320 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3321 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003322 }
3323 }
3324 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003325 }
3326
Craig Mautner27084302013-03-25 08:05:25 -07003327 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003328 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003329 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003330 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3331 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3332 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003333 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner15df08a2015-04-01 12:17:18 -07003334 pw.print(prefix); pw.println("mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003335 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003336
Craig Mautner20e72272013-04-01 13:45:53 -07003337 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003338 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003339 }
3340
Dianne Hackborn390517b2013-05-30 15:03:32 -07003341 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3342 boolean needSep, String prefix) {
3343 if (activity != null) {
3344 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3345 if (needSep) {
3346 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003347 }
3348 pw.print(prefix);
3349 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003350 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003351 }
3352 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003353 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003354 }
3355
Craig Mautner8d341ef2013-03-26 09:03:27 -07003356 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3357 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003358 boolean printed = false;
3359 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003360 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3361 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003362 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003363 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003364 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003365 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003366 final ActivityStack stack = stacks.get(stackNdx);
3367 StringBuilder stackHeader = new StringBuilder(128);
3368 stackHeader.append(" Stack #");
3369 stackHeader.append(stack.mStackId);
3370 stackHeader.append(":");
3371 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3372 needSep, stackHeader.toString());
3373 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3374 !dumpAll, false, dumpPackage, true,
3375 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003376
Craig Mautner4a1cb222013-12-04 16:14:06 -08003377 needSep = printed;
3378 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3379 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003380 if (pr) {
3381 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003382 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003383 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003384 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3385 " mResumedActivity: ");
3386 if (pr) {
3387 printed = true;
3388 needSep = false;
3389 }
3390 if (dumpAll) {
3391 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3392 " mLastPausedActivity: ");
3393 if (pr) {
3394 printed = true;
3395 needSep = true;
3396 }
3397 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3398 needSep, " mLastNoHistoryActivity: ");
3399 }
3400 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003401 }
3402 }
3403
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003404 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3405 false, dumpPackage, true, " Activities waiting to finish:", null);
3406 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3407 false, dumpPackage, true, " Activities waiting to stop:", null);
3408 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3409 false, dumpPackage, true, " Activities waiting for another to become visible:",
3410 null);
3411 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3412 false, dumpPackage, true, " Activities waiting to sleep:", null);
3413 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3414 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003415
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003416 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003417 }
3418
Dianne Hackborn390517b2013-05-30 15:03:32 -07003419 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003420 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003421 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003422 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003423 String innerPrefix = null;
3424 String[] args = null;
3425 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003426 for (int i=list.size()-1; i>=0; i--) {
3427 final ActivityRecord r = list.get(i);
3428 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3429 continue;
3430 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003431 if (innerPrefix == null) {
3432 innerPrefix = prefix + " ";
3433 args = new String[0];
3434 }
3435 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003436 final boolean full = !brief && (complete || !r.isInHistory());
3437 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003438 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003439 needNL = false;
3440 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003441 if (header1 != null) {
3442 pw.println(header1);
3443 header1 = null;
3444 }
3445 if (header2 != null) {
3446 pw.println(header2);
3447 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003448 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003449 if (lastTask != r.task) {
3450 lastTask = r.task;
3451 pw.print(prefix);
3452 pw.print(full ? "* " : " ");
3453 pw.println(lastTask);
3454 if (full) {
3455 lastTask.dump(pw, prefix + " ");
3456 } else if (complete) {
3457 // Complete + brief == give a summary. Isn't that obvious?!?
3458 if (lastTask.intent != null) {
3459 pw.print(prefix); pw.print(" ");
3460 pw.println(lastTask.intent.toInsecureStringWithClip());
3461 }
3462 }
3463 }
3464 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3465 pw.print(" #"); pw.print(i); pw.print(": ");
3466 pw.println(r);
3467 if (full) {
3468 r.dump(pw, innerPrefix);
3469 } else if (complete) {
3470 // Complete + brief == give a summary. Isn't that obvious?!?
3471 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3472 if (r.app != null) {
3473 pw.print(innerPrefix); pw.println(r.app);
3474 }
3475 }
3476 if (client && r.app != null && r.app.thread != null) {
3477 // flush anything that is already in the PrintWriter since the thread is going
3478 // to write to the file descriptor directly
3479 pw.flush();
3480 try {
3481 TransferPipe tp = new TransferPipe();
3482 try {
3483 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3484 r.appToken, innerPrefix, args);
3485 // Short timeout, since blocking here can
3486 // deadlock with the application.
3487 tp.go(fd, 2000);
3488 } finally {
3489 tp.kill();
3490 }
3491 } catch (IOException e) {
3492 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3493 } catch (RemoteException e) {
3494 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3495 }
3496 needNL = true;
3497 }
3498 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003499 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003500 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003501
Craig Mautnerf3333272013-04-22 10:55:53 -07003502 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003503 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003504 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3505 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003506 }
3507
3508 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003509 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003510 }
3511
3512 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003513 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003514 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3515 }
3516
Craig Mautner05d29032013-05-03 13:40:13 -07003517 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003518 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3519 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3520 }
Craig Mautner05d29032013-05-03 13:40:13 -07003521 }
3522
Craig Mautner0eea92c2013-05-16 13:35:39 -07003523 void removeSleepTimeouts() {
3524 mSleepTimeout = false;
3525 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3526 }
3527
3528 final void scheduleSleepTimeout() {
3529 removeSleepTimeouts();
3530 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3531 }
3532
Craig Mautner4a1cb222013-12-04 16:14:06 -08003533 @Override
3534 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003535 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003536 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3537 }
3538
3539 @Override
3540 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003541 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003542 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3543 }
3544
3545 @Override
3546 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003547 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003548 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3549 }
3550
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003551 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003552 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003553 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003554 newDisplay = mActivityDisplays.get(displayId) == null;
3555 if (newDisplay) {
3556 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003557 if (activityDisplay.mDisplay == null) {
3558 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3559 return;
3560 }
Craig Mautner4504de52013-12-20 09:06:56 -08003561 mActivityDisplays.put(displayId, activityDisplay);
3562 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003563 }
Craig Mautner4504de52013-12-20 09:06:56 -08003564 if (newDisplay) {
3565 mWindowManager.onDisplayAdded(displayId);
3566 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003567 }
3568
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003569 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003570 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003571 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3572 if (activityDisplay != null) {
3573 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003574 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003575 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003576 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003577 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003578 }
3579 }
3580 mWindowManager.onDisplayRemoved(displayId);
3581 }
3582
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003583 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003584 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003585 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3586 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003587 // TODO: Update the bounds.
3588 }
3589 }
3590 mWindowManager.onDisplayChanged(displayId);
3591 }
3592
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003593 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003594 StackInfo info = new StackInfo();
3595 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3596 info.displayId = Display.DEFAULT_DISPLAY;
3597 info.stackId = stack.mStackId;
3598
3599 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3600 final int numTasks = tasks.size();
3601 int[] taskIds = new int[numTasks];
3602 String[] taskNames = new String[numTasks];
3603 for (int i = 0; i < numTasks; ++i) {
3604 final TaskRecord task = tasks.get(i);
3605 taskIds[i] = task.taskId;
3606 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3607 : task.realActivity != null ? task.realActivity.flattenToString()
3608 : task.getTopActivity() != null ? task.getTopActivity().packageName
3609 : "unknown";
3610 }
3611 info.taskIds = taskIds;
3612 info.taskNames = taskNames;
3613 return info;
3614 }
3615
3616 StackInfo getStackInfoLocked(int stackId) {
3617 ActivityStack stack = getStack(stackId);
3618 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003619 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 }
3621 return null;
3622 }
3623
3624 ArrayList<StackInfo> getAllStackInfosLocked() {
3625 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003626 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3627 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003628 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003629 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003630 }
3631 }
3632 return list;
3633 }
3634
Craig Mautner15df08a2015-04-01 12:17:18 -07003635 TaskRecord getLockedTaskLocked() {
3636 final int top = mLockTaskModeTasks.size() - 1;
3637 if (top >= 0) {
3638 return mLockTaskModeTasks.get(top);
3639 }
3640 return null;
3641 }
3642
3643 boolean isLockedTask(TaskRecord task) {
3644 return mLockTaskModeTasks.contains(task);
3645 }
3646
3647 boolean isLastLockedTask(TaskRecord task) {
3648 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3649 }
3650
3651 void removeLockedTaskLocked(final TaskRecord task) {
3652 if (mLockTaskModeTasks.remove(task) && mLockTaskModeTasks.isEmpty()) {
3653 // Last one.
3654 final Message lockTaskMsg = Message.obtain();
3655 lockTaskMsg.arg1 = task.userId;
3656 lockTaskMsg.what = LOCK_TASK_END_MSG;
3657 mHandler.sendMessage(lockTaskMsg);
3658 }
3659 }
3660
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003661 void showLockTaskToast() {
3662 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003663 }
3664
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003665 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3666 if (mLockTaskModeTasks.contains(task)) {
3667 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3668 }
3669 }
3670
Benjamin Franz43261142015-02-11 15:59:44 +00003671 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003672 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003673 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003674 final TaskRecord lockedTask = getLockedTaskLocked();
3675 if (lockedTask != null) {
3676 removeLockedTaskLocked(lockedTask);
3677 if (!mLockTaskModeTasks.isEmpty()) {
3678 // There are locked tasks remaining, can only finish this task, not unlock it.
3679 lockedTask.performClearTaskLocked();
3680 resumeTopActivitiesLocked();
3681 return;
3682 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003683 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003684 return;
3685 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003686
3687 // Should have already been checked, but do it again.
3688 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003689 return;
3690 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003691 if (isLockTaskModeViolation(task)) {
3692 Slog.e(TAG, "setLockTaskMode: Attempt to start an unauthorized lock task.");
3693 return;
3694 }
3695
3696 if (mLockTaskModeTasks.isEmpty()) {
3697 // First locktask.
3698 final Message lockTaskMsg = Message.obtain();
3699 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3700 lockTaskMsg.arg1 = task.userId;
3701 lockTaskMsg.what = LOCK_TASK_START_MSG;
3702 lockTaskMsg.arg2 = lockTaskModeState;
3703 mHandler.sendMessage(lockTaskMsg);
3704 }
3705 // Add it or move it to the top.
3706 mLockTaskModeTasks.remove(task);
3707 mLockTaskModeTasks.add(task);
3708
3709 if (task.mLockTaskUid == -1) {
3710 task.mLockTaskUid = task.mCallingUid;
3711 }
Craig Mautner299f9602015-01-26 09:47:33 -08003712 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003713 resumeTopActivitiesLocked();
3714 }
3715
3716 boolean isLockTaskModeViolation(TaskRecord task) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003717 if (getLockedTaskLocked() == task) {
3718 return false;
3719 }
3720 final int lockTaskAuth = task.mLockTaskAuth;
3721 switch (lockTaskAuth) {
3722 case LOCK_TASK_AUTH_DONT_LOCK:
3723 return !mLockTaskModeTasks.isEmpty();
3724 case LOCK_TASK_AUTH_LAUNCHABLE:
3725 case LOCK_TASK_AUTH_WHITELISTED:
3726 return false;
3727 case LOCK_TASK_AUTH_PINNABLE:
3728 // Pinnable tasks can't be launched on top of locktask tasks.
3729 return !mLockTaskModeTasks.isEmpty();
3730 default:
3731 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3732 return true;
3733 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003734 }
3735
Craig Mautner15df08a2015-04-01 12:17:18 -07003736 void onLockTaskPackagesUpdatedLocked() {
3737 boolean didSomething = false;
3738 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3739 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
3740 if (lockedTask.mLockTaskMode != LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED) {
3741 continue;
3742 }
3743 final boolean wasLaunchable = lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE;
3744 lockedTask.setLockTaskAuth();
3745 if (wasLaunchable && lockedTask.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE) {
3746 // Lost whitelisting authorization. End it now.
3747 removeLockedTaskLocked(lockedTask);
3748 lockedTask.performClearTaskLocked();
3749 didSomething = true;
3750 }
3751 }
3752 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3753 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3754 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3755 final ActivityStack stack = stacks.get(stackNdx);
3756 stack.onLockTaskPackagesUpdatedLocked();
3757 }
3758 }
3759 if (didSomething) {
3760 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003761 }
3762 }
3763
Benjamin Franz43261142015-02-11 15:59:44 +00003764 int getLockTaskModeState() {
3765 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003766 }
3767
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003768 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003769
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003770 public ActivityStackSupervisorHandler(Looper looper) {
3771 super(looper);
3772 }
3773
Craig Mautnerf3333272013-04-22 10:55:53 -07003774 void activityIdleInternal(ActivityRecord r) {
3775 synchronized (mService) {
3776 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3777 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003778 }
3779
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003780 @Override
3781 public void handleMessage(Message msg) {
3782 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003783 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003784 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003785 if (mService.mDidDexOpt) {
3786 mService.mDidDexOpt = false;
3787 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3788 nmsg.obj = msg.obj;
3789 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3790 return;
3791 }
3792 // We don't at this point know if the activity is fullscreen,
3793 // so we need to be conservative and assume it isn't.
3794 activityIdleInternal((ActivityRecord)msg.obj);
3795 } break;
3796 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003797 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003798 activityIdleInternal((ActivityRecord)msg.obj);
3799 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003800 case RESUME_TOP_ACTIVITY_MSG: {
3801 synchronized (mService) {
3802 resumeTopActivitiesLocked();
3803 }
3804 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003805 case SLEEP_TIMEOUT_MSG: {
3806 synchronized (mService) {
3807 if (mService.isSleepingOrShuttingDown()) {
3808 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3809 mSleepTimeout = true;
3810 checkReadyForSleepLocked();
3811 }
3812 }
3813 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003814 case LAUNCH_TIMEOUT_MSG: {
3815 if (mService.mDidDexOpt) {
3816 mService.mDidDexOpt = false;
3817 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3818 return;
3819 }
3820 synchronized (mService) {
3821 if (mLaunchingActivity.isHeld()) {
3822 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3823 if (VALIDATE_WAKE_LOCK_CALLER
3824 && Binder.getCallingUid() != Process.myUid()) {
3825 throw new IllegalStateException("Calling must be system uid");
3826 }
3827 mLaunchingActivity.release();
3828 }
3829 }
3830 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003832 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003833 } break;
3834 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003835 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003836 } break;
3837 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003838 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003839 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003840 case CONTAINER_CALLBACK_VISIBILITY: {
3841 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003842 final IActivityContainerCallback callback = container.mCallback;
3843 if (callback != null) {
3844 try {
3845 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3846 } catch (RemoteException e) {
3847 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003848 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003849 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003850 case LOCK_TASK_START_MSG: {
3851 // When lock task starts, we disable the status bars.
3852 try {
Jason Monk62515be2014-05-21 16:06:19 -04003853 if (mLockTaskNotify == null) {
3854 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003855 }
Jason Monk62515be2014-05-21 16:06:19 -04003856 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003857 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003858 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003859 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003860 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003861 flags = StatusBarManager.DISABLE_MASK
3862 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003863 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003864 flags = StatusBarManager.DISABLE_MASK
3865 & (~StatusBarManager.DISABLE_BACK)
3866 & (~StatusBarManager.DISABLE_HOME)
3867 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003868 }
3869 getStatusBarService().disable(flags, mToken,
3870 mService.mContext.getPackageName());
3871 }
3872 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003873 if (getDevicePolicyManager() != null) {
3874 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003875 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003876 }
justinzhang5286d3f2014-05-12 17:06:01 -04003877 } catch (RemoteException ex) {
3878 throw new RuntimeException(ex);
3879 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003880 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003881 case LOCK_TASK_END_MSG: {
3882 // When lock task ends, we enable the status bars.
3883 try {
Jason Monk62515be2014-05-21 16:06:19 -04003884 if (getStatusBarService() != null) {
3885 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3886 mService.mContext.getPackageName());
3887 }
3888 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003889 if (getDevicePolicyManager() != null) {
3890 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3891 msg.arg1);
3892 }
Jason Monk62515be2014-05-21 16:06:19 -04003893 if (mLockTaskNotify == null) {
3894 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3895 }
3896 mLockTaskNotify.show(false);
3897 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003898 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003899 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003900 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003901 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003902 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003903 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003904 new LockPatternUtils(mService.mContext)
3905 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003906 }
3907 } catch (SettingNotFoundException e) {
3908 // No setting, don't lock.
3909 }
justinzhang5286d3f2014-05-12 17:06:01 -04003910 } catch (RemoteException ex) {
3911 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003912 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003913 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003914 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003915 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003916 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3917 if (mLockTaskNotify == null) {
3918 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3919 }
3920 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3921 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003922 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3923 final ActivityContainer container = (ActivityContainer) msg.obj;
3924 final IActivityContainerCallback callback = container.mCallback;
3925 if (callback != null) {
3926 try {
3927 callback.onAllActivitiesComplete(container.asBinder());
3928 } catch (RemoteException e) {
3929 }
3930 }
3931 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003932 case LAUNCH_TASK_BEHIND_COMPLETE: {
3933 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003934 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003935 if (r != null) {
3936 handleLaunchTaskBehindCompleteLocked(r);
3937 }
3938 }
3939 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003940 }
3941 }
3942 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003943
Ying Wangb081a592014-04-22 15:20:16 -07003944 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003945 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003946 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003947 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003948 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003949 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003950 ActivityRecord mParentActivity = null;
3951 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003952
Craig Mautnere3a00d72014-04-16 08:31:19 -07003953 boolean mVisible = true;
3954
Craig Mautner4a1cb222013-12-04 16:14:06 -08003955 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003956 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003957
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003958 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3959 final static int CONTAINER_STATE_NO_SURFACE = 1;
3960 final static int CONTAINER_STATE_FINISHING = 2;
3961 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3962
3963 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003964 synchronized (mService) {
3965 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003966 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003967 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003968 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003969 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003970 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003971
Craig Mautnere0a38842013-12-16 16:14:02 -08003972 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003973 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08003974 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003975 mActivityDisplay = activityDisplay;
3976 mStack.mDisplayId = activityDisplay.mDisplayId;
3977 mStack.mStacks = activityDisplay.mStacks;
3978
3979 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003980 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003981 }
3982
3983 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003984 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003985 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003986 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3987 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003988 return;
3989 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003990 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003991 }
3992 }
3993
3994 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003995 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003996 synchronized (mService) {
3997 if (mActivityDisplay != null) {
3998 return mActivityDisplay.mDisplayId;
3999 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004000 }
4001 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004002 }
4003
Jeff Brownca9bc702014-02-11 14:32:56 -08004004 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004005 public int getStackId() {
4006 synchronized (mService) {
4007 return mStackId;
4008 }
4009 }
4010
4011 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004012 public boolean injectEvent(InputEvent event) {
4013 final long origId = Binder.clearCallingIdentity();
4014 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004015 synchronized (mService) {
4016 if (mActivityDisplay != null) {
4017 return mInputManagerInternal.injectInputEvent(event,
4018 mActivityDisplay.mDisplayId,
4019 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4020 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004021 }
4022 return false;
4023 } finally {
4024 Binder.restoreCallingIdentity(origId);
4025 }
4026 }
4027
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004028 @Override
4029 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004030 synchronized (mService) {
4031 if (mContainerState == CONTAINER_STATE_FINISHING) {
4032 return;
4033 }
4034 mContainerState = CONTAINER_STATE_FINISHING;
4035
Craig Mautnerd163e752014-06-13 17:18:47 -07004036 long origId = Binder.clearCallingIdentity();
4037 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004038 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004039 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004040 } finally {
4041 Binder.restoreCallingIdentity(origId);
4042 }
4043 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004044 }
4045
Craig Mautner60257702014-09-17 15:02:33 -07004046 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004047 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004048 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004049 if (mActivityDisplay != null) {
4050 mActivityDisplay.detachActivitiesLocked(mStack);
4051 mActivityDisplay = null;
4052 mStack.mDisplayId = -1;
4053 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004054 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004055 }
4056 }
4057
4058 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004059 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004060 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004061 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004062 Binder.getCallingUid(), mCurrentUser, false,
4063 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004064
Craig Mautnere0a38842013-12-16 16:14:02 -08004065 // TODO: Switch to user app stacks here.
4066 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004067 final Uri data = intent.getData();
4068 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4069 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004070 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004071 checkEmbeddedAllowedInner(userId, intent, mimeType);
4072
4073 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4074 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4075 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004076 }
4077
4078 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004079 public final int startActivityIntentSender(IIntentSender intentSender)
4080 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004081 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4082
4083 if (!(intentSender instanceof PendingIntentRecord)) {
4084 throw new IllegalArgumentException("Bad PendingIntent object");
4085 }
4086
Craig Mautnerb9168362015-02-26 20:40:19 -08004087 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004088 Binder.getCallingUid(), mCurrentUser, false,
4089 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004090
4091 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4092 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4093 pendingIntent.key.requestResolvedType);
4094
4095 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4096 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4097 }
4098
4099 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004100 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004101 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004102 throw new SecurityException(
4103 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4104 }
4105 }
4106
Craig Mautnerdf88d732014-01-27 09:21:32 -08004107 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004108 public IBinder asBinder() {
4109 return this;
4110 }
4111
Craig Mautner4504de52013-12-20 09:06:56 -08004112 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004113 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004114 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004115 }
4116
Craig Mautner4a1cb222013-12-04 16:14:06 -08004117 ActivityStackSupervisor getOuter() {
4118 return ActivityStackSupervisor.this;
4119 }
4120
Craig Mautnerd163e752014-06-13 17:18:47 -07004121 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004122 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004123 }
4124
4125 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004126 synchronized (mService) {
4127 if (mActivityDisplay != null) {
4128 mActivityDisplay.getBounds(outBounds);
4129 } else {
4130 outBounds.set(0, 0);
4131 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004132 }
4133 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004134
Craig Mautner6985bad2014-04-21 15:22:06 -07004135 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004136 void setVisible(boolean visible) {
4137 if (mVisible != visible) {
4138 mVisible = visible;
4139 if (mCallback != null) {
4140 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4141 0 /* unused */, this).sendToTarget();
4142 }
4143 }
4144 }
4145
Craig Mautner6985bad2014-04-21 15:22:06 -07004146 void setDrawn() {
4147 }
4148
Craig Mautner1b4bf852014-05-26 15:06:32 -07004149 // You can always start a new task on a regular ActivityStack.
4150 boolean isEligibleForNewTasks() {
4151 return true;
4152 }
4153
Craig Mautnerd163e752014-06-13 17:18:47 -07004154 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004155 detachLocked();
4156 deleteActivityContainer(this);
4157 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004158 }
4159
Craig Mautner34b73df2014-01-12 21:11:08 -08004160 @Override
4161 public String toString() {
4162 return mIdString + (mActivityDisplay == null ? "N" : "A");
4163 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004164 }
4165
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004166 private class VirtualActivityContainer extends ActivityContainer {
4167 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004168 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004169
4170 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4171 super(getNextStackId());
4172 mParentActivity = parent;
4173 mCallback = callback;
4174 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004175 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004176 }
4177
4178 @Override
4179 public void setSurface(Surface surface, int width, int height, int density) {
4180 super.setSurface(surface, width, height, density);
4181
4182 synchronized (mService) {
4183 final long origId = Binder.clearCallingIdentity();
4184 try {
4185 setSurfaceLocked(surface, width, height, density);
4186 } finally {
4187 Binder.restoreCallingIdentity(origId);
4188 }
4189 }
4190 }
4191
4192 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4193 if (mContainerState == CONTAINER_STATE_FINISHING) {
4194 return;
4195 }
4196 VirtualActivityDisplay virtualActivityDisplay =
4197 (VirtualActivityDisplay) mActivityDisplay;
4198 if (virtualActivityDisplay == null) {
4199 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004200 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004201 mActivityDisplay = virtualActivityDisplay;
4202 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4203 attachToDisplayLocked(virtualActivityDisplay);
4204 }
4205
4206 if (mSurface != null) {
4207 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004208 }
4209
Craig Mautner6985bad2014-04-21 15:22:06 -07004210 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004211 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004212 mStack.resumeTopActivityLocked(null);
4213 } else {
4214 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004215 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004216 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004217 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004218 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004219 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004220
Craig Mautnerd163e752014-06-13 17:18:47 -07004221 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004222
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004223 if (DEBUG_STACK) Slog.d(TAG_STACK,
4224 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004225 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004226
Craig Mautner6985bad2014-04-21 15:22:06 -07004227 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004228 boolean isAttachedLocked() {
4229 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004230 }
4231
4232 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004233 void setDrawn() {
4234 synchronized (mService) {
4235 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004236 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004237 }
4238 }
4239
Craig Mautner1b4bf852014-05-26 15:06:32 -07004240 // Never start a new task on an ActivityView if it isn't explicitly specified.
4241 @Override
4242 boolean isEligibleForNewTasks() {
4243 return false;
4244 }
4245
Craig Mautnerd163e752014-06-13 17:18:47 -07004246 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004247 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004248 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4249 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4250 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4251 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4252 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004253 }
4254 }
4255
Craig Mautner4a1cb222013-12-04 16:14:06 -08004256 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4257 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004258 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004259 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004260 int mDisplayId;
4261 Display mDisplay;
4262 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004263
Craig Mautner4a1cb222013-12-04 16:14:06 -08004264 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4265 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004266 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004267
Jose Lima4b6c6692014-08-12 17:41:12 -07004268 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004269
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004270 ActivityDisplay() {
4271 }
Craig Mautner4504de52013-12-20 09:06:56 -08004272
Craig Mautner1a70a162014-09-13 12:09:31 -07004273 // After instantiation, check that mDisplay is not null before using this. The alternative
4274 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004275 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004276 final Display display = mDisplayManager.getDisplay(displayId);
4277 if (display == null) {
4278 return;
4279 }
4280 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004281 }
4282
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004283 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004284 mDisplay = display;
4285 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004286 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004287 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004288
4289 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004290 if (DEBUG_STACK) Slog.v(TAG_STACK,
4291 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004292 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004293 }
4294
Craig Mautnere0a38842013-12-16 16:14:02 -08004295 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004296 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004297 + " from displayId=" + mDisplayId);
4298 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004299 }
4300
4301 void getBounds(Point bounds) {
4302 mDisplay.getDisplayInfo(mDisplayInfo);
4303 bounds.x = mDisplayInfo.appWidth;
4304 bounds.y = mDisplayInfo.appHeight;
4305 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004306
Jose Lima4b6c6692014-08-12 17:41:12 -07004307 void setVisibleBehindActivity(ActivityRecord r) {
4308 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004309 }
4310
Jose Lima4b6c6692014-08-12 17:41:12 -07004311 boolean hasVisibleBehindActivity() {
4312 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004313 }
4314
Craig Mautner34b73df2014-01-12 21:11:08 -08004315 @Override
4316 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004317 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4318 }
4319 }
4320
4321 class VirtualActivityDisplay extends ActivityDisplay {
4322 VirtualDisplay mVirtualDisplay;
4323
Craig Mautner6985bad2014-04-21 15:22:06 -07004324 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004325 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004326 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4327 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4328 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4329 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004330
4331 init(mVirtualDisplay.getDisplay());
4332
4333 mWindowManager.handleDisplayAdded(mDisplayId);
4334 }
4335
4336 void setSurface(Surface surface) {
4337 if (mVirtualDisplay != null) {
4338 mVirtualDisplay.setSurface(surface);
4339 }
4340 }
4341
4342 @Override
4343 void detachActivitiesLocked(ActivityStack stack) {
4344 super.detachActivitiesLocked(stack);
4345 if (mVirtualDisplay != null) {
4346 mVirtualDisplay.release();
4347 mVirtualDisplay = null;
4348 }
4349 }
4350
4351 @Override
4352 public String toString() {
4353 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004354 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004355 }
Jose Lima58e66d62014-05-27 20:00:27 -07004356
4357 private boolean isLeanbackOnlyDevice() {
4358 boolean onLeanbackOnly = false;
4359 try {
4360 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4361 PackageManager.FEATURE_LEANBACK_ONLY);
4362 } catch (RemoteException e) {
4363 // noop
4364 }
4365
4366 return onLeanbackOnly;
4367 }
Craig Mautner27084302013-03-25 08:05:25 -07004368}