blob: c564a6a08b483906a743a973637932e883aba556 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070020import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070022import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
23import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070024import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080025import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070026import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070027import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
28import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
29import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070030import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner8d341ef2013-03-26 09:03:27 -070031
Craig Mautner2420ead2013-04-01 17:13:20 -070032import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070033import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080034import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070035import android.app.ActivityOptions;
36import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080037import android.app.IActivityContainer;
38import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070039import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070040import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070042import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070043import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070045import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040046import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040047import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070048import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070049import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070051import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070053import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.content.pm.ApplicationInfo;
55import android.content.pm.PackageManager;
56import android.content.pm.ResolveInfo;
57import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080058import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080059import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080060import android.hardware.display.DisplayManager;
61import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080062import android.hardware.display.DisplayManagerGlobal;
63import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080064import android.hardware.input.InputManager;
65import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080066import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070067import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070068import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070069import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070070import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070071import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070072import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070073import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070074import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070075import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070076import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070077import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040078import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070079import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070080import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070081import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070082import android.os.WorkSource;
Jason Monk62515be2014-05-21 16:06:19 -040083import android.provider.Settings;
84import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070085import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070086import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070087import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070088import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080089import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070090
Craig Mautner4a1cb222013-12-04 16:14:06 -080091import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080092import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080093import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080094import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080095import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070096import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070097import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080098import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070099import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400100import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400101import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800102import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700103import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700104import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700105
justinzhang5286d3f2014-05-12 17:06:01 -0400106
Craig Mautner8d341ef2013-03-26 09:03:27 -0700107import java.io.FileDescriptor;
108import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700109import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700110import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700111import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700112
Craig Mautner4a1cb222013-12-04 16:14:06 -0800113public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800114 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700115 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
116 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700117 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
118 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
119 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
120 private static final String TAG_STACK = TAG + POSTFIX_STACK;
121 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
122 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
123 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800124
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800125 static final boolean DEBUG = DEBUG_ALL || false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700126 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
127 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700128 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700129 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700130 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700131 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
132 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautner2568c3a2015-03-26 14:22:34 -0700133 static final boolean DEBUG_STATES = DEBUG || true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700134 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700135
Craig Mautner2219a1b2013-03-25 09:44:30 -0700136 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700137
Craig Mautnerf3333272013-04-22 10:55:53 -0700138 /** How long we wait until giving up on the last activity telling us it is idle. */
139 static final int IDLE_TIMEOUT = 10*1000;
140
Craig Mautner0eea92c2013-05-16 13:35:39 -0700141 /** How long we can hold the sleep wake lock before giving up. */
142 static final int SLEEP_TIMEOUT = 5*1000;
143
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700144 // How long we can hold the launch wake lock before giving up.
145 static final int LAUNCH_TIMEOUT = 10*1000;
146
Craig Mautner05d29032013-05-03 13:40:13 -0700147 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
148 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
149 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700150 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700151 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800152 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
153 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
154 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700155 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400156 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
157 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700158 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
159 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
160 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161
Craig Mautner4504de52013-12-20 09:06:56 -0800162 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700163
Jason Monk62515be2014-05-21 16:06:19 -0400164 private static final String LOCK_TASK_TAG = "Lock-to-App";
165
justinzhang5286d3f2014-05-12 17:06:01 -0400166 /** Status Bar Service **/
167 private IBinder mToken = new Binder();
168 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400169 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400170
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700171 // For debugging to make sure the caller when acquiring/releasing our
172 // wake lock is the system process.
173 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700174
Craig Mautner27084302013-03-25 08:05:25 -0700175 final ActivityManagerService mService;
176
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800177 private final RecentTasks mRecentTasks;
178
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700179 final ActivityStackSupervisorHandler mHandler;
180
181 /** Short cut */
182 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800183 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700184
Craig Mautner8d341ef2013-03-26 09:03:27 -0700185 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700186 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700187
188 /** Task identifier that activities are currently being started in. Incremented each time a
189 * new task is created. */
190 private int mCurTaskId = 0;
191
Craig Mautner2420ead2013-04-01 17:13:20 -0700192 /** The current user */
193 private int mCurrentUser;
194
Craig Mautnere0a38842013-12-16 16:14:02 -0800195 /** The stack containing the launcher app. Assumed to always be attached to
196 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700197 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700198
Craig Mautnere0a38842013-12-16 16:14:02 -0800199 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700200 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700201
Craig Mautner4a1cb222013-12-04 16:14:06 -0800202 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
203 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800204 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800205 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700206
207 /** List of activities that are waiting for a new activity to become visible before completing
208 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800209 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700210
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700211 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800212 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700213
214 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800215 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700216
Craig Mautnerde4ef022013-04-07 19:01:33 -0700217 /** List of activities that are ready to be stopped, but waiting for the next activity to
218 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800219 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700220
Craig Mautnerf3333272013-04-22 10:55:53 -0700221 /** List of activities that are ready to be finished, but waiting for the previous activity to
222 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800223 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700224
Craig Mautner0eea92c2013-05-16 13:35:39 -0700225 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800226 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700227
Craig Mautnerf3333272013-04-22 10:55:53 -0700228 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800229 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700230
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700231 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800232 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700233
Craig Mautnerde4ef022013-04-07 19:01:33 -0700234 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
235 * is being brought in front of us. */
236 boolean mUserLeaving = false;
237
Craig Mautner0eea92c2013-05-16 13:35:39 -0700238 /** Set when we have taken too long waiting to go to sleep. */
239 boolean mSleepTimeout = false;
240
Jose Lima58e66d62014-05-27 20:00:27 -0700241 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
242 * setWindowManager is called. **/
243 private boolean mLeanbackOnlyDevice;
244
Craig Mautner0eea92c2013-05-16 13:35:39 -0700245 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700246 * We don't want to allow the device to go to sleep while in the process
247 * of launching an activity. This is primarily to allow alarm intent
248 * receivers to launch an activity and get that to run before the device
249 * goes back to sleep.
250 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700251 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700252
253 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700254 * Set when the system is going to sleep, until we have
255 * successfully paused the current activity and released our wake lock.
256 * At that point the system is allowed to actually sleep.
257 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700258 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700259
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700260 /** Stack id of the front stack when user switched, indexed by userId. */
261 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700262
Craig Mautner4504de52013-12-20 09:06:56 -0800263 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800264 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700265 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800266
267 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700268 private final SparseArray<ActivityDisplay> mActivityDisplays =
269 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800270
Jeff Brownca9bc702014-02-11 14:32:56 -0800271 InputManagerInternal mInputManagerInternal;
272
Craig Mautneraea74a52014-03-08 14:23:10 -0800273 /** If non-null then the task specified remains in front and no other tasks may be started
274 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400275 TaskRecord mLockTaskModeTask;
Benjamin Franz43261142015-02-11 15:59:44 +0000276 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700277 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
278 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000279 */
280 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400281 /**
282 * Notifies the user when entering/exiting lock-task.
283 */
284 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800285
Craig Mautneree36c772014-07-16 14:56:05 -0700286 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
287 = new ArrayList<PendingActivityLaunch>();
288
Craig Mautner42d04db2014-11-06 12:13:23 -0800289 /** Used to keep resumeTopActivityLocked() from being entered recursively */
290 boolean inResumeTopActivity;
291
Craig Mautneree36c772014-07-16 14:56:05 -0700292 /**
293 * Description of a request to start a new activity, which has been held
294 * due to app switches being disabled.
295 */
296 static class PendingActivityLaunch {
297 final ActivityRecord r;
298 final ActivityRecord sourceRecord;
299 final int startFlags;
300 final ActivityStack stack;
301
302 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
303 int _startFlags, ActivityStack _stack) {
304 r = _r;
305 sourceRecord = _sourceRecord;
306 startFlags = _startFlags;
307 stack = _stack;
308 }
309 }
310
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800311 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700312 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800313 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700314 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
315 }
316
317 /**
318 * At the time when the constructor runs, the power manager has not yet been
319 * initialized. So we initialize our wakelocks afterwards.
320 */
321 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700323 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700324 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700325 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700326 }
327
justinzhang5286d3f2014-05-12 17:06:01 -0400328 // This function returns a IStatusBarService. The value is from ServiceManager.
329 // getService and is cached.
330 private IStatusBarService getStatusBarService() {
331 synchronized (mService) {
332 if (mStatusBarService == null) {
333 mStatusBarService = IStatusBarService.Stub.asInterface(
334 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
335 if (mStatusBarService == null) {
336 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
337 }
338 }
339 return mStatusBarService;
340 }
341 }
342
Jason Monk35c62a42014-06-17 10:24:47 -0400343 private IDevicePolicyManager getDevicePolicyManager() {
344 synchronized (mService) {
345 if (mDevicePolicyManager == null) {
346 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
347 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
348 if (mDevicePolicyManager == null) {
349 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
350 }
351 }
352 return mDevicePolicyManager;
353 }
354 }
355
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700356 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800357 synchronized (mService) {
358 mWindowManager = wm;
359
360 mDisplayManager =
361 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
362 mDisplayManager.registerDisplayListener(this, null);
363
364 Display[] displays = mDisplayManager.getDisplays();
365 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
366 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800367 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700368 if (activityDisplay.mDisplay == null) {
369 throw new IllegalStateException("Default Display does not exist");
370 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800371 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800372 }
373
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700374 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800376
377 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700378
379 // Initialize this here, now that we can get a valid reference to PackageManager.
380 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381 }
Craig Mautner27084302013-03-25 08:05:25 -0700382 }
383
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200384 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700385 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200386 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700387 }
388
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700389 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800390 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700391 }
392
Craig Mautnerde4ef022013-04-07 19:01:33 -0700393 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800394 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700395 }
396
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800397 /** Top of all visible stacks is/should always be equal to the focused stack.
398 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
399 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700400 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700401 if (stack == null) {
402 return false;
403 }
404
Craig Mautnerdf88d732014-01-27 09:21:32 -0800405 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
406 if (parent != null) {
407 stack = parent.task.stack;
408 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800409 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700410 }
411
Craig Mautner299f9602015-01-26 09:47:33 -0800412 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800413 moveHomeStack(toFront, reason, null);
414 }
415
416 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800417 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800418 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800419 if (topNdx <= 0) {
420 return;
421 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700422
423 // The home stack should either be at the top or bottom of the stack list.
424 if ((toFront && (stacks.get(topNdx) != mHomeStack))
425 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700426 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700427 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
428 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800429 stacks.remove(mHomeStack);
430 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700431 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800432
433 if (lastFocusedStack != null) {
434 mLastFocusedStack = lastFocusedStack;
435 }
436 mFocusedStack = stacks.get(topNdx);
437
Craig Mautnerde313752015-01-22 14:28:03 -0800438 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
439 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800440 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800441
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800442 if (mService.mBooting || !mService.mBooted) {
443 final ActivityRecord r = topRunningActivityLocked();
444 if (r != null && r.idle) {
445 checkFinishBootingLocked();
446 }
447 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700448 }
449
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700450 /** Returns true if the focus activity was adjusted to the home stack top activity. */
451 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700452 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
453 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700454 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700455 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700456
Craig Mautner84984fa2014-06-19 11:19:20 -0700457 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700458
459 final ActivityRecord top = mHomeStack.topRunningActivityLocked(null);
460 if (top == null) {
461 return false;
462 }
463 mService.setFocusedActivityLocked(top, reason);
464 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700465 }
466
Craig Mautner299f9602015-01-26 09:47:33 -0800467 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700468 if (!mService.mBooting && !mService.mBooted) {
469 // Not ready yet!
470 return false;
471 }
472
Craig Mautner84984fa2014-06-19 11:19:20 -0700473 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
474 mWindowManager.showRecentApps();
475 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700476 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700477
Craig Mautner84984fa2014-06-19 11:19:20 -0700478 if (prev != null) {
479 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
480 }
481
Craig Mautnera8a90e02013-06-28 15:24:50 -0700482 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700483 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800484 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700485 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700486 }
Craig Mautner299f9602015-01-26 09:47:33 -0800487 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700488 }
489
Craig Mautner8d341ef2013-03-26 09:03:27 -0700490 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700491 return anyTaskForIdLocked(id, true);
492 }
493
494 /**
495 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
496 * @param id Id of the task we would like returned.
497 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
498 * restore the task from recents to the active list.
499 */
500 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800501 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800502 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800503 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800504 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
505 ActivityStack stack = stacks.get(stackNdx);
506 TaskRecord task = stack.taskForIdLocked(id);
507 if (task != null) {
508 return task;
509 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700510 }
511 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800512
513 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700514 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800515 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800516 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700517 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800518 return null;
519 }
520
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700521 if (!restoreFromRecents) {
522 return task;
523 }
524
Wale Ogunwale7de05352014-12-12 15:21:33 -0800525 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700526 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
527 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800528 return null;
529 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700530 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800531 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700532 }
533
Craig Mautner6170f732013-04-02 13:05:23 -0700534 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800535 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800536 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800537 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800538 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
539 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
540 if (r != null) {
541 return r;
542 }
Craig Mautner6170f732013-04-02 13:05:23 -0700543 }
544 }
545 return null;
546 }
547
Craig Mautneref73ee12014-04-23 11:45:37 -0700548 void setNextTaskId(int taskId) {
549 if (taskId > mCurTaskId) {
550 mCurTaskId = taskId;
551 }
552 }
553
Craig Mautner8d341ef2013-03-26 09:03:27 -0700554 int getNextTaskId() {
555 do {
556 mCurTaskId++;
557 if (mCurTaskId <= 0) {
558 mCurTaskId = 1;
559 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700560 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700561 return mCurTaskId;
562 }
563
Craig Mautnerde4ef022013-04-07 19:01:33 -0700564 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800565 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700566 if (stack == null) {
567 return null;
568 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700569 ActivityRecord resumedActivity = stack.mResumedActivity;
570 if (resumedActivity == null || resumedActivity.app == null) {
571 resumedActivity = stack.mPausingActivity;
572 if (resumedActivity == null || resumedActivity.app == null) {
573 resumedActivity = stack.topRunningActivityLocked(null);
574 }
575 }
576 return resumedActivity;
577 }
578
Dianne Hackbornff072722014-09-24 10:56:28 -0700579 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700580 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800581 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800582 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
583 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800584 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
585 final ActivityStack stack = stacks.get(stackNdx);
586 if (!isFrontStack(stack)) {
587 continue;
588 }
589 ActivityRecord hr = stack.topRunningActivityLocked(null);
590 if (hr != null) {
591 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
592 && processName.equals(hr.processName)) {
593 try {
George Mount2c92c972014-03-20 09:38:23 -0700594 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800595 didSomething = true;
596 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700597 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800598 Slog.w(TAG, "Exception in new application when starting activity "
599 + hr.intent.getComponent().flattenToShortString(), e);
600 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700601 }
Craig Mautner20e72272013-04-01 13:45:53 -0700602 }
Craig Mautner20e72272013-04-01 13:45:53 -0700603 }
604 }
605 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700606 if (!didSomething) {
607 ensureActivitiesVisibleLocked(null, 0);
608 }
Craig Mautner20e72272013-04-01 13:45:53 -0700609 return didSomething;
610 }
611
612 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
614 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800615 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
616 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800617 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800618 continue;
619 }
620 final ActivityRecord resumedActivity = stack.mResumedActivity;
621 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800622 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
623 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800624 return false;
625 }
Craig Mautner20e72272013-04-01 13:45:53 -0700626 }
627 }
628 return true;
629 }
630
Craig Mautnerde4ef022013-04-07 19:01:33 -0700631 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800632 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
633 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
635 final ActivityStack stack = stacks.get(stackNdx);
636 if (isFrontStack(stack)) {
637 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700638 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800639 return false;
640 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700641 }
642 }
643 }
644 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700645 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800646 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
647 mLastFocusedStack + " to=" + mFocusedStack);
648 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700649 return true;
650 }
651
652 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
654 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
656 final ActivityStack stack = stacks.get(stackNdx);
657 final ActivityRecord r = stack.mResumedActivity;
Craig Mautner8c14c152015-01-15 17:32:07 -0800658 if (r != null && (!r.nowVisible || mWaitingVisibleActivities.contains(r))) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800659 return false;
660 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700661 }
662 }
663 return true;
664 }
665
Craig Mautner2acc3892013-09-23 10:28:14 -0700666 /**
667 * Pause all activities in either all of the stacks or just the back stacks.
668 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700669 * @return true if any activity was paused as a result of this call.
670 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700671 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700672 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
674 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
676 final ActivityStack stack = stacks.get(stackNdx);
677 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
678 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
679 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700680 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
681 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800682 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700683 }
684 }
685 return someActivityPaused;
686 }
687
Craig Mautnerde4ef022013-04-07 19:01:33 -0700688 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700689 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800690 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
691 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800692 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
693 final ActivityStack stack = stacks.get(stackNdx);
694 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700695 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800696 if (DEBUG_STATES) {
697 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
698 pausing = false;
699 } else {
700 return false;
701 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700702 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700703 }
704 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700705 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700706 }
707
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700708 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
709 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500710 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800711 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
712 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
713 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
714 final ActivityStack stack = stacks.get(stackNdx);
715 if (stack.mResumedActivity != null &&
716 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700717 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800718 }
719 }
720 }
721 }
722
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700723 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700724 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700725 }
726
727 void sendWaitingVisibleReportLocked(ActivityRecord r) {
728 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700729 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700730 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700731 if (w.who == null) {
732 changed = true;
733 w.timeout = false;
734 if (r != null) {
735 w.who = new ComponentName(r.info.packageName, r.info.name);
736 }
737 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
738 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700739 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700740 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700741 if (changed) {
742 mService.notifyAll();
743 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700744 }
745
746 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
747 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700748 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700749 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700750 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700751 if (w.who == null) {
752 changed = true;
753 w.timeout = timeout;
754 if (r != null) {
755 w.who = new ComponentName(r.info.packageName, r.info.name);
756 }
757 w.thisTime = thisTime;
758 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700759 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700760 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700761 if (changed) {
762 mService.notifyAll();
763 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700764 }
765
Craig Mautner29219d92013-04-16 20:19:12 -0700766 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800767 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700768 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
769 if (r != null) {
770 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700771 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700772
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800774 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800775 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
776 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700777 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700778 r = stack.topRunningActivityLocked(null);
779 if (r != null) {
780 return r;
781 }
782 }
783 }
784 return null;
785 }
786
Dianne Hackborn09233282014-04-30 11:33:59 -0700787 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700788 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800789 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
790 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800791 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800792 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800793 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800794 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
795 final ActivityStack stack = stacks.get(stackNdx);
796 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
797 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700798 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700799 }
800 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700801
802 // The lists are already sorted from most recent to oldest. Just pull the most recent off
803 // each list and add it to list. Stop when all lists are empty or maxNum reached.
804 while (maxNum > 0) {
805 long mostRecentActiveTime = Long.MIN_VALUE;
806 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800807 final int numTaskLists = runningTaskLists.size();
808 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
809 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700810 if (!stackTaskList.isEmpty()) {
811 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
812 if (lastActiveTime > mostRecentActiveTime) {
813 mostRecentActiveTime = lastActiveTime;
814 selectedStackList = stackTaskList;
815 }
816 }
817 }
818 if (selectedStackList != null) {
819 list.add(selectedStackList.remove(0));
820 --maxNum;
821 } else {
822 break;
823 }
824 }
Craig Mautner20e72272013-04-01 13:45:53 -0700825 }
826
Craig Mautner23ac33b2013-04-01 16:26:35 -0700827 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700828 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700829 // Collect information about the target of the Intent.
830 ActivityInfo aInfo;
831 try {
832 ResolveInfo rInfo =
833 AppGlobals.getPackageManager().resolveIntent(
834 intent, resolvedType,
835 PackageManager.MATCH_DEFAULT_ONLY
836 | ActivityManagerService.STOCK_PM_FLAGS, userId);
837 aInfo = rInfo != null ? rInfo.activityInfo : null;
838 } catch (RemoteException e) {
839 aInfo = null;
840 }
841
842 if (aInfo != null) {
843 // Store the found target back into the intent, because now that
844 // we have it we never want to do this again. For example, if the
845 // user navigates back to this point in the history, we should
846 // always restart the exact same activity.
847 intent.setComponent(new ComponentName(
848 aInfo.applicationInfo.packageName, aInfo.name));
849
850 // Don't debug things in the system process
851 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
852 if (!aInfo.processName.equals("system")) {
853 mService.setDebugApp(aInfo.processName, true, false);
854 }
855 }
856
857 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
858 if (!aInfo.processName.equals("system")) {
859 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
860 }
861 }
862
Jeff Hao1b012d32014-08-20 10:35:34 -0700863 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700864 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700865 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700866 }
867 }
868 }
869 return aInfo;
870 }
871
Craig Mautner299f9602015-01-26 09:47:33 -0800872 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
873 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700874 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
875 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700876 }
877
Craig Mautner23ac33b2013-04-01 16:26:35 -0700878 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700879 String callingPackage, Intent intent, String resolvedType,
880 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700881 IBinder resultTo, String resultWho, int requestCode, int startFlags,
882 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700883 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700884 // Refuse possible leaked file descriptors
885 if (intent != null && intent.hasFileDescriptors()) {
886 throw new IllegalArgumentException("File descriptors passed in Intent");
887 }
888 boolean componentSpecified = intent.getComponent() != null;
889
890 // Don't modify the client's object!
891 intent = new Intent(intent);
892
893 // Collect information about the target of the Intent.
894 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700895 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700896
Craig Mautnere0a38842013-12-16 16:14:02 -0800897 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700898 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800899 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700900 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800901 // Cannot start a child activity if the parent is not resumed.
902 return ActivityManager.START_CANCELED;
903 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700904 final int realCallingPid = Binder.getCallingPid();
905 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700906 int callingPid;
907 if (callingUid >= 0) {
908 callingPid = -1;
909 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700910 callingPid = realCallingPid;
911 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700912 } else {
913 callingPid = callingUid = -1;
914 }
915
Craig Mautnere0a38842013-12-16 16:14:02 -0800916 final ActivityStack stack;
917 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800918 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800919 } else {
920 stack = container.mStack;
921 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800922 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700923 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700924 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700925
926 final long origId = Binder.clearCallingIdentity();
927
928 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800929 (aInfo.applicationInfo.privateFlags
930 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700931 // This may be a heavy-weight process! Check to see if we already
932 // have another, different heavy-weight process running.
933 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
934 if (mService.mHeavyWeightProcess != null &&
935 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
936 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700937 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700938 if (caller != null) {
939 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
940 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700941 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700942 } else {
943 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700944 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700945 + intent.toString());
946 ActivityOptions.abort(options);
947 return ActivityManager.START_PERMISSION_DENIED;
948 }
949 }
950
951 IIntentSender target = mService.getIntentSenderLocked(
952 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700953 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700954 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
955 | PendingIntent.FLAG_ONE_SHOT, null);
956
957 Intent newIntent = new Intent();
958 if (requestCode >= 0) {
959 // Caller is requesting a result.
960 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
961 }
962 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
963 new IntentSender(target));
964 if (mService.mHeavyWeightProcess.activities.size() > 0) {
965 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
966 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
967 hist.packageName);
968 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
969 hist.task.taskId);
970 }
971 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
972 aInfo.packageName);
973 newIntent.setFlags(intent.getFlags());
974 newIntent.setClassName("android",
975 HeavyWeightSwitcherActivity.class.getName());
976 intent = newIntent;
977 resolvedType = null;
978 caller = null;
979 callingUid = Binder.getCallingUid();
980 callingPid = Binder.getCallingPid();
981 componentSpecified = true;
982 try {
983 ResolveInfo rInfo =
984 AppGlobals.getPackageManager().resolveIntent(
985 intent, null,
986 PackageManager.MATCH_DEFAULT_ONLY
987 | ActivityManagerService.STOCK_PM_FLAGS, userId);
988 aInfo = rInfo != null ? rInfo.activityInfo : null;
989 aInfo = mService.getActivityInfoForUser(aInfo, userId);
990 } catch (RemoteException e) {
991 aInfo = null;
992 }
993 }
994 }
995 }
996
Dianne Hackborn91097de2014-04-04 18:02:06 -0700997 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
998 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700999 requestCode, callingPid, callingUid, callingPackage,
1000 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001001 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001002
Craig Mautner85c11a82014-07-17 12:38:18 -07001003 Binder.restoreCallingIdentity(origId);
1004
Craig Mautnerde4ef022013-04-07 19:01:33 -07001005 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001006 // If the caller also wants to switch to a new configuration,
1007 // do so now. This allows a clean switch, as we are waiting
1008 // for the current activity to pause (so we will not destroy
1009 // it), and have not yet started the next activity.
1010 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1011 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001012 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001013 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001014 "Updating to new configuration after starting activity.");
1015 mService.updateConfigurationLocked(config, null, false, false);
1016 }
1017
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018 if (outResult != null) {
1019 outResult.result = res;
1020 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001021 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001022 do {
1023 try {
1024 mService.wait();
1025 } catch (InterruptedException e) {
1026 }
1027 } while (!outResult.timeout && outResult.who == null);
1028 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001029 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001030 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001031 outResult.timeout = false;
1032 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1033 outResult.totalTime = 0;
1034 outResult.thisTime = 0;
1035 } else {
1036 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001037 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001038 do {
1039 try {
1040 mService.wait();
1041 } catch (InterruptedException e) {
1042 }
1043 } while (!outResult.timeout && outResult.who == null);
1044 }
1045 }
1046 }
1047
1048 return res;
1049 }
1050 }
1051
1052 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1053 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1054 Bundle options, int userId) {
1055 if (intents == null) {
1056 throw new NullPointerException("intents is null");
1057 }
1058 if (resolvedTypes == null) {
1059 throw new NullPointerException("resolvedTypes is null");
1060 }
1061 if (intents.length != resolvedTypes.length) {
1062 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1063 }
1064
Craig Mautner23ac33b2013-04-01 16:26:35 -07001065
1066 int callingPid;
1067 if (callingUid >= 0) {
1068 callingPid = -1;
1069 } else if (caller == null) {
1070 callingPid = Binder.getCallingPid();
1071 callingUid = Binder.getCallingUid();
1072 } else {
1073 callingPid = callingUid = -1;
1074 }
1075 final long origId = Binder.clearCallingIdentity();
1076 try {
1077 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001078 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001079 for (int i=0; i<intents.length; i++) {
1080 Intent intent = intents[i];
1081 if (intent == null) {
1082 continue;
1083 }
1084
1085 // Refuse possible leaked file descriptors
1086 if (intent != null && intent.hasFileDescriptors()) {
1087 throw new IllegalArgumentException("File descriptors passed in Intent");
1088 }
1089
1090 boolean componentSpecified = intent.getComponent() != null;
1091
1092 // Don't modify the client's object!
1093 intent = new Intent(intent);
1094
1095 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001096 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001097 // TODO: New, check if this is correct
1098 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1099
1100 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001101 (aInfo.applicationInfo.privateFlags
1102 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001103 throw new IllegalArgumentException(
1104 "FLAG_CANT_SAVE_STATE not supported here");
1105 }
1106
1107 Bundle theseOptions;
1108 if (options != null && i == intents.length-1) {
1109 theseOptions = options;
1110 } else {
1111 theseOptions = null;
1112 }
Craig Mautner6170f732013-04-02 13:05:23 -07001113 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001114 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1115 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001116 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001117 if (res < 0) {
1118 return res;
1119 }
1120
1121 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1122 }
1123 }
1124 } finally {
1125 Binder.restoreCallingIdentity(origId);
1126 }
1127
1128 return ActivityManager.START_SUCCESS;
1129 }
1130
Craig Mautner2420ead2013-04-01 17:13:20 -07001131 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001132 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001133 throws RemoteException {
1134
Craig Mautner2568c3a2015-03-26 14:22:34 -07001135 if (andResume) {
1136 r.startFreezingScreenLocked(app, 0);
1137 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001138
Craig Mautner2568c3a2015-03-26 14:22:34 -07001139 // schedule launch ticks to collect information about slow apps.
1140 r.startLaunchTickingLocked();
1141 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001142
1143 // Have the window manager re-evaluate the orientation of
1144 // the screen based on the new activity order. Note that
1145 // as a result of this, it can call back into the activity
1146 // manager with a new orientation. We don't care about that,
1147 // because the activity is not currently running so we are
1148 // just restarting it anyway.
1149 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001150 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001151 mService.mConfiguration,
1152 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1153 mService.updateConfigurationLocked(config, r, false, false);
1154 }
1155
1156 r.app = app;
1157 app.waitingToKill = null;
1158 r.launchCount++;
1159 r.lastLaunchTime = SystemClock.uptimeMillis();
1160
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001161 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001162
1163 int idx = app.activities.indexOf(r);
1164 if (idx < 0) {
1165 app.activities.add(r);
1166 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001167 mService.updateLruProcessLocked(app, true, null);
1168 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001169
1170 final ActivityStack stack = r.task.stack;
1171 try {
1172 if (app.thread == null) {
1173 throw new RemoteException();
1174 }
1175 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001176 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001177 if (andResume) {
1178 results = r.results;
1179 newIntents = r.newIntents;
1180 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001181 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1182 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1183 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001184 if (andResume) {
1185 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1186 r.userId, System.identityHashCode(r),
1187 r.task.taskId, r.shortComponentName);
1188 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001189 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001190 // Home process is the root process of the task.
1191 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001192 }
1193 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1194 r.sleeping = false;
1195 r.forceNewConfig = false;
1196 mService.showAskCompatModeDialogLocked(r);
1197 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001198 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001199 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1200 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1201 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001202 final String profileFile = mService.mProfileFile;
1203 if (profileFile != null) {
1204 ParcelFileDescriptor profileFd = mService.mProfileFd;
1205 if (profileFd != null) {
1206 try {
1207 profileFd = profileFd.dup();
1208 } catch (IOException e) {
1209 if (profileFd != null) {
1210 try {
1211 profileFd.close();
1212 } catch (IOException o) {
1213 }
1214 profileFd = null;
1215 }
1216 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001217 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001218
1219 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1220 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001221 }
1222 }
1223 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001224
Craig Mautner2568c3a2015-03-26 14:22:34 -07001225 if (andResume) {
1226 app.hasShownUi = true;
1227 app.pendingUiClean = true;
1228 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001229 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001230 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001231 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001232 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1233 r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1234 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001235
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001236 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001237 // This may be a heavy-weight process! Note that the package
1238 // manager will ensure that only activity can run in the main
1239 // process of the .apk, which is the only thing that will be
1240 // considered heavy-weight.
1241 if (app.processName.equals(app.info.packageName)) {
1242 if (mService.mHeavyWeightProcess != null
1243 && mService.mHeavyWeightProcess != app) {
1244 Slog.w(TAG, "Starting new heavy weight process " + app
1245 + " when already running "
1246 + mService.mHeavyWeightProcess);
1247 }
1248 mService.mHeavyWeightProcess = app;
1249 Message msg = mService.mHandler.obtainMessage(
1250 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1251 msg.obj = r;
1252 mService.mHandler.sendMessage(msg);
1253 }
1254 }
1255
1256 } catch (RemoteException e) {
1257 if (r.launchFailed) {
1258 // This is the second time we failed -- finish activity
1259 // and give up.
1260 Slog.e(TAG, "Second failure launching "
1261 + r.intent.getComponent().flattenToShortString()
1262 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001263 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001264 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1265 "2nd-crash", false);
1266 return false;
1267 }
1268
1269 // This is the first time we failed -- restart process and
1270 // retry.
1271 app.activities.remove(r);
1272 throw e;
1273 }
1274
1275 r.launchFailed = false;
1276 if (stack.updateLRUListLocked(r)) {
1277 Slog.w(TAG, "Activity " + r
1278 + " being launched, but already in LRU list");
1279 }
1280
1281 if (andResume) {
1282 // As part of the process of launching, ActivityThread also performs
1283 // a resume.
1284 stack.minimalResumeActivityLocked(r);
1285 } else {
1286 // This activity is not starting in the resumed state... which
1287 // should look like we asked it to pause+stop (but remain visible),
1288 // and it has done so and reported back the current icicle and
1289 // other state.
1290 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1291 + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001292 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001293 r.stopped = true;
1294 }
1295
1296 // Launch the new version setup screen if needed. We do this -after-
1297 // launching the initial activity (that is, home), so that it can have
1298 // a chance to initialize itself while in the background, making the
1299 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001300 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001301 mService.startSetupActivityLocked();
1302 }
1303
Dianne Hackborn465fa392014-09-14 14:21:18 -07001304 // Update any services we are bound to that might care about whether
1305 // their client may have activities.
1306 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1307
Craig Mautner2420ead2013-04-01 17:13:20 -07001308 return true;
1309 }
1310
Craig Mautnere79d42682013-04-01 19:01:53 -07001311 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001312 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001313 // Is this activity's application already running?
1314 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001315 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001316
1317 r.task.stack.setLaunchTime(r);
1318
1319 if (app != null && app.thread != null) {
1320 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001321 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1322 || !"android".equals(r.info.packageName)) {
1323 // Don't add this if it is a platform component that is marked
1324 // to run in multiple processes, because this is actually
1325 // part of the framework so doesn't make sense to track as a
1326 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001327 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1328 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001329 }
George Mount2c92c972014-03-20 09:38:23 -07001330 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001331 return;
1332 } catch (RemoteException e) {
1333 Slog.w(TAG, "Exception when starting activity "
1334 + r.intent.getComponent().flattenToShortString(), e);
1335 }
1336
1337 // If a dead object exception was thrown -- fall through to
1338 // restart the application.
1339 }
1340
1341 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001342 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001343 }
1344
Craig Mautner6170f732013-04-02 13:05:23 -07001345 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001346 Intent intent, String resolvedType, ActivityInfo aInfo,
1347 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1348 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001349 int callingPid, int callingUid, String callingPackage,
1350 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001351 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1352 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001353 int err = ActivityManager.START_SUCCESS;
1354
1355 ProcessRecord callerApp = null;
1356 if (caller != null) {
1357 callerApp = mService.getRecordForAppLocked(caller);
1358 if (callerApp != null) {
1359 callingPid = callerApp.pid;
1360 callingUid = callerApp.info.uid;
1361 } else {
1362 Slog.w(TAG, "Unable to find app for caller " + caller
1363 + " (pid=" + callingPid + ") when starting: "
1364 + intent.toString());
1365 err = ActivityManager.START_PERMISSION_DENIED;
1366 }
1367 }
1368
1369 if (err == ActivityManager.START_SUCCESS) {
1370 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1371 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001372 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001373 + " on display " + (container == null ? (mFocusedStack == null ?
1374 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1375 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1376 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001377 }
1378
1379 ActivityRecord sourceRecord = null;
1380 ActivityRecord resultRecord = null;
1381 if (resultTo != null) {
1382 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001383 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1384 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001385 if (sourceRecord != null) {
1386 if (requestCode >= 0 && !sourceRecord.finishing) {
1387 resultRecord = sourceRecord;
1388 }
1389 }
1390 }
Craig Mautner6170f732013-04-02 13:05:23 -07001391
Dianne Hackborn91097de2014-04-04 18:02:06 -07001392 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001393
Dianne Hackborn95465202014-09-15 16:21:55 -07001394 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001395 // Transfer the result target from the source activity to the new
1396 // one being started, including any failures.
1397 if (requestCode >= 0) {
1398 ActivityOptions.abort(options);
1399 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1400 }
1401 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001402 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1403 resultRecord = null;
1404 }
Craig Mautner6170f732013-04-02 13:05:23 -07001405 resultWho = sourceRecord.resultWho;
1406 requestCode = sourceRecord.requestCode;
1407 sourceRecord.resultTo = null;
1408 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001409 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001410 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001411 if (sourceRecord.launchedFromUid == callingUid) {
1412 // The new activity is being launched from the same uid as the previous
1413 // activity in the flow, and asking to forward its result back to the
1414 // previous. In this case the activity is serving as a trampoline between
1415 // the two, so we also want to update its launchedFromPackage to be the
1416 // same as the previous activity. Note that this is safe, since we know
1417 // these two packages come from the same uid; the caller could just as
1418 // well have supplied that same package name itself. This specifially
1419 // deals with the case of an intent picker/chooser being launched in the app
1420 // flow to redirect to an activity picked by the user, where we want the final
1421 // activity to consider it to have been launched by the previous app activity.
1422 callingPackage = sourceRecord.launchedFromPackage;
1423 }
Craig Mautner6170f732013-04-02 13:05:23 -07001424 }
1425
1426 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1427 // We couldn't find a class that can handle the given Intent.
1428 // That's the end of that!
1429 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1430 }
1431
1432 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1433 // We couldn't find the specific class specified in the Intent.
1434 // Also the end of the line.
1435 err = ActivityManager.START_CLASS_NOT_FOUND;
1436 }
1437
Dianne Hackborn91097de2014-04-04 18:02:06 -07001438 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1439 && sourceRecord.task.voiceSession != null) {
1440 // If this activity is being launched as part of a voice session, we need
1441 // to ensure that it is safe to do so. If the upcoming activity will also
1442 // be part of the voice session, we can only launch it if it has explicitly
1443 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001444 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001445 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1446 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001447 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1448 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001449 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1450 }
1451 } catch (RemoteException e) {
1452 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1453 }
1454 }
1455 }
1456
1457 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1458 // If the caller is starting a new voice session, just make sure the target
1459 // is actually allowing it to run this way.
1460 try {
1461 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1462 intent, resolvedType)) {
1463 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1464 }
1465 } catch (RemoteException e) {
1466 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1467 }
1468 }
1469
louis_changcd5d1982014-08-01 10:09:08 +08001470 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1471
Craig Mautner6170f732013-04-02 13:05:23 -07001472 if (err != ActivityManager.START_SUCCESS) {
1473 if (resultRecord != null) {
1474 resultStack.sendActivityResultLocked(-1,
1475 resultRecord, resultWho, requestCode,
1476 Activity.RESULT_CANCELED, null);
1477 }
Craig Mautner6170f732013-04-02 13:05:23 -07001478 ActivityOptions.abort(options);
1479 return err;
1480 }
1481
1482 final int startAnyPerm = mService.checkPermission(
1483 START_ANY_ACTIVITY, callingPid, callingUid);
1484 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1485 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1486 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1487 if (resultRecord != null) {
1488 resultStack.sendActivityResultLocked(-1,
1489 resultRecord, resultWho, requestCode,
1490 Activity.RESULT_CANCELED, null);
1491 }
Craig Mautner6170f732013-04-02 13:05:23 -07001492 String msg;
1493 if (!aInfo.exported) {
1494 msg = "Permission Denial: starting " + intent.toString()
1495 + " from " + callerApp + " (pid=" + callingPid
1496 + ", uid=" + callingUid + ")"
1497 + " not exported from uid " + aInfo.applicationInfo.uid;
1498 } else {
1499 msg = "Permission Denial: starting " + intent.toString()
1500 + " from " + callerApp + " (pid=" + callingPid
1501 + ", uid=" + callingUid + ")"
1502 + " requires " + aInfo.permission;
1503 }
1504 Slog.w(TAG, msg);
1505 throw new SecurityException(msg);
1506 }
1507
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001508 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001509 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001510
Craig Mautner6170f732013-04-02 13:05:23 -07001511 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001512 try {
1513 // The Intent we give to the watcher has the extra data
1514 // stripped off, since it can contain private information.
1515 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001516 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001517 aInfo.applicationInfo.packageName);
1518 } catch (RemoteException e) {
1519 mService.mController = null;
1520 }
Ben Gruver5e207332013-04-03 17:41:37 -07001521 }
Craig Mautner6170f732013-04-02 13:05:23 -07001522
Ben Gruver5e207332013-04-03 17:41:37 -07001523 if (abort) {
1524 if (resultRecord != null) {
1525 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001526 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001527 }
Ben Gruver5e207332013-04-03 17:41:37 -07001528 // We pretend to the caller that it was really started, but
1529 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001530 ActivityOptions.abort(options);
1531 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001532 }
1533
1534 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001535 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001536 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001537 if (outActivity != null) {
1538 outActivity[0] = r;
1539 }
1540
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001541 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001542 if (voiceSession == null && (stack.mResumedActivity == null
1543 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001544 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1545 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001546 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001547 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001548 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001549 ActivityOptions.abort(options);
1550 return ActivityManager.START_SWITCHES_CANCELED;
1551 }
1552 }
1553
1554 if (mService.mDidAppSwitch) {
1555 // This is the second allowed switch since we stopped switches,
1556 // so now just generally allow switches. Use case: user presses
1557 // home (switches disabled, switch to home, mDidAppSwitch now true);
1558 // user taps a home icon (coming from home so allowed, we hit here
1559 // and now allow anyone to switch again).
1560 mService.mAppSwitchesAllowedTime = 0;
1561 } else {
1562 mService.mDidAppSwitch = true;
1563 }
1564
Craig Mautneree36c772014-07-16 14:56:05 -07001565 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001566
Dianne Hackborn91097de2014-04-04 18:02:06 -07001567 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001568 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001569
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001570 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001571 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001572 // activity start, but we are not actually doing an activity
1573 // switch... just dismiss the keyguard now, because we
1574 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001575 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001576 }
1577 return err;
1578 }
1579
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001580 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001581 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001582
1583 // On leanback only devices we should keep all activities in the same stack.
1584 if (!mLeanbackOnlyDevice &&
1585 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001586
1587 ActivityStack stack;
1588
Wale Ogunwale7d701172015-03-11 15:36:30 -07001589 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001590 stack = task.stack;
1591 if (stack.isOnHomeDisplay()) {
1592 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001593 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1594 "computeStackFocus: Setting " + "focused stack to r=" + r
1595 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001596 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001597 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001598 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001599 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001600 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001601 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001602 }
1603
Craig Mautnere0a38842013-12-16 16:14:02 -08001604 final ActivityContainer container = r.mInitialActivityContainer;
1605 if (container != null) {
1606 // The first time put it on the desired stack, after this put on task stack.
1607 r.mInitialActivityContainer = null;
1608 return container.mStack;
1609 }
1610
Craig Mautner1b4bf852014-05-26 15:06:32 -07001611 if (mFocusedStack != mHomeStack && (!newTask ||
1612 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001613 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001614 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001615 return mFocusedStack;
1616 }
1617
Craig Mautnere0a38842013-12-16 16:14:02 -08001618 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1619 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001620 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001621 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001622 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001623 "computeStackFocus: Setting focused stack=" + stack);
1624 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001625 }
1626 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001627
Craig Mautner4a1cb222013-12-04 16:14:06 -08001628 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001629 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001630 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1631 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001632 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001633 }
1634 return mHomeStack;
1635 }
1636
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001637 boolean setFocusedStack(ActivityRecord r, String reason) {
1638 if (r == null) {
1639 // Not sure what you are trying to do, but it is not going to work...
1640 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001641 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001642 final TaskRecord task = r.task;
1643 if (task == null || task.stack == null) {
1644 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1645 return false;
1646 }
1647 task.stack.moveToFront(reason);
1648 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001649 }
1650
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001651 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001652 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001653 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001654 final Intent intent = r.intent;
1655 final int callingUid = r.launchedFromUid;
1656
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001657 // In some flows in to this function, we retrieve the task record and hold on to it
1658 // without a lock before calling back in to here... so the task at this point may
1659 // not actually be in recents. Check for that, and if it isn't in recents just
1660 // consider it invalid.
1661 if (inTask != null && !inTask.inRecents) {
1662 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1663 inTask = null;
1664 }
1665
Craig Mautnerad400af2014-08-13 16:41:36 -07001666 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001667 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1668 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001669
Craig Mautnera228ae92014-07-09 05:44:55 -07001670 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001671 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001672 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001673 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1674 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1675 "\"singleInstance\" or \"singleTask\"");
1676 launchFlags &=
1677 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1678 } else {
1679 switch (r.info.documentLaunchMode) {
1680 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1681 break;
1682 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1683 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1684 break;
1685 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1686 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1687 break;
1688 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1689 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1690 break;
1691 }
1692 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001693
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001694 final boolean launchTaskBehind = r.mLaunchTaskBehind
1695 && !launchSingleTask && !launchSingleInstance
1696 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001697
Wale Ogunwale7d701172015-03-11 15:36:30 -07001698 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1699 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001700 // For whatever reason this activity is being launched into a new
1701 // task... yet the caller has requested a result back. Well, that
1702 // is pretty messed up, so instead immediately send back a cancel
1703 // and let the new task continue launched as normal without a
1704 // dependency on its originator.
1705 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1706 r.resultTo.task.stack.sendActivityResultLocked(-1,
1707 r.resultTo, r.resultWho, r.requestCode,
1708 Activity.RESULT_CANCELED, null);
1709 r.resultTo = null;
1710 }
1711
1712 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1713 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1714 }
1715
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001716 // If we are actually going to launch in to a new task, there are some cases where
1717 // we further want to do multiple task.
1718 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1719 if (launchTaskBehind
1720 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1721 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1722 }
1723 }
1724
Craig Mautner8849a5e2013-04-02 16:41:03 -07001725 // We'll invoke onUserLeaving before onPause only if the launching
1726 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001727 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001728 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1729 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001730
1731 // If the caller has asked not to resume at this point, we make note
1732 // of this in the record so that we can skip it when trying to find
1733 // the top running activity.
1734 if (!doResume) {
1735 r.delayedResume = true;
1736 }
1737
Craig Mautnera254cd72014-05-25 16:47:39 -07001738 ActivityRecord notTop =
1739 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001740
1741 // If the onlyIfNeeded flag is set, then we can do this if the activity
1742 // being launched is the same as the one making the call... or, as
1743 // a special case, if we do not know the caller then we count the
1744 // current top activity as the caller.
1745 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1746 ActivityRecord checkedCaller = sourceRecord;
1747 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001748 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001749 }
1750 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1751 // Caller is not the same as launcher, so always needed.
1752 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1753 }
1754 }
1755
Craig Mautner8849a5e2013-04-02 16:41:03 -07001756 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001757 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001758
1759 // If the caller is not coming from another activity, but has given us an
1760 // explicit task into which they would like us to launch the new activity,
1761 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001762 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1763 final Intent baseIntent = inTask.getBaseIntent();
1764 final ActivityRecord root = inTask.getRootActivity();
1765 if (baseIntent == null) {
1766 ActivityOptions.abort(options);
1767 throw new IllegalArgumentException("Launching into task without base intent: "
1768 + inTask);
1769 }
1770
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001771 // If this task is empty, then we are adding the first activity -- it
1772 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001773 if (launchSingleInstance || launchSingleTask) {
1774 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1775 ActivityOptions.abort(options);
1776 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1777 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001778 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001779 if (root != null) {
1780 ActivityOptions.abort(options);
1781 throw new IllegalArgumentException("Caller with inTask " + inTask
1782 + " has root " + root + " but target is singleInstance/Task");
1783 }
1784 }
1785
1786 // If task is empty, then adopt the interesting intent launch flags in to the
1787 // activity being started.
1788 if (root == null) {
1789 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1790 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1791 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1792 launchFlags = (launchFlags&~flagsOfInterest)
1793 | (baseIntent.getFlags()&flagsOfInterest);
1794 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001795 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001796 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001797
Dianne Hackborn962d5352014-08-29 16:27:40 -07001798 // If the task is not empty and the caller is asking to start it as the root
1799 // of a new task, then we don't actually want to start this on the task. We
1800 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001801 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001802 addingToTask = false;
1803
1804 } else {
1805 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001806 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001807
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001808 reuseTask = inTask;
1809 } else {
1810 inTask = null;
1811 }
1812
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001813 if (inTask == null) {
1814 if (sourceRecord == null) {
1815 // This activity is not being started from another... in this
1816 // case we -always- start a new task.
1817 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1818 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1819 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1820 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1821 }
1822 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1823 // The original activity who is starting us is running as a single
1824 // instance... this new activity it is starting must go on its
1825 // own task.
1826 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1827 } else if (launchSingleInstance || launchSingleTask) {
1828 // The activity being started is a single instance... it always
1829 // gets launched into its own task.
1830 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1831 }
1832 }
1833
1834 ActivityInfo newTaskInfo = null;
1835 Intent newTaskIntent = null;
1836 ActivityStack sourceStack;
1837 if (sourceRecord != null) {
1838 if (sourceRecord.finishing) {
1839 // If the source is finishing, we can't further count it as our source. This
1840 // is because the task it is associated with may now be empty and on its way out,
1841 // so we don't want to blindly throw it in to that task. Instead we will take
1842 // the NEW_TASK flow and try to find a task for it. But save the task information
1843 // so it can be used when creating the new task.
1844 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1845 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1846 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1847 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1848 newTaskInfo = sourceRecord.info;
1849 newTaskIntent = sourceRecord.task.intent;
1850 }
1851 sourceRecord = null;
1852 sourceStack = null;
1853 } else {
1854 sourceStack = sourceRecord.task.stack;
1855 }
1856 } else {
1857 sourceStack = null;
1858 }
1859
1860 boolean movedHome = false;
1861 ActivityStack targetStack;
1862
1863 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001864 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001865
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001866 // We may want to try to place the new activity in to an existing task. We always
1867 // do this if the target activity is singleTask or singleInstance; we will also do
1868 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1869 // us to still place it in a new task: multi task, always doc mode, or being asked to
1870 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001871 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1872 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001873 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001874 // If bring to front is requested, and no result is requested and we have not
1875 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001876 // we can find a task that was started with this same
1877 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001878 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001879 // See if there is a task to bring to the front. If this is
1880 // a SINGLE_INSTANCE activity, there can be one and only one
1881 // instance of it in the history, and it is always in its own
1882 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001883 ActivityRecord intentActivity = !launchSingleInstance ?
1884 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001885 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001886 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001887 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001888 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001889 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1890 }
Craig Mautner29219d92013-04-16 20:19:12 -07001891 if (r.task == null) {
1892 r.task = intentActivity.task;
1893 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001894 if (intentActivity.task.intent == null) {
1895 // This task was started because of movement of
1896 // the activity based on affinity... now that we
1897 // are actually launching it, we can assign the
1898 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001899 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001900 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001901 targetStack = intentActivity.task.stack;
1902 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001903 // If the target task is not in the front, then we need
1904 // to bring it to the front... except... well, with
1905 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1906 // to have the same behavior as if a new instance was
1907 // being started, which means not bringing it to the front
1908 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001909 final ActivityStack focusStack = getFocusedStack();
1910 ActivityRecord curTop = (focusStack == null)
1911 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001912 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001913 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001914 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001915 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001916 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1917 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001918 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001919 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001920 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1921 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001922 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001923 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1924 options, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001925 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001926 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001927 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1928 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001929 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001930 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001931 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001932 options = null;
1933 }
1934 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001935 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001936 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001937 + " from " + intentActivity);
1938 targetStack.moveToFront("intentActivityFound");
1939 }
1940
Craig Mautner8849a5e2013-04-02 16:41:03 -07001941 // If the caller has requested that the target task be
1942 // reset, then do so.
1943 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1944 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1945 }
1946 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1947 // We don't need to start a new activity, and
1948 // the client said not to do anything if that
1949 // is the case, so this is it! And for paranoia, make
1950 // sure we have correctly resumed the top activity.
1951 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001952 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001953
1954 // Make sure to notify Keyguard as well if we are not running an app
1955 // transition later.
1956 if (!movedToFront) {
1957 notifyActivityDrawnForKeyguard();
1958 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001959 } else {
1960 ActivityOptions.abort(options);
1961 }
1962 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1963 }
1964 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001965 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1966 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001967 // The caller has requested to completely replace any
1968 // existing task with its new activity. Well that should
1969 // not be too hard...
1970 reuseTask = intentActivity.task;
1971 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001972 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001973 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001974 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001975 // In this situation we want to remove all activities
1976 // from the task up to the one being started. In most
1977 // cases this means we are resetting the task to its
1978 // initial state.
1979 ActivityRecord top =
1980 intentActivity.task.performClearTaskLocked(r, launchFlags);
1981 if (top != null) {
1982 if (top.frontOfTask) {
1983 // Activity aliases may mean we use different
1984 // intents for the top activity, so make sure
1985 // the task now has the identity of the new
1986 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001987 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001988 }
1989 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1990 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001991 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001992 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07001993 // A special case: we need to start the activity because it is not
1994 // currently running, and the caller has asked to clear the current
1995 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07001996 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07001997 // Now pretend like this activity is being started by the top of its
1998 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07001999 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002000 TaskRecord task = sourceRecord.task;
2001 if (task != null && task.stack == null) {
2002 // Target stack got cleared when we all activities were removed
2003 // above. Go ahead and reset it.
2004 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2005 targetStack.addTask(
2006 task, !launchTaskBehind /* toTop */, false /* moving */);
2007 }
2008
Craig Mautner8849a5e2013-04-02 16:41:03 -07002009 }
2010 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2011 // In this case the top activity on the task is the
2012 // same as the one being launched, so we take that
2013 // as a request to bring the task to the foreground.
2014 // If the top activity in the task is the root
2015 // activity, deliver this new intent to it if it
2016 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002017 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002018 && intentActivity.realActivity.equals(r.realActivity)) {
2019 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2020 intentActivity.task);
2021 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002022 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002023 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002024 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2025 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002026 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2027 // In this case we are launching the root activity
2028 // of the task, but with a different intent. We
2029 // should start a new instance on top.
2030 addingToTask = true;
2031 sourceRecord = intentActivity;
2032 }
2033 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2034 // In this case an activity is being launched in to an
2035 // existing task, without resetting that task. This
2036 // is typically the situation of launching an activity
2037 // from a notification or shortcut. We want to place
2038 // the new activity on top of the current task.
2039 addingToTask = true;
2040 sourceRecord = intentActivity;
2041 } else if (!intentActivity.task.rootWasReset) {
2042 // In this case we are launching in to an existing task
2043 // that has not yet been started from its front door.
2044 // The current task has been brought to the front.
2045 // Ideally, we'd probably like to place this new task
2046 // at the bottom of its stack, but that's a little hard
2047 // to do with the current organization of the code so
2048 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002049 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002050 }
2051 if (!addingToTask && reuseTask == null) {
2052 // We didn't do anything... but it was needed (a.k.a., client
2053 // don't use that intent!) And for paranoia, make
2054 // sure we have correctly resumed the top activity.
2055 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002056 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002057 if (!movedToFront) {
2058 // Make sure to notify Keyguard as well if we are not running an app
2059 // transition later.
2060 notifyActivityDrawnForKeyguard();
2061 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002062 } else {
2063 ActivityOptions.abort(options);
2064 }
2065 return ActivityManager.START_TASK_TO_FRONT;
2066 }
2067 }
2068 }
2069 }
2070
2071 //String uri = r.intent.toURI();
2072 //Intent intent2 = new Intent(uri);
2073 //Slog.i(TAG, "Given intent: " + r.intent);
2074 //Slog.i(TAG, "URI is: " + uri);
2075 //Slog.i(TAG, "To intent: " + intent2);
2076
2077 if (r.packageName != null) {
2078 // If the activity being launched is the same as the one currently
2079 // at the top, then we need to check if it should only be launched
2080 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002081 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002082 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002083 if (top != null && r.resultTo == null) {
2084 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2085 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002086 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002087 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002088 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2089 top.task);
2090 // For paranoia, make sure we have correctly
2091 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002092 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002093 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002094 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002095 }
2096 ActivityOptions.abort(options);
2097 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2098 // We don't need to start a new activity, and
2099 // the client said not to do anything if that
2100 // is the case, so this is it!
2101 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2102 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002103 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002104 return ActivityManager.START_DELIVERED_TO_TOP;
2105 }
2106 }
2107 }
2108 }
2109
2110 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002111 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002112 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2113 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002114 }
2115 ActivityOptions.abort(options);
2116 return ActivityManager.START_CLASS_NOT_FOUND;
2117 }
2118
2119 boolean newTask = false;
2120 boolean keepCurTransition = false;
2121
Craig Mautnerbb742462014-07-07 15:28:55 -07002122 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002123 sourceRecord.task : null;
2124
Craig Mautner8849a5e2013-04-02 16:41:03 -07002125 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002126 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002127 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002128 if (isLockTaskModeViolation(reuseTask)) {
2129 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2130 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2131 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002132 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002133 targetStack = computeStackFocus(r, newTask);
2134 targetStack.moveToFront("startingNewTask");
2135
Craig Mautner8849a5e2013-04-02 16:41:03 -07002136 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002137 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2138 newTaskInfo != null ? newTaskInfo : r.info,
2139 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002140 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2141 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002142 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2143 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002144 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002145 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002146 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002147 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002148 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002149 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2150 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002151 // Caller wants to appear on home activity, so before starting
2152 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002153 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002154 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002155 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002156 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002157 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002158 if (isLockTaskModeViolation(sourceTask)) {
2159 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2160 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2161 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002162 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002163 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002164 final TaskRecord topTask = targetStack.topTask();
2165 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002166 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2167 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002168 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002169 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002170 // In this case, we are adding the activity to an existing
2171 // task, but the caller has asked to clear that task if the
2172 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002173 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002174 keepCurTransition = true;
2175 if (top != null) {
2176 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002177 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002178 // For paranoia, make sure we have correctly
2179 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002180 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002181 if (doResume) {
2182 targetStack.resumeTopActivityLocked(null);
2183 }
2184 ActivityOptions.abort(options);
2185 return ActivityManager.START_DELIVERED_TO_TOP;
2186 }
2187 } else if (!addingToTask &&
2188 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2189 // In this case, we are launching an activity in our own task
2190 // that may already be running somewhere in the history, and
2191 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002192 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002193 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002194 final TaskRecord task = top.task;
2195 task.moveActivityToFrontLocked(top);
2196 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002197 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002198 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002199 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002200 if (doResume) {
2201 targetStack.resumeTopActivityLocked(null);
2202 }
2203 return ActivityManager.START_DELIVERED_TO_TOP;
2204 }
2205 }
2206 // An existing activity is starting this new activity, so we want
2207 // to keep the new one in the same task as the one that is starting
2208 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002209 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002210 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002211 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002212
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002213 } else if (inTask != null) {
2214 // The calling is asking that the new activity be started in an explicit
2215 // task it has provided to us.
2216 if (isLockTaskModeViolation(inTask)) {
2217 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2218 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2219 }
2220 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002221 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002222
2223 // Check whether we should actually launch the new activity in to the task,
2224 // or just reuse the current activity on top.
2225 ActivityRecord top = inTask.getTopActivity();
2226 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2227 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2228 || launchSingleTop || launchSingleTask) {
2229 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2230 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2231 // We don't need to start a new activity, and
2232 // the client said not to do anything if that
2233 // is the case, so this is it!
2234 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2235 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002236 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002237 return ActivityManager.START_DELIVERED_TO_TOP;
2238 }
2239 }
2240
Dianne Hackborn962d5352014-08-29 16:27:40 -07002241 if (!addingToTask) {
2242 // We don't actually want to have this activity added to the task, so just
2243 // stop here but still tell the caller that we consumed the intent.
2244 ActivityOptions.abort(options);
2245 return ActivityManager.START_TASK_TO_FRONT;
2246 }
2247
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002248 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002249 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002250 + " in explicit task " + r.task);
2251
Craig Mautner8849a5e2013-04-02 16:41:03 -07002252 } else {
2253 // This not being started from an existing activity, and not part
2254 // of a new task... just put it in the top task, though these days
2255 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002256 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002257 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002258 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002259 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002260 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002261 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002262 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002263 + " in new guessed " + r.task);
2264 }
2265
2266 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002267 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002268
Craig Mautner76e2a762014-06-24 09:05:43 -07002269 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2270 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2271 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002272 if (newTask) {
2273 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2274 }
2275 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002276 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002277 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002278 if (!launchTaskBehind) {
2279 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002280 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002281 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002282 return ActivityManager.START_SUCCESS;
2283 }
2284
Craig Mautneree36c772014-07-16 14:56:05 -07002285 final void doPendingActivityLaunchesLocked(boolean doResume) {
2286 while (!mPendingActivityLaunches.isEmpty()) {
2287 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002288 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002289 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002290 }
2291 }
2292
Craig Mautner7f13ed32014-07-28 14:00:35 -07002293 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002294 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2295 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002296 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002297 mPendingActivityLaunches.remove(palNdx);
2298 }
2299 }
2300 }
2301
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002302 void setLaunchSource(int uid) {
2303 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2304 }
2305
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002306 void acquireLaunchWakelock() {
2307 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2308 throw new IllegalStateException("Calling must be system uid");
2309 }
2310 mLaunchingActivity.acquire();
2311 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2312 // To be safe, don't allow the wake lock to be held for too long.
2313 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2314 }
2315 }
2316
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002317 /**
2318 * Called when the frontmost task is idle.
2319 * @return the state of mService.mBooting before this was called.
2320 */
2321 private boolean checkFinishBootingLocked() {
2322 final boolean booting = mService.mBooting;
2323 boolean enableScreen = false;
2324 mService.mBooting = false;
2325 if (!mService.mBooted) {
2326 mService.mBooted = true;
2327 enableScreen = true;
2328 }
2329 if (booting || enableScreen) {
2330 mService.postFinishBooting(booting, enableScreen);
2331 }
2332 return booting;
2333 }
2334
Craig Mautnerf3333272013-04-22 10:55:53 -07002335 // Checked.
2336 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2337 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002338 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002339
Craig Mautnerf3333272013-04-22 10:55:53 -07002340 ArrayList<ActivityRecord> stops = null;
2341 ArrayList<ActivityRecord> finishes = null;
2342 ArrayList<UserStartedState> startingUsers = null;
2343 int NS = 0;
2344 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002345 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002346 boolean activityRemoved = false;
2347
Wale Ogunwale7d701172015-03-11 15:36:30 -07002348 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002349 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002350 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2351 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002352 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2353 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002354 if (fromTimeout) {
2355 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002356 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002357
2358 // This is a hack to semi-deal with a race condition
2359 // in the client where it can be constructed with a
2360 // newer configuration from when we asked it to launch.
2361 // We'll update with whatever configuration it now says
2362 // it used to launch.
2363 if (config != null) {
2364 r.configuration = config;
2365 }
2366
2367 // We are now idle. If someone is waiting for a thumbnail from
2368 // us, we can now deliver.
2369 r.idle = true;
2370
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002371 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002372 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002373 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002374 }
2375 }
2376
2377 if (allResumedActivitiesIdle()) {
2378 if (r != null) {
2379 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002380 }
2381
2382 if (mLaunchingActivity.isHeld()) {
2383 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2384 if (VALIDATE_WAKE_LOCK_CALLER &&
2385 Binder.getCallingUid() != Process.myUid()) {
2386 throw new IllegalStateException("Calling must be system uid");
2387 }
2388 mLaunchingActivity.release();
2389 }
2390 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002391 }
2392
2393 // Atomically retrieve all of the other things to do.
2394 stops = processStoppingActivitiesLocked(true);
2395 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002396 if ((NF = mFinishingActivities.size()) > 0) {
2397 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002398 mFinishingActivities.clear();
2399 }
2400
Craig Mautnerf3333272013-04-22 10:55:53 -07002401 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002402 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002403 mStartingUsers.clear();
2404 }
2405
Craig Mautnerf3333272013-04-22 10:55:53 -07002406 // Stop any activities that are scheduled to do so but have been
2407 // waiting for the next one to start.
2408 for (int i = 0; i < NS; i++) {
2409 r = stops.get(i);
2410 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002411 if (stack != null) {
2412 if (r.finishing) {
2413 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2414 } else {
2415 stack.stopActivityLocked(r);
2416 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002417 }
2418 }
2419
2420 // Finish any activities that are scheduled to do so but have been
2421 // waiting for the next one to start.
2422 for (int i = 0; i < NF; i++) {
2423 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002424 final ActivityStack stack = r.task.stack;
2425 if (stack != null) {
2426 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2427 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002428 }
2429
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002430 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002431 // Complete user switch
2432 if (startingUsers != null) {
2433 for (int i = 0; i < startingUsers.size(); i++) {
2434 mService.finishUserSwitch(startingUsers.get(i));
2435 }
2436 }
2437 // Complete starting up of background users
2438 if (mStartingBackgroundUsers.size() > 0) {
2439 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2440 mStartingBackgroundUsers.clear();
2441 for (int i = 0; i < startingUsers.size(); i++) {
2442 mService.finishUserBoot(startingUsers.get(i));
2443 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002444 }
2445 }
2446
2447 mService.trimApplications();
2448 //dump();
2449 //mWindowManager.dump();
2450
Craig Mautnerf3333272013-04-22 10:55:53 -07002451 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002452 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002453 }
2454
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002455 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002456 }
2457
Craig Mautner8e569572013-10-11 17:36:59 -07002458 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002459 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002460 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2461 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002462 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2463 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2464 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002465 }
Craig Mautner19091252013-10-05 00:03:53 -07002466 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002467 }
2468
2469 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002470 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2471 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002472 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2473 stacks.get(stackNdx).closeSystemDialogsLocked();
2474 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002475 }
2476 }
2477
Craig Mautner93529a42013-10-04 15:03:13 -07002478 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002479 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002480 }
2481
Craig Mautner8d341ef2013-03-26 09:03:27 -07002482 /**
2483 * @return true if some activity was finished (or would have finished if doit were true).
2484 */
2485 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2486 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002487 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2488 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002489 final int numStacks = stacks.size();
2490 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2491 final ActivityStack stack = stacks.get(stackNdx);
2492 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2493 didSomething = true;
2494 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002495 }
2496 }
2497 return didSomething;
2498 }
2499
Dianne Hackborna413dc02013-07-12 12:02:55 -07002500 void updatePreviousProcessLocked(ActivityRecord r) {
2501 // Now that this process has stopped, we may want to consider
2502 // it to be the previous app to try to keep around in case
2503 // the user wants to return to it.
2504
2505 // First, found out what is currently the foreground app, so that
2506 // we don't blow away the previous app if this activity is being
2507 // hosted by the process that is actually still the foreground.
2508 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002509 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2510 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002511 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2512 final ActivityStack stack = stacks.get(stackNdx);
2513 if (isFrontStack(stack)) {
2514 if (stack.mResumedActivity != null) {
2515 fgApp = stack.mResumedActivity.app;
2516 } else if (stack.mPausingActivity != null) {
2517 fgApp = stack.mPausingActivity.app;
2518 }
2519 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002520 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002521 }
2522 }
2523
2524 // Now set this one as the previous process, only if that really
2525 // makes sense to.
2526 if (r.app != null && fgApp != null && r.app != fgApp
2527 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002528 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002529 mService.mPreviousProcess = r.app;
2530 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2531 }
2532 }
2533
Craig Mautner05d29032013-05-03 13:40:13 -07002534 boolean resumeTopActivitiesLocked() {
2535 return resumeTopActivitiesLocked(null, null, null);
2536 }
2537
2538 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2539 Bundle targetOptions) {
2540 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002541 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002542 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002543 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002544 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002545 if (isFrontStack(targetStack)) {
2546 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2547 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002548
Craig Mautnere0a38842013-12-16 16:14:02 -08002549 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2550 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002551 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2552 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002553 if (stack == targetStack) {
2554 // Already started above.
2555 continue;
2556 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002557 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002558 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002559 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002560 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002561 }
Craig Mautner05d29032013-05-03 13:40:13 -07002562 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002563 }
2564
Todd Kennedy539db512014-12-15 09:57:55 -08002565 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002566 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2567 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002568 final int numStacks = stacks.size();
2569 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2570 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002571 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002572 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002573 }
2574 }
2575
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002576 void finishVoiceTask(IVoiceInteractionSession session) {
2577 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2578 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2579 final int numStacks = stacks.size();
2580 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2581 final ActivityStack stack = stacks.get(stackNdx);
2582 stack.finishVoiceTask(session);
2583 }
2584 }
2585 }
2586
Craig Mautner299f9602015-01-26 09:47:33 -08002587 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002588 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2589 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002590 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002591 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2592 // Caller wants the home activity moved with it. To accomplish this,
2593 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002594 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002595 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002596 if (task.stack == null) {
2597 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2598 + task + " to front. Stack is null");
2599 return;
2600 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002601 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002602 if (DEBUG_STACK) Slog.d(TAG_STACK,
2603 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002604 }
2605
Craig Mautner967212c2013-04-13 21:10:58 -07002606 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002607 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2608 if (activityContainer != null) {
2609 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002610 }
2611 return null;
2612 }
2613
Craig Mautner967212c2013-04-13 21:10:58 -07002614 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002615 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002616 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2617 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002618 }
2619 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002620 }
2621
Craig Mautner4a1cb222013-12-04 16:14:06 -08002622 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002623 ActivityRecord homeActivity = getHomeActivity();
2624 if (homeActivity != null) {
2625 return homeActivity.appToken;
2626 }
2627 return null;
2628 }
2629
2630 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002631 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2632 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2633 final TaskRecord task = tasks.get(taskNdx);
2634 if (task.isHomeTask()) {
2635 final ArrayList<ActivityRecord> activities = task.mActivities;
2636 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2637 final ActivityRecord r = activities.get(activityNdx);
2638 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002639 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002640 }
2641 }
2642 }
2643 }
2644 return null;
2645 }
2646
Todd Kennedyca4d8422015-01-15 15:19:22 -08002647 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002648 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002649 ActivityContainer activityContainer =
2650 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002651 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002652 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002653 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002654 return activityContainer;
2655 }
2656
Craig Mautner34b73df2014-01-12 21:11:08 -08002657 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002658 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2659 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2660 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002661 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2662 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002663 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002664 }
2665 }
2666
Craig Mautner95da1082014-02-24 17:54:35 -08002667 void deleteActivityContainer(IActivityContainer container) {
2668 ActivityContainer activityContainer = (ActivityContainer)container;
2669 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002670 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2671 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002672 final int stackId = activityContainer.mStackId;
2673 mActivityContainers.remove(stackId);
2674 mWindowManager.removeStack(stackId);
2675 }
2676 }
2677
Wale Ogunwale60454db2015-01-23 16:05:07 -08002678 void resizeStackLocked(int stackId, Rect bounds) {
2679 final ActivityStack stack = getStack(stackId);
2680 if (stack == null) {
2681 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2682 return;
2683 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002684
2685 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002686 if (r != null && !r.task.mResizeable) {
2687 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002688 return;
2689 }
2690
Wale Ogunwale60454db2015-01-23 16:05:07 -08002691 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2692 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002693 if (r != null) {
2694 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2695 // And we need to make sure at this point that all other activities
2696 // are made visible with the correct configuration.
2697 ensureActivitiesVisibleLocked(r, 0);
2698 if (!updated) {
2699 resumeTopActivitiesLocked(stack, null, null);
2700 }
2701 }
2702 }
2703 }
2704
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002705 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2706 * current task stack if it only has one entry, moving the task to a stack that matches the
2707 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2708 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2709 task.mResizeable = true;
2710 final ActivityStack currentStack = task.stack;
2711 if (currentStack.isHomeStack()) {
2712 // Can't move task off the home stack. Sorry!
2713 return;
2714 }
2715
2716 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2717 if (matchingStackId != -1) {
2718 // There is already a stack with the right bounds!
2719 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2720 // Nothing to do here. Already in the right stack...
2721 return;
2722 }
2723 // Move task to stack with matching bounds.
2724 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2725 return;
2726 }
2727
2728 if (currentStack != null && currentStack.numTasks() == 1) {
2729 // Just resize the current stack since this is the task in it.
2730 resizeStackLocked(currentStack.mStackId, bounds);
2731 return;
2732 }
2733
2734 // Create new stack and move the task to it.
2735 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2736 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2737 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2738
2739 if (newStack == null) {
2740 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2741 return;
2742 }
2743 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2744 resizeStackLocked(newStack.mStackId, bounds);
2745 }
2746
Todd Kennedy4900bf92015-01-16 16:05:14 -08002747 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002748 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2749 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002750 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002751 }
2752
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002753 ActivityContainer activityContainer = new ActivityContainer(stackId);
2754 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002755 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002756 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002757 }
2758
2759 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002760 while (true) {
2761 if (++mLastStackId <= HOME_STACK_ID) {
2762 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002763 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002764 if (getStack(mLastStackId) == null) {
2765 break;
2766 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002767 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002768 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002769 }
2770
Wale Ogunwale7de05352014-12-12 15:21:33 -08002771 private boolean restoreRecentTaskLocked(TaskRecord task) {
2772 ActivityStack stack = null;
2773 // Determine stack to restore task to.
2774 if (mLeanbackOnlyDevice) {
2775 // There is only one stack for lean back devices.
2776 stack = mHomeStack;
2777 } else {
2778 // Look for the top stack on the home display that isn't the home stack.
2779 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2780 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2781 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002782 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002783 stack = tmpStack;
2784 break;
2785 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002786 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002787 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002788
2789 if (stack == null) {
2790 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2791 // before an application stack is created...Go ahead and create one on the default
2792 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002793 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002794 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002795 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002796 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2797 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002798 }
2799
2800 if (stack == null) {
2801 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002802 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2803 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002804 return false;
2805 }
2806
2807 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002808 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2809 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002810 final ArrayList<ActivityRecord> activities = task.mActivities;
2811 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2812 final ActivityRecord r = activities.get(activityNdx);
2813 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2814 r.info.screenOrientation, r.fullscreen,
2815 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2816 r.userId, r.info.configChanges, task.voiceSession != null,
2817 r.mLaunchTaskBehind);
2818 }
2819 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002820 }
2821
Craig Mautner299f9602015-01-26 09:47:33 -08002822 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002823 final TaskRecord task = anyTaskForIdLocked(taskId);
2824 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002825 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002826 return;
2827 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002828 final ActivityStack stack = getStack(stackId);
2829 if (stack == null) {
2830 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2831 return;
2832 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002833 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2834 if (task.stack != null) {
2835 task.stack.removeTask(task, "moveTaskToStack", false);
2836 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002837 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002838 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002839 }
2840
Craig Mautnerac6f8432013-07-17 13:24:59 -07002841 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002842 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002843 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2844 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002845 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2846 final ActivityStack stack = stacks.get(stackNdx);
2847 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002848 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002849 continue;
2850 }
2851 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002852 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2853 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002854 continue;
2855 }
2856 final ActivityRecord ar = stack.findTaskLocked(r);
2857 if (ar != null) {
2858 return ar;
2859 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002860 }
2861 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002862 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002863 return null;
2864 }
2865
2866 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002867 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2868 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002869 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2870 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2871 if (ar != null) {
2872 return ar;
2873 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002874 }
2875 }
2876 return null;
2877 }
2878
Craig Mautner8d341ef2013-03-26 09:03:27 -07002879 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002880 scheduleSleepTimeout();
2881 if (!mGoingToSleep.isHeld()) {
2882 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002883 if (mLaunchingActivity.isHeld()) {
2884 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2885 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002886 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002887 mLaunchingActivity.release();
2888 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002889 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002890 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002891 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002892 }
2893
2894 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002895 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002896
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002897 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002898 final long endTime = System.currentTimeMillis() + timeout;
2899 while (true) {
2900 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002901 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2902 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002903 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2904 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2905 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002906 }
2907 if (cantShutdown) {
2908 long timeRemaining = endTime - System.currentTimeMillis();
2909 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002910 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002911 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002912 } catch (InterruptedException e) {
2913 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002914 } else {
2915 Slog.w(TAG, "Activity manager shutdown timed out");
2916 timedout = true;
2917 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002918 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002919 } else {
2920 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002921 }
2922 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002923
2924 // Force checkReadyForSleep to complete.
2925 mSleepTimeout = true;
2926 checkReadyForSleepLocked();
2927
Craig Mautner8d341ef2013-03-26 09:03:27 -07002928 return timedout;
2929 }
2930
2931 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002932 removeSleepTimeouts();
2933 if (mGoingToSleep.isHeld()) {
2934 mGoingToSleep.release();
2935 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002936 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2937 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002938 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2939 final ActivityStack stack = stacks.get(stackNdx);
2940 stack.awakeFromSleepingLocked();
2941 if (isFrontStack(stack)) {
2942 resumeTopActivitiesLocked();
2943 }
Craig Mautner5314a402013-09-26 12:40:16 -07002944 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002945 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002946 mGoingToSleepActivities.clear();
2947 }
2948
2949 void activitySleptLocked(ActivityRecord r) {
2950 mGoingToSleepActivities.remove(r);
2951 checkReadyForSleepLocked();
2952 }
2953
2954 void checkReadyForSleepLocked() {
2955 if (!mService.isSleepingOrShuttingDown()) {
2956 // Do not care.
2957 return;
2958 }
2959
2960 if (!mSleepTimeout) {
2961 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002962 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2963 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002964 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2965 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2966 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002967 }
2968
2969 if (mStoppingActivities.size() > 0) {
2970 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002971 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002972 + mStoppingActivities.size() + " activities");
2973 scheduleIdleLocked();
2974 dontSleep = true;
2975 }
2976
2977 if (mGoingToSleepActivities.size() > 0) {
2978 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002979 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002980 + mGoingToSleepActivities.size() + " activities");
2981 dontSleep = true;
2982 }
2983
2984 if (dontSleep) {
2985 return;
2986 }
2987 }
2988
Craig Mautnere0a38842013-12-16 16:14:02 -08002989 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2990 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002991 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2992 stacks.get(stackNdx).goToSleep();
2993 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002994 }
2995
2996 removeSleepTimeouts();
2997
2998 if (mGoingToSleep.isHeld()) {
2999 mGoingToSleep.release();
3000 }
3001 if (mService.mShuttingDown) {
3002 mService.notifyAll();
3003 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003004 }
3005
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003006 boolean reportResumedActivityLocked(ActivityRecord r) {
3007 final ActivityStack stack = r.task.stack;
3008 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003009 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003010 }
3011 if (allResumedActivitiesComplete()) {
3012 ensureActivitiesVisibleLocked(null, 0);
3013 mWindowManager.executeAppTransition();
3014 return true;
3015 }
3016 return false;
3017 }
3018
Craig Mautner8d341ef2013-03-26 09:03:27 -07003019 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003020 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3021 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003022 final int numStacks = stacks.size();
3023 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3024 final ActivityStack stack = stacks.get(stackNdx);
3025 stack.handleAppCrashLocked(app);
3026 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003027 }
3028 }
3029
Jose Lima4b6c6692014-08-12 17:41:12 -07003030 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003031 final ActivityStack stack = r.task.stack;
3032 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003033 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3034 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003035 return false;
3036 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003037 final boolean isVisible = stack.hasVisibleBehindActivity();
3038 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3039 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003040
3041 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003042 if (top == null || top == r || (visible == isVisible)) {
3043 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3044 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003045 return true;
3046 }
3047
3048 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003049 if (visible && top.fullscreen) {
3050 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003051 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003052 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003053 " top.app.thread=" + top.app.thread);
3054 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003055 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3056 // Only the activity set as currently visible behind should actively reset its
3057 // visible behind state.
3058 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3059 + visible + " stack.getVisibleBehindActivity()=" +
3060 stack.getVisibleBehindActivity() + " r=" + r);
3061 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003062 }
3063
Jose Lima4b6c6692014-08-12 17:41:12 -07003064 stack.setVisibleBehindActivity(visible ? r : null);
3065 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003066 // Make the activity immediately above r opaque.
3067 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3068 if (next != null) {
3069 mService.convertFromTranslucent(next.appToken);
3070 }
3071 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003072 if (top.app != null && top.app.thread != null) {
3073 // Notify the top app of the change.
3074 try {
3075 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3076 } catch (RemoteException e) {
3077 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003078 }
3079 return true;
3080 }
3081
Craig Mautnerbb742462014-07-07 15:28:55 -07003082 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3083 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3084 r.mLaunchTaskBehind = false;
3085 final TaskRecord task = r.task;
3086 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003087 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003088 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003089 mWindowManager.setAppVisibility(r.appToken, false);
3090 }
3091
3092 void scheduleLaunchTaskBehindComplete(IBinder token) {
3093 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3094 }
3095
Craig Mautnerde4ef022013-04-07 19:01:33 -07003096 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003097 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003098 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3099 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003100 final int topStackNdx = stacks.size() - 1;
3101 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3102 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003103 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003104 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003105 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003106 }
3107
3108 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003109 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3110 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003111 final int numStacks = stacks.size();
3112 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3113 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003114 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003115 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003116 }
3117 }
3118
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003119 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3120 // Examine all activities currently running in the process.
3121 TaskRecord firstTask = null;
3122 // Tasks is non-null only if two or more tasks are found.
3123 ArraySet<TaskRecord> tasks = null;
3124 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3125 for (int i=0; i<app.activities.size(); i++) {
3126 ActivityRecord r = app.activities.get(i);
3127 // First, if we find an activity that is in the process of being destroyed,
3128 // then we just aren't going to do anything for now; we want things to settle
3129 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003130 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003131 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3132 return;
3133 }
3134 // Don't consider any activies that are currently not in a state where they
3135 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003136 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3137 || r.state == PAUSED || r.state == STOPPING) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003138 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3139 continue;
3140 }
3141 if (r.task != null) {
3142 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3143 + " from " + r);
3144 if (firstTask == null) {
3145 firstTask = r.task;
3146 } else if (firstTask != r.task) {
3147 if (tasks == null) {
3148 tasks = new ArraySet<>();
3149 tasks.add(firstTask);
3150 }
3151 tasks.add(r.task);
3152 }
3153 }
3154 }
3155 if (tasks == null) {
3156 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3157 return;
3158 }
3159 // If we have activities in multiple tasks that are in a position to be destroyed,
3160 // let's iterate through the tasks and release the oldest one.
3161 final int numDisplays = mActivityDisplays.size();
3162 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3163 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3164 // Step through all stacks starting from behind, to hit the oldest things first.
3165 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3166 final ActivityStack stack = stacks.get(stackNdx);
3167 // Try to release activities in this stack; if we manage to, we are done.
3168 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3169 return;
3170 }
3171 }
3172 }
3173 }
3174
Craig Mautner8d341ef2013-03-26 09:03:27 -07003175 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003176 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003177 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003178 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003179
Craig Mautner858d8a62013-04-23 17:08:34 -07003180 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003181 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3182 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003183 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003184 final ActivityStack stack = stacks.get(stackNdx);
3185 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003186 TaskRecord task = stack.topTask();
3187 if (task != null) {
3188 mWindowManager.moveTaskToTop(task.taskId);
3189 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003190 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003191 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003192
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003193 ActivityStack stack = getStack(restoreStackId);
3194 if (stack == null) {
3195 stack = mHomeStack;
3196 }
3197 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003198 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003199 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003200 TaskRecord task = stack.topTask();
3201 if (task != null) {
3202 mWindowManager.moveTaskToTop(task.taskId);
3203 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003204 } else {
3205 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003206 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003207 }
Craig Mautner93529a42013-10-04 15:03:13 -07003208 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003209 }
3210
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003211 /**
3212 * Add background users to send boot completed events to.
3213 * @param userId The user being started in the background
3214 * @param uss The state object for the user.
3215 */
3216 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3217 mStartingBackgroundUsers.add(uss);
3218 }
3219
Craig Mautnerde4ef022013-04-07 19:01:33 -07003220 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003221 ArrayList<ActivityRecord> stops = null;
3222
3223 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003224 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3225 ActivityRecord s = mStoppingActivities.get(activityNdx);
3226 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003227 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003228 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3229 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003230 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003231 if (s.finishing) {
3232 // If this activity is finishing, it is sitting on top of
3233 // everyone else but we now know it is no longer needed...
3234 // so get rid of it. Otherwise, we need to go through the
3235 // normal flow and hide it once we determine that it is
3236 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003237 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003238 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003239 }
3240 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003241 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003242 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003243 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003244 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003245 }
3246 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003247 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003248 }
3249 }
3250
3251 return stops;
3252 }
3253
Craig Mautnercf910b02013-04-23 11:23:27 -07003254 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003255 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3256 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3257 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3258 final ActivityStack stack = stacks.get(stackNdx);
3259 final ActivityRecord r = stack.topRunningActivityLocked(null);
3260 final ActivityState state = r == null ? DESTROYED : r.state;
3261 if (isFrontStack(stack)) {
3262 if (r == null) Slog.e(TAG,
3263 "validateTop...: null top activity, stack=" + stack);
3264 else {
3265 final ActivityRecord pausing = stack.mPausingActivity;
3266 if (pausing != null && pausing == r) Slog.e(TAG,
3267 "validateTop...: top stack has pausing activity r=" + r
3268 + " state=" + state);
3269 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3270 "validateTop...: activity in front not resumed r=" + r
3271 + " state=" + state);
3272 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003273 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003274 final ActivityRecord resumed = stack.mResumedActivity;
3275 if (resumed != null && resumed == r) Slog.e(TAG,
3276 "validateTop...: back stack has resumed activity r=" + r
3277 + " state=" + state);
3278 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3279 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003280 }
3281 }
3282 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003283 }
3284
Craig Mautner27084302013-03-25 08:05:25 -07003285 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003286 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003287 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003288 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3289 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3290 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003291 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003292 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003293
Craig Mautner20e72272013-04-01 13:45:53 -07003294 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003295 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003296 }
3297
Dianne Hackborn390517b2013-05-30 15:03:32 -07003298 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3299 boolean needSep, String prefix) {
3300 if (activity != null) {
3301 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3302 if (needSep) {
3303 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003304 }
3305 pw.print(prefix);
3306 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003307 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003308 }
3309 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003310 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003311 }
3312
Craig Mautner8d341ef2013-03-26 09:03:27 -07003313 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3314 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003315 boolean printed = false;
3316 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003317 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3318 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003319 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003320 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003321 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003322 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003323 final ActivityStack stack = stacks.get(stackNdx);
3324 StringBuilder stackHeader = new StringBuilder(128);
3325 stackHeader.append(" Stack #");
3326 stackHeader.append(stack.mStackId);
3327 stackHeader.append(":");
3328 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3329 needSep, stackHeader.toString());
3330 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3331 !dumpAll, false, dumpPackage, true,
3332 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003333
Craig Mautner4a1cb222013-12-04 16:14:06 -08003334 needSep = printed;
3335 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3336 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003337 if (pr) {
3338 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003339 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003340 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003341 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3342 " mResumedActivity: ");
3343 if (pr) {
3344 printed = true;
3345 needSep = false;
3346 }
3347 if (dumpAll) {
3348 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3349 " mLastPausedActivity: ");
3350 if (pr) {
3351 printed = true;
3352 needSep = true;
3353 }
3354 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3355 needSep, " mLastNoHistoryActivity: ");
3356 }
3357 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003358 }
3359 }
3360
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003361 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3362 false, dumpPackage, true, " Activities waiting to finish:", null);
3363 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3364 false, dumpPackage, true, " Activities waiting to stop:", null);
3365 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3366 false, dumpPackage, true, " Activities waiting for another to become visible:",
3367 null);
3368 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3369 false, dumpPackage, true, " Activities waiting to sleep:", null);
3370 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3371 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003372
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003373 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003374 }
3375
Dianne Hackborn390517b2013-05-30 15:03:32 -07003376 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003377 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003378 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003379 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003380 String innerPrefix = null;
3381 String[] args = null;
3382 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003383 for (int i=list.size()-1; i>=0; i--) {
3384 final ActivityRecord r = list.get(i);
3385 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3386 continue;
3387 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003388 if (innerPrefix == null) {
3389 innerPrefix = prefix + " ";
3390 args = new String[0];
3391 }
3392 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003393 final boolean full = !brief && (complete || !r.isInHistory());
3394 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003395 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003396 needNL = false;
3397 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003398 if (header1 != null) {
3399 pw.println(header1);
3400 header1 = null;
3401 }
3402 if (header2 != null) {
3403 pw.println(header2);
3404 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003405 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003406 if (lastTask != r.task) {
3407 lastTask = r.task;
3408 pw.print(prefix);
3409 pw.print(full ? "* " : " ");
3410 pw.println(lastTask);
3411 if (full) {
3412 lastTask.dump(pw, prefix + " ");
3413 } else if (complete) {
3414 // Complete + brief == give a summary. Isn't that obvious?!?
3415 if (lastTask.intent != null) {
3416 pw.print(prefix); pw.print(" ");
3417 pw.println(lastTask.intent.toInsecureStringWithClip());
3418 }
3419 }
3420 }
3421 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3422 pw.print(" #"); pw.print(i); pw.print(": ");
3423 pw.println(r);
3424 if (full) {
3425 r.dump(pw, innerPrefix);
3426 } else if (complete) {
3427 // Complete + brief == give a summary. Isn't that obvious?!?
3428 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3429 if (r.app != null) {
3430 pw.print(innerPrefix); pw.println(r.app);
3431 }
3432 }
3433 if (client && r.app != null && r.app.thread != null) {
3434 // flush anything that is already in the PrintWriter since the thread is going
3435 // to write to the file descriptor directly
3436 pw.flush();
3437 try {
3438 TransferPipe tp = new TransferPipe();
3439 try {
3440 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3441 r.appToken, innerPrefix, args);
3442 // Short timeout, since blocking here can
3443 // deadlock with the application.
3444 tp.go(fd, 2000);
3445 } finally {
3446 tp.kill();
3447 }
3448 } catch (IOException e) {
3449 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3450 } catch (RemoteException e) {
3451 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3452 }
3453 needNL = true;
3454 }
3455 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003456 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003457 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003458
Craig Mautnerf3333272013-04-22 10:55:53 -07003459 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003460 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003461 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3462 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003463 }
3464
3465 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003466 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003467 }
3468
3469 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003470 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003471 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3472 }
3473
Craig Mautner05d29032013-05-03 13:40:13 -07003474 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003475 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3476 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3477 }
Craig Mautner05d29032013-05-03 13:40:13 -07003478 }
3479
Craig Mautner0eea92c2013-05-16 13:35:39 -07003480 void removeSleepTimeouts() {
3481 mSleepTimeout = false;
3482 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3483 }
3484
3485 final void scheduleSleepTimeout() {
3486 removeSleepTimeouts();
3487 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3488 }
3489
Craig Mautner4a1cb222013-12-04 16:14:06 -08003490 @Override
3491 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003492 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003493 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3494 }
3495
3496 @Override
3497 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003498 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003499 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3500 }
3501
3502 @Override
3503 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003504 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003505 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3506 }
3507
3508 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003509 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003510 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003511 newDisplay = mActivityDisplays.get(displayId) == null;
3512 if (newDisplay) {
3513 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003514 if (activityDisplay.mDisplay == null) {
3515 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3516 return;
3517 }
Craig Mautner4504de52013-12-20 09:06:56 -08003518 mActivityDisplays.put(displayId, activityDisplay);
3519 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003520 }
Craig Mautner4504de52013-12-20 09:06:56 -08003521 if (newDisplay) {
3522 mWindowManager.onDisplayAdded(displayId);
3523 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003524 }
3525
3526 public void handleDisplayRemovedLocked(int displayId) {
3527 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003528 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3529 if (activityDisplay != null) {
3530 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003531 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003532 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003533 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003534 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003535 }
3536 }
3537 mWindowManager.onDisplayRemoved(displayId);
3538 }
3539
3540 public void handleDisplayChangedLocked(int displayId) {
3541 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003542 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3543 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003544 // TODO: Update the bounds.
3545 }
3546 }
3547 mWindowManager.onDisplayChanged(displayId);
3548 }
3549
3550 StackInfo getStackInfo(ActivityStack stack) {
3551 StackInfo info = new StackInfo();
3552 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3553 info.displayId = Display.DEFAULT_DISPLAY;
3554 info.stackId = stack.mStackId;
3555
3556 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3557 final int numTasks = tasks.size();
3558 int[] taskIds = new int[numTasks];
3559 String[] taskNames = new String[numTasks];
3560 for (int i = 0; i < numTasks; ++i) {
3561 final TaskRecord task = tasks.get(i);
3562 taskIds[i] = task.taskId;
3563 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3564 : task.realActivity != null ? task.realActivity.flattenToString()
3565 : task.getTopActivity() != null ? task.getTopActivity().packageName
3566 : "unknown";
3567 }
3568 info.taskIds = taskIds;
3569 info.taskNames = taskNames;
3570 return info;
3571 }
3572
3573 StackInfo getStackInfoLocked(int stackId) {
3574 ActivityStack stack = getStack(stackId);
3575 if (stack != null) {
3576 return getStackInfo(stack);
3577 }
3578 return null;
3579 }
3580
3581 ArrayList<StackInfo> getAllStackInfosLocked() {
3582 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003583 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3584 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003585 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3586 list.add(getStackInfo(stacks.get(ndx)));
3587 }
3588 }
3589 return list;
3590 }
3591
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003592 void showLockTaskToast() {
3593 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003594 }
3595
Benjamin Franz43261142015-02-11 15:59:44 +00003596 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003597 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003598 // Take out of lock task mode if necessary
3599 if (mLockTaskModeTask != null) {
3600 final Message lockTaskMsg = Message.obtain();
3601 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3602 lockTaskMsg.what = LOCK_TASK_END_MSG;
3603 mLockTaskModeTask = null;
3604 mHandler.sendMessage(lockTaskMsg);
3605 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003606 return;
3607 }
3608 if (isLockTaskModeViolation(task)) {
3609 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3610 return;
3611 }
3612 mLockTaskModeTask = task;
Craig Mautner299f9602015-01-26 09:47:33 -08003613 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003614 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003615
3616 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003617 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3618 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003619 lockTaskMsg.what = LOCK_TASK_START_MSG;
Benjamin Franz43261142015-02-11 15:59:44 +00003620 lockTaskMsg.arg2 = lockTaskModeState;
justinzhang5286d3f2014-05-12 17:06:01 -04003621 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003622 }
3623
3624 boolean isLockTaskModeViolation(TaskRecord task) {
3625 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3626 }
3627
3628 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3629 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003630 final Message lockTaskMsg = Message.obtain();
3631 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3632 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003633 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003634 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003635 }
3636 }
3637
Benjamin Franz43261142015-02-11 15:59:44 +00003638 int getLockTaskModeState() {
3639 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003640 }
3641
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003642 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003643
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003644 public ActivityStackSupervisorHandler(Looper looper) {
3645 super(looper);
3646 }
3647
Craig Mautnerf3333272013-04-22 10:55:53 -07003648 void activityIdleInternal(ActivityRecord r) {
3649 synchronized (mService) {
3650 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3651 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003652 }
3653
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003654 @Override
3655 public void handleMessage(Message msg) {
3656 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003657 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003658 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003659 if (mService.mDidDexOpt) {
3660 mService.mDidDexOpt = false;
3661 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3662 nmsg.obj = msg.obj;
3663 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3664 return;
3665 }
3666 // We don't at this point know if the activity is fullscreen,
3667 // so we need to be conservative and assume it isn't.
3668 activityIdleInternal((ActivityRecord)msg.obj);
3669 } break;
3670 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003671 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003672 activityIdleInternal((ActivityRecord)msg.obj);
3673 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003674 case RESUME_TOP_ACTIVITY_MSG: {
3675 synchronized (mService) {
3676 resumeTopActivitiesLocked();
3677 }
3678 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003679 case SLEEP_TIMEOUT_MSG: {
3680 synchronized (mService) {
3681 if (mService.isSleepingOrShuttingDown()) {
3682 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3683 mSleepTimeout = true;
3684 checkReadyForSleepLocked();
3685 }
3686 }
3687 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003688 case LAUNCH_TIMEOUT_MSG: {
3689 if (mService.mDidDexOpt) {
3690 mService.mDidDexOpt = false;
3691 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3692 return;
3693 }
3694 synchronized (mService) {
3695 if (mLaunchingActivity.isHeld()) {
3696 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3697 if (VALIDATE_WAKE_LOCK_CALLER
3698 && Binder.getCallingUid() != Process.myUid()) {
3699 throw new IllegalStateException("Calling must be system uid");
3700 }
3701 mLaunchingActivity.release();
3702 }
3703 }
3704 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003705 case HANDLE_DISPLAY_ADDED: {
3706 handleDisplayAddedLocked(msg.arg1);
3707 } break;
3708 case HANDLE_DISPLAY_CHANGED: {
3709 handleDisplayChangedLocked(msg.arg1);
3710 } break;
3711 case HANDLE_DISPLAY_REMOVED: {
3712 handleDisplayRemovedLocked(msg.arg1);
3713 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003714 case CONTAINER_CALLBACK_VISIBILITY: {
3715 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003716 final IActivityContainerCallback callback = container.mCallback;
3717 if (callback != null) {
3718 try {
3719 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3720 } catch (RemoteException e) {
3721 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003722 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003723 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003724 case LOCK_TASK_START_MSG: {
3725 // When lock task starts, we disable the status bars.
3726 try {
Jason Monk62515be2014-05-21 16:06:19 -04003727 if (mLockTaskNotify == null) {
3728 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003729 }
Jason Monk62515be2014-05-21 16:06:19 -04003730 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003731 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003732 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003733 int flags = 0;
3734 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_LOCKED) {
3735 flags = StatusBarManager.DISABLE_MASK
3736 & (~StatusBarManager.DISABLE_BACK);
3737 } else if (mLockTaskModeState ==
3738 ActivityManager.LOCK_TASK_MODE_PINNED) {
3739 flags = StatusBarManager.DISABLE_MASK
3740 & (~StatusBarManager.DISABLE_BACK)
3741 & (~StatusBarManager.DISABLE_HOME)
3742 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003743 }
3744 getStatusBarService().disable(flags, mToken,
3745 mService.mContext.getPackageName());
3746 }
3747 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003748 if (getDevicePolicyManager() != null) {
3749 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003750 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003751 }
justinzhang5286d3f2014-05-12 17:06:01 -04003752 } catch (RemoteException ex) {
3753 throw new RuntimeException(ex);
3754 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003755 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003756 case LOCK_TASK_END_MSG: {
3757 // When lock task ends, we enable the status bars.
3758 try {
Jason Monk62515be2014-05-21 16:06:19 -04003759 if (getStatusBarService() != null) {
3760 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3761 mService.mContext.getPackageName());
3762 }
3763 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003764 if (getDevicePolicyManager() != null) {
3765 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3766 msg.arg1);
3767 }
Jason Monk62515be2014-05-21 16:06:19 -04003768 if (mLockTaskNotify == null) {
3769 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3770 }
3771 mLockTaskNotify.show(false);
3772 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003773 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003774 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003775 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Benjamin Franz43261142015-02-11 15:59:44 +00003776 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_PINNED &&
3777 shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003778 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003779 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003780 new LockPatternUtils(mService.mContext)
3781 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003782 }
3783 } catch (SettingNotFoundException e) {
3784 // No setting, don't lock.
3785 }
justinzhang5286d3f2014-05-12 17:06:01 -04003786 } catch (RemoteException ex) {
3787 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003788 } finally {
3789 mLockTaskModeState = ActivityManager.LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003790 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003791 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003792 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3793 final ActivityContainer container = (ActivityContainer) msg.obj;
3794 final IActivityContainerCallback callback = container.mCallback;
3795 if (callback != null) {
3796 try {
3797 callback.onAllActivitiesComplete(container.asBinder());
3798 } catch (RemoteException e) {
3799 }
3800 }
3801 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003802 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3803 synchronized (mService) {
3804 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3805 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003806 final ActivityContainer container = (ActivityContainer) msg.obj;
3807 container.mStack.finishAllActivitiesLocked(true);
3808 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003809 }
3810 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003811 case LAUNCH_TASK_BEHIND_COMPLETE: {
3812 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003813 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003814 if (r != null) {
3815 handleLaunchTaskBehindCompleteLocked(r);
3816 }
3817 }
3818 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003819 }
3820 }
3821 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003822
Ying Wangb081a592014-04-22 15:20:16 -07003823 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003824 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003825 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003826 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003827 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003829 ActivityRecord mParentActivity = null;
3830 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003831
Craig Mautnere3a00d72014-04-16 08:31:19 -07003832 boolean mVisible = true;
3833
Craig Mautner4a1cb222013-12-04 16:14:06 -08003834 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003835 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003836
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003837 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3838 final static int CONTAINER_STATE_NO_SURFACE = 1;
3839 final static int CONTAINER_STATE_FINISHING = 2;
3840 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3841
3842 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003843 synchronized (mService) {
3844 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003845 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003846 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003847 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003849 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003850
Craig Mautnere0a38842013-12-16 16:14:02 -08003851 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003852 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08003853 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003854 mActivityDisplay = activityDisplay;
3855 mStack.mDisplayId = activityDisplay.mDisplayId;
3856 mStack.mStacks = activityDisplay.mStacks;
3857
3858 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003859 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003860 }
3861
3862 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003863 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003864 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003865 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3866 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003867 return;
3868 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003869 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003870 }
3871 }
3872
3873 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003874 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003875 synchronized (mService) {
3876 if (mActivityDisplay != null) {
3877 return mActivityDisplay.mDisplayId;
3878 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003879 }
3880 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003881 }
3882
Jeff Brownca9bc702014-02-11 14:32:56 -08003883 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003884 public int getStackId() {
3885 synchronized (mService) {
3886 return mStackId;
3887 }
3888 }
3889
3890 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003891 public boolean injectEvent(InputEvent event) {
3892 final long origId = Binder.clearCallingIdentity();
3893 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003894 synchronized (mService) {
3895 if (mActivityDisplay != null) {
3896 return mInputManagerInternal.injectInputEvent(event,
3897 mActivityDisplay.mDisplayId,
3898 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3899 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003900 }
3901 return false;
3902 } finally {
3903 Binder.restoreCallingIdentity(origId);
3904 }
3905 }
3906
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003907 @Override
3908 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003909 synchronized (mService) {
3910 if (mContainerState == CONTAINER_STATE_FINISHING) {
3911 return;
3912 }
3913 mContainerState = CONTAINER_STATE_FINISHING;
3914
3915 final Message msg =
3916 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003917 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003918
3919 long origId = Binder.clearCallingIdentity();
3920 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003921 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003922 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003923 } finally {
3924 Binder.restoreCallingIdentity(origId);
3925 }
3926 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003927 }
3928
Craig Mautner60257702014-09-17 15:02:33 -07003929 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003930 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003931 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003932 if (mActivityDisplay != null) {
3933 mActivityDisplay.detachActivitiesLocked(mStack);
3934 mActivityDisplay = null;
3935 mStack.mDisplayId = -1;
3936 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003937 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003938 }
3939 }
3940
3941 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003942 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003943 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08003944 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003945 Binder.getCallingUid(), mCurrentUser, false,
3946 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003947
Craig Mautnere0a38842013-12-16 16:14:02 -08003948 // TODO: Switch to user app stacks here.
3949 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08003950 final Uri data = intent.getData();
3951 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
3952 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08003953 }
Craig Mautnerb9168362015-02-26 20:40:19 -08003954 checkEmbeddedAllowedInner(userId, intent, mimeType);
3955
3956 intent.addFlags(FORCE_NEW_TASK_FLAGS);
3957 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
3958 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003959 }
3960
3961 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003962 public final int startActivityIntentSender(IIntentSender intentSender)
3963 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003964 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3965
3966 if (!(intentSender instanceof PendingIntentRecord)) {
3967 throw new IllegalArgumentException("Bad PendingIntent object");
3968 }
3969
Craig Mautnerb9168362015-02-26 20:40:19 -08003970 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003971 Binder.getCallingUid(), mCurrentUser, false,
3972 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003973
3974 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3975 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3976 pendingIntent.key.requestResolvedType);
3977
3978 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3979 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3980 }
3981
3982 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003983 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003984 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003985 throw new SecurityException(
3986 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3987 }
3988 }
3989
Craig Mautnerdf88d732014-01-27 09:21:32 -08003990 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003991 public IBinder asBinder() {
3992 return this;
3993 }
3994
Craig Mautner4504de52013-12-20 09:06:56 -08003995 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003996 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003997 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003998 }
3999
Craig Mautner4a1cb222013-12-04 16:14:06 -08004000 ActivityStackSupervisor getOuter() {
4001 return ActivityStackSupervisor.this;
4002 }
4003
Craig Mautnerd163e752014-06-13 17:18:47 -07004004 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004005 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004006 }
4007
4008 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004009 synchronized (mService) {
4010 if (mActivityDisplay != null) {
4011 mActivityDisplay.getBounds(outBounds);
4012 } else {
4013 outBounds.set(0, 0);
4014 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004015 }
4016 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004017
Craig Mautner6985bad2014-04-21 15:22:06 -07004018 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004019 void setVisible(boolean visible) {
4020 if (mVisible != visible) {
4021 mVisible = visible;
4022 if (mCallback != null) {
4023 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4024 0 /* unused */, this).sendToTarget();
4025 }
4026 }
4027 }
4028
Craig Mautner6985bad2014-04-21 15:22:06 -07004029 void setDrawn() {
4030 }
4031
Craig Mautner1b4bf852014-05-26 15:06:32 -07004032 // You can always start a new task on a regular ActivityStack.
4033 boolean isEligibleForNewTasks() {
4034 return true;
4035 }
4036
Craig Mautnerd163e752014-06-13 17:18:47 -07004037 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004038 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
4039 detachLocked();
4040 deleteActivityContainer(this);
4041 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004042 }
4043
Craig Mautner34b73df2014-01-12 21:11:08 -08004044 @Override
4045 public String toString() {
4046 return mIdString + (mActivityDisplay == null ? "N" : "A");
4047 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004048 }
4049
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004050 private class VirtualActivityContainer extends ActivityContainer {
4051 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004052 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004053
4054 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4055 super(getNextStackId());
4056 mParentActivity = parent;
4057 mCallback = callback;
4058 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004059 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004060 }
4061
4062 @Override
4063 public void setSurface(Surface surface, int width, int height, int density) {
4064 super.setSurface(surface, width, height, density);
4065
4066 synchronized (mService) {
4067 final long origId = Binder.clearCallingIdentity();
4068 try {
4069 setSurfaceLocked(surface, width, height, density);
4070 } finally {
4071 Binder.restoreCallingIdentity(origId);
4072 }
4073 }
4074 }
4075
4076 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4077 if (mContainerState == CONTAINER_STATE_FINISHING) {
4078 return;
4079 }
4080 VirtualActivityDisplay virtualActivityDisplay =
4081 (VirtualActivityDisplay) mActivityDisplay;
4082 if (virtualActivityDisplay == null) {
4083 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004084 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004085 mActivityDisplay = virtualActivityDisplay;
4086 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4087 attachToDisplayLocked(virtualActivityDisplay);
4088 }
4089
4090 if (mSurface != null) {
4091 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004092 }
4093
Craig Mautner6985bad2014-04-21 15:22:06 -07004094 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004095 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004096 mStack.resumeTopActivityLocked(null);
4097 } else {
4098 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004099 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004100 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004101 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004102 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004103 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004104
Craig Mautnerd163e752014-06-13 17:18:47 -07004105 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004106
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004107 if (DEBUG_STACK) Slog.d(TAG_STACK,
4108 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004109 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004110
Craig Mautner6985bad2014-04-21 15:22:06 -07004111 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004112 boolean isAttachedLocked() {
4113 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004114 }
4115
4116 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004117 void setDrawn() {
4118 synchronized (mService) {
4119 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004120 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004121 }
4122 }
4123
Craig Mautner1b4bf852014-05-26 15:06:32 -07004124 // Never start a new task on an ActivityView if it isn't explicitly specified.
4125 @Override
4126 boolean isEligibleForNewTasks() {
4127 return false;
4128 }
4129
Craig Mautnerd163e752014-06-13 17:18:47 -07004130 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004131 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004132 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4133 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4134 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4135 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4136 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004137 }
4138 }
4139
Craig Mautner4a1cb222013-12-04 16:14:06 -08004140 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4141 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004142 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004143 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004144 int mDisplayId;
4145 Display mDisplay;
4146 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004147
Craig Mautner4a1cb222013-12-04 16:14:06 -08004148 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4149 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004150 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004151
Jose Lima4b6c6692014-08-12 17:41:12 -07004152 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004153
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004154 ActivityDisplay() {
4155 }
Craig Mautner4504de52013-12-20 09:06:56 -08004156
Craig Mautner1a70a162014-09-13 12:09:31 -07004157 // After instantiation, check that mDisplay is not null before using this. The alternative
4158 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004159 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004160 final Display display = mDisplayManager.getDisplay(displayId);
4161 if (display == null) {
4162 return;
4163 }
4164 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004165 }
4166
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004167 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004168 mDisplay = display;
4169 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004170 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004171 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004172
4173 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004174 if (DEBUG_STACK) Slog.v(TAG_STACK,
4175 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004176 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004177 }
4178
Craig Mautnere0a38842013-12-16 16:14:02 -08004179 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004180 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004181 + " from displayId=" + mDisplayId);
4182 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004183 }
4184
4185 void getBounds(Point bounds) {
4186 mDisplay.getDisplayInfo(mDisplayInfo);
4187 bounds.x = mDisplayInfo.appWidth;
4188 bounds.y = mDisplayInfo.appHeight;
4189 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004190
Jose Lima4b6c6692014-08-12 17:41:12 -07004191 void setVisibleBehindActivity(ActivityRecord r) {
4192 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004193 }
4194
Jose Lima4b6c6692014-08-12 17:41:12 -07004195 boolean hasVisibleBehindActivity() {
4196 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004197 }
4198
Craig Mautner34b73df2014-01-12 21:11:08 -08004199 @Override
4200 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004201 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4202 }
4203 }
4204
4205 class VirtualActivityDisplay extends ActivityDisplay {
4206 VirtualDisplay mVirtualDisplay;
4207
Craig Mautner6985bad2014-04-21 15:22:06 -07004208 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004209 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004210 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4211 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4212 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4213 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004214
4215 init(mVirtualDisplay.getDisplay());
4216
4217 mWindowManager.handleDisplayAdded(mDisplayId);
4218 }
4219
4220 void setSurface(Surface surface) {
4221 if (mVirtualDisplay != null) {
4222 mVirtualDisplay.setSurface(surface);
4223 }
4224 }
4225
4226 @Override
4227 void detachActivitiesLocked(ActivityStack stack) {
4228 super.detachActivitiesLocked(stack);
4229 if (mVirtualDisplay != null) {
4230 mVirtualDisplay.release();
4231 mVirtualDisplay = null;
4232 }
4233 }
4234
4235 @Override
4236 public String toString() {
4237 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004238 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004239 }
Jose Lima58e66d62014-05-27 20:00:27 -07004240
4241 private boolean isLeanbackOnlyDevice() {
4242 boolean onLeanbackOnly = false;
4243 try {
4244 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4245 PackageManager.FEATURE_LEANBACK_ONLY);
4246 } catch (RemoteException e) {
4247 // noop
4248 }
4249
4250 return onLeanbackOnly;
4251 }
Craig Mautner27084302013-03-25 08:05:25 -07004252}