blob: 669dff8dae150f2c30aab3be42cd83a425798ae1 [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() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800653 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800654 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
655 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800656 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
657 final ActivityStack stack = stacks.get(stackNdx);
658 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800659 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700660 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800661 return false;
662 }
663 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700665 }
666 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800667 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700668 }
669
Craig Mautner2acc3892013-09-23 10:28:14 -0700670 /**
671 * Pause all activities in either all of the stacks or just the back stacks.
672 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700673 * @return true if any activity was paused as a result of this call.
674 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700675 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700676 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800677 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
678 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
680 final ActivityStack stack = stacks.get(stackNdx);
681 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
682 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
683 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700684 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
685 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700687 }
688 }
689 return someActivityPaused;
690 }
691
Craig Mautnerde4ef022013-04-07 19:01:33 -0700692 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700693 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800694 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
695 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800696 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
697 final ActivityStack stack = stacks.get(stackNdx);
698 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700699 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800700 if (DEBUG_STATES) {
701 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
702 pausing = false;
703 } else {
704 return false;
705 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700706 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700707 }
708 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700709 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700710 }
711
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700712 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
713 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500714 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800715 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
716 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
717 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
718 final ActivityStack stack = stacks.get(stackNdx);
719 if (stack.mResumedActivity != null &&
720 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700721 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800722 }
723 }
724 }
725 }
726
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700727 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700728 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700729 }
730
731 void sendWaitingVisibleReportLocked(ActivityRecord r) {
732 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700733 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700734 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700735 if (w.who == null) {
736 changed = true;
737 w.timeout = false;
738 if (r != null) {
739 w.who = new ComponentName(r.info.packageName, r.info.name);
740 }
741 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
742 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700743 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700744 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700745 if (changed) {
746 mService.notifyAll();
747 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700748 }
749
750 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
751 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700752 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700753 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700754 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700755 if (w.who == null) {
756 changed = true;
757 w.timeout = timeout;
758 if (r != null) {
759 w.who = new ComponentName(r.info.packageName, r.info.name);
760 }
761 w.thisTime = thisTime;
762 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700763 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700764 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700765 if (changed) {
766 mService.notifyAll();
767 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700768 }
769
Craig Mautner29219d92013-04-16 20:19:12 -0700770 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800771 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700772 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
773 if (r != null) {
774 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700775 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700776
Craig Mautner4a1cb222013-12-04 16:14:06 -0800777 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800778 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800779 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
780 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700781 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700782 r = stack.topRunningActivityLocked(null);
783 if (r != null) {
784 return r;
785 }
786 }
787 }
788 return null;
789 }
790
Dianne Hackborn09233282014-04-30 11:33:59 -0700791 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700792 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800793 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
794 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800795 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800796 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800797 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800798 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
799 final ActivityStack stack = stacks.get(stackNdx);
800 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
801 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700802 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700803 }
804 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700805
806 // The lists are already sorted from most recent to oldest. Just pull the most recent off
807 // each list and add it to list. Stop when all lists are empty or maxNum reached.
808 while (maxNum > 0) {
809 long mostRecentActiveTime = Long.MIN_VALUE;
810 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800811 final int numTaskLists = runningTaskLists.size();
812 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
813 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700814 if (!stackTaskList.isEmpty()) {
815 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
816 if (lastActiveTime > mostRecentActiveTime) {
817 mostRecentActiveTime = lastActiveTime;
818 selectedStackList = stackTaskList;
819 }
820 }
821 }
822 if (selectedStackList != null) {
823 list.add(selectedStackList.remove(0));
824 --maxNum;
825 } else {
826 break;
827 }
828 }
Craig Mautner20e72272013-04-01 13:45:53 -0700829 }
830
Craig Mautner23ac33b2013-04-01 16:26:35 -0700831 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700832 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700833 // Collect information about the target of the Intent.
834 ActivityInfo aInfo;
835 try {
836 ResolveInfo rInfo =
837 AppGlobals.getPackageManager().resolveIntent(
838 intent, resolvedType,
839 PackageManager.MATCH_DEFAULT_ONLY
840 | ActivityManagerService.STOCK_PM_FLAGS, userId);
841 aInfo = rInfo != null ? rInfo.activityInfo : null;
842 } catch (RemoteException e) {
843 aInfo = null;
844 }
845
846 if (aInfo != null) {
847 // Store the found target back into the intent, because now that
848 // we have it we never want to do this again. For example, if the
849 // user navigates back to this point in the history, we should
850 // always restart the exact same activity.
851 intent.setComponent(new ComponentName(
852 aInfo.applicationInfo.packageName, aInfo.name));
853
854 // Don't debug things in the system process
855 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
856 if (!aInfo.processName.equals("system")) {
857 mService.setDebugApp(aInfo.processName, true, false);
858 }
859 }
860
861 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
862 if (!aInfo.processName.equals("system")) {
863 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
864 }
865 }
866
Jeff Hao1b012d32014-08-20 10:35:34 -0700867 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700868 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700869 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700870 }
871 }
872 }
873 return aInfo;
874 }
875
Craig Mautner299f9602015-01-26 09:47:33 -0800876 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
877 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700878 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
879 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700880 }
881
Craig Mautner23ac33b2013-04-01 16:26:35 -0700882 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700883 String callingPackage, Intent intent, String resolvedType,
884 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700885 IBinder resultTo, String resultWho, int requestCode, int startFlags,
886 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700887 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700888 // Refuse possible leaked file descriptors
889 if (intent != null && intent.hasFileDescriptors()) {
890 throw new IllegalArgumentException("File descriptors passed in Intent");
891 }
892 boolean componentSpecified = intent.getComponent() != null;
893
894 // Don't modify the client's object!
895 intent = new Intent(intent);
896
897 // Collect information about the target of the Intent.
898 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700899 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700900
Craig Mautnere0a38842013-12-16 16:14:02 -0800901 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700902 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800903 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700904 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800905 // Cannot start a child activity if the parent is not resumed.
906 return ActivityManager.START_CANCELED;
907 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700908 final int realCallingPid = Binder.getCallingPid();
909 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700910 int callingPid;
911 if (callingUid >= 0) {
912 callingPid = -1;
913 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700914 callingPid = realCallingPid;
915 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700916 } else {
917 callingPid = callingUid = -1;
918 }
919
Craig Mautnere0a38842013-12-16 16:14:02 -0800920 final ActivityStack stack;
921 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800922 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800923 } else {
924 stack = container.mStack;
925 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800926 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700927 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700928 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700929
930 final long origId = Binder.clearCallingIdentity();
931
932 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800933 (aInfo.applicationInfo.privateFlags
934 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700935 // This may be a heavy-weight process! Check to see if we already
936 // have another, different heavy-weight process running.
937 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
938 if (mService.mHeavyWeightProcess != null &&
939 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
940 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700941 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700942 if (caller != null) {
943 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
944 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700945 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700946 } else {
947 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700948 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700949 + intent.toString());
950 ActivityOptions.abort(options);
951 return ActivityManager.START_PERMISSION_DENIED;
952 }
953 }
954
955 IIntentSender target = mService.getIntentSenderLocked(
956 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700957 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700958 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
959 | PendingIntent.FLAG_ONE_SHOT, null);
960
961 Intent newIntent = new Intent();
962 if (requestCode >= 0) {
963 // Caller is requesting a result.
964 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
965 }
966 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
967 new IntentSender(target));
968 if (mService.mHeavyWeightProcess.activities.size() > 0) {
969 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
970 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
971 hist.packageName);
972 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
973 hist.task.taskId);
974 }
975 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
976 aInfo.packageName);
977 newIntent.setFlags(intent.getFlags());
978 newIntent.setClassName("android",
979 HeavyWeightSwitcherActivity.class.getName());
980 intent = newIntent;
981 resolvedType = null;
982 caller = null;
983 callingUid = Binder.getCallingUid();
984 callingPid = Binder.getCallingPid();
985 componentSpecified = true;
986 try {
987 ResolveInfo rInfo =
988 AppGlobals.getPackageManager().resolveIntent(
989 intent, null,
990 PackageManager.MATCH_DEFAULT_ONLY
991 | ActivityManagerService.STOCK_PM_FLAGS, userId);
992 aInfo = rInfo != null ? rInfo.activityInfo : null;
993 aInfo = mService.getActivityInfoForUser(aInfo, userId);
994 } catch (RemoteException e) {
995 aInfo = null;
996 }
997 }
998 }
999 }
1000
Dianne Hackborn91097de2014-04-04 18:02:06 -07001001 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1002 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001003 requestCode, callingPid, callingUid, callingPackage,
1004 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001005 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001006
Craig Mautner85c11a82014-07-17 12:38:18 -07001007 Binder.restoreCallingIdentity(origId);
1008
Craig Mautnerde4ef022013-04-07 19:01:33 -07001009 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001010 // If the caller also wants to switch to a new configuration,
1011 // do so now. This allows a clean switch, as we are waiting
1012 // for the current activity to pause (so we will not destroy
1013 // it), and have not yet started the next activity.
1014 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1015 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001016 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001017 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018 "Updating to new configuration after starting activity.");
1019 mService.updateConfigurationLocked(config, null, false, false);
1020 }
1021
Craig Mautner23ac33b2013-04-01 16:26:35 -07001022 if (outResult != null) {
1023 outResult.result = res;
1024 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001025 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001026 do {
1027 try {
1028 mService.wait();
1029 } catch (InterruptedException e) {
1030 }
1031 } while (!outResult.timeout && outResult.who == null);
1032 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001033 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001034 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001035 outResult.timeout = false;
1036 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1037 outResult.totalTime = 0;
1038 outResult.thisTime = 0;
1039 } else {
1040 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001041 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001042 do {
1043 try {
1044 mService.wait();
1045 } catch (InterruptedException e) {
1046 }
1047 } while (!outResult.timeout && outResult.who == null);
1048 }
1049 }
1050 }
1051
1052 return res;
1053 }
1054 }
1055
1056 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1057 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1058 Bundle options, int userId) {
1059 if (intents == null) {
1060 throw new NullPointerException("intents is null");
1061 }
1062 if (resolvedTypes == null) {
1063 throw new NullPointerException("resolvedTypes is null");
1064 }
1065 if (intents.length != resolvedTypes.length) {
1066 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1067 }
1068
Craig Mautner23ac33b2013-04-01 16:26:35 -07001069
1070 int callingPid;
1071 if (callingUid >= 0) {
1072 callingPid = -1;
1073 } else if (caller == null) {
1074 callingPid = Binder.getCallingPid();
1075 callingUid = Binder.getCallingUid();
1076 } else {
1077 callingPid = callingUid = -1;
1078 }
1079 final long origId = Binder.clearCallingIdentity();
1080 try {
1081 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001082 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001083 for (int i=0; i<intents.length; i++) {
1084 Intent intent = intents[i];
1085 if (intent == null) {
1086 continue;
1087 }
1088
1089 // Refuse possible leaked file descriptors
1090 if (intent != null && intent.hasFileDescriptors()) {
1091 throw new IllegalArgumentException("File descriptors passed in Intent");
1092 }
1093
1094 boolean componentSpecified = intent.getComponent() != null;
1095
1096 // Don't modify the client's object!
1097 intent = new Intent(intent);
1098
1099 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001100 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001101 // TODO: New, check if this is correct
1102 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1103
1104 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001105 (aInfo.applicationInfo.privateFlags
1106 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001107 throw new IllegalArgumentException(
1108 "FLAG_CANT_SAVE_STATE not supported here");
1109 }
1110
1111 Bundle theseOptions;
1112 if (options != null && i == intents.length-1) {
1113 theseOptions = options;
1114 } else {
1115 theseOptions = null;
1116 }
Craig Mautner6170f732013-04-02 13:05:23 -07001117 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001118 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1119 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001120 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001121 if (res < 0) {
1122 return res;
1123 }
1124
1125 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1126 }
1127 }
1128 } finally {
1129 Binder.restoreCallingIdentity(origId);
1130 }
1131
1132 return ActivityManager.START_SUCCESS;
1133 }
1134
Craig Mautner2420ead2013-04-01 17:13:20 -07001135 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001136 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001137 throws RemoteException {
1138
Craig Mautner2568c3a2015-03-26 14:22:34 -07001139 if (andResume) {
1140 r.startFreezingScreenLocked(app, 0);
1141 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001142
Craig Mautner2568c3a2015-03-26 14:22:34 -07001143 // schedule launch ticks to collect information about slow apps.
1144 r.startLaunchTickingLocked();
1145 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001146
1147 // Have the window manager re-evaluate the orientation of
1148 // the screen based on the new activity order. Note that
1149 // as a result of this, it can call back into the activity
1150 // manager with a new orientation. We don't care about that,
1151 // because the activity is not currently running so we are
1152 // just restarting it anyway.
1153 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001154 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001155 mService.mConfiguration,
1156 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1157 mService.updateConfigurationLocked(config, r, false, false);
1158 }
1159
1160 r.app = app;
1161 app.waitingToKill = null;
1162 r.launchCount++;
1163 r.lastLaunchTime = SystemClock.uptimeMillis();
1164
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001165 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001166
1167 int idx = app.activities.indexOf(r);
1168 if (idx < 0) {
1169 app.activities.add(r);
1170 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001171 mService.updateLruProcessLocked(app, true, null);
1172 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001173
1174 final ActivityStack stack = r.task.stack;
1175 try {
1176 if (app.thread == null) {
1177 throw new RemoteException();
1178 }
1179 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001180 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001181 if (andResume) {
1182 results = r.results;
1183 newIntents = r.newIntents;
1184 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001185 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1186 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1187 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001188 if (andResume) {
1189 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1190 r.userId, System.identityHashCode(r),
1191 r.task.taskId, r.shortComponentName);
1192 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001193 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001194 // Home process is the root process of the task.
1195 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001196 }
1197 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1198 r.sleeping = false;
1199 r.forceNewConfig = false;
1200 mService.showAskCompatModeDialogLocked(r);
1201 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001202 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001203 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1204 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1205 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001206 final String profileFile = mService.mProfileFile;
1207 if (profileFile != null) {
1208 ParcelFileDescriptor profileFd = mService.mProfileFd;
1209 if (profileFd != null) {
1210 try {
1211 profileFd = profileFd.dup();
1212 } catch (IOException e) {
1213 if (profileFd != null) {
1214 try {
1215 profileFd.close();
1216 } catch (IOException o) {
1217 }
1218 profileFd = null;
1219 }
1220 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001221 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001222
1223 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1224 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001225 }
1226 }
1227 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001228
Craig Mautner2568c3a2015-03-26 14:22:34 -07001229 if (andResume) {
1230 app.hasShownUi = true;
1231 app.pendingUiClean = true;
1232 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001233 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001234 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001235 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001236 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1237 r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1238 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001239
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001240 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001241 // This may be a heavy-weight process! Note that the package
1242 // manager will ensure that only activity can run in the main
1243 // process of the .apk, which is the only thing that will be
1244 // considered heavy-weight.
1245 if (app.processName.equals(app.info.packageName)) {
1246 if (mService.mHeavyWeightProcess != null
1247 && mService.mHeavyWeightProcess != app) {
1248 Slog.w(TAG, "Starting new heavy weight process " + app
1249 + " when already running "
1250 + mService.mHeavyWeightProcess);
1251 }
1252 mService.mHeavyWeightProcess = app;
1253 Message msg = mService.mHandler.obtainMessage(
1254 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1255 msg.obj = r;
1256 mService.mHandler.sendMessage(msg);
1257 }
1258 }
1259
1260 } catch (RemoteException e) {
1261 if (r.launchFailed) {
1262 // This is the second time we failed -- finish activity
1263 // and give up.
1264 Slog.e(TAG, "Second failure launching "
1265 + r.intent.getComponent().flattenToShortString()
1266 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001267 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1269 "2nd-crash", false);
1270 return false;
1271 }
1272
1273 // This is the first time we failed -- restart process and
1274 // retry.
1275 app.activities.remove(r);
1276 throw e;
1277 }
1278
1279 r.launchFailed = false;
1280 if (stack.updateLRUListLocked(r)) {
1281 Slog.w(TAG, "Activity " + r
1282 + " being launched, but already in LRU list");
1283 }
1284
1285 if (andResume) {
1286 // As part of the process of launching, ActivityThread also performs
1287 // a resume.
1288 stack.minimalResumeActivityLocked(r);
1289 } else {
1290 // This activity is not starting in the resumed state... which
1291 // should look like we asked it to pause+stop (but remain visible),
1292 // and it has done so and reported back the current icicle and
1293 // other state.
1294 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1295 + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001296 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001297 r.stopped = true;
1298 }
1299
1300 // Launch the new version setup screen if needed. We do this -after-
1301 // launching the initial activity (that is, home), so that it can have
1302 // a chance to initialize itself while in the background, making the
1303 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001304 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001305 mService.startSetupActivityLocked();
1306 }
1307
Dianne Hackborn465fa392014-09-14 14:21:18 -07001308 // Update any services we are bound to that might care about whether
1309 // their client may have activities.
1310 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1311
Craig Mautner2420ead2013-04-01 17:13:20 -07001312 return true;
1313 }
1314
Craig Mautnere79d42682013-04-01 19:01:53 -07001315 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001316 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001317 // Is this activity's application already running?
1318 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001319 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001320
1321 r.task.stack.setLaunchTime(r);
1322
1323 if (app != null && app.thread != null) {
1324 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001325 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1326 || !"android".equals(r.info.packageName)) {
1327 // Don't add this if it is a platform component that is marked
1328 // to run in multiple processes, because this is actually
1329 // part of the framework so doesn't make sense to track as a
1330 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001331 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1332 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001333 }
George Mount2c92c972014-03-20 09:38:23 -07001334 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001335 return;
1336 } catch (RemoteException e) {
1337 Slog.w(TAG, "Exception when starting activity "
1338 + r.intent.getComponent().flattenToShortString(), e);
1339 }
1340
1341 // If a dead object exception was thrown -- fall through to
1342 // restart the application.
1343 }
1344
1345 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001346 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001347 }
1348
Craig Mautner6170f732013-04-02 13:05:23 -07001349 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001350 Intent intent, String resolvedType, ActivityInfo aInfo,
1351 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1352 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001353 int callingPid, int callingUid, String callingPackage,
1354 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001355 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1356 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001357 int err = ActivityManager.START_SUCCESS;
1358
1359 ProcessRecord callerApp = null;
1360 if (caller != null) {
1361 callerApp = mService.getRecordForAppLocked(caller);
1362 if (callerApp != null) {
1363 callingPid = callerApp.pid;
1364 callingUid = callerApp.info.uid;
1365 } else {
1366 Slog.w(TAG, "Unable to find app for caller " + caller
1367 + " (pid=" + callingPid + ") when starting: "
1368 + intent.toString());
1369 err = ActivityManager.START_PERMISSION_DENIED;
1370 }
1371 }
1372
1373 if (err == ActivityManager.START_SUCCESS) {
1374 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1375 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001376 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001377 + " on display " + (container == null ? (mFocusedStack == null ?
1378 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1379 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1380 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001381 }
1382
1383 ActivityRecord sourceRecord = null;
1384 ActivityRecord resultRecord = null;
1385 if (resultTo != null) {
1386 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001387 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1388 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001389 if (sourceRecord != null) {
1390 if (requestCode >= 0 && !sourceRecord.finishing) {
1391 resultRecord = sourceRecord;
1392 }
1393 }
1394 }
Craig Mautner6170f732013-04-02 13:05:23 -07001395
Dianne Hackborn91097de2014-04-04 18:02:06 -07001396 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001397
Dianne Hackborn95465202014-09-15 16:21:55 -07001398 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001399 // Transfer the result target from the source activity to the new
1400 // one being started, including any failures.
1401 if (requestCode >= 0) {
1402 ActivityOptions.abort(options);
1403 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1404 }
1405 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001406 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1407 resultRecord = null;
1408 }
Craig Mautner6170f732013-04-02 13:05:23 -07001409 resultWho = sourceRecord.resultWho;
1410 requestCode = sourceRecord.requestCode;
1411 sourceRecord.resultTo = null;
1412 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001413 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001414 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001415 if (sourceRecord.launchedFromUid == callingUid) {
1416 // The new activity is being launched from the same uid as the previous
1417 // activity in the flow, and asking to forward its result back to the
1418 // previous. In this case the activity is serving as a trampoline between
1419 // the two, so we also want to update its launchedFromPackage to be the
1420 // same as the previous activity. Note that this is safe, since we know
1421 // these two packages come from the same uid; the caller could just as
1422 // well have supplied that same package name itself. This specifially
1423 // deals with the case of an intent picker/chooser being launched in the app
1424 // flow to redirect to an activity picked by the user, where we want the final
1425 // activity to consider it to have been launched by the previous app activity.
1426 callingPackage = sourceRecord.launchedFromPackage;
1427 }
Craig Mautner6170f732013-04-02 13:05:23 -07001428 }
1429
1430 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1431 // We couldn't find a class that can handle the given Intent.
1432 // That's the end of that!
1433 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1434 }
1435
1436 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1437 // We couldn't find the specific class specified in the Intent.
1438 // Also the end of the line.
1439 err = ActivityManager.START_CLASS_NOT_FOUND;
1440 }
1441
Dianne Hackborn91097de2014-04-04 18:02:06 -07001442 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1443 && sourceRecord.task.voiceSession != null) {
1444 // If this activity is being launched as part of a voice session, we need
1445 // to ensure that it is safe to do so. If the upcoming activity will also
1446 // be part of the voice session, we can only launch it if it has explicitly
1447 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001448 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001449 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1450 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001451 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1452 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001453 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1454 }
1455 } catch (RemoteException e) {
1456 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1457 }
1458 }
1459 }
1460
1461 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1462 // If the caller is starting a new voice session, just make sure the target
1463 // is actually allowing it to run this way.
1464 try {
1465 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1466 intent, resolvedType)) {
1467 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1468 }
1469 } catch (RemoteException e) {
1470 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1471 }
1472 }
1473
louis_changcd5d1982014-08-01 10:09:08 +08001474 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1475
Craig Mautner6170f732013-04-02 13:05:23 -07001476 if (err != ActivityManager.START_SUCCESS) {
1477 if (resultRecord != null) {
1478 resultStack.sendActivityResultLocked(-1,
1479 resultRecord, resultWho, requestCode,
1480 Activity.RESULT_CANCELED, null);
1481 }
Craig Mautner6170f732013-04-02 13:05:23 -07001482 ActivityOptions.abort(options);
1483 return err;
1484 }
1485
1486 final int startAnyPerm = mService.checkPermission(
1487 START_ANY_ACTIVITY, callingPid, callingUid);
1488 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1489 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1490 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1491 if (resultRecord != null) {
1492 resultStack.sendActivityResultLocked(-1,
1493 resultRecord, resultWho, requestCode,
1494 Activity.RESULT_CANCELED, null);
1495 }
Craig Mautner6170f732013-04-02 13:05:23 -07001496 String msg;
1497 if (!aInfo.exported) {
1498 msg = "Permission Denial: starting " + intent.toString()
1499 + " from " + callerApp + " (pid=" + callingPid
1500 + ", uid=" + callingUid + ")"
1501 + " not exported from uid " + aInfo.applicationInfo.uid;
1502 } else {
1503 msg = "Permission Denial: starting " + intent.toString()
1504 + " from " + callerApp + " (pid=" + callingPid
1505 + ", uid=" + callingUid + ")"
1506 + " requires " + aInfo.permission;
1507 }
1508 Slog.w(TAG, msg);
1509 throw new SecurityException(msg);
1510 }
1511
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001512 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001513 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001514
Craig Mautner6170f732013-04-02 13:05:23 -07001515 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001516 try {
1517 // The Intent we give to the watcher has the extra data
1518 // stripped off, since it can contain private information.
1519 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001520 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001521 aInfo.applicationInfo.packageName);
1522 } catch (RemoteException e) {
1523 mService.mController = null;
1524 }
Ben Gruver5e207332013-04-03 17:41:37 -07001525 }
Craig Mautner6170f732013-04-02 13:05:23 -07001526
Ben Gruver5e207332013-04-03 17:41:37 -07001527 if (abort) {
1528 if (resultRecord != null) {
1529 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001530 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001531 }
Ben Gruver5e207332013-04-03 17:41:37 -07001532 // We pretend to the caller that it was really started, but
1533 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001534 ActivityOptions.abort(options);
1535 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001536 }
1537
1538 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001539 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001540 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001541 if (outActivity != null) {
1542 outActivity[0] = r;
1543 }
1544
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001545 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001546 if (voiceSession == null && (stack.mResumedActivity == null
1547 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001548 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1549 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001550 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001551 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001552 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001553 ActivityOptions.abort(options);
1554 return ActivityManager.START_SWITCHES_CANCELED;
1555 }
1556 }
1557
1558 if (mService.mDidAppSwitch) {
1559 // This is the second allowed switch since we stopped switches,
1560 // so now just generally allow switches. Use case: user presses
1561 // home (switches disabled, switch to home, mDidAppSwitch now true);
1562 // user taps a home icon (coming from home so allowed, we hit here
1563 // and now allow anyone to switch again).
1564 mService.mAppSwitchesAllowedTime = 0;
1565 } else {
1566 mService.mDidAppSwitch = true;
1567 }
1568
Craig Mautneree36c772014-07-16 14:56:05 -07001569 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001570
Dianne Hackborn91097de2014-04-04 18:02:06 -07001571 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001572 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001573
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001574 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001575 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001576 // activity start, but we are not actually doing an activity
1577 // switch... just dismiss the keyguard now, because we
1578 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001579 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001580 }
1581 return err;
1582 }
1583
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001584 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001585 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001586
1587 // On leanback only devices we should keep all activities in the same stack.
1588 if (!mLeanbackOnlyDevice &&
1589 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001590
1591 ActivityStack stack;
1592
Wale Ogunwale7d701172015-03-11 15:36:30 -07001593 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001594 stack = task.stack;
1595 if (stack.isOnHomeDisplay()) {
1596 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001597 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1598 "computeStackFocus: Setting " + "focused stack to r=" + r
1599 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001600 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001601 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001602 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001603 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001604 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001605 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001606 }
1607
Craig Mautnere0a38842013-12-16 16:14:02 -08001608 final ActivityContainer container = r.mInitialActivityContainer;
1609 if (container != null) {
1610 // The first time put it on the desired stack, after this put on task stack.
1611 r.mInitialActivityContainer = null;
1612 return container.mStack;
1613 }
1614
Craig Mautner1b4bf852014-05-26 15:06:32 -07001615 if (mFocusedStack != mHomeStack && (!newTask ||
1616 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001617 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001618 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001619 return mFocusedStack;
1620 }
1621
Craig Mautnere0a38842013-12-16 16:14:02 -08001622 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1623 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001624 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001625 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001626 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001627 "computeStackFocus: Setting focused stack=" + stack);
1628 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001629 }
1630 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001631
Craig Mautner4a1cb222013-12-04 16:14:06 -08001632 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001633 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001634 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1635 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001636 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001637 }
1638 return mHomeStack;
1639 }
1640
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001641 boolean setFocusedStack(ActivityRecord r, String reason) {
1642 if (r == null) {
1643 // Not sure what you are trying to do, but it is not going to work...
1644 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001645 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001646 final TaskRecord task = r.task;
1647 if (task == null || task.stack == null) {
1648 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1649 return false;
1650 }
1651 task.stack.moveToFront(reason);
1652 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001653 }
1654
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001655 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001656 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001657 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001658 final Intent intent = r.intent;
1659 final int callingUid = r.launchedFromUid;
1660
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001661 // In some flows in to this function, we retrieve the task record and hold on to it
1662 // without a lock before calling back in to here... so the task at this point may
1663 // not actually be in recents. Check for that, and if it isn't in recents just
1664 // consider it invalid.
1665 if (inTask != null && !inTask.inRecents) {
1666 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1667 inTask = null;
1668 }
1669
Craig Mautnerad400af2014-08-13 16:41:36 -07001670 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001671 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1672 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001673
Craig Mautnera228ae92014-07-09 05:44:55 -07001674 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001675 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001676 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001677 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1678 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1679 "\"singleInstance\" or \"singleTask\"");
1680 launchFlags &=
1681 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1682 } else {
1683 switch (r.info.documentLaunchMode) {
1684 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1685 break;
1686 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1687 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1688 break;
1689 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1690 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1691 break;
1692 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1693 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1694 break;
1695 }
1696 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001697
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001698 final boolean launchTaskBehind = r.mLaunchTaskBehind
1699 && !launchSingleTask && !launchSingleInstance
1700 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001701
Wale Ogunwale7d701172015-03-11 15:36:30 -07001702 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1703 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001704 // For whatever reason this activity is being launched into a new
1705 // task... yet the caller has requested a result back. Well, that
1706 // is pretty messed up, so instead immediately send back a cancel
1707 // and let the new task continue launched as normal without a
1708 // dependency on its originator.
1709 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1710 r.resultTo.task.stack.sendActivityResultLocked(-1,
1711 r.resultTo, r.resultWho, r.requestCode,
1712 Activity.RESULT_CANCELED, null);
1713 r.resultTo = null;
1714 }
1715
1716 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1717 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1718 }
1719
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001720 // If we are actually going to launch in to a new task, there are some cases where
1721 // we further want to do multiple task.
1722 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1723 if (launchTaskBehind
1724 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1725 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1726 }
1727 }
1728
Craig Mautner8849a5e2013-04-02 16:41:03 -07001729 // We'll invoke onUserLeaving before onPause only if the launching
1730 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001731 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001732 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1733 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001734
1735 // If the caller has asked not to resume at this point, we make note
1736 // of this in the record so that we can skip it when trying to find
1737 // the top running activity.
1738 if (!doResume) {
1739 r.delayedResume = true;
1740 }
1741
Craig Mautnera254cd72014-05-25 16:47:39 -07001742 ActivityRecord notTop =
1743 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001744
1745 // If the onlyIfNeeded flag is set, then we can do this if the activity
1746 // being launched is the same as the one making the call... or, as
1747 // a special case, if we do not know the caller then we count the
1748 // current top activity as the caller.
1749 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1750 ActivityRecord checkedCaller = sourceRecord;
1751 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001752 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001753 }
1754 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1755 // Caller is not the same as launcher, so always needed.
1756 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1757 }
1758 }
1759
Craig Mautner8849a5e2013-04-02 16:41:03 -07001760 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001761 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001762
1763 // If the caller is not coming from another activity, but has given us an
1764 // explicit task into which they would like us to launch the new activity,
1765 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001766 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1767 final Intent baseIntent = inTask.getBaseIntent();
1768 final ActivityRecord root = inTask.getRootActivity();
1769 if (baseIntent == null) {
1770 ActivityOptions.abort(options);
1771 throw new IllegalArgumentException("Launching into task without base intent: "
1772 + inTask);
1773 }
1774
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001775 // If this task is empty, then we are adding the first activity -- it
1776 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001777 if (launchSingleInstance || launchSingleTask) {
1778 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1779 ActivityOptions.abort(options);
1780 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1781 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001782 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001783 if (root != null) {
1784 ActivityOptions.abort(options);
1785 throw new IllegalArgumentException("Caller with inTask " + inTask
1786 + " has root " + root + " but target is singleInstance/Task");
1787 }
1788 }
1789
1790 // If task is empty, then adopt the interesting intent launch flags in to the
1791 // activity being started.
1792 if (root == null) {
1793 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1794 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1795 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1796 launchFlags = (launchFlags&~flagsOfInterest)
1797 | (baseIntent.getFlags()&flagsOfInterest);
1798 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001799 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001800 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001801
Dianne Hackborn962d5352014-08-29 16:27:40 -07001802 // If the task is not empty and the caller is asking to start it as the root
1803 // of a new task, then we don't actually want to start this on the task. We
1804 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001805 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001806 addingToTask = false;
1807
1808 } else {
1809 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001810 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001811
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001812 reuseTask = inTask;
1813 } else {
1814 inTask = null;
1815 }
1816
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001817 if (inTask == null) {
1818 if (sourceRecord == null) {
1819 // This activity is not being started from another... in this
1820 // case we -always- start a new task.
1821 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1822 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1823 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1824 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1825 }
1826 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1827 // The original activity who is starting us is running as a single
1828 // instance... this new activity it is starting must go on its
1829 // own task.
1830 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1831 } else if (launchSingleInstance || launchSingleTask) {
1832 // The activity being started is a single instance... it always
1833 // gets launched into its own task.
1834 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1835 }
1836 }
1837
1838 ActivityInfo newTaskInfo = null;
1839 Intent newTaskIntent = null;
1840 ActivityStack sourceStack;
1841 if (sourceRecord != null) {
1842 if (sourceRecord.finishing) {
1843 // If the source is finishing, we can't further count it as our source. This
1844 // is because the task it is associated with may now be empty and on its way out,
1845 // so we don't want to blindly throw it in to that task. Instead we will take
1846 // the NEW_TASK flow and try to find a task for it. But save the task information
1847 // so it can be used when creating the new task.
1848 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1849 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1850 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1851 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1852 newTaskInfo = sourceRecord.info;
1853 newTaskIntent = sourceRecord.task.intent;
1854 }
1855 sourceRecord = null;
1856 sourceStack = null;
1857 } else {
1858 sourceStack = sourceRecord.task.stack;
1859 }
1860 } else {
1861 sourceStack = null;
1862 }
1863
1864 boolean movedHome = false;
1865 ActivityStack targetStack;
1866
1867 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001868 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001869
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001870 // We may want to try to place the new activity in to an existing task. We always
1871 // do this if the target activity is singleTask or singleInstance; we will also do
1872 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1873 // us to still place it in a new task: multi task, always doc mode, or being asked to
1874 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001875 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1876 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001877 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001878 // If bring to front is requested, and no result is requested and we have not
1879 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001880 // we can find a task that was started with this same
1881 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001882 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001883 // See if there is a task to bring to the front. If this is
1884 // a SINGLE_INSTANCE activity, there can be one and only one
1885 // instance of it in the history, and it is always in its own
1886 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001887 ActivityRecord intentActivity = !launchSingleInstance ?
1888 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001889 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001890 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001891 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001892 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001893 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1894 }
Craig Mautner29219d92013-04-16 20:19:12 -07001895 if (r.task == null) {
1896 r.task = intentActivity.task;
1897 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001898 if (intentActivity.task.intent == null) {
1899 // This task was started because of movement of
1900 // the activity based on affinity... now that we
1901 // are actually launching it, we can assign the
1902 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001903 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001904 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001905 targetStack = intentActivity.task.stack;
1906 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001907 // If the target task is not in the front, then we need
1908 // to bring it to the front... except... well, with
1909 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1910 // to have the same behavior as if a new instance was
1911 // being started, which means not bringing it to the front
1912 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001913 final ActivityStack focusStack = getFocusedStack();
1914 ActivityRecord curTop = (focusStack == null)
1915 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001916 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001917 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001918 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001919 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001920 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1921 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001922 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001923 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001924 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1925 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001926 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001927 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1928 options, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001929 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001930 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001931 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1932 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001933 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001934 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001935 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001936 options = null;
1937 }
1938 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001939 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001940 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001941 + " from " + intentActivity);
1942 targetStack.moveToFront("intentActivityFound");
1943 }
1944
Craig Mautner8849a5e2013-04-02 16:41:03 -07001945 // If the caller has requested that the target task be
1946 // reset, then do so.
1947 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1948 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1949 }
1950 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1951 // We don't need to start a new activity, and
1952 // the client said not to do anything if that
1953 // is the case, so this is it! And for paranoia, make
1954 // sure we have correctly resumed the top activity.
1955 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001956 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001957
1958 // Make sure to notify Keyguard as well if we are not running an app
1959 // transition later.
1960 if (!movedToFront) {
1961 notifyActivityDrawnForKeyguard();
1962 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001963 } else {
1964 ActivityOptions.abort(options);
1965 }
1966 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1967 }
1968 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001969 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1970 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001971 // The caller has requested to completely replace any
1972 // existing task with its new activity. Well that should
1973 // not be too hard...
1974 reuseTask = intentActivity.task;
1975 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001976 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001977 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001978 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001979 // In this situation we want to remove all activities
1980 // from the task up to the one being started. In most
1981 // cases this means we are resetting the task to its
1982 // initial state.
1983 ActivityRecord top =
1984 intentActivity.task.performClearTaskLocked(r, launchFlags);
1985 if (top != null) {
1986 if (top.frontOfTask) {
1987 // Activity aliases may mean we use different
1988 // intents for the top activity, so make sure
1989 // the task now has the identity of the new
1990 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001991 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001992 }
1993 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1994 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001995 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001996 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07001997 // A special case: we need to start the activity because it is not
1998 // currently running, and the caller has asked to clear the current
1999 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002000 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002001 // Now pretend like this activity is being started by the top of its
2002 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002003 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002004 TaskRecord task = sourceRecord.task;
2005 if (task != null && task.stack == null) {
2006 // Target stack got cleared when we all activities were removed
2007 // above. Go ahead and reset it.
2008 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2009 targetStack.addTask(
2010 task, !launchTaskBehind /* toTop */, false /* moving */);
2011 }
2012
Craig Mautner8849a5e2013-04-02 16:41:03 -07002013 }
2014 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2015 // In this case the top activity on the task is the
2016 // same as the one being launched, so we take that
2017 // as a request to bring the task to the foreground.
2018 // If the top activity in the task is the root
2019 // activity, deliver this new intent to it if it
2020 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002021 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002022 && intentActivity.realActivity.equals(r.realActivity)) {
2023 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2024 intentActivity.task);
2025 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002026 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002027 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002028 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2029 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002030 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2031 // In this case we are launching the root activity
2032 // of the task, but with a different intent. We
2033 // should start a new instance on top.
2034 addingToTask = true;
2035 sourceRecord = intentActivity;
2036 }
2037 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2038 // In this case an activity is being launched in to an
2039 // existing task, without resetting that task. This
2040 // is typically the situation of launching an activity
2041 // from a notification or shortcut. We want to place
2042 // the new activity on top of the current task.
2043 addingToTask = true;
2044 sourceRecord = intentActivity;
2045 } else if (!intentActivity.task.rootWasReset) {
2046 // In this case we are launching in to an existing task
2047 // that has not yet been started from its front door.
2048 // The current task has been brought to the front.
2049 // Ideally, we'd probably like to place this new task
2050 // at the bottom of its stack, but that's a little hard
2051 // to do with the current organization of the code so
2052 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002053 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002054 }
2055 if (!addingToTask && reuseTask == null) {
2056 // We didn't do anything... but it was needed (a.k.a., client
2057 // don't use that intent!) And for paranoia, make
2058 // sure we have correctly resumed the top activity.
2059 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002060 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002061 if (!movedToFront) {
2062 // Make sure to notify Keyguard as well if we are not running an app
2063 // transition later.
2064 notifyActivityDrawnForKeyguard();
2065 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002066 } else {
2067 ActivityOptions.abort(options);
2068 }
2069 return ActivityManager.START_TASK_TO_FRONT;
2070 }
2071 }
2072 }
2073 }
2074
2075 //String uri = r.intent.toURI();
2076 //Intent intent2 = new Intent(uri);
2077 //Slog.i(TAG, "Given intent: " + r.intent);
2078 //Slog.i(TAG, "URI is: " + uri);
2079 //Slog.i(TAG, "To intent: " + intent2);
2080
2081 if (r.packageName != null) {
2082 // If the activity being launched is the same as the one currently
2083 // at the top, then we need to check if it should only be launched
2084 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002085 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002086 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002087 if (top != null && r.resultTo == null) {
2088 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2089 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002090 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002091 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002092 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2093 top.task);
2094 // For paranoia, make sure we have correctly
2095 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002096 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002097 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002098 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002099 }
2100 ActivityOptions.abort(options);
2101 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2102 // We don't need to start a new activity, and
2103 // the client said not to do anything if that
2104 // is the case, so this is it!
2105 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2106 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002107 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002108 return ActivityManager.START_DELIVERED_TO_TOP;
2109 }
2110 }
2111 }
2112 }
2113
2114 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002115 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002116 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2117 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002118 }
2119 ActivityOptions.abort(options);
2120 return ActivityManager.START_CLASS_NOT_FOUND;
2121 }
2122
2123 boolean newTask = false;
2124 boolean keepCurTransition = false;
2125
Craig Mautnerbb742462014-07-07 15:28:55 -07002126 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002127 sourceRecord.task : null;
2128
Craig Mautner8849a5e2013-04-02 16:41:03 -07002129 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002130 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002131 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002132 if (isLockTaskModeViolation(reuseTask)) {
2133 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2134 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2135 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002136 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002137 targetStack = computeStackFocus(r, newTask);
2138 targetStack.moveToFront("startingNewTask");
2139
Craig Mautner8849a5e2013-04-02 16:41:03 -07002140 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002141 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2142 newTaskInfo != null ? newTaskInfo : r.info,
2143 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002144 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2145 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002146 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2147 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002148 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002149 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002150 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002151 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002152 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002153 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2154 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002155 // Caller wants to appear on home activity, so before starting
2156 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002157 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002158 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002159 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002160 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002161 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002162 if (isLockTaskModeViolation(sourceTask)) {
2163 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2164 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2165 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002166 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002167 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002168 final TaskRecord topTask = targetStack.topTask();
2169 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002170 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2171 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002172 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002173 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002174 // In this case, we are adding the activity to an existing
2175 // task, but the caller has asked to clear that task if the
2176 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002177 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002178 keepCurTransition = true;
2179 if (top != null) {
2180 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002181 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002182 // For paranoia, make sure we have correctly
2183 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002184 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002185 if (doResume) {
2186 targetStack.resumeTopActivityLocked(null);
2187 }
2188 ActivityOptions.abort(options);
2189 return ActivityManager.START_DELIVERED_TO_TOP;
2190 }
2191 } else if (!addingToTask &&
2192 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2193 // In this case, we are launching an activity in our own task
2194 // that may already be running somewhere in the history, and
2195 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002196 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002197 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002198 final TaskRecord task = top.task;
2199 task.moveActivityToFrontLocked(top);
2200 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002201 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002202 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002203 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002204 if (doResume) {
2205 targetStack.resumeTopActivityLocked(null);
2206 }
2207 return ActivityManager.START_DELIVERED_TO_TOP;
2208 }
2209 }
2210 // An existing activity is starting this new activity, so we want
2211 // to keep the new one in the same task as the one that is starting
2212 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002213 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002214 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002215 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002216
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002217 } else if (inTask != null) {
2218 // The calling is asking that the new activity be started in an explicit
2219 // task it has provided to us.
2220 if (isLockTaskModeViolation(inTask)) {
2221 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2222 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2223 }
2224 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002225 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002226
2227 // Check whether we should actually launch the new activity in to the task,
2228 // or just reuse the current activity on top.
2229 ActivityRecord top = inTask.getTopActivity();
2230 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2231 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2232 || launchSingleTop || launchSingleTask) {
2233 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2234 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2235 // We don't need to start a new activity, and
2236 // the client said not to do anything if that
2237 // is the case, so this is it!
2238 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2239 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002240 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002241 return ActivityManager.START_DELIVERED_TO_TOP;
2242 }
2243 }
2244
Dianne Hackborn962d5352014-08-29 16:27:40 -07002245 if (!addingToTask) {
2246 // We don't actually want to have this activity added to the task, so just
2247 // stop here but still tell the caller that we consumed the intent.
2248 ActivityOptions.abort(options);
2249 return ActivityManager.START_TASK_TO_FRONT;
2250 }
2251
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002252 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002253 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002254 + " in explicit task " + r.task);
2255
Craig Mautner8849a5e2013-04-02 16:41:03 -07002256 } else {
2257 // This not being started from an existing activity, and not part
2258 // of a new task... just put it in the top task, though these days
2259 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002260 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002261 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002262 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002263 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002264 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002265 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002266 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002267 + " in new guessed " + r.task);
2268 }
2269
2270 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002271 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002272
Craig Mautner76e2a762014-06-24 09:05:43 -07002273 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2274 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2275 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002276 if (newTask) {
2277 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2278 }
2279 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002280 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002281 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002282 if (!launchTaskBehind) {
2283 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002284 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002285 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002286 return ActivityManager.START_SUCCESS;
2287 }
2288
Craig Mautneree36c772014-07-16 14:56:05 -07002289 final void doPendingActivityLaunchesLocked(boolean doResume) {
2290 while (!mPendingActivityLaunches.isEmpty()) {
2291 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002292 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002293 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002294 }
2295 }
2296
Craig Mautner7f13ed32014-07-28 14:00:35 -07002297 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002298 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2299 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002300 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002301 mPendingActivityLaunches.remove(palNdx);
2302 }
2303 }
2304 }
2305
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002306 void setLaunchSource(int uid) {
2307 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2308 }
2309
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002310 void acquireLaunchWakelock() {
2311 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2312 throw new IllegalStateException("Calling must be system uid");
2313 }
2314 mLaunchingActivity.acquire();
2315 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2316 // To be safe, don't allow the wake lock to be held for too long.
2317 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2318 }
2319 }
2320
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002321 /**
2322 * Called when the frontmost task is idle.
2323 * @return the state of mService.mBooting before this was called.
2324 */
2325 private boolean checkFinishBootingLocked() {
2326 final boolean booting = mService.mBooting;
2327 boolean enableScreen = false;
2328 mService.mBooting = false;
2329 if (!mService.mBooted) {
2330 mService.mBooted = true;
2331 enableScreen = true;
2332 }
2333 if (booting || enableScreen) {
2334 mService.postFinishBooting(booting, enableScreen);
2335 }
2336 return booting;
2337 }
2338
Craig Mautnerf3333272013-04-22 10:55:53 -07002339 // Checked.
2340 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2341 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002342 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002343
Craig Mautnerf3333272013-04-22 10:55:53 -07002344 ArrayList<ActivityRecord> stops = null;
2345 ArrayList<ActivityRecord> finishes = null;
2346 ArrayList<UserStartedState> startingUsers = null;
2347 int NS = 0;
2348 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002349 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002350 boolean activityRemoved = false;
2351
Wale Ogunwale7d701172015-03-11 15:36:30 -07002352 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002353 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002354 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2355 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002356 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2357 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002358 if (fromTimeout) {
2359 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002360 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002361
2362 // This is a hack to semi-deal with a race condition
2363 // in the client where it can be constructed with a
2364 // newer configuration from when we asked it to launch.
2365 // We'll update with whatever configuration it now says
2366 // it used to launch.
2367 if (config != null) {
2368 r.configuration = config;
2369 }
2370
2371 // We are now idle. If someone is waiting for a thumbnail from
2372 // us, we can now deliver.
2373 r.idle = true;
2374
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002375 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002376 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002377 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002378 }
2379 }
2380
2381 if (allResumedActivitiesIdle()) {
2382 if (r != null) {
2383 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002384 }
2385
2386 if (mLaunchingActivity.isHeld()) {
2387 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2388 if (VALIDATE_WAKE_LOCK_CALLER &&
2389 Binder.getCallingUid() != Process.myUid()) {
2390 throw new IllegalStateException("Calling must be system uid");
2391 }
2392 mLaunchingActivity.release();
2393 }
2394 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002395 }
2396
2397 // Atomically retrieve all of the other things to do.
2398 stops = processStoppingActivitiesLocked(true);
2399 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002400 if ((NF = mFinishingActivities.size()) > 0) {
2401 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002402 mFinishingActivities.clear();
2403 }
2404
Craig Mautnerf3333272013-04-22 10:55:53 -07002405 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002406 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002407 mStartingUsers.clear();
2408 }
2409
Craig Mautnerf3333272013-04-22 10:55:53 -07002410 // Stop any activities that are scheduled to do so but have been
2411 // waiting for the next one to start.
2412 for (int i = 0; i < NS; i++) {
2413 r = stops.get(i);
2414 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002415 if (stack != null) {
2416 if (r.finishing) {
2417 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2418 } else {
2419 stack.stopActivityLocked(r);
2420 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002421 }
2422 }
2423
2424 // Finish any activities that are scheduled to do so but have been
2425 // waiting for the next one to start.
2426 for (int i = 0; i < NF; i++) {
2427 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002428 final ActivityStack stack = r.task.stack;
2429 if (stack != null) {
2430 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2431 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002432 }
2433
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002434 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002435 // Complete user switch
2436 if (startingUsers != null) {
2437 for (int i = 0; i < startingUsers.size(); i++) {
2438 mService.finishUserSwitch(startingUsers.get(i));
2439 }
2440 }
2441 // Complete starting up of background users
2442 if (mStartingBackgroundUsers.size() > 0) {
2443 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2444 mStartingBackgroundUsers.clear();
2445 for (int i = 0; i < startingUsers.size(); i++) {
2446 mService.finishUserBoot(startingUsers.get(i));
2447 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002448 }
2449 }
2450
2451 mService.trimApplications();
2452 //dump();
2453 //mWindowManager.dump();
2454
Craig Mautnerf3333272013-04-22 10:55:53 -07002455 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002456 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002457 }
2458
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002459 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002460 }
2461
Craig Mautner8e569572013-10-11 17:36:59 -07002462 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002463 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002464 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2465 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002466 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2467 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2468 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002469 }
Craig Mautner19091252013-10-05 00:03:53 -07002470 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002471 }
2472
2473 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002474 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2475 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002476 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2477 stacks.get(stackNdx).closeSystemDialogsLocked();
2478 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002479 }
2480 }
2481
Craig Mautner93529a42013-10-04 15:03:13 -07002482 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002483 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002484 }
2485
Craig Mautner8d341ef2013-03-26 09:03:27 -07002486 /**
2487 * @return true if some activity was finished (or would have finished if doit were true).
2488 */
2489 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2490 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002491 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2492 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002493 final int numStacks = stacks.size();
2494 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2495 final ActivityStack stack = stacks.get(stackNdx);
2496 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2497 didSomething = true;
2498 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002499 }
2500 }
2501 return didSomething;
2502 }
2503
Dianne Hackborna413dc02013-07-12 12:02:55 -07002504 void updatePreviousProcessLocked(ActivityRecord r) {
2505 // Now that this process has stopped, we may want to consider
2506 // it to be the previous app to try to keep around in case
2507 // the user wants to return to it.
2508
2509 // First, found out what is currently the foreground app, so that
2510 // we don't blow away the previous app if this activity is being
2511 // hosted by the process that is actually still the foreground.
2512 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002513 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2514 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002515 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2516 final ActivityStack stack = stacks.get(stackNdx);
2517 if (isFrontStack(stack)) {
2518 if (stack.mResumedActivity != null) {
2519 fgApp = stack.mResumedActivity.app;
2520 } else if (stack.mPausingActivity != null) {
2521 fgApp = stack.mPausingActivity.app;
2522 }
2523 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002524 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002525 }
2526 }
2527
2528 // Now set this one as the previous process, only if that really
2529 // makes sense to.
2530 if (r.app != null && fgApp != null && r.app != fgApp
2531 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002532 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002533 mService.mPreviousProcess = r.app;
2534 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2535 }
2536 }
2537
Craig Mautner05d29032013-05-03 13:40:13 -07002538 boolean resumeTopActivitiesLocked() {
2539 return resumeTopActivitiesLocked(null, null, null);
2540 }
2541
2542 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2543 Bundle targetOptions) {
2544 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002545 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002546 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002547 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002548 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002549 if (isFrontStack(targetStack)) {
2550 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2551 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002552
Craig Mautnere0a38842013-12-16 16:14:02 -08002553 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2554 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002555 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2556 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002557 if (stack == targetStack) {
2558 // Already started above.
2559 continue;
2560 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002561 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002562 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002563 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002564 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002565 }
Craig Mautner05d29032013-05-03 13:40:13 -07002566 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002567 }
2568
Todd Kennedy539db512014-12-15 09:57:55 -08002569 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002570 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2571 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002572 final int numStacks = stacks.size();
2573 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2574 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002575 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002576 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002577 }
2578 }
2579
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002580 void finishVoiceTask(IVoiceInteractionSession session) {
2581 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2582 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2583 final int numStacks = stacks.size();
2584 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2585 final ActivityStack stack = stacks.get(stackNdx);
2586 stack.finishVoiceTask(session);
2587 }
2588 }
2589 }
2590
Craig Mautner299f9602015-01-26 09:47:33 -08002591 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002592 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2593 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002594 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002595 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2596 // Caller wants the home activity moved with it. To accomplish this,
2597 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002598 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002599 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002600 if (task.stack == null) {
2601 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2602 + task + " to front. Stack is null");
2603 return;
2604 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002605 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002606 if (DEBUG_STACK) Slog.d(TAG_STACK,
2607 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002608 }
2609
Craig Mautner967212c2013-04-13 21:10:58 -07002610 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002611 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2612 if (activityContainer != null) {
2613 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002614 }
2615 return null;
2616 }
2617
Craig Mautner967212c2013-04-13 21:10:58 -07002618 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002619 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002620 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2621 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002622 }
2623 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002624 }
2625
Craig Mautner4a1cb222013-12-04 16:14:06 -08002626 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002627 ActivityRecord homeActivity = getHomeActivity();
2628 if (homeActivity != null) {
2629 return homeActivity.appToken;
2630 }
2631 return null;
2632 }
2633
2634 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002635 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2636 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2637 final TaskRecord task = tasks.get(taskNdx);
2638 if (task.isHomeTask()) {
2639 final ArrayList<ActivityRecord> activities = task.mActivities;
2640 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2641 final ActivityRecord r = activities.get(activityNdx);
2642 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002643 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002644 }
2645 }
2646 }
2647 }
2648 return null;
2649 }
2650
Todd Kennedyca4d8422015-01-15 15:19:22 -08002651 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002652 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002653 ActivityContainer activityContainer =
2654 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002655 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002656 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002657 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002658 return activityContainer;
2659 }
2660
Craig Mautner34b73df2014-01-12 21:11:08 -08002661 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002662 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2663 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2664 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002665 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2666 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002667 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002668 }
2669 }
2670
Craig Mautner95da1082014-02-24 17:54:35 -08002671 void deleteActivityContainer(IActivityContainer container) {
2672 ActivityContainer activityContainer = (ActivityContainer)container;
2673 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002674 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2675 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002676 final int stackId = activityContainer.mStackId;
2677 mActivityContainers.remove(stackId);
2678 mWindowManager.removeStack(stackId);
2679 }
2680 }
2681
Wale Ogunwale60454db2015-01-23 16:05:07 -08002682 void resizeStackLocked(int stackId, Rect bounds) {
2683 final ActivityStack stack = getStack(stackId);
2684 if (stack == null) {
2685 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2686 return;
2687 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002688
2689 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002690 if (r != null && !r.task.mResizeable) {
2691 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002692 return;
2693 }
2694
Wale Ogunwale60454db2015-01-23 16:05:07 -08002695 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2696 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002697 if (r != null) {
2698 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2699 // And we need to make sure at this point that all other activities
2700 // are made visible with the correct configuration.
2701 ensureActivitiesVisibleLocked(r, 0);
2702 if (!updated) {
2703 resumeTopActivitiesLocked(stack, null, null);
2704 }
2705 }
2706 }
2707 }
2708
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002709 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2710 * current task stack if it only has one entry, moving the task to a stack that matches the
2711 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2712 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2713 task.mResizeable = true;
2714 final ActivityStack currentStack = task.stack;
2715 if (currentStack.isHomeStack()) {
2716 // Can't move task off the home stack. Sorry!
2717 return;
2718 }
2719
2720 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2721 if (matchingStackId != -1) {
2722 // There is already a stack with the right bounds!
2723 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2724 // Nothing to do here. Already in the right stack...
2725 return;
2726 }
2727 // Move task to stack with matching bounds.
2728 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2729 return;
2730 }
2731
2732 if (currentStack != null && currentStack.numTasks() == 1) {
2733 // Just resize the current stack since this is the task in it.
2734 resizeStackLocked(currentStack.mStackId, bounds);
2735 return;
2736 }
2737
2738 // Create new stack and move the task to it.
2739 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2740 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2741 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2742
2743 if (newStack == null) {
2744 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2745 return;
2746 }
2747 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2748 resizeStackLocked(newStack.mStackId, bounds);
2749 }
2750
Todd Kennedy4900bf92015-01-16 16:05:14 -08002751 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002752 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2753 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002754 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002755 }
2756
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002757 ActivityContainer activityContainer = new ActivityContainer(stackId);
2758 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002759 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002760 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002761 }
2762
2763 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002764 while (true) {
2765 if (++mLastStackId <= HOME_STACK_ID) {
2766 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002767 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002768 if (getStack(mLastStackId) == null) {
2769 break;
2770 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002771 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002772 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002773 }
2774
Wale Ogunwale7de05352014-12-12 15:21:33 -08002775 private boolean restoreRecentTaskLocked(TaskRecord task) {
2776 ActivityStack stack = null;
2777 // Determine stack to restore task to.
2778 if (mLeanbackOnlyDevice) {
2779 // There is only one stack for lean back devices.
2780 stack = mHomeStack;
2781 } else {
2782 // Look for the top stack on the home display that isn't the home stack.
2783 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2784 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2785 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002786 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002787 stack = tmpStack;
2788 break;
2789 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002790 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002791 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002792
2793 if (stack == null) {
2794 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2795 // before an application stack is created...Go ahead and create one on the default
2796 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002797 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002798 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002799 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002800 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2801 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002802 }
2803
2804 if (stack == null) {
2805 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002806 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2807 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002808 return false;
2809 }
2810
2811 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002812 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2813 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002814 final ArrayList<ActivityRecord> activities = task.mActivities;
2815 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2816 final ActivityRecord r = activities.get(activityNdx);
2817 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2818 r.info.screenOrientation, r.fullscreen,
2819 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2820 r.userId, r.info.configChanges, task.voiceSession != null,
2821 r.mLaunchTaskBehind);
2822 }
2823 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002824 }
2825
Craig Mautner299f9602015-01-26 09:47:33 -08002826 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002827 final TaskRecord task = anyTaskForIdLocked(taskId);
2828 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002829 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002830 return;
2831 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002832 final ActivityStack stack = getStack(stackId);
2833 if (stack == null) {
2834 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2835 return;
2836 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002837 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2838 if (task.stack != null) {
2839 task.stack.removeTask(task, "moveTaskToStack", false);
2840 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002841 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002842 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002843 }
2844
Craig Mautnerac6f8432013-07-17 13:24:59 -07002845 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002846 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002847 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2848 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002849 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2850 final ActivityStack stack = stacks.get(stackNdx);
2851 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002852 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002853 continue;
2854 }
2855 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002856 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2857 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002858 continue;
2859 }
2860 final ActivityRecord ar = stack.findTaskLocked(r);
2861 if (ar != null) {
2862 return ar;
2863 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002864 }
2865 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002866 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002867 return null;
2868 }
2869
2870 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002871 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2872 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002873 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2874 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2875 if (ar != null) {
2876 return ar;
2877 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002878 }
2879 }
2880 return null;
2881 }
2882
Craig Mautner8d341ef2013-03-26 09:03:27 -07002883 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002884 scheduleSleepTimeout();
2885 if (!mGoingToSleep.isHeld()) {
2886 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002887 if (mLaunchingActivity.isHeld()) {
2888 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2889 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002890 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002891 mLaunchingActivity.release();
2892 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002893 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002894 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002895 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002896 }
2897
2898 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002899 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002900
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002901 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002902 final long endTime = System.currentTimeMillis() + timeout;
2903 while (true) {
2904 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002905 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2906 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002907 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2908 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2909 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002910 }
2911 if (cantShutdown) {
2912 long timeRemaining = endTime - System.currentTimeMillis();
2913 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002914 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002915 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002916 } catch (InterruptedException e) {
2917 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002918 } else {
2919 Slog.w(TAG, "Activity manager shutdown timed out");
2920 timedout = true;
2921 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002922 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002923 } else {
2924 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002925 }
2926 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002927
2928 // Force checkReadyForSleep to complete.
2929 mSleepTimeout = true;
2930 checkReadyForSleepLocked();
2931
Craig Mautner8d341ef2013-03-26 09:03:27 -07002932 return timedout;
2933 }
2934
2935 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002936 removeSleepTimeouts();
2937 if (mGoingToSleep.isHeld()) {
2938 mGoingToSleep.release();
2939 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002940 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2941 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002942 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2943 final ActivityStack stack = stacks.get(stackNdx);
2944 stack.awakeFromSleepingLocked();
2945 if (isFrontStack(stack)) {
2946 resumeTopActivitiesLocked();
2947 }
Craig Mautner5314a402013-09-26 12:40:16 -07002948 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002949 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002950 mGoingToSleepActivities.clear();
2951 }
2952
2953 void activitySleptLocked(ActivityRecord r) {
2954 mGoingToSleepActivities.remove(r);
2955 checkReadyForSleepLocked();
2956 }
2957
2958 void checkReadyForSleepLocked() {
2959 if (!mService.isSleepingOrShuttingDown()) {
2960 // Do not care.
2961 return;
2962 }
2963
2964 if (!mSleepTimeout) {
2965 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002966 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2967 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002968 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2969 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2970 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002971 }
2972
2973 if (mStoppingActivities.size() > 0) {
2974 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002975 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002976 + mStoppingActivities.size() + " activities");
2977 scheduleIdleLocked();
2978 dontSleep = true;
2979 }
2980
2981 if (mGoingToSleepActivities.size() > 0) {
2982 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002983 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002984 + mGoingToSleepActivities.size() + " activities");
2985 dontSleep = true;
2986 }
2987
2988 if (dontSleep) {
2989 return;
2990 }
2991 }
2992
Craig Mautnere0a38842013-12-16 16:14:02 -08002993 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2994 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002995 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2996 stacks.get(stackNdx).goToSleep();
2997 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002998 }
2999
3000 removeSleepTimeouts();
3001
3002 if (mGoingToSleep.isHeld()) {
3003 mGoingToSleep.release();
3004 }
3005 if (mService.mShuttingDown) {
3006 mService.notifyAll();
3007 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003008 }
3009
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003010 boolean reportResumedActivityLocked(ActivityRecord r) {
3011 final ActivityStack stack = r.task.stack;
3012 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003013 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003014 }
3015 if (allResumedActivitiesComplete()) {
3016 ensureActivitiesVisibleLocked(null, 0);
3017 mWindowManager.executeAppTransition();
3018 return true;
3019 }
3020 return false;
3021 }
3022
Craig Mautner8d341ef2013-03-26 09:03:27 -07003023 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003024 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3025 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003026 final int numStacks = stacks.size();
3027 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3028 final ActivityStack stack = stacks.get(stackNdx);
3029 stack.handleAppCrashLocked(app);
3030 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003031 }
3032 }
3033
Jose Lima4b6c6692014-08-12 17:41:12 -07003034 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003035 final ActivityStack stack = r.task.stack;
3036 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003037 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3038 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003039 return false;
3040 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003041 final boolean isVisible = stack.hasVisibleBehindActivity();
3042 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3043 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003044
3045 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003046 if (top == null || top == r || (visible == isVisible)) {
3047 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3048 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003049 return true;
3050 }
3051
3052 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003053 if (visible && top.fullscreen) {
3054 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003055 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003056 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003057 " top.app.thread=" + top.app.thread);
3058 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003059 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3060 // Only the activity set as currently visible behind should actively reset its
3061 // visible behind state.
3062 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3063 + visible + " stack.getVisibleBehindActivity()=" +
3064 stack.getVisibleBehindActivity() + " r=" + r);
3065 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003066 }
3067
Jose Lima4b6c6692014-08-12 17:41:12 -07003068 stack.setVisibleBehindActivity(visible ? r : null);
3069 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003070 // Make the activity immediately above r opaque.
3071 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3072 if (next != null) {
3073 mService.convertFromTranslucent(next.appToken);
3074 }
3075 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003076 if (top.app != null && top.app.thread != null) {
3077 // Notify the top app of the change.
3078 try {
3079 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3080 } catch (RemoteException e) {
3081 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003082 }
3083 return true;
3084 }
3085
Craig Mautnerbb742462014-07-07 15:28:55 -07003086 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3087 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3088 r.mLaunchTaskBehind = false;
3089 final TaskRecord task = r.task;
3090 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003091 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003092 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003093 mWindowManager.setAppVisibility(r.appToken, false);
3094 }
3095
3096 void scheduleLaunchTaskBehindComplete(IBinder token) {
3097 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3098 }
3099
Craig Mautnerde4ef022013-04-07 19:01:33 -07003100 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003101 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003102 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3103 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003104 final int topStackNdx = stacks.size() - 1;
3105 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3106 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003107 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003108 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003109 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003110 }
3111
3112 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003113 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3114 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003115 final int numStacks = stacks.size();
3116 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3117 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003118 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003119 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003120 }
3121 }
3122
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003123 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3124 // Examine all activities currently running in the process.
3125 TaskRecord firstTask = null;
3126 // Tasks is non-null only if two or more tasks are found.
3127 ArraySet<TaskRecord> tasks = null;
3128 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3129 for (int i=0; i<app.activities.size(); i++) {
3130 ActivityRecord r = app.activities.get(i);
3131 // First, if we find an activity that is in the process of being destroyed,
3132 // then we just aren't going to do anything for now; we want things to settle
3133 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003134 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003135 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3136 return;
3137 }
3138 // Don't consider any activies that are currently not in a state where they
3139 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003140 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3141 || r.state == PAUSED || r.state == STOPPING) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003142 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3143 continue;
3144 }
3145 if (r.task != null) {
3146 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3147 + " from " + r);
3148 if (firstTask == null) {
3149 firstTask = r.task;
3150 } else if (firstTask != r.task) {
3151 if (tasks == null) {
3152 tasks = new ArraySet<>();
3153 tasks.add(firstTask);
3154 }
3155 tasks.add(r.task);
3156 }
3157 }
3158 }
3159 if (tasks == null) {
3160 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3161 return;
3162 }
3163 // If we have activities in multiple tasks that are in a position to be destroyed,
3164 // let's iterate through the tasks and release the oldest one.
3165 final int numDisplays = mActivityDisplays.size();
3166 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3167 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3168 // Step through all stacks starting from behind, to hit the oldest things first.
3169 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3170 final ActivityStack stack = stacks.get(stackNdx);
3171 // Try to release activities in this stack; if we manage to, we are done.
3172 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3173 return;
3174 }
3175 }
3176 }
3177 }
3178
Craig Mautner8d341ef2013-03-26 09:03:27 -07003179 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003180 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003181 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003182 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003183
Craig Mautner858d8a62013-04-23 17:08:34 -07003184 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003185 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3186 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003187 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003188 final ActivityStack stack = stacks.get(stackNdx);
3189 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003190 TaskRecord task = stack.topTask();
3191 if (task != null) {
3192 mWindowManager.moveTaskToTop(task.taskId);
3193 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003194 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003195 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003196
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003197 ActivityStack stack = getStack(restoreStackId);
3198 if (stack == null) {
3199 stack = mHomeStack;
3200 }
3201 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003202 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003203 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003204 TaskRecord task = stack.topTask();
3205 if (task != null) {
3206 mWindowManager.moveTaskToTop(task.taskId);
3207 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003208 } else {
3209 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003210 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003211 }
Craig Mautner93529a42013-10-04 15:03:13 -07003212 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003213 }
3214
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003215 /**
3216 * Add background users to send boot completed events to.
3217 * @param userId The user being started in the background
3218 * @param uss The state object for the user.
3219 */
3220 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3221 mStartingBackgroundUsers.add(uss);
3222 }
3223
Craig Mautnerde4ef022013-04-07 19:01:33 -07003224 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003225 ArrayList<ActivityRecord> stops = null;
3226
3227 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003228 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3229 ActivityRecord s = mStoppingActivities.get(activityNdx);
3230 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003231 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003232 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3233 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003234 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003235 if (s.finishing) {
3236 // If this activity is finishing, it is sitting on top of
3237 // everyone else but we now know it is no longer needed...
3238 // so get rid of it. Otherwise, we need to go through the
3239 // normal flow and hide it once we determine that it is
3240 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003241 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003242 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003243 }
3244 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003245 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003246 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003247 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003248 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003249 }
3250 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003251 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003252 }
3253 }
3254
3255 return stops;
3256 }
3257
Craig Mautnercf910b02013-04-23 11:23:27 -07003258 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003259 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3260 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3261 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3262 final ActivityStack stack = stacks.get(stackNdx);
3263 final ActivityRecord r = stack.topRunningActivityLocked(null);
3264 final ActivityState state = r == null ? DESTROYED : r.state;
3265 if (isFrontStack(stack)) {
3266 if (r == null) Slog.e(TAG,
3267 "validateTop...: null top activity, stack=" + stack);
3268 else {
3269 final ActivityRecord pausing = stack.mPausingActivity;
3270 if (pausing != null && pausing == r) Slog.e(TAG,
3271 "validateTop...: top stack has pausing activity r=" + r
3272 + " state=" + state);
3273 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3274 "validateTop...: activity in front not resumed r=" + r
3275 + " state=" + state);
3276 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003277 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003278 final ActivityRecord resumed = stack.mResumedActivity;
3279 if (resumed != null && resumed == r) Slog.e(TAG,
3280 "validateTop...: back stack has resumed activity r=" + r
3281 + " state=" + state);
3282 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3283 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003284 }
3285 }
3286 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003287 }
3288
Craig Mautner27084302013-03-25 08:05:25 -07003289 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003290 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003291 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003292 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3293 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3294 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003295 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003296 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003297
Craig Mautner20e72272013-04-01 13:45:53 -07003298 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003299 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003300 }
3301
Dianne Hackborn390517b2013-05-30 15:03:32 -07003302 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3303 boolean needSep, String prefix) {
3304 if (activity != null) {
3305 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3306 if (needSep) {
3307 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003308 }
3309 pw.print(prefix);
3310 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003311 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003312 }
3313 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003314 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003315 }
3316
Craig Mautner8d341ef2013-03-26 09:03:27 -07003317 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3318 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003319 boolean printed = false;
3320 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003321 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3322 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003323 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003324 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003325 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003326 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003327 final ActivityStack stack = stacks.get(stackNdx);
3328 StringBuilder stackHeader = new StringBuilder(128);
3329 stackHeader.append(" Stack #");
3330 stackHeader.append(stack.mStackId);
3331 stackHeader.append(":");
3332 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3333 needSep, stackHeader.toString());
3334 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3335 !dumpAll, false, dumpPackage, true,
3336 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003337
Craig Mautner4a1cb222013-12-04 16:14:06 -08003338 needSep = printed;
3339 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3340 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003341 if (pr) {
3342 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003343 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003344 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003345 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3346 " mResumedActivity: ");
3347 if (pr) {
3348 printed = true;
3349 needSep = false;
3350 }
3351 if (dumpAll) {
3352 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3353 " mLastPausedActivity: ");
3354 if (pr) {
3355 printed = true;
3356 needSep = true;
3357 }
3358 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3359 needSep, " mLastNoHistoryActivity: ");
3360 }
3361 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003362 }
3363 }
3364
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003365 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3366 false, dumpPackage, true, " Activities waiting to finish:", null);
3367 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3368 false, dumpPackage, true, " Activities waiting to stop:", null);
3369 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3370 false, dumpPackage, true, " Activities waiting for another to become visible:",
3371 null);
3372 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3373 false, dumpPackage, true, " Activities waiting to sleep:", null);
3374 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3375 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003376
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003377 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003378 }
3379
Dianne Hackborn390517b2013-05-30 15:03:32 -07003380 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003381 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003382 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003383 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003384 String innerPrefix = null;
3385 String[] args = null;
3386 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003387 for (int i=list.size()-1; i>=0; i--) {
3388 final ActivityRecord r = list.get(i);
3389 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3390 continue;
3391 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003392 if (innerPrefix == null) {
3393 innerPrefix = prefix + " ";
3394 args = new String[0];
3395 }
3396 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003397 final boolean full = !brief && (complete || !r.isInHistory());
3398 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003399 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003400 needNL = false;
3401 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003402 if (header1 != null) {
3403 pw.println(header1);
3404 header1 = null;
3405 }
3406 if (header2 != null) {
3407 pw.println(header2);
3408 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003409 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003410 if (lastTask != r.task) {
3411 lastTask = r.task;
3412 pw.print(prefix);
3413 pw.print(full ? "* " : " ");
3414 pw.println(lastTask);
3415 if (full) {
3416 lastTask.dump(pw, prefix + " ");
3417 } else if (complete) {
3418 // Complete + brief == give a summary. Isn't that obvious?!?
3419 if (lastTask.intent != null) {
3420 pw.print(prefix); pw.print(" ");
3421 pw.println(lastTask.intent.toInsecureStringWithClip());
3422 }
3423 }
3424 }
3425 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3426 pw.print(" #"); pw.print(i); pw.print(": ");
3427 pw.println(r);
3428 if (full) {
3429 r.dump(pw, innerPrefix);
3430 } else if (complete) {
3431 // Complete + brief == give a summary. Isn't that obvious?!?
3432 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3433 if (r.app != null) {
3434 pw.print(innerPrefix); pw.println(r.app);
3435 }
3436 }
3437 if (client && r.app != null && r.app.thread != null) {
3438 // flush anything that is already in the PrintWriter since the thread is going
3439 // to write to the file descriptor directly
3440 pw.flush();
3441 try {
3442 TransferPipe tp = new TransferPipe();
3443 try {
3444 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3445 r.appToken, innerPrefix, args);
3446 // Short timeout, since blocking here can
3447 // deadlock with the application.
3448 tp.go(fd, 2000);
3449 } finally {
3450 tp.kill();
3451 }
3452 } catch (IOException e) {
3453 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3454 } catch (RemoteException e) {
3455 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3456 }
3457 needNL = true;
3458 }
3459 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003460 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003461 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003462
Craig Mautnerf3333272013-04-22 10:55:53 -07003463 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003464 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003465 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3466 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003467 }
3468
3469 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003470 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003471 }
3472
3473 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003474 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003475 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3476 }
3477
Craig Mautner05d29032013-05-03 13:40:13 -07003478 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003479 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3480 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3481 }
Craig Mautner05d29032013-05-03 13:40:13 -07003482 }
3483
Craig Mautner0eea92c2013-05-16 13:35:39 -07003484 void removeSleepTimeouts() {
3485 mSleepTimeout = false;
3486 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3487 }
3488
3489 final void scheduleSleepTimeout() {
3490 removeSleepTimeouts();
3491 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3492 }
3493
Craig Mautner4a1cb222013-12-04 16:14:06 -08003494 @Override
3495 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003496 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003497 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3498 }
3499
3500 @Override
3501 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003502 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003503 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3504 }
3505
3506 @Override
3507 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003508 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003509 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3510 }
3511
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003512 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003513 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003514 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003515 newDisplay = mActivityDisplays.get(displayId) == null;
3516 if (newDisplay) {
3517 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003518 if (activityDisplay.mDisplay == null) {
3519 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3520 return;
3521 }
Craig Mautner4504de52013-12-20 09:06:56 -08003522 mActivityDisplays.put(displayId, activityDisplay);
3523 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003524 }
Craig Mautner4504de52013-12-20 09:06:56 -08003525 if (newDisplay) {
3526 mWindowManager.onDisplayAdded(displayId);
3527 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003528 }
3529
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003530 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003531 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003532 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3533 if (activityDisplay != null) {
3534 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003535 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003536 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003537 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003538 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003539 }
3540 }
3541 mWindowManager.onDisplayRemoved(displayId);
3542 }
3543
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003544 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003545 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003546 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3547 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003548 // TODO: Update the bounds.
3549 }
3550 }
3551 mWindowManager.onDisplayChanged(displayId);
3552 }
3553
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003554 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003555 StackInfo info = new StackInfo();
3556 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3557 info.displayId = Display.DEFAULT_DISPLAY;
3558 info.stackId = stack.mStackId;
3559
3560 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3561 final int numTasks = tasks.size();
3562 int[] taskIds = new int[numTasks];
3563 String[] taskNames = new String[numTasks];
3564 for (int i = 0; i < numTasks; ++i) {
3565 final TaskRecord task = tasks.get(i);
3566 taskIds[i] = task.taskId;
3567 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3568 : task.realActivity != null ? task.realActivity.flattenToString()
3569 : task.getTopActivity() != null ? task.getTopActivity().packageName
3570 : "unknown";
3571 }
3572 info.taskIds = taskIds;
3573 info.taskNames = taskNames;
3574 return info;
3575 }
3576
3577 StackInfo getStackInfoLocked(int stackId) {
3578 ActivityStack stack = getStack(stackId);
3579 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003580 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003581 }
3582 return null;
3583 }
3584
3585 ArrayList<StackInfo> getAllStackInfosLocked() {
3586 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003587 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3588 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003589 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003590 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003591 }
3592 }
3593 return list;
3594 }
3595
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003596 void showLockTaskToast() {
3597 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003598 }
3599
Benjamin Franz43261142015-02-11 15:59:44 +00003600 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003601 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003602 // Take out of lock task mode if necessary
3603 if (mLockTaskModeTask != null) {
3604 final Message lockTaskMsg = Message.obtain();
3605 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3606 lockTaskMsg.what = LOCK_TASK_END_MSG;
3607 mLockTaskModeTask = null;
3608 mHandler.sendMessage(lockTaskMsg);
3609 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003610 return;
3611 }
3612 if (isLockTaskModeViolation(task)) {
3613 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3614 return;
3615 }
3616 mLockTaskModeTask = task;
Craig Mautner299f9602015-01-26 09:47:33 -08003617 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003618 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003619
3620 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003621 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3622 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003623 lockTaskMsg.what = LOCK_TASK_START_MSG;
Benjamin Franz43261142015-02-11 15:59:44 +00003624 lockTaskMsg.arg2 = lockTaskModeState;
justinzhang5286d3f2014-05-12 17:06:01 -04003625 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003626 }
3627
3628 boolean isLockTaskModeViolation(TaskRecord task) {
3629 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3630 }
3631
3632 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3633 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003634 final Message lockTaskMsg = Message.obtain();
3635 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3636 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003637 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003638 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003639 }
3640 }
3641
Benjamin Franz43261142015-02-11 15:59:44 +00003642 int getLockTaskModeState() {
3643 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003644 }
3645
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003646 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003647
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003648 public ActivityStackSupervisorHandler(Looper looper) {
3649 super(looper);
3650 }
3651
Craig Mautnerf3333272013-04-22 10:55:53 -07003652 void activityIdleInternal(ActivityRecord r) {
3653 synchronized (mService) {
3654 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3655 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003656 }
3657
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003658 @Override
3659 public void handleMessage(Message msg) {
3660 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003661 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003662 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003663 if (mService.mDidDexOpt) {
3664 mService.mDidDexOpt = false;
3665 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3666 nmsg.obj = msg.obj;
3667 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3668 return;
3669 }
3670 // We don't at this point know if the activity is fullscreen,
3671 // so we need to be conservative and assume it isn't.
3672 activityIdleInternal((ActivityRecord)msg.obj);
3673 } break;
3674 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003675 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003676 activityIdleInternal((ActivityRecord)msg.obj);
3677 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003678 case RESUME_TOP_ACTIVITY_MSG: {
3679 synchronized (mService) {
3680 resumeTopActivitiesLocked();
3681 }
3682 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003683 case SLEEP_TIMEOUT_MSG: {
3684 synchronized (mService) {
3685 if (mService.isSleepingOrShuttingDown()) {
3686 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3687 mSleepTimeout = true;
3688 checkReadyForSleepLocked();
3689 }
3690 }
3691 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003692 case LAUNCH_TIMEOUT_MSG: {
3693 if (mService.mDidDexOpt) {
3694 mService.mDidDexOpt = false;
3695 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3696 return;
3697 }
3698 synchronized (mService) {
3699 if (mLaunchingActivity.isHeld()) {
3700 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3701 if (VALIDATE_WAKE_LOCK_CALLER
3702 && Binder.getCallingUid() != Process.myUid()) {
3703 throw new IllegalStateException("Calling must be system uid");
3704 }
3705 mLaunchingActivity.release();
3706 }
3707 }
3708 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003709 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003710 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003711 } break;
3712 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003713 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003714 } break;
3715 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003716 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003717 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003718 case CONTAINER_CALLBACK_VISIBILITY: {
3719 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003720 final IActivityContainerCallback callback = container.mCallback;
3721 if (callback != null) {
3722 try {
3723 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3724 } catch (RemoteException e) {
3725 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003726 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003727 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003728 case LOCK_TASK_START_MSG: {
3729 // When lock task starts, we disable the status bars.
3730 try {
Jason Monk62515be2014-05-21 16:06:19 -04003731 if (mLockTaskNotify == null) {
3732 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003733 }
Jason Monk62515be2014-05-21 16:06:19 -04003734 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003735 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003736 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003737 int flags = 0;
3738 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_LOCKED) {
3739 flags = StatusBarManager.DISABLE_MASK
3740 & (~StatusBarManager.DISABLE_BACK);
3741 } else if (mLockTaskModeState ==
3742 ActivityManager.LOCK_TASK_MODE_PINNED) {
3743 flags = StatusBarManager.DISABLE_MASK
3744 & (~StatusBarManager.DISABLE_BACK)
3745 & (~StatusBarManager.DISABLE_HOME)
3746 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003747 }
3748 getStatusBarService().disable(flags, mToken,
3749 mService.mContext.getPackageName());
3750 }
3751 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003752 if (getDevicePolicyManager() != null) {
3753 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003754 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003755 }
justinzhang5286d3f2014-05-12 17:06:01 -04003756 } catch (RemoteException ex) {
3757 throw new RuntimeException(ex);
3758 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003759 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003760 case LOCK_TASK_END_MSG: {
3761 // When lock task ends, we enable the status bars.
3762 try {
Jason Monk62515be2014-05-21 16:06:19 -04003763 if (getStatusBarService() != null) {
3764 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3765 mService.mContext.getPackageName());
3766 }
3767 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003768 if (getDevicePolicyManager() != null) {
3769 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3770 msg.arg1);
3771 }
Jason Monk62515be2014-05-21 16:06:19 -04003772 if (mLockTaskNotify == null) {
3773 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3774 }
3775 mLockTaskNotify.show(false);
3776 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003777 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003778 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003779 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Benjamin Franz43261142015-02-11 15:59:44 +00003780 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_PINNED &&
3781 shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003782 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003783 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003784 new LockPatternUtils(mService.mContext)
3785 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003786 }
3787 } catch (SettingNotFoundException e) {
3788 // No setting, don't lock.
3789 }
justinzhang5286d3f2014-05-12 17:06:01 -04003790 } catch (RemoteException ex) {
3791 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003792 } finally {
3793 mLockTaskModeState = ActivityManager.LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003794 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003795 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003796 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3797 final ActivityContainer container = (ActivityContainer) msg.obj;
3798 final IActivityContainerCallback callback = container.mCallback;
3799 if (callback != null) {
3800 try {
3801 callback.onAllActivitiesComplete(container.asBinder());
3802 } catch (RemoteException e) {
3803 }
3804 }
3805 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003806 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3807 synchronized (mService) {
3808 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3809 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003810 final ActivityContainer container = (ActivityContainer) msg.obj;
3811 container.mStack.finishAllActivitiesLocked(true);
3812 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003813 }
3814 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003815 case LAUNCH_TASK_BEHIND_COMPLETE: {
3816 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003817 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003818 if (r != null) {
3819 handleLaunchTaskBehindCompleteLocked(r);
3820 }
3821 }
3822 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003823 }
3824 }
3825 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003826
Ying Wangb081a592014-04-22 15:20:16 -07003827 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003828 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003829 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003830 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003831 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003832 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003833 ActivityRecord mParentActivity = null;
3834 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003835
Craig Mautnere3a00d72014-04-16 08:31:19 -07003836 boolean mVisible = true;
3837
Craig Mautner4a1cb222013-12-04 16:14:06 -08003838 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003839 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003841 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3842 final static int CONTAINER_STATE_NO_SURFACE = 1;
3843 final static int CONTAINER_STATE_FINISHING = 2;
3844 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3845
3846 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003847 synchronized (mService) {
3848 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003849 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003850 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003851 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003852 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003853 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003854
Craig Mautnere0a38842013-12-16 16:14:02 -08003855 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003856 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08003857 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003858 mActivityDisplay = activityDisplay;
3859 mStack.mDisplayId = activityDisplay.mDisplayId;
3860 mStack.mStacks = activityDisplay.mStacks;
3861
3862 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003863 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003864 }
3865
3866 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003867 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003868 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003869 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3870 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003871 return;
3872 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003873 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003874 }
3875 }
3876
3877 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003878 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003879 synchronized (mService) {
3880 if (mActivityDisplay != null) {
3881 return mActivityDisplay.mDisplayId;
3882 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003883 }
3884 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003885 }
3886
Jeff Brownca9bc702014-02-11 14:32:56 -08003887 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003888 public int getStackId() {
3889 synchronized (mService) {
3890 return mStackId;
3891 }
3892 }
3893
3894 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003895 public boolean injectEvent(InputEvent event) {
3896 final long origId = Binder.clearCallingIdentity();
3897 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003898 synchronized (mService) {
3899 if (mActivityDisplay != null) {
3900 return mInputManagerInternal.injectInputEvent(event,
3901 mActivityDisplay.mDisplayId,
3902 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3903 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003904 }
3905 return false;
3906 } finally {
3907 Binder.restoreCallingIdentity(origId);
3908 }
3909 }
3910
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003911 @Override
3912 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003913 synchronized (mService) {
3914 if (mContainerState == CONTAINER_STATE_FINISHING) {
3915 return;
3916 }
3917 mContainerState = CONTAINER_STATE_FINISHING;
3918
3919 final Message msg =
3920 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003921 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003922
3923 long origId = Binder.clearCallingIdentity();
3924 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003925 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003926 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003927 } finally {
3928 Binder.restoreCallingIdentity(origId);
3929 }
3930 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003931 }
3932
Craig Mautner60257702014-09-17 15:02:33 -07003933 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003934 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003935 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003936 if (mActivityDisplay != null) {
3937 mActivityDisplay.detachActivitiesLocked(mStack);
3938 mActivityDisplay = null;
3939 mStack.mDisplayId = -1;
3940 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003941 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003942 }
3943 }
3944
3945 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003946 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003947 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08003948 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003949 Binder.getCallingUid(), mCurrentUser, false,
3950 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003951
Craig Mautnere0a38842013-12-16 16:14:02 -08003952 // TODO: Switch to user app stacks here.
3953 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08003954 final Uri data = intent.getData();
3955 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
3956 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08003957 }
Craig Mautnerb9168362015-02-26 20:40:19 -08003958 checkEmbeddedAllowedInner(userId, intent, mimeType);
3959
3960 intent.addFlags(FORCE_NEW_TASK_FLAGS);
3961 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
3962 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003963 }
3964
3965 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003966 public final int startActivityIntentSender(IIntentSender intentSender)
3967 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003968 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3969
3970 if (!(intentSender instanceof PendingIntentRecord)) {
3971 throw new IllegalArgumentException("Bad PendingIntent object");
3972 }
3973
Craig Mautnerb9168362015-02-26 20:40:19 -08003974 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003975 Binder.getCallingUid(), mCurrentUser, false,
3976 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003977
3978 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3979 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3980 pendingIntent.key.requestResolvedType);
3981
3982 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3983 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3984 }
3985
3986 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003987 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003988 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003989 throw new SecurityException(
3990 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3991 }
3992 }
3993
Craig Mautnerdf88d732014-01-27 09:21:32 -08003994 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003995 public IBinder asBinder() {
3996 return this;
3997 }
3998
Craig Mautner4504de52013-12-20 09:06:56 -08003999 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004000 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004001 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004002 }
4003
Craig Mautner4a1cb222013-12-04 16:14:06 -08004004 ActivityStackSupervisor getOuter() {
4005 return ActivityStackSupervisor.this;
4006 }
4007
Craig Mautnerd163e752014-06-13 17:18:47 -07004008 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004009 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004010 }
4011
4012 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004013 synchronized (mService) {
4014 if (mActivityDisplay != null) {
4015 mActivityDisplay.getBounds(outBounds);
4016 } else {
4017 outBounds.set(0, 0);
4018 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004019 }
4020 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004021
Craig Mautner6985bad2014-04-21 15:22:06 -07004022 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004023 void setVisible(boolean visible) {
4024 if (mVisible != visible) {
4025 mVisible = visible;
4026 if (mCallback != null) {
4027 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4028 0 /* unused */, this).sendToTarget();
4029 }
4030 }
4031 }
4032
Craig Mautner6985bad2014-04-21 15:22:06 -07004033 void setDrawn() {
4034 }
4035
Craig Mautner1b4bf852014-05-26 15:06:32 -07004036 // You can always start a new task on a regular ActivityStack.
4037 boolean isEligibleForNewTasks() {
4038 return true;
4039 }
4040
Craig Mautnerd163e752014-06-13 17:18:47 -07004041 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004042 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
4043 detachLocked();
4044 deleteActivityContainer(this);
4045 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004046 }
4047
Craig Mautner34b73df2014-01-12 21:11:08 -08004048 @Override
4049 public String toString() {
4050 return mIdString + (mActivityDisplay == null ? "N" : "A");
4051 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004052 }
4053
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004054 private class VirtualActivityContainer extends ActivityContainer {
4055 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004056 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004057
4058 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4059 super(getNextStackId());
4060 mParentActivity = parent;
4061 mCallback = callback;
4062 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004063 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004064 }
4065
4066 @Override
4067 public void setSurface(Surface surface, int width, int height, int density) {
4068 super.setSurface(surface, width, height, density);
4069
4070 synchronized (mService) {
4071 final long origId = Binder.clearCallingIdentity();
4072 try {
4073 setSurfaceLocked(surface, width, height, density);
4074 } finally {
4075 Binder.restoreCallingIdentity(origId);
4076 }
4077 }
4078 }
4079
4080 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4081 if (mContainerState == CONTAINER_STATE_FINISHING) {
4082 return;
4083 }
4084 VirtualActivityDisplay virtualActivityDisplay =
4085 (VirtualActivityDisplay) mActivityDisplay;
4086 if (virtualActivityDisplay == null) {
4087 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004088 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004089 mActivityDisplay = virtualActivityDisplay;
4090 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4091 attachToDisplayLocked(virtualActivityDisplay);
4092 }
4093
4094 if (mSurface != null) {
4095 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004096 }
4097
Craig Mautner6985bad2014-04-21 15:22:06 -07004098 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004099 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004100 mStack.resumeTopActivityLocked(null);
4101 } else {
4102 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004103 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004104 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004105 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004106 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004107 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004108
Craig Mautnerd163e752014-06-13 17:18:47 -07004109 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004110
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004111 if (DEBUG_STACK) Slog.d(TAG_STACK,
4112 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004113 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004114
Craig Mautner6985bad2014-04-21 15:22:06 -07004115 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004116 boolean isAttachedLocked() {
4117 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004118 }
4119
4120 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004121 void setDrawn() {
4122 synchronized (mService) {
4123 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004124 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004125 }
4126 }
4127
Craig Mautner1b4bf852014-05-26 15:06:32 -07004128 // Never start a new task on an ActivityView if it isn't explicitly specified.
4129 @Override
4130 boolean isEligibleForNewTasks() {
4131 return false;
4132 }
4133
Craig Mautnerd163e752014-06-13 17:18:47 -07004134 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004135 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004136 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4137 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4138 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4139 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4140 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004141 }
4142 }
4143
Craig Mautner4a1cb222013-12-04 16:14:06 -08004144 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4145 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004146 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004147 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004148 int mDisplayId;
4149 Display mDisplay;
4150 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004151
Craig Mautner4a1cb222013-12-04 16:14:06 -08004152 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4153 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004154 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004155
Jose Lima4b6c6692014-08-12 17:41:12 -07004156 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004157
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004158 ActivityDisplay() {
4159 }
Craig Mautner4504de52013-12-20 09:06:56 -08004160
Craig Mautner1a70a162014-09-13 12:09:31 -07004161 // After instantiation, check that mDisplay is not null before using this. The alternative
4162 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004163 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004164 final Display display = mDisplayManager.getDisplay(displayId);
4165 if (display == null) {
4166 return;
4167 }
4168 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004169 }
4170
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004171 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004172 mDisplay = display;
4173 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004174 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004175 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004176
4177 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004178 if (DEBUG_STACK) Slog.v(TAG_STACK,
4179 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004180 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004181 }
4182
Craig Mautnere0a38842013-12-16 16:14:02 -08004183 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004184 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004185 + " from displayId=" + mDisplayId);
4186 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004187 }
4188
4189 void getBounds(Point bounds) {
4190 mDisplay.getDisplayInfo(mDisplayInfo);
4191 bounds.x = mDisplayInfo.appWidth;
4192 bounds.y = mDisplayInfo.appHeight;
4193 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004194
Jose Lima4b6c6692014-08-12 17:41:12 -07004195 void setVisibleBehindActivity(ActivityRecord r) {
4196 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004197 }
4198
Jose Lima4b6c6692014-08-12 17:41:12 -07004199 boolean hasVisibleBehindActivity() {
4200 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004201 }
4202
Craig Mautner34b73df2014-01-12 21:11:08 -08004203 @Override
4204 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004205 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4206 }
4207 }
4208
4209 class VirtualActivityDisplay extends ActivityDisplay {
4210 VirtualDisplay mVirtualDisplay;
4211
Craig Mautner6985bad2014-04-21 15:22:06 -07004212 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004213 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004214 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4215 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4216 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4217 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004218
4219 init(mVirtualDisplay.getDisplay());
4220
4221 mWindowManager.handleDisplayAdded(mDisplayId);
4222 }
4223
4224 void setSurface(Surface surface) {
4225 if (mVirtualDisplay != null) {
4226 mVirtualDisplay.setSurface(surface);
4227 }
4228 }
4229
4230 @Override
4231 void detachActivitiesLocked(ActivityStack stack) {
4232 super.detachActivitiesLocked(stack);
4233 if (mVirtualDisplay != null) {
4234 mVirtualDisplay.release();
4235 mVirtualDisplay = null;
4236 }
4237 }
4238
4239 @Override
4240 public String toString() {
4241 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004242 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004243 }
Jose Lima58e66d62014-05-27 20:00:27 -07004244
4245 private boolean isLeanbackOnlyDevice() {
4246 boolean onLeanbackOnly = false;
4247 try {
4248 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4249 PackageManager.FEATURE_LEANBACK_ONLY);
4250 } catch (RemoteException e) {
4251 // noop
4252 }
4253
4254 return onLeanbackOnly;
4255 }
Craig Mautner27084302013-03-25 08:05:25 -07004256}