blob: d08cddcf7f4697532c32bff8329dce809bf68dfd [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 Mautnereb0e38a2015-04-02 20:22:54 -0700133 static final boolean DEBUG_STATES = DEBUG || false;
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;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700159 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160
Craig Mautner4504de52013-12-20 09:06:56 -0800161 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700162
Jason Monk62515be2014-05-21 16:06:19 -0400163 private static final String LOCK_TASK_TAG = "Lock-to-App";
164
justinzhang5286d3f2014-05-12 17:06:01 -0400165 /** Status Bar Service **/
166 private IBinder mToken = new Binder();
167 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400168 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400169
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700170 // For debugging to make sure the caller when acquiring/releasing our
171 // wake lock is the system process.
172 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700173
Craig Mautner27084302013-03-25 08:05:25 -0700174 final ActivityManagerService mService;
175
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800176 private final RecentTasks mRecentTasks;
177
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700178 final ActivityStackSupervisorHandler mHandler;
179
180 /** Short cut */
181 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800182 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700183
Craig Mautner8d341ef2013-03-26 09:03:27 -0700184 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700185 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700186
187 /** Task identifier that activities are currently being started in. Incremented each time a
188 * new task is created. */
189 private int mCurTaskId = 0;
190
Craig Mautner2420ead2013-04-01 17:13:20 -0700191 /** The current user */
192 private int mCurrentUser;
193
Craig Mautnere0a38842013-12-16 16:14:02 -0800194 /** The stack containing the launcher app. Assumed to always be attached to
195 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700196 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700197
Craig Mautnere0a38842013-12-16 16:14:02 -0800198 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700199 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700200
Craig Mautner4a1cb222013-12-04 16:14:06 -0800201 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
202 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800203 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800204 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700205
206 /** List of activities that are waiting for a new activity to become visible before completing
207 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800208 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700209
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700210 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800211 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700212
213 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800214 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700215
Craig Mautnerde4ef022013-04-07 19:01:33 -0700216 /** List of activities that are ready to be stopped, but waiting for the next activity to
217 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800218 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700219
Craig Mautnerf3333272013-04-22 10:55:53 -0700220 /** List of activities that are ready to be finished, but waiting for the previous activity to
221 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800222 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700223
Craig Mautner0eea92c2013-05-16 13:35:39 -0700224 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800225 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700226
Craig Mautnerf3333272013-04-22 10:55:53 -0700227 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800228 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700229
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700230 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800231 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700232
Craig Mautnerde4ef022013-04-07 19:01:33 -0700233 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
234 * is being brought in front of us. */
235 boolean mUserLeaving = false;
236
Craig Mautner0eea92c2013-05-16 13:35:39 -0700237 /** Set when we have taken too long waiting to go to sleep. */
238 boolean mSleepTimeout = false;
239
Jose Lima58e66d62014-05-27 20:00:27 -0700240 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
241 * setWindowManager is called. **/
242 private boolean mLeanbackOnlyDevice;
243
Craig Mautner0eea92c2013-05-16 13:35:39 -0700244 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700245 * We don't want to allow the device to go to sleep while in the process
246 * of launching an activity. This is primarily to allow alarm intent
247 * receivers to launch an activity and get that to run before the device
248 * goes back to sleep.
249 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700250 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700251
252 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700253 * Set when the system is going to sleep, until we have
254 * successfully paused the current activity and released our wake lock.
255 * At that point the system is allowed to actually sleep.
256 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700257 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700258
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700259 /** Stack id of the front stack when user switched, indexed by userId. */
260 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700261
Craig Mautner4504de52013-12-20 09:06:56 -0800262 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800263 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700264 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800265
266 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700267 private final SparseArray<ActivityDisplay> mActivityDisplays =
268 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800269
Jeff Brownca9bc702014-02-11 14:32:56 -0800270 InputManagerInternal mInputManagerInternal;
271
Craig Mautneraea74a52014-03-08 14:23:10 -0800272 /** If non-null then the task specified remains in front and no other tasks may be started
273 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400274 TaskRecord mLockTaskModeTask;
Benjamin Franz43261142015-02-11 15:59:44 +0000275 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700276 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
277 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000278 */
279 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400280 /**
281 * Notifies the user when entering/exiting lock-task.
282 */
283 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800284
Craig Mautneree36c772014-07-16 14:56:05 -0700285 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
286 = new ArrayList<PendingActivityLaunch>();
287
Craig Mautner42d04db2014-11-06 12:13:23 -0800288 /** Used to keep resumeTopActivityLocked() from being entered recursively */
289 boolean inResumeTopActivity;
290
Craig Mautneree36c772014-07-16 14:56:05 -0700291 /**
292 * Description of a request to start a new activity, which has been held
293 * due to app switches being disabled.
294 */
295 static class PendingActivityLaunch {
296 final ActivityRecord r;
297 final ActivityRecord sourceRecord;
298 final int startFlags;
299 final ActivityStack stack;
300
301 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
302 int _startFlags, ActivityStack _stack) {
303 r = _r;
304 sourceRecord = _sourceRecord;
305 startFlags = _startFlags;
306 stack = _stack;
307 }
308 }
309
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800310 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700311 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800312 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700313 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
314 }
315
316 /**
317 * At the time when the constructor runs, the power manager has not yet been
318 * initialized. So we initialize our wakelocks afterwards.
319 */
320 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800321 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700322 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700323 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700324 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700325 }
326
justinzhang5286d3f2014-05-12 17:06:01 -0400327 // This function returns a IStatusBarService. The value is from ServiceManager.
328 // getService and is cached.
329 private IStatusBarService getStatusBarService() {
330 synchronized (mService) {
331 if (mStatusBarService == null) {
332 mStatusBarService = IStatusBarService.Stub.asInterface(
333 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
334 if (mStatusBarService == null) {
335 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
336 }
337 }
338 return mStatusBarService;
339 }
340 }
341
Jason Monk35c62a42014-06-17 10:24:47 -0400342 private IDevicePolicyManager getDevicePolicyManager() {
343 synchronized (mService) {
344 if (mDevicePolicyManager == null) {
345 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
346 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
347 if (mDevicePolicyManager == null) {
348 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
349 }
350 }
351 return mDevicePolicyManager;
352 }
353 }
354
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700355 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356 synchronized (mService) {
357 mWindowManager = wm;
358
359 mDisplayManager =
360 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
361 mDisplayManager.registerDisplayListener(this, null);
362
363 Display[] displays = mDisplayManager.getDisplays();
364 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
365 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800366 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700367 if (activityDisplay.mDisplay == null) {
368 throw new IllegalStateException("Default Display does not exist");
369 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800370 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800371 }
372
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700373 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800374 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800375
376 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700377
378 // Initialize this here, now that we can get a valid reference to PackageManager.
379 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380 }
Craig Mautner27084302013-03-25 08:05:25 -0700381 }
382
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200383 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700384 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200385 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700386 }
387
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700388 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800389 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700390 }
391
Craig Mautnerde4ef022013-04-07 19:01:33 -0700392 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800393 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700394 }
395
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800396 /** Top of all visible stacks is/should always be equal to the focused stack.
397 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
398 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700399 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700400 if (stack == null) {
401 return false;
402 }
403
Craig Mautnerdf88d732014-01-27 09:21:32 -0800404 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
405 if (parent != null) {
406 stack = parent.task.stack;
407 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800408 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700409 }
410
Craig Mautner299f9602015-01-26 09:47:33 -0800411 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800412 moveHomeStack(toFront, reason, null);
413 }
414
415 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800416 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800417 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800418 if (topNdx <= 0) {
419 return;
420 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700421
422 // The home stack should either be at the top or bottom of the stack list.
423 if ((toFront && (stacks.get(topNdx) != mHomeStack))
424 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700425 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700426 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
427 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800428 stacks.remove(mHomeStack);
429 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700430 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800431
432 if (lastFocusedStack != null) {
433 mLastFocusedStack = lastFocusedStack;
434 }
435 mFocusedStack = stacks.get(topNdx);
436
Craig Mautnerde313752015-01-22 14:28:03 -0800437 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
438 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800439 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800440
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800441 if (mService.mBooting || !mService.mBooted) {
442 final ActivityRecord r = topRunningActivityLocked();
443 if (r != null && r.idle) {
444 checkFinishBootingLocked();
445 }
446 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700447 }
448
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700449 /** Returns true if the focus activity was adjusted to the home stack top activity. */
450 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700451 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
452 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700453 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700454 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700455
Craig Mautner84984fa2014-06-19 11:19:20 -0700456 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700457
458 final ActivityRecord top = mHomeStack.topRunningActivityLocked(null);
459 if (top == null) {
460 return false;
461 }
462 mService.setFocusedActivityLocked(top, reason);
463 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700464 }
465
Craig Mautner299f9602015-01-26 09:47:33 -0800466 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700467 if (!mService.mBooting && !mService.mBooted) {
468 // Not ready yet!
469 return false;
470 }
471
Craig Mautner84984fa2014-06-19 11:19:20 -0700472 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
473 mWindowManager.showRecentApps();
474 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700475 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700476
Craig Mautner84984fa2014-06-19 11:19:20 -0700477 if (prev != null) {
478 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
479 }
480
Craig Mautnera8a90e02013-06-28 15:24:50 -0700481 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700482 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800483 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700484 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700485 }
Craig Mautner299f9602015-01-26 09:47:33 -0800486 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700487 }
488
Craig Mautner8d341ef2013-03-26 09:03:27 -0700489 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700490 return anyTaskForIdLocked(id, true);
491 }
492
493 /**
494 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
495 * @param id Id of the task we would like returned.
496 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
497 * restore the task from recents to the active list.
498 */
499 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800500 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800501 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800502 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800503 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
504 ActivityStack stack = stacks.get(stackNdx);
505 TaskRecord task = stack.taskForIdLocked(id);
506 if (task != null) {
507 return task;
508 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700509 }
510 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800511
512 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700513 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800514 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800515 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700516 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800517 return null;
518 }
519
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700520 if (!restoreFromRecents) {
521 return task;
522 }
523
Wale Ogunwale7de05352014-12-12 15:21:33 -0800524 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700525 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
526 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800527 return null;
528 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700529 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800530 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700531 }
532
Craig Mautner6170f732013-04-02 13:05:23 -0700533 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800534 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800535 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800536 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800537 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
538 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
539 if (r != null) {
540 return r;
541 }
Craig Mautner6170f732013-04-02 13:05:23 -0700542 }
543 }
544 return null;
545 }
546
Craig Mautneref73ee12014-04-23 11:45:37 -0700547 void setNextTaskId(int taskId) {
548 if (taskId > mCurTaskId) {
549 mCurTaskId = taskId;
550 }
551 }
552
Craig Mautner8d341ef2013-03-26 09:03:27 -0700553 int getNextTaskId() {
554 do {
555 mCurTaskId++;
556 if (mCurTaskId <= 0) {
557 mCurTaskId = 1;
558 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700559 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700560 return mCurTaskId;
561 }
562
Craig Mautnerde4ef022013-04-07 19:01:33 -0700563 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800564 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700565 if (stack == null) {
566 return null;
567 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700568 ActivityRecord resumedActivity = stack.mResumedActivity;
569 if (resumedActivity == null || resumedActivity.app == null) {
570 resumedActivity = stack.mPausingActivity;
571 if (resumedActivity == null || resumedActivity.app == null) {
572 resumedActivity = stack.topRunningActivityLocked(null);
573 }
574 }
575 return resumedActivity;
576 }
577
Dianne Hackbornff072722014-09-24 10:56:28 -0700578 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700579 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800580 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800581 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
582 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800583 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
584 final ActivityStack stack = stacks.get(stackNdx);
585 if (!isFrontStack(stack)) {
586 continue;
587 }
588 ActivityRecord hr = stack.topRunningActivityLocked(null);
589 if (hr != null) {
590 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
591 && processName.equals(hr.processName)) {
592 try {
George Mount2c92c972014-03-20 09:38:23 -0700593 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 didSomething = true;
595 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700596 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597 Slog.w(TAG, "Exception in new application when starting activity "
598 + hr.intent.getComponent().flattenToShortString(), e);
599 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700600 }
Craig Mautner20e72272013-04-01 13:45:53 -0700601 }
Craig Mautner20e72272013-04-01 13:45:53 -0700602 }
603 }
604 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700605 if (!didSomething) {
606 ensureActivitiesVisibleLocked(null, 0);
607 }
Craig Mautner20e72272013-04-01 13:45:53 -0700608 return didSomething;
609 }
610
611 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800612 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
613 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800614 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
615 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800616 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800617 continue;
618 }
619 final ActivityRecord resumedActivity = stack.mResumedActivity;
620 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800621 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
622 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800623 return false;
624 }
Craig Mautner20e72272013-04-01 13:45:53 -0700625 }
626 }
627 return true;
628 }
629
Craig Mautnerde4ef022013-04-07 19:01:33 -0700630 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800631 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
632 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800633 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
634 final ActivityStack stack = stacks.get(stackNdx);
635 if (isFrontStack(stack)) {
636 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700637 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800638 return false;
639 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700640 }
641 }
642 }
643 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800645 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
646 mLastFocusedStack + " to=" + mFocusedStack);
647 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700648 return true;
649 }
650
651 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800652 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
654 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
656 final ActivityStack stack = stacks.get(stackNdx);
657 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800658 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700659 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800660 return false;
661 }
662 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700664 }
665 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800666 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700667 }
668
Craig Mautner2acc3892013-09-23 10:28:14 -0700669 /**
670 * Pause all activities in either all of the stacks or just the back stacks.
671 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700672 * @return true if any activity was paused as a result of this call.
673 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700674 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700675 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800676 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
677 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
679 final ActivityStack stack = stacks.get(stackNdx);
680 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
681 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
682 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700683 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
684 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700686 }
687 }
688 return someActivityPaused;
689 }
690
Craig Mautnerde4ef022013-04-07 19:01:33 -0700691 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700692 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800693 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
694 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800695 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
696 final ActivityStack stack = stacks.get(stackNdx);
697 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700698 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800699 if (DEBUG_STATES) {
700 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
701 pausing = false;
702 } else {
703 return false;
704 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700705 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700706 }
707 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700708 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700709 }
710
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700711 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
712 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500713 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800714 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
715 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
716 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
717 final ActivityStack stack = stacks.get(stackNdx);
718 if (stack.mResumedActivity != null &&
719 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700720 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800721 }
722 }
723 }
724 }
725
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700726 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700727 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700728 }
729
730 void sendWaitingVisibleReportLocked(ActivityRecord r) {
731 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700732 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700733 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700734 if (w.who == null) {
735 changed = true;
736 w.timeout = false;
737 if (r != null) {
738 w.who = new ComponentName(r.info.packageName, r.info.name);
739 }
740 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
741 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700742 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700743 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700744 if (changed) {
745 mService.notifyAll();
746 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700747 }
748
749 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
750 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700751 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700752 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700753 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700754 if (w.who == null) {
755 changed = true;
756 w.timeout = timeout;
757 if (r != null) {
758 w.who = new ComponentName(r.info.packageName, r.info.name);
759 }
760 w.thisTime = thisTime;
761 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700762 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700763 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700764 if (changed) {
765 mService.notifyAll();
766 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700767 }
768
Craig Mautner29219d92013-04-16 20:19:12 -0700769 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800770 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700771 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
772 if (r != null) {
773 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700774 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700775
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800777 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800778 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
779 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700780 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700781 r = stack.topRunningActivityLocked(null);
782 if (r != null) {
783 return r;
784 }
785 }
786 }
787 return null;
788 }
789
Dianne Hackborn09233282014-04-30 11:33:59 -0700790 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700791 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800792 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
793 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800794 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800795 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800796 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800797 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
798 final ActivityStack stack = stacks.get(stackNdx);
799 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
800 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700801 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700802 }
803 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700804
805 // The lists are already sorted from most recent to oldest. Just pull the most recent off
806 // each list and add it to list. Stop when all lists are empty or maxNum reached.
807 while (maxNum > 0) {
808 long mostRecentActiveTime = Long.MIN_VALUE;
809 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800810 final int numTaskLists = runningTaskLists.size();
811 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
812 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700813 if (!stackTaskList.isEmpty()) {
814 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
815 if (lastActiveTime > mostRecentActiveTime) {
816 mostRecentActiveTime = lastActiveTime;
817 selectedStackList = stackTaskList;
818 }
819 }
820 }
821 if (selectedStackList != null) {
822 list.add(selectedStackList.remove(0));
823 --maxNum;
824 } else {
825 break;
826 }
827 }
Craig Mautner20e72272013-04-01 13:45:53 -0700828 }
829
Craig Mautner23ac33b2013-04-01 16:26:35 -0700830 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700831 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700832 // Collect information about the target of the Intent.
833 ActivityInfo aInfo;
834 try {
835 ResolveInfo rInfo =
836 AppGlobals.getPackageManager().resolveIntent(
837 intent, resolvedType,
838 PackageManager.MATCH_DEFAULT_ONLY
839 | ActivityManagerService.STOCK_PM_FLAGS, userId);
840 aInfo = rInfo != null ? rInfo.activityInfo : null;
841 } catch (RemoteException e) {
842 aInfo = null;
843 }
844
845 if (aInfo != null) {
846 // Store the found target back into the intent, because now that
847 // we have it we never want to do this again. For example, if the
848 // user navigates back to this point in the history, we should
849 // always restart the exact same activity.
850 intent.setComponent(new ComponentName(
851 aInfo.applicationInfo.packageName, aInfo.name));
852
853 // Don't debug things in the system process
854 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
855 if (!aInfo.processName.equals("system")) {
856 mService.setDebugApp(aInfo.processName, true, false);
857 }
858 }
859
860 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
861 if (!aInfo.processName.equals("system")) {
862 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
863 }
864 }
865
Jeff Hao1b012d32014-08-20 10:35:34 -0700866 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700867 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700868 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700869 }
870 }
871 }
872 return aInfo;
873 }
874
Craig Mautner299f9602015-01-26 09:47:33 -0800875 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
876 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700877 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
878 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700879 }
880
Craig Mautner23ac33b2013-04-01 16:26:35 -0700881 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700882 String callingPackage, Intent intent, String resolvedType,
883 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700884 IBinder resultTo, String resultWho, int requestCode, int startFlags,
885 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700886 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700887 // Refuse possible leaked file descriptors
888 if (intent != null && intent.hasFileDescriptors()) {
889 throw new IllegalArgumentException("File descriptors passed in Intent");
890 }
891 boolean componentSpecified = intent.getComponent() != null;
892
893 // Don't modify the client's object!
894 intent = new Intent(intent);
895
896 // Collect information about the target of the Intent.
897 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700898 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700899
Craig Mautnere0a38842013-12-16 16:14:02 -0800900 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700901 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800902 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700903 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800904 // Cannot start a child activity if the parent is not resumed.
905 return ActivityManager.START_CANCELED;
906 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700907 final int realCallingPid = Binder.getCallingPid();
908 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700909 int callingPid;
910 if (callingUid >= 0) {
911 callingPid = -1;
912 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700913 callingPid = realCallingPid;
914 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700915 } else {
916 callingPid = callingUid = -1;
917 }
918
Craig Mautnere0a38842013-12-16 16:14:02 -0800919 final ActivityStack stack;
920 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800921 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800922 } else {
923 stack = container.mStack;
924 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800925 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700926 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700927 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700928
929 final long origId = Binder.clearCallingIdentity();
930
931 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800932 (aInfo.applicationInfo.privateFlags
933 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700934 // This may be a heavy-weight process! Check to see if we already
935 // have another, different heavy-weight process running.
936 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
937 if (mService.mHeavyWeightProcess != null &&
938 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
939 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700940 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700941 if (caller != null) {
942 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
943 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700944 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700945 } else {
946 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700947 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700948 + intent.toString());
949 ActivityOptions.abort(options);
950 return ActivityManager.START_PERMISSION_DENIED;
951 }
952 }
953
954 IIntentSender target = mService.getIntentSenderLocked(
955 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700956 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700957 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
958 | PendingIntent.FLAG_ONE_SHOT, null);
959
960 Intent newIntent = new Intent();
961 if (requestCode >= 0) {
962 // Caller is requesting a result.
963 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
964 }
965 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
966 new IntentSender(target));
967 if (mService.mHeavyWeightProcess.activities.size() > 0) {
968 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
969 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
970 hist.packageName);
971 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
972 hist.task.taskId);
973 }
974 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
975 aInfo.packageName);
976 newIntent.setFlags(intent.getFlags());
977 newIntent.setClassName("android",
978 HeavyWeightSwitcherActivity.class.getName());
979 intent = newIntent;
980 resolvedType = null;
981 caller = null;
982 callingUid = Binder.getCallingUid();
983 callingPid = Binder.getCallingPid();
984 componentSpecified = true;
985 try {
986 ResolveInfo rInfo =
987 AppGlobals.getPackageManager().resolveIntent(
988 intent, null,
989 PackageManager.MATCH_DEFAULT_ONLY
990 | ActivityManagerService.STOCK_PM_FLAGS, userId);
991 aInfo = rInfo != null ? rInfo.activityInfo : null;
992 aInfo = mService.getActivityInfoForUser(aInfo, userId);
993 } catch (RemoteException e) {
994 aInfo = null;
995 }
996 }
997 }
998 }
999
Dianne Hackborn91097de2014-04-04 18:02:06 -07001000 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1001 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001002 requestCode, callingPid, callingUid, callingPackage,
1003 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001004 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001005
Craig Mautner85c11a82014-07-17 12:38:18 -07001006 Binder.restoreCallingIdentity(origId);
1007
Craig Mautnerde4ef022013-04-07 19:01:33 -07001008 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001009 // If the caller also wants to switch to a new configuration,
1010 // do so now. This allows a clean switch, as we are waiting
1011 // for the current activity to pause (so we will not destroy
1012 // it), and have not yet started the next activity.
1013 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1014 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001015 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001016 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 "Updating to new configuration after starting activity.");
1018 mService.updateConfigurationLocked(config, null, false, false);
1019 }
1020
Craig Mautner23ac33b2013-04-01 16:26:35 -07001021 if (outResult != null) {
1022 outResult.result = res;
1023 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001024 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001025 do {
1026 try {
1027 mService.wait();
1028 } catch (InterruptedException e) {
1029 }
1030 } while (!outResult.timeout && outResult.who == null);
1031 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001032 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001033 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001034 outResult.timeout = false;
1035 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1036 outResult.totalTime = 0;
1037 outResult.thisTime = 0;
1038 } else {
1039 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001041 do {
1042 try {
1043 mService.wait();
1044 } catch (InterruptedException e) {
1045 }
1046 } while (!outResult.timeout && outResult.who == null);
1047 }
1048 }
1049 }
1050
1051 return res;
1052 }
1053 }
1054
1055 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1056 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1057 Bundle options, int userId) {
1058 if (intents == null) {
1059 throw new NullPointerException("intents is null");
1060 }
1061 if (resolvedTypes == null) {
1062 throw new NullPointerException("resolvedTypes is null");
1063 }
1064 if (intents.length != resolvedTypes.length) {
1065 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1066 }
1067
Craig Mautner23ac33b2013-04-01 16:26:35 -07001068
1069 int callingPid;
1070 if (callingUid >= 0) {
1071 callingPid = -1;
1072 } else if (caller == null) {
1073 callingPid = Binder.getCallingPid();
1074 callingUid = Binder.getCallingUid();
1075 } else {
1076 callingPid = callingUid = -1;
1077 }
1078 final long origId = Binder.clearCallingIdentity();
1079 try {
1080 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001081 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001082 for (int i=0; i<intents.length; i++) {
1083 Intent intent = intents[i];
1084 if (intent == null) {
1085 continue;
1086 }
1087
1088 // Refuse possible leaked file descriptors
1089 if (intent != null && intent.hasFileDescriptors()) {
1090 throw new IllegalArgumentException("File descriptors passed in Intent");
1091 }
1092
1093 boolean componentSpecified = intent.getComponent() != null;
1094
1095 // Don't modify the client's object!
1096 intent = new Intent(intent);
1097
1098 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001099 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001100 // TODO: New, check if this is correct
1101 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1102
1103 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001104 (aInfo.applicationInfo.privateFlags
1105 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001106 throw new IllegalArgumentException(
1107 "FLAG_CANT_SAVE_STATE not supported here");
1108 }
1109
1110 Bundle theseOptions;
1111 if (options != null && i == intents.length-1) {
1112 theseOptions = options;
1113 } else {
1114 theseOptions = null;
1115 }
Craig Mautner6170f732013-04-02 13:05:23 -07001116 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001117 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1118 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001119 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001120 if (res < 0) {
1121 return res;
1122 }
1123
1124 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1125 }
1126 }
1127 } finally {
1128 Binder.restoreCallingIdentity(origId);
1129 }
1130
1131 return ActivityManager.START_SUCCESS;
1132 }
1133
Craig Mautner2420ead2013-04-01 17:13:20 -07001134 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001135 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001136 throws RemoteException {
1137
Craig Mautner2568c3a2015-03-26 14:22:34 -07001138 if (andResume) {
1139 r.startFreezingScreenLocked(app, 0);
1140 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001141
Craig Mautner2568c3a2015-03-26 14:22:34 -07001142 // schedule launch ticks to collect information about slow apps.
1143 r.startLaunchTickingLocked();
1144 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001145
1146 // Have the window manager re-evaluate the orientation of
1147 // the screen based on the new activity order. Note that
1148 // as a result of this, it can call back into the activity
1149 // manager with a new orientation. We don't care about that,
1150 // because the activity is not currently running so we are
1151 // just restarting it anyway.
1152 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001153 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001154 mService.mConfiguration,
1155 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1156 mService.updateConfigurationLocked(config, r, false, false);
1157 }
1158
1159 r.app = app;
1160 app.waitingToKill = null;
1161 r.launchCount++;
1162 r.lastLaunchTime = SystemClock.uptimeMillis();
1163
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001164 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001165
1166 int idx = app.activities.indexOf(r);
1167 if (idx < 0) {
1168 app.activities.add(r);
1169 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001170 mService.updateLruProcessLocked(app, true, null);
1171 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001172
1173 final ActivityStack stack = r.task.stack;
1174 try {
1175 if (app.thread == null) {
1176 throw new RemoteException();
1177 }
1178 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001179 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001180 if (andResume) {
1181 results = r.results;
1182 newIntents = r.newIntents;
1183 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001184 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1185 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1186 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001187 if (andResume) {
1188 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1189 r.userId, System.identityHashCode(r),
1190 r.task.taskId, r.shortComponentName);
1191 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001192 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001193 // Home process is the root process of the task.
1194 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001195 }
1196 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1197 r.sleeping = false;
1198 r.forceNewConfig = false;
1199 mService.showAskCompatModeDialogLocked(r);
1200 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001201 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1203 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1204 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001205 final String profileFile = mService.mProfileFile;
1206 if (profileFile != null) {
1207 ParcelFileDescriptor profileFd = mService.mProfileFd;
1208 if (profileFd != null) {
1209 try {
1210 profileFd = profileFd.dup();
1211 } catch (IOException e) {
1212 if (profileFd != null) {
1213 try {
1214 profileFd.close();
1215 } catch (IOException o) {
1216 }
1217 profileFd = null;
1218 }
1219 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001220 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001221
1222 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1223 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001224 }
1225 }
1226 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001227
Craig Mautner2568c3a2015-03-26 14:22:34 -07001228 if (andResume) {
1229 app.hasShownUi = true;
1230 app.pendingUiClean = true;
1231 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001232 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001234 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001235 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
1236 r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
1237 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001238
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001239 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 // This may be a heavy-weight process! Note that the package
1241 // manager will ensure that only activity can run in the main
1242 // process of the .apk, which is the only thing that will be
1243 // considered heavy-weight.
1244 if (app.processName.equals(app.info.packageName)) {
1245 if (mService.mHeavyWeightProcess != null
1246 && mService.mHeavyWeightProcess != app) {
1247 Slog.w(TAG, "Starting new heavy weight process " + app
1248 + " when already running "
1249 + mService.mHeavyWeightProcess);
1250 }
1251 mService.mHeavyWeightProcess = app;
1252 Message msg = mService.mHandler.obtainMessage(
1253 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1254 msg.obj = r;
1255 mService.mHandler.sendMessage(msg);
1256 }
1257 }
1258
1259 } catch (RemoteException e) {
1260 if (r.launchFailed) {
1261 // This is the second time we failed -- finish activity
1262 // and give up.
1263 Slog.e(TAG, "Second failure launching "
1264 + r.intent.getComponent().flattenToShortString()
1265 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001266 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1268 "2nd-crash", false);
1269 return false;
1270 }
1271
1272 // This is the first time we failed -- restart process and
1273 // retry.
1274 app.activities.remove(r);
1275 throw e;
1276 }
1277
1278 r.launchFailed = false;
1279 if (stack.updateLRUListLocked(r)) {
1280 Slog.w(TAG, "Activity " + r
1281 + " being launched, but already in LRU list");
1282 }
1283
1284 if (andResume) {
1285 // As part of the process of launching, ActivityThread also performs
1286 // a resume.
1287 stack.minimalResumeActivityLocked(r);
1288 } else {
1289 // This activity is not starting in the resumed state... which
1290 // should look like we asked it to pause+stop (but remain visible),
1291 // and it has done so and reported back the current icicle and
1292 // other state.
1293 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1294 + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001295 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 r.stopped = true;
1297 }
1298
1299 // Launch the new version setup screen if needed. We do this -after-
1300 // launching the initial activity (that is, home), so that it can have
1301 // a chance to initialize itself while in the background, making the
1302 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001303 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001304 mService.startSetupActivityLocked();
1305 }
1306
Dianne Hackborn465fa392014-09-14 14:21:18 -07001307 // Update any services we are bound to that might care about whether
1308 // their client may have activities.
1309 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1310
Craig Mautner2420ead2013-04-01 17:13:20 -07001311 return true;
1312 }
1313
Craig Mautnere79d42682013-04-01 19:01:53 -07001314 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001315 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001316 // Is this activity's application already running?
1317 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001318 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001319
1320 r.task.stack.setLaunchTime(r);
1321
1322 if (app != null && app.thread != null) {
1323 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001324 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1325 || !"android".equals(r.info.packageName)) {
1326 // Don't add this if it is a platform component that is marked
1327 // to run in multiple processes, because this is actually
1328 // part of the framework so doesn't make sense to track as a
1329 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001330 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1331 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001332 }
George Mount2c92c972014-03-20 09:38:23 -07001333 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001334 return;
1335 } catch (RemoteException e) {
1336 Slog.w(TAG, "Exception when starting activity "
1337 + r.intent.getComponent().flattenToShortString(), e);
1338 }
1339
1340 // If a dead object exception was thrown -- fall through to
1341 // restart the application.
1342 }
1343
1344 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001345 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001346 }
1347
Craig Mautner6170f732013-04-02 13:05:23 -07001348 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001349 Intent intent, String resolvedType, ActivityInfo aInfo,
1350 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1351 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001352 int callingPid, int callingUid, String callingPackage,
1353 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001354 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1355 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001356 int err = ActivityManager.START_SUCCESS;
1357
1358 ProcessRecord callerApp = null;
1359 if (caller != null) {
1360 callerApp = mService.getRecordForAppLocked(caller);
1361 if (callerApp != null) {
1362 callingPid = callerApp.pid;
1363 callingUid = callerApp.info.uid;
1364 } else {
1365 Slog.w(TAG, "Unable to find app for caller " + caller
1366 + " (pid=" + callingPid + ") when starting: "
1367 + intent.toString());
1368 err = ActivityManager.START_PERMISSION_DENIED;
1369 }
1370 }
1371
1372 if (err == ActivityManager.START_SUCCESS) {
1373 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1374 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001375 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001376 + " on display " + (container == null ? (mFocusedStack == null ?
1377 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1378 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1379 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001380 }
1381
1382 ActivityRecord sourceRecord = null;
1383 ActivityRecord resultRecord = null;
1384 if (resultTo != null) {
1385 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001386 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1387 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001388 if (sourceRecord != null) {
1389 if (requestCode >= 0 && !sourceRecord.finishing) {
1390 resultRecord = sourceRecord;
1391 }
1392 }
1393 }
Craig Mautner6170f732013-04-02 13:05:23 -07001394
Dianne Hackborn91097de2014-04-04 18:02:06 -07001395 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001396
Dianne Hackborn95465202014-09-15 16:21:55 -07001397 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001398 // Transfer the result target from the source activity to the new
1399 // one being started, including any failures.
1400 if (requestCode >= 0) {
1401 ActivityOptions.abort(options);
1402 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1403 }
1404 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001405 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1406 resultRecord = null;
1407 }
Craig Mautner6170f732013-04-02 13:05:23 -07001408 resultWho = sourceRecord.resultWho;
1409 requestCode = sourceRecord.requestCode;
1410 sourceRecord.resultTo = null;
1411 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001412 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001413 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001414 if (sourceRecord.launchedFromUid == callingUid) {
1415 // The new activity is being launched from the same uid as the previous
1416 // activity in the flow, and asking to forward its result back to the
1417 // previous. In this case the activity is serving as a trampoline between
1418 // the two, so we also want to update its launchedFromPackage to be the
1419 // same as the previous activity. Note that this is safe, since we know
1420 // these two packages come from the same uid; the caller could just as
1421 // well have supplied that same package name itself. This specifially
1422 // deals with the case of an intent picker/chooser being launched in the app
1423 // flow to redirect to an activity picked by the user, where we want the final
1424 // activity to consider it to have been launched by the previous app activity.
1425 callingPackage = sourceRecord.launchedFromPackage;
1426 }
Craig Mautner6170f732013-04-02 13:05:23 -07001427 }
1428
1429 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1430 // We couldn't find a class that can handle the given Intent.
1431 // That's the end of that!
1432 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1433 }
1434
1435 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1436 // We couldn't find the specific class specified in the Intent.
1437 // Also the end of the line.
1438 err = ActivityManager.START_CLASS_NOT_FOUND;
1439 }
1440
Dianne Hackborn91097de2014-04-04 18:02:06 -07001441 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1442 && sourceRecord.task.voiceSession != null) {
1443 // If this activity is being launched as part of a voice session, we need
1444 // to ensure that it is safe to do so. If the upcoming activity will also
1445 // be part of the voice session, we can only launch it if it has explicitly
1446 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001447 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001448 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1449 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001450 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1451 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001452 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1453 }
1454 } catch (RemoteException e) {
1455 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1456 }
1457 }
1458 }
1459
1460 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1461 // If the caller is starting a new voice session, just make sure the target
1462 // is actually allowing it to run this way.
1463 try {
1464 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1465 intent, resolvedType)) {
1466 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1467 }
1468 } catch (RemoteException e) {
1469 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1470 }
1471 }
1472
louis_changcd5d1982014-08-01 10:09:08 +08001473 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1474
Craig Mautner6170f732013-04-02 13:05:23 -07001475 if (err != ActivityManager.START_SUCCESS) {
1476 if (resultRecord != null) {
1477 resultStack.sendActivityResultLocked(-1,
1478 resultRecord, resultWho, requestCode,
1479 Activity.RESULT_CANCELED, null);
1480 }
Craig Mautner6170f732013-04-02 13:05:23 -07001481 ActivityOptions.abort(options);
1482 return err;
1483 }
1484
1485 final int startAnyPerm = mService.checkPermission(
1486 START_ANY_ACTIVITY, callingPid, callingUid);
1487 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1488 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1489 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1490 if (resultRecord != null) {
1491 resultStack.sendActivityResultLocked(-1,
1492 resultRecord, resultWho, requestCode,
1493 Activity.RESULT_CANCELED, null);
1494 }
Craig Mautner6170f732013-04-02 13:05:23 -07001495 String msg;
1496 if (!aInfo.exported) {
1497 msg = "Permission Denial: starting " + intent.toString()
1498 + " from " + callerApp + " (pid=" + callingPid
1499 + ", uid=" + callingUid + ")"
1500 + " not exported from uid " + aInfo.applicationInfo.uid;
1501 } else {
1502 msg = "Permission Denial: starting " + intent.toString()
1503 + " from " + callerApp + " (pid=" + callingPid
1504 + ", uid=" + callingUid + ")"
1505 + " requires " + aInfo.permission;
1506 }
1507 Slog.w(TAG, msg);
1508 throw new SecurityException(msg);
1509 }
1510
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001511 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001512 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001513
Craig Mautner6170f732013-04-02 13:05:23 -07001514 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001515 try {
1516 // The Intent we give to the watcher has the extra data
1517 // stripped off, since it can contain private information.
1518 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001519 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001520 aInfo.applicationInfo.packageName);
1521 } catch (RemoteException e) {
1522 mService.mController = null;
1523 }
Ben Gruver5e207332013-04-03 17:41:37 -07001524 }
Craig Mautner6170f732013-04-02 13:05:23 -07001525
Ben Gruver5e207332013-04-03 17:41:37 -07001526 if (abort) {
1527 if (resultRecord != null) {
1528 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001529 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001530 }
Ben Gruver5e207332013-04-03 17:41:37 -07001531 // We pretend to the caller that it was really started, but
1532 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001533 ActivityOptions.abort(options);
1534 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001535 }
1536
1537 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001538 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001539 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001540 if (outActivity != null) {
1541 outActivity[0] = r;
1542 }
1543
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001544 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001545 if (voiceSession == null && (stack.mResumedActivity == null
1546 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001547 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1548 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001549 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001550 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001551 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001552 ActivityOptions.abort(options);
1553 return ActivityManager.START_SWITCHES_CANCELED;
1554 }
1555 }
1556
1557 if (mService.mDidAppSwitch) {
1558 // This is the second allowed switch since we stopped switches,
1559 // so now just generally allow switches. Use case: user presses
1560 // home (switches disabled, switch to home, mDidAppSwitch now true);
1561 // user taps a home icon (coming from home so allowed, we hit here
1562 // and now allow anyone to switch again).
1563 mService.mAppSwitchesAllowedTime = 0;
1564 } else {
1565 mService.mDidAppSwitch = true;
1566 }
1567
Craig Mautneree36c772014-07-16 14:56:05 -07001568 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001569
Dianne Hackborn91097de2014-04-04 18:02:06 -07001570 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001571 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001572
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001573 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001574 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001575 // activity start, but we are not actually doing an activity
1576 // switch... just dismiss the keyguard now, because we
1577 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001578 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001579 }
1580 return err;
1581 }
1582
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001583 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001584 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001585
1586 // On leanback only devices we should keep all activities in the same stack.
1587 if (!mLeanbackOnlyDevice &&
1588 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001589
1590 ActivityStack stack;
1591
Wale Ogunwale7d701172015-03-11 15:36:30 -07001592 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001593 stack = task.stack;
1594 if (stack.isOnHomeDisplay()) {
1595 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001596 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1597 "computeStackFocus: Setting " + "focused stack to r=" + r
1598 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001599 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001600 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001601 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001602 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001603 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001604 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001605 }
1606
Craig Mautnere0a38842013-12-16 16:14:02 -08001607 final ActivityContainer container = r.mInitialActivityContainer;
1608 if (container != null) {
1609 // The first time put it on the desired stack, after this put on task stack.
1610 r.mInitialActivityContainer = null;
1611 return container.mStack;
1612 }
1613
Craig Mautner1b4bf852014-05-26 15:06:32 -07001614 if (mFocusedStack != mHomeStack && (!newTask ||
1615 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001616 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001617 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001618 return mFocusedStack;
1619 }
1620
Craig Mautnere0a38842013-12-16 16:14:02 -08001621 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1622 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001623 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001624 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001625 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001626 "computeStackFocus: Setting focused stack=" + stack);
1627 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001628 }
1629 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001630
Craig Mautner4a1cb222013-12-04 16:14:06 -08001631 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001632 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001633 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1634 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001635 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001636 }
1637 return mHomeStack;
1638 }
1639
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001640 boolean setFocusedStack(ActivityRecord r, String reason) {
1641 if (r == null) {
1642 // Not sure what you are trying to do, but it is not going to work...
1643 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001644 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001645 final TaskRecord task = r.task;
1646 if (task == null || task.stack == null) {
1647 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1648 return false;
1649 }
1650 task.stack.moveToFront(reason);
1651 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001652 }
1653
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001654 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001655 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001656 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001657 final Intent intent = r.intent;
1658 final int callingUid = r.launchedFromUid;
1659
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001660 // In some flows in to this function, we retrieve the task record and hold on to it
1661 // without a lock before calling back in to here... so the task at this point may
1662 // not actually be in recents. Check for that, and if it isn't in recents just
1663 // consider it invalid.
1664 if (inTask != null && !inTask.inRecents) {
1665 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1666 inTask = null;
1667 }
1668
Craig Mautnerad400af2014-08-13 16:41:36 -07001669 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001670 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1671 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001672
Craig Mautnera228ae92014-07-09 05:44:55 -07001673 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001674 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001675 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001676 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1677 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1678 "\"singleInstance\" or \"singleTask\"");
1679 launchFlags &=
1680 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1681 } else {
1682 switch (r.info.documentLaunchMode) {
1683 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1684 break;
1685 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1686 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1687 break;
1688 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1689 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1690 break;
1691 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1692 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1693 break;
1694 }
1695 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001696
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001697 final boolean launchTaskBehind = r.mLaunchTaskBehind
1698 && !launchSingleTask && !launchSingleInstance
1699 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001700
Wale Ogunwale7d701172015-03-11 15:36:30 -07001701 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1702 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001703 // For whatever reason this activity is being launched into a new
1704 // task... yet the caller has requested a result back. Well, that
1705 // is pretty messed up, so instead immediately send back a cancel
1706 // and let the new task continue launched as normal without a
1707 // dependency on its originator.
1708 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1709 r.resultTo.task.stack.sendActivityResultLocked(-1,
1710 r.resultTo, r.resultWho, r.requestCode,
1711 Activity.RESULT_CANCELED, null);
1712 r.resultTo = null;
1713 }
1714
1715 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1716 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1717 }
1718
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001719 // If we are actually going to launch in to a new task, there are some cases where
1720 // we further want to do multiple task.
1721 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1722 if (launchTaskBehind
1723 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1724 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1725 }
1726 }
1727
Craig Mautner8849a5e2013-04-02 16:41:03 -07001728 // We'll invoke onUserLeaving before onPause only if the launching
1729 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001730 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001731 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1732 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001733
1734 // If the caller has asked not to resume at this point, we make note
1735 // of this in the record so that we can skip it when trying to find
1736 // the top running activity.
1737 if (!doResume) {
1738 r.delayedResume = true;
1739 }
1740
Craig Mautnera254cd72014-05-25 16:47:39 -07001741 ActivityRecord notTop =
1742 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001743
1744 // If the onlyIfNeeded flag is set, then we can do this if the activity
1745 // being launched is the same as the one making the call... or, as
1746 // a special case, if we do not know the caller then we count the
1747 // current top activity as the caller.
1748 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1749 ActivityRecord checkedCaller = sourceRecord;
1750 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001751 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001752 }
1753 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1754 // Caller is not the same as launcher, so always needed.
1755 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1756 }
1757 }
1758
Craig Mautner8849a5e2013-04-02 16:41:03 -07001759 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001760 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001761
1762 // If the caller is not coming from another activity, but has given us an
1763 // explicit task into which they would like us to launch the new activity,
1764 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001765 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1766 final Intent baseIntent = inTask.getBaseIntent();
1767 final ActivityRecord root = inTask.getRootActivity();
1768 if (baseIntent == null) {
1769 ActivityOptions.abort(options);
1770 throw new IllegalArgumentException("Launching into task without base intent: "
1771 + inTask);
1772 }
1773
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001774 // If this task is empty, then we are adding the first activity -- it
1775 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001776 if (launchSingleInstance || launchSingleTask) {
1777 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1778 ActivityOptions.abort(options);
1779 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1780 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001781 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001782 if (root != null) {
1783 ActivityOptions.abort(options);
1784 throw new IllegalArgumentException("Caller with inTask " + inTask
1785 + " has root " + root + " but target is singleInstance/Task");
1786 }
1787 }
1788
1789 // If task is empty, then adopt the interesting intent launch flags in to the
1790 // activity being started.
1791 if (root == null) {
1792 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1793 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1794 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1795 launchFlags = (launchFlags&~flagsOfInterest)
1796 | (baseIntent.getFlags()&flagsOfInterest);
1797 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001798 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001799 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001800
Dianne Hackborn962d5352014-08-29 16:27:40 -07001801 // If the task is not empty and the caller is asking to start it as the root
1802 // of a new task, then we don't actually want to start this on the task. We
1803 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001804 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001805 addingToTask = false;
1806
1807 } else {
1808 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001809 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001810
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001811 reuseTask = inTask;
1812 } else {
1813 inTask = null;
1814 }
1815
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001816 if (inTask == null) {
1817 if (sourceRecord == null) {
1818 // This activity is not being started from another... in this
1819 // case we -always- start a new task.
1820 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1821 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1822 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1823 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1824 }
1825 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1826 // The original activity who is starting us is running as a single
1827 // instance... this new activity it is starting must go on its
1828 // own task.
1829 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1830 } else if (launchSingleInstance || launchSingleTask) {
1831 // The activity being started is a single instance... it always
1832 // gets launched into its own task.
1833 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1834 }
1835 }
1836
1837 ActivityInfo newTaskInfo = null;
1838 Intent newTaskIntent = null;
1839 ActivityStack sourceStack;
1840 if (sourceRecord != null) {
1841 if (sourceRecord.finishing) {
1842 // If the source is finishing, we can't further count it as our source. This
1843 // is because the task it is associated with may now be empty and on its way out,
1844 // so we don't want to blindly throw it in to that task. Instead we will take
1845 // the NEW_TASK flow and try to find a task for it. But save the task information
1846 // so it can be used when creating the new task.
1847 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1848 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1849 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1850 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1851 newTaskInfo = sourceRecord.info;
1852 newTaskIntent = sourceRecord.task.intent;
1853 }
1854 sourceRecord = null;
1855 sourceStack = null;
1856 } else {
1857 sourceStack = sourceRecord.task.stack;
1858 }
1859 } else {
1860 sourceStack = null;
1861 }
1862
1863 boolean movedHome = false;
1864 ActivityStack targetStack;
1865
1866 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001867 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001868
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001869 // We may want to try to place the new activity in to an existing task. We always
1870 // do this if the target activity is singleTask or singleInstance; we will also do
1871 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1872 // us to still place it in a new task: multi task, always doc mode, or being asked to
1873 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001874 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1875 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001876 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001877 // If bring to front is requested, and no result is requested and we have not
1878 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001879 // we can find a task that was started with this same
1880 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001881 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001882 // See if there is a task to bring to the front. If this is
1883 // a SINGLE_INSTANCE activity, there can be one and only one
1884 // instance of it in the history, and it is always in its own
1885 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001886 ActivityRecord intentActivity = !launchSingleInstance ?
1887 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001888 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001889 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001890 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001891 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001892 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1893 }
Craig Mautner29219d92013-04-16 20:19:12 -07001894 if (r.task == null) {
1895 r.task = intentActivity.task;
1896 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001897 if (intentActivity.task.intent == null) {
1898 // This task was started because of movement of
1899 // the activity based on affinity... now that we
1900 // are actually launching it, we can assign the
1901 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001902 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001903 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001904 targetStack = intentActivity.task.stack;
1905 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001906 // If the target task is not in the front, then we need
1907 // to bring it to the front... except... well, with
1908 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1909 // to have the same behavior as if a new instance was
1910 // being started, which means not bringing it to the front
1911 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001912 final ActivityStack focusStack = getFocusedStack();
1913 ActivityRecord curTop = (focusStack == null)
1914 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001915 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001916 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001917 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001918 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001919 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1920 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001921 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001922 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001923 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1924 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001925 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001926 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1927 options, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001928 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001929 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001930 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1931 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001932 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001933 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001934 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001935 options = null;
1936 }
1937 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001938 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001939 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001940 + " from " + intentActivity);
1941 targetStack.moveToFront("intentActivityFound");
1942 }
1943
Craig Mautner8849a5e2013-04-02 16:41:03 -07001944 // If the caller has requested that the target task be
1945 // reset, then do so.
1946 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1947 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1948 }
1949 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1950 // We don't need to start a new activity, and
1951 // the client said not to do anything if that
1952 // is the case, so this is it! And for paranoia, make
1953 // sure we have correctly resumed the top activity.
1954 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001955 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001956
1957 // Make sure to notify Keyguard as well if we are not running an app
1958 // transition later.
1959 if (!movedToFront) {
1960 notifyActivityDrawnForKeyguard();
1961 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001962 } else {
1963 ActivityOptions.abort(options);
1964 }
1965 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1966 }
1967 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001968 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
1969 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001970 // The caller has requested to completely replace any
1971 // existing task with its new activity. Well that should
1972 // not be too hard...
1973 reuseTask = intentActivity.task;
1974 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001975 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001976 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001977 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001978 // In this situation we want to remove all activities
1979 // from the task up to the one being started. In most
1980 // cases this means we are resetting the task to its
1981 // initial state.
1982 ActivityRecord top =
1983 intentActivity.task.performClearTaskLocked(r, launchFlags);
1984 if (top != null) {
1985 if (top.frontOfTask) {
1986 // Activity aliases may mean we use different
1987 // intents for the top activity, so make sure
1988 // the task now has the identity of the new
1989 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001990 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001991 }
1992 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1993 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001994 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001995 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07001996 // A special case: we need to start the activity because it is not
1997 // currently running, and the caller has asked to clear the current
1998 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07001999 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002000 // Now pretend like this activity is being started by the top of its
2001 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002002 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002003 TaskRecord task = sourceRecord.task;
2004 if (task != null && task.stack == null) {
2005 // Target stack got cleared when we all activities were removed
2006 // above. Go ahead and reset it.
2007 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2008 targetStack.addTask(
2009 task, !launchTaskBehind /* toTop */, false /* moving */);
2010 }
2011
Craig Mautner8849a5e2013-04-02 16:41:03 -07002012 }
2013 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2014 // In this case the top activity on the task is the
2015 // same as the one being launched, so we take that
2016 // as a request to bring the task to the foreground.
2017 // If the top activity in the task is the root
2018 // activity, deliver this new intent to it if it
2019 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002020 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002021 && intentActivity.realActivity.equals(r.realActivity)) {
2022 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2023 intentActivity.task);
2024 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002025 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002026 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002027 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2028 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002029 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2030 // In this case we are launching the root activity
2031 // of the task, but with a different intent. We
2032 // should start a new instance on top.
2033 addingToTask = true;
2034 sourceRecord = intentActivity;
2035 }
2036 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2037 // In this case an activity is being launched in to an
2038 // existing task, without resetting that task. This
2039 // is typically the situation of launching an activity
2040 // from a notification or shortcut. We want to place
2041 // the new activity on top of the current task.
2042 addingToTask = true;
2043 sourceRecord = intentActivity;
2044 } else if (!intentActivity.task.rootWasReset) {
2045 // In this case we are launching in to an existing task
2046 // that has not yet been started from its front door.
2047 // The current task has been brought to the front.
2048 // Ideally, we'd probably like to place this new task
2049 // at the bottom of its stack, but that's a little hard
2050 // to do with the current organization of the code so
2051 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002052 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002053 }
2054 if (!addingToTask && reuseTask == null) {
2055 // We didn't do anything... but it was needed (a.k.a., client
2056 // don't use that intent!) And for paranoia, make
2057 // sure we have correctly resumed the top activity.
2058 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002059 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002060 if (!movedToFront) {
2061 // Make sure to notify Keyguard as well if we are not running an app
2062 // transition later.
2063 notifyActivityDrawnForKeyguard();
2064 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002065 } else {
2066 ActivityOptions.abort(options);
2067 }
2068 return ActivityManager.START_TASK_TO_FRONT;
2069 }
2070 }
2071 }
2072 }
2073
2074 //String uri = r.intent.toURI();
2075 //Intent intent2 = new Intent(uri);
2076 //Slog.i(TAG, "Given intent: " + r.intent);
2077 //Slog.i(TAG, "URI is: " + uri);
2078 //Slog.i(TAG, "To intent: " + intent2);
2079
2080 if (r.packageName != null) {
2081 // If the activity being launched is the same as the one currently
2082 // at the top, then we need to check if it should only be launched
2083 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002084 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002085 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002086 if (top != null && r.resultTo == null) {
2087 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2088 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002089 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002090 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002091 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2092 top.task);
2093 // For paranoia, make sure we have correctly
2094 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002095 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002096 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002097 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002098 }
2099 ActivityOptions.abort(options);
2100 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2101 // We don't need to start a new activity, and
2102 // the client said not to do anything if that
2103 // is the case, so this is it!
2104 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2105 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002106 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002107 return ActivityManager.START_DELIVERED_TO_TOP;
2108 }
2109 }
2110 }
2111 }
2112
2113 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002114 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002115 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2116 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002117 }
2118 ActivityOptions.abort(options);
2119 return ActivityManager.START_CLASS_NOT_FOUND;
2120 }
2121
2122 boolean newTask = false;
2123 boolean keepCurTransition = false;
2124
Craig Mautnerbb742462014-07-07 15:28:55 -07002125 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002126 sourceRecord.task : null;
2127
Craig Mautner8849a5e2013-04-02 16:41:03 -07002128 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002129 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002130 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002131 if (isLockTaskModeViolation(reuseTask)) {
2132 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2133 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2134 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002135 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002136 targetStack = computeStackFocus(r, newTask);
2137 targetStack.moveToFront("startingNewTask");
2138
Craig Mautner8849a5e2013-04-02 16:41:03 -07002139 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002140 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2141 newTaskInfo != null ? newTaskInfo : r.info,
2142 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002143 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2144 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002145 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2146 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002147 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002148 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002149 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002150 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002151 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002152 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2153 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002154 // Caller wants to appear on home activity, so before starting
2155 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002156 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002157 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002158 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002159 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002160 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002161 if (isLockTaskModeViolation(sourceTask)) {
2162 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2163 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2164 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002165 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002166 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002167 final TaskRecord topTask = targetStack.topTask();
2168 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002169 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2170 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002171 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002172 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002173 // In this case, we are adding the activity to an existing
2174 // task, but the caller has asked to clear that task if the
2175 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002176 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002177 keepCurTransition = true;
2178 if (top != null) {
2179 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002180 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002181 // For paranoia, make sure we have correctly
2182 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002183 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002184 if (doResume) {
2185 targetStack.resumeTopActivityLocked(null);
2186 }
2187 ActivityOptions.abort(options);
2188 return ActivityManager.START_DELIVERED_TO_TOP;
2189 }
2190 } else if (!addingToTask &&
2191 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2192 // In this case, we are launching an activity in our own task
2193 // that may already be running somewhere in the history, and
2194 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002195 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002197 final TaskRecord task = top.task;
2198 task.moveActivityToFrontLocked(top);
2199 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002200 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002201 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002202 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002203 if (doResume) {
2204 targetStack.resumeTopActivityLocked(null);
2205 }
2206 return ActivityManager.START_DELIVERED_TO_TOP;
2207 }
2208 }
2209 // An existing activity is starting this new activity, so we want
2210 // to keep the new one in the same task as the one that is starting
2211 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002212 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002213 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002214 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002215
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002216 } else if (inTask != null) {
2217 // The calling is asking that the new activity be started in an explicit
2218 // task it has provided to us.
2219 if (isLockTaskModeViolation(inTask)) {
2220 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2221 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2222 }
2223 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002224 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002225
2226 // Check whether we should actually launch the new activity in to the task,
2227 // or just reuse the current activity on top.
2228 ActivityRecord top = inTask.getTopActivity();
2229 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2230 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2231 || launchSingleTop || launchSingleTask) {
2232 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2233 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2234 // We don't need to start a new activity, and
2235 // the client said not to do anything if that
2236 // is the case, so this is it!
2237 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2238 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002239 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002240 return ActivityManager.START_DELIVERED_TO_TOP;
2241 }
2242 }
2243
Dianne Hackborn962d5352014-08-29 16:27:40 -07002244 if (!addingToTask) {
2245 // We don't actually want to have this activity added to the task, so just
2246 // stop here but still tell the caller that we consumed the intent.
2247 ActivityOptions.abort(options);
2248 return ActivityManager.START_TASK_TO_FRONT;
2249 }
2250
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002251 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002252 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002253 + " in explicit task " + r.task);
2254
Craig Mautner8849a5e2013-04-02 16:41:03 -07002255 } else {
2256 // This not being started from an existing activity, and not part
2257 // of a new task... just put it in the top task, though these days
2258 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002259 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002260 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002261 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002262 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002263 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002264 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002265 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002266 + " in new guessed " + r.task);
2267 }
2268
2269 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002270 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002271
Craig Mautner76e2a762014-06-24 09:05:43 -07002272 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2273 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2274 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002275 if (newTask) {
2276 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2277 }
2278 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002279 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002280 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002281 if (!launchTaskBehind) {
2282 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002283 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002284 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002285 return ActivityManager.START_SUCCESS;
2286 }
2287
Craig Mautneree36c772014-07-16 14:56:05 -07002288 final void doPendingActivityLaunchesLocked(boolean doResume) {
2289 while (!mPendingActivityLaunches.isEmpty()) {
2290 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002291 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002292 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002293 }
2294 }
2295
Craig Mautner7f13ed32014-07-28 14:00:35 -07002296 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002297 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2298 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002299 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002300 mPendingActivityLaunches.remove(palNdx);
2301 }
2302 }
2303 }
2304
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002305 void setLaunchSource(int uid) {
2306 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2307 }
2308
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002309 void acquireLaunchWakelock() {
2310 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2311 throw new IllegalStateException("Calling must be system uid");
2312 }
2313 mLaunchingActivity.acquire();
2314 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2315 // To be safe, don't allow the wake lock to be held for too long.
2316 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2317 }
2318 }
2319
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002320 /**
2321 * Called when the frontmost task is idle.
2322 * @return the state of mService.mBooting before this was called.
2323 */
2324 private boolean checkFinishBootingLocked() {
2325 final boolean booting = mService.mBooting;
2326 boolean enableScreen = false;
2327 mService.mBooting = false;
2328 if (!mService.mBooted) {
2329 mService.mBooted = true;
2330 enableScreen = true;
2331 }
2332 if (booting || enableScreen) {
2333 mService.postFinishBooting(booting, enableScreen);
2334 }
2335 return booting;
2336 }
2337
Craig Mautnerf3333272013-04-22 10:55:53 -07002338 // Checked.
2339 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2340 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002341 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002342
Craig Mautnerf3333272013-04-22 10:55:53 -07002343 ArrayList<ActivityRecord> stops = null;
2344 ArrayList<ActivityRecord> finishes = null;
2345 ArrayList<UserStartedState> startingUsers = null;
2346 int NS = 0;
2347 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002348 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002349 boolean activityRemoved = false;
2350
Wale Ogunwale7d701172015-03-11 15:36:30 -07002351 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002352 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002353 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2354 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002355 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2356 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002357 if (fromTimeout) {
2358 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002359 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002360
2361 // This is a hack to semi-deal with a race condition
2362 // in the client where it can be constructed with a
2363 // newer configuration from when we asked it to launch.
2364 // We'll update with whatever configuration it now says
2365 // it used to launch.
2366 if (config != null) {
2367 r.configuration = config;
2368 }
2369
2370 // We are now idle. If someone is waiting for a thumbnail from
2371 // us, we can now deliver.
2372 r.idle = true;
2373
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002374 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002375 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002376 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002377 }
2378 }
2379
2380 if (allResumedActivitiesIdle()) {
2381 if (r != null) {
2382 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002383 }
2384
2385 if (mLaunchingActivity.isHeld()) {
2386 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2387 if (VALIDATE_WAKE_LOCK_CALLER &&
2388 Binder.getCallingUid() != Process.myUid()) {
2389 throw new IllegalStateException("Calling must be system uid");
2390 }
2391 mLaunchingActivity.release();
2392 }
2393 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002394 }
2395
2396 // Atomically retrieve all of the other things to do.
2397 stops = processStoppingActivitiesLocked(true);
2398 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002399 if ((NF = mFinishingActivities.size()) > 0) {
2400 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002401 mFinishingActivities.clear();
2402 }
2403
Craig Mautnerf3333272013-04-22 10:55:53 -07002404 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002405 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002406 mStartingUsers.clear();
2407 }
2408
Craig Mautnerf3333272013-04-22 10:55:53 -07002409 // Stop any activities that are scheduled to do so but have been
2410 // waiting for the next one to start.
2411 for (int i = 0; i < NS; i++) {
2412 r = stops.get(i);
2413 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002414 if (stack != null) {
2415 if (r.finishing) {
2416 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2417 } else {
2418 stack.stopActivityLocked(r);
2419 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002420 }
2421 }
2422
2423 // Finish any activities that are scheduled to do so but have been
2424 // waiting for the next one to start.
2425 for (int i = 0; i < NF; i++) {
2426 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002427 final ActivityStack stack = r.task.stack;
2428 if (stack != null) {
2429 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2430 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002431 }
2432
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002433 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002434 // Complete user switch
2435 if (startingUsers != null) {
2436 for (int i = 0; i < startingUsers.size(); i++) {
2437 mService.finishUserSwitch(startingUsers.get(i));
2438 }
2439 }
2440 // Complete starting up of background users
2441 if (mStartingBackgroundUsers.size() > 0) {
2442 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2443 mStartingBackgroundUsers.clear();
2444 for (int i = 0; i < startingUsers.size(); i++) {
2445 mService.finishUserBoot(startingUsers.get(i));
2446 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002447 }
2448 }
2449
2450 mService.trimApplications();
2451 //dump();
2452 //mWindowManager.dump();
2453
Craig Mautnerf3333272013-04-22 10:55:53 -07002454 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002455 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002456 }
2457
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002458 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002459 }
2460
Craig Mautner8e569572013-10-11 17:36:59 -07002461 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002462 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002463 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2464 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002465 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2466 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2467 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002468 }
Craig Mautner19091252013-10-05 00:03:53 -07002469 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002470 }
2471
2472 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002473 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2474 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002475 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2476 stacks.get(stackNdx).closeSystemDialogsLocked();
2477 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002478 }
2479 }
2480
Craig Mautner93529a42013-10-04 15:03:13 -07002481 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002482 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002483 }
2484
Craig Mautner8d341ef2013-03-26 09:03:27 -07002485 /**
2486 * @return true if some activity was finished (or would have finished if doit were true).
2487 */
2488 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2489 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002490 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2491 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002492 final int numStacks = stacks.size();
2493 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2494 final ActivityStack stack = stacks.get(stackNdx);
2495 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2496 didSomething = true;
2497 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002498 }
2499 }
2500 return didSomething;
2501 }
2502
Dianne Hackborna413dc02013-07-12 12:02:55 -07002503 void updatePreviousProcessLocked(ActivityRecord r) {
2504 // Now that this process has stopped, we may want to consider
2505 // it to be the previous app to try to keep around in case
2506 // the user wants to return to it.
2507
2508 // First, found out what is currently the foreground app, so that
2509 // we don't blow away the previous app if this activity is being
2510 // hosted by the process that is actually still the foreground.
2511 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002512 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2513 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002514 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2515 final ActivityStack stack = stacks.get(stackNdx);
2516 if (isFrontStack(stack)) {
2517 if (stack.mResumedActivity != null) {
2518 fgApp = stack.mResumedActivity.app;
2519 } else if (stack.mPausingActivity != null) {
2520 fgApp = stack.mPausingActivity.app;
2521 }
2522 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002523 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002524 }
2525 }
2526
2527 // Now set this one as the previous process, only if that really
2528 // makes sense to.
2529 if (r.app != null && fgApp != null && r.app != fgApp
2530 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002531 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002532 mService.mPreviousProcess = r.app;
2533 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2534 }
2535 }
2536
Craig Mautner05d29032013-05-03 13:40:13 -07002537 boolean resumeTopActivitiesLocked() {
2538 return resumeTopActivitiesLocked(null, null, null);
2539 }
2540
2541 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2542 Bundle targetOptions) {
2543 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002544 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002545 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002546 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002547 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002548 if (isFrontStack(targetStack)) {
2549 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2550 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002551
Craig Mautnere0a38842013-12-16 16:14:02 -08002552 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2553 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002554 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2555 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002556 if (stack == targetStack) {
2557 // Already started above.
2558 continue;
2559 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002560 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002561 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002562 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002563 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002564 }
Craig Mautner05d29032013-05-03 13:40:13 -07002565 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002566 }
2567
Todd Kennedy539db512014-12-15 09:57:55 -08002568 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002569 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2570 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002571 final int numStacks = stacks.size();
2572 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2573 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002574 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002575 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002576 }
2577 }
2578
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002579 void finishVoiceTask(IVoiceInteractionSession session) {
2580 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2581 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2582 final int numStacks = stacks.size();
2583 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2584 final ActivityStack stack = stacks.get(stackNdx);
2585 stack.finishVoiceTask(session);
2586 }
2587 }
2588 }
2589
Craig Mautner299f9602015-01-26 09:47:33 -08002590 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002591 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2592 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002593 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002594 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2595 // Caller wants the home activity moved with it. To accomplish this,
2596 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002597 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002598 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002599 if (task.stack == null) {
2600 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2601 + task + " to front. Stack is null");
2602 return;
2603 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002604 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002605 if (DEBUG_STACK) Slog.d(TAG_STACK,
2606 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002607 }
2608
Craig Mautner967212c2013-04-13 21:10:58 -07002609 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002610 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2611 if (activityContainer != null) {
2612 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002613 }
2614 return null;
2615 }
2616
Craig Mautner967212c2013-04-13 21:10:58 -07002617 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002618 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002619 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2620 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002621 }
2622 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002623 }
2624
Craig Mautner4a1cb222013-12-04 16:14:06 -08002625 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002626 ActivityRecord homeActivity = getHomeActivity();
2627 if (homeActivity != null) {
2628 return homeActivity.appToken;
2629 }
2630 return null;
2631 }
2632
2633 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002634 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2635 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2636 final TaskRecord task = tasks.get(taskNdx);
2637 if (task.isHomeTask()) {
2638 final ArrayList<ActivityRecord> activities = task.mActivities;
2639 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2640 final ActivityRecord r = activities.get(activityNdx);
2641 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002642 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002643 }
2644 }
2645 }
2646 }
2647 return null;
2648 }
2649
Todd Kennedyca4d8422015-01-15 15:19:22 -08002650 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002651 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002652 ActivityContainer activityContainer =
2653 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002654 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002655 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002656 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002657 return activityContainer;
2658 }
2659
Craig Mautner34b73df2014-01-12 21:11:08 -08002660 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002661 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2662 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2663 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002664 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2665 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002666 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002667 }
2668 }
2669
Craig Mautner95da1082014-02-24 17:54:35 -08002670 void deleteActivityContainer(IActivityContainer container) {
2671 ActivityContainer activityContainer = (ActivityContainer)container;
2672 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002673 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2674 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002675 final int stackId = activityContainer.mStackId;
2676 mActivityContainers.remove(stackId);
2677 mWindowManager.removeStack(stackId);
2678 }
2679 }
2680
Wale Ogunwale60454db2015-01-23 16:05:07 -08002681 void resizeStackLocked(int stackId, Rect bounds) {
2682 final ActivityStack stack = getStack(stackId);
2683 if (stack == null) {
2684 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2685 return;
2686 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002687
2688 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002689 if (r != null && !r.task.mResizeable) {
2690 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002691 return;
2692 }
2693
Wale Ogunwale60454db2015-01-23 16:05:07 -08002694 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2695 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002696 if (r != null) {
2697 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2698 // And we need to make sure at this point that all other activities
2699 // are made visible with the correct configuration.
2700 ensureActivitiesVisibleLocked(r, 0);
2701 if (!updated) {
2702 resumeTopActivitiesLocked(stack, null, null);
2703 }
2704 }
2705 }
2706 }
2707
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002708 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2709 * current task stack if it only has one entry, moving the task to a stack that matches the
2710 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2711 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2712 task.mResizeable = true;
2713 final ActivityStack currentStack = task.stack;
2714 if (currentStack.isHomeStack()) {
2715 // Can't move task off the home stack. Sorry!
2716 return;
2717 }
2718
2719 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2720 if (matchingStackId != -1) {
2721 // There is already a stack with the right bounds!
2722 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2723 // Nothing to do here. Already in the right stack...
2724 return;
2725 }
2726 // Move task to stack with matching bounds.
2727 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2728 return;
2729 }
2730
2731 if (currentStack != null && currentStack.numTasks() == 1) {
2732 // Just resize the current stack since this is the task in it.
2733 resizeStackLocked(currentStack.mStackId, bounds);
2734 return;
2735 }
2736
2737 // Create new stack and move the task to it.
2738 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2739 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2740 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2741
2742 if (newStack == null) {
2743 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2744 return;
2745 }
2746 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2747 resizeStackLocked(newStack.mStackId, bounds);
2748 }
2749
Todd Kennedy4900bf92015-01-16 16:05:14 -08002750 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002751 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2752 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002753 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002754 }
2755
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002756 ActivityContainer activityContainer = new ActivityContainer(stackId);
2757 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002758 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002759 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002760 }
2761
2762 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002763 while (true) {
2764 if (++mLastStackId <= HOME_STACK_ID) {
2765 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002766 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002767 if (getStack(mLastStackId) == null) {
2768 break;
2769 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002770 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002771 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002772 }
2773
Wale Ogunwale7de05352014-12-12 15:21:33 -08002774 private boolean restoreRecentTaskLocked(TaskRecord task) {
2775 ActivityStack stack = null;
2776 // Determine stack to restore task to.
2777 if (mLeanbackOnlyDevice) {
2778 // There is only one stack for lean back devices.
2779 stack = mHomeStack;
2780 } else {
2781 // Look for the top stack on the home display that isn't the home stack.
2782 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2783 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2784 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002785 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002786 stack = tmpStack;
2787 break;
2788 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002789 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002790 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002791
2792 if (stack == null) {
2793 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2794 // before an application stack is created...Go ahead and create one on the default
2795 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002796 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002797 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002798 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002799 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2800 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002801 }
2802
2803 if (stack == null) {
2804 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002805 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2806 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002807 return false;
2808 }
2809
2810 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002811 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2812 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002813 final ArrayList<ActivityRecord> activities = task.mActivities;
2814 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2815 final ActivityRecord r = activities.get(activityNdx);
2816 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2817 r.info.screenOrientation, r.fullscreen,
2818 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
2819 r.userId, r.info.configChanges, task.voiceSession != null,
2820 r.mLaunchTaskBehind);
2821 }
2822 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002823 }
2824
Craig Mautner299f9602015-01-26 09:47:33 -08002825 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002826 final TaskRecord task = anyTaskForIdLocked(taskId);
2827 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002828 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002829 return;
2830 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002831 final ActivityStack stack = getStack(stackId);
2832 if (stack == null) {
2833 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2834 return;
2835 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002836 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2837 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07002838 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002839 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002840 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002841 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002842 }
2843
Craig Mautnerac6f8432013-07-17 13:24:59 -07002844 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002845 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002846 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2847 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002848 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2849 final ActivityStack stack = stacks.get(stackNdx);
2850 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002851 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002852 continue;
2853 }
2854 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002855 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2856 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002857 continue;
2858 }
2859 final ActivityRecord ar = stack.findTaskLocked(r);
2860 if (ar != null) {
2861 return ar;
2862 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002863 }
2864 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002865 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002866 return null;
2867 }
2868
2869 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002870 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2871 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002872 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2873 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2874 if (ar != null) {
2875 return ar;
2876 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002877 }
2878 }
2879 return null;
2880 }
2881
Craig Mautner8d341ef2013-03-26 09:03:27 -07002882 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002883 scheduleSleepTimeout();
2884 if (!mGoingToSleep.isHeld()) {
2885 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002886 if (mLaunchingActivity.isHeld()) {
2887 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2888 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002889 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002890 mLaunchingActivity.release();
2891 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002892 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002893 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002894 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002895 }
2896
2897 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002898 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002899
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002900 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002901 final long endTime = System.currentTimeMillis() + timeout;
2902 while (true) {
2903 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002904 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2905 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002906 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2907 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2908 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002909 }
2910 if (cantShutdown) {
2911 long timeRemaining = endTime - System.currentTimeMillis();
2912 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002913 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002914 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002915 } catch (InterruptedException e) {
2916 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002917 } else {
2918 Slog.w(TAG, "Activity manager shutdown timed out");
2919 timedout = true;
2920 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002921 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002922 } else {
2923 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002924 }
2925 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002926
2927 // Force checkReadyForSleep to complete.
2928 mSleepTimeout = true;
2929 checkReadyForSleepLocked();
2930
Craig Mautner8d341ef2013-03-26 09:03:27 -07002931 return timedout;
2932 }
2933
2934 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002935 removeSleepTimeouts();
2936 if (mGoingToSleep.isHeld()) {
2937 mGoingToSleep.release();
2938 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002939 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2940 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002941 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2942 final ActivityStack stack = stacks.get(stackNdx);
2943 stack.awakeFromSleepingLocked();
2944 if (isFrontStack(stack)) {
2945 resumeTopActivitiesLocked();
2946 }
Craig Mautner5314a402013-09-26 12:40:16 -07002947 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002948 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002949 mGoingToSleepActivities.clear();
2950 }
2951
2952 void activitySleptLocked(ActivityRecord r) {
2953 mGoingToSleepActivities.remove(r);
2954 checkReadyForSleepLocked();
2955 }
2956
2957 void checkReadyForSleepLocked() {
2958 if (!mService.isSleepingOrShuttingDown()) {
2959 // Do not care.
2960 return;
2961 }
2962
2963 if (!mSleepTimeout) {
2964 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002965 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2966 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002967 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2968 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2969 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002970 }
2971
2972 if (mStoppingActivities.size() > 0) {
2973 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002974 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002975 + mStoppingActivities.size() + " activities");
2976 scheduleIdleLocked();
2977 dontSleep = true;
2978 }
2979
2980 if (mGoingToSleepActivities.size() > 0) {
2981 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002982 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002983 + mGoingToSleepActivities.size() + " activities");
2984 dontSleep = true;
2985 }
2986
2987 if (dontSleep) {
2988 return;
2989 }
2990 }
2991
Craig Mautnere0a38842013-12-16 16:14:02 -08002992 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2993 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002994 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2995 stacks.get(stackNdx).goToSleep();
2996 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002997 }
2998
2999 removeSleepTimeouts();
3000
3001 if (mGoingToSleep.isHeld()) {
3002 mGoingToSleep.release();
3003 }
3004 if (mService.mShuttingDown) {
3005 mService.notifyAll();
3006 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003007 }
3008
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003009 boolean reportResumedActivityLocked(ActivityRecord r) {
3010 final ActivityStack stack = r.task.stack;
3011 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003012 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003013 }
3014 if (allResumedActivitiesComplete()) {
3015 ensureActivitiesVisibleLocked(null, 0);
3016 mWindowManager.executeAppTransition();
3017 return true;
3018 }
3019 return false;
3020 }
3021
Craig Mautner8d341ef2013-03-26 09:03:27 -07003022 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003023 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3024 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003025 final int numStacks = stacks.size();
3026 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3027 final ActivityStack stack = stacks.get(stackNdx);
3028 stack.handleAppCrashLocked(app);
3029 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003030 }
3031 }
3032
Jose Lima4b6c6692014-08-12 17:41:12 -07003033 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003034 final ActivityStack stack = r.task.stack;
3035 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003036 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3037 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003038 return false;
3039 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003040 final boolean isVisible = stack.hasVisibleBehindActivity();
3041 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3042 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003043
3044 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003045 if (top == null || top == r || (visible == isVisible)) {
3046 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3047 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003048 return true;
3049 }
3050
3051 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003052 if (visible && top.fullscreen) {
3053 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003054 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003055 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003056 " top.app.thread=" + top.app.thread);
3057 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003058 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3059 // Only the activity set as currently visible behind should actively reset its
3060 // visible behind state.
3061 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3062 + visible + " stack.getVisibleBehindActivity()=" +
3063 stack.getVisibleBehindActivity() + " r=" + r);
3064 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003065 }
3066
Jose Lima4b6c6692014-08-12 17:41:12 -07003067 stack.setVisibleBehindActivity(visible ? r : null);
3068 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003069 // Make the activity immediately above r opaque.
3070 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3071 if (next != null) {
3072 mService.convertFromTranslucent(next.appToken);
3073 }
3074 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003075 if (top.app != null && top.app.thread != null) {
3076 // Notify the top app of the change.
3077 try {
3078 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3079 } catch (RemoteException e) {
3080 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003081 }
3082 return true;
3083 }
3084
Craig Mautnerbb742462014-07-07 15:28:55 -07003085 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3086 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3087 r.mLaunchTaskBehind = false;
3088 final TaskRecord task = r.task;
3089 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003090 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003091 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003092 mWindowManager.setAppVisibility(r.appToken, false);
3093 }
3094
3095 void scheduleLaunchTaskBehindComplete(IBinder token) {
3096 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3097 }
3098
Craig Mautnerde4ef022013-04-07 19:01:33 -07003099 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003100 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003101 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3102 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003103 final int topStackNdx = stacks.size() - 1;
3104 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3105 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003106 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003107 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003108 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003109 }
3110
3111 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003112 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3113 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003114 final int numStacks = stacks.size();
3115 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3116 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003117 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003118 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003119 }
3120 }
3121
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003122 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3123 // Examine all activities currently running in the process.
3124 TaskRecord firstTask = null;
3125 // Tasks is non-null only if two or more tasks are found.
3126 ArraySet<TaskRecord> tasks = null;
3127 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3128 for (int i=0; i<app.activities.size(); i++) {
3129 ActivityRecord r = app.activities.get(i);
3130 // First, if we find an activity that is in the process of being destroyed,
3131 // then we just aren't going to do anything for now; we want things to settle
3132 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003133 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003134 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3135 return;
3136 }
3137 // Don't consider any activies that are currently not in a state where they
3138 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003139 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3140 || r.state == PAUSED || r.state == STOPPING) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003141 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3142 continue;
3143 }
3144 if (r.task != null) {
3145 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3146 + " from " + r);
3147 if (firstTask == null) {
3148 firstTask = r.task;
3149 } else if (firstTask != r.task) {
3150 if (tasks == null) {
3151 tasks = new ArraySet<>();
3152 tasks.add(firstTask);
3153 }
3154 tasks.add(r.task);
3155 }
3156 }
3157 }
3158 if (tasks == null) {
3159 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3160 return;
3161 }
3162 // If we have activities in multiple tasks that are in a position to be destroyed,
3163 // let's iterate through the tasks and release the oldest one.
3164 final int numDisplays = mActivityDisplays.size();
3165 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3166 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3167 // Step through all stacks starting from behind, to hit the oldest things first.
3168 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3169 final ActivityStack stack = stacks.get(stackNdx);
3170 // Try to release activities in this stack; if we manage to, we are done.
3171 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3172 return;
3173 }
3174 }
3175 }
3176 }
3177
Craig Mautner8d341ef2013-03-26 09:03:27 -07003178 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003179 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003180 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003181 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003182
Craig Mautner858d8a62013-04-23 17:08:34 -07003183 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003184 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3185 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003186 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003187 final ActivityStack stack = stacks.get(stackNdx);
3188 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003189 TaskRecord task = stack.topTask();
3190 if (task != null) {
3191 mWindowManager.moveTaskToTop(task.taskId);
3192 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003193 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003194 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003195
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003196 ActivityStack stack = getStack(restoreStackId);
3197 if (stack == null) {
3198 stack = mHomeStack;
3199 }
3200 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003201 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003202 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003203 TaskRecord task = stack.topTask();
3204 if (task != null) {
3205 mWindowManager.moveTaskToTop(task.taskId);
3206 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003207 } else {
3208 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003209 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003210 }
Craig Mautner93529a42013-10-04 15:03:13 -07003211 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003212 }
3213
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003214 /**
3215 * Add background users to send boot completed events to.
3216 * @param userId The user being started in the background
3217 * @param uss The state object for the user.
3218 */
3219 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3220 mStartingBackgroundUsers.add(uss);
3221 }
3222
Craig Mautnerde4ef022013-04-07 19:01:33 -07003223 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003224 ArrayList<ActivityRecord> stops = null;
3225
3226 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003227 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3228 ActivityRecord s = mStoppingActivities.get(activityNdx);
3229 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003230 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003231 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3232 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003233 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003234 if (s.finishing) {
3235 // If this activity is finishing, it is sitting on top of
3236 // everyone else but we now know it is no longer needed...
3237 // so get rid of it. Otherwise, we need to go through the
3238 // normal flow and hide it once we determine that it is
3239 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003240 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003241 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003242 }
3243 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003244 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003245 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003246 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003247 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003248 }
3249 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003250 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003251 }
3252 }
3253
3254 return stops;
3255 }
3256
Craig Mautnercf910b02013-04-23 11:23:27 -07003257 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003258 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3259 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3260 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3261 final ActivityStack stack = stacks.get(stackNdx);
3262 final ActivityRecord r = stack.topRunningActivityLocked(null);
3263 final ActivityState state = r == null ? DESTROYED : r.state;
3264 if (isFrontStack(stack)) {
3265 if (r == null) Slog.e(TAG,
3266 "validateTop...: null top activity, stack=" + stack);
3267 else {
3268 final ActivityRecord pausing = stack.mPausingActivity;
3269 if (pausing != null && pausing == r) Slog.e(TAG,
3270 "validateTop...: top stack has pausing activity r=" + r
3271 + " state=" + state);
3272 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3273 "validateTop...: activity in front not resumed r=" + r
3274 + " state=" + state);
3275 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003276 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003277 final ActivityRecord resumed = stack.mResumedActivity;
3278 if (resumed != null && resumed == r) Slog.e(TAG,
3279 "validateTop...: back stack has resumed activity r=" + r
3280 + " state=" + state);
3281 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3282 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003283 }
3284 }
3285 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003286 }
3287
Craig Mautner27084302013-03-25 08:05:25 -07003288 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003289 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003290 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003291 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3292 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3293 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003294 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003295 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003296
Craig Mautner20e72272013-04-01 13:45:53 -07003297 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003298 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003299 }
3300
Dianne Hackborn390517b2013-05-30 15:03:32 -07003301 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3302 boolean needSep, String prefix) {
3303 if (activity != null) {
3304 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3305 if (needSep) {
3306 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003307 }
3308 pw.print(prefix);
3309 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003310 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003311 }
3312 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003313 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003314 }
3315
Craig Mautner8d341ef2013-03-26 09:03:27 -07003316 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3317 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003318 boolean printed = false;
3319 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003320 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3321 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003322 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003323 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003324 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003325 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003326 final ActivityStack stack = stacks.get(stackNdx);
3327 StringBuilder stackHeader = new StringBuilder(128);
3328 stackHeader.append(" Stack #");
3329 stackHeader.append(stack.mStackId);
3330 stackHeader.append(":");
3331 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3332 needSep, stackHeader.toString());
3333 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3334 !dumpAll, false, dumpPackage, true,
3335 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003336
Craig Mautner4a1cb222013-12-04 16:14:06 -08003337 needSep = printed;
3338 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3339 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003340 if (pr) {
3341 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003342 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003343 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003344 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3345 " mResumedActivity: ");
3346 if (pr) {
3347 printed = true;
3348 needSep = false;
3349 }
3350 if (dumpAll) {
3351 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3352 " mLastPausedActivity: ");
3353 if (pr) {
3354 printed = true;
3355 needSep = true;
3356 }
3357 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3358 needSep, " mLastNoHistoryActivity: ");
3359 }
3360 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003361 }
3362 }
3363
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003364 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3365 false, dumpPackage, true, " Activities waiting to finish:", null);
3366 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3367 false, dumpPackage, true, " Activities waiting to stop:", null);
3368 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3369 false, dumpPackage, true, " Activities waiting for another to become visible:",
3370 null);
3371 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3372 false, dumpPackage, true, " Activities waiting to sleep:", null);
3373 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3374 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003375
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003376 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003377 }
3378
Dianne Hackborn390517b2013-05-30 15:03:32 -07003379 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003380 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003381 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003382 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003383 String innerPrefix = null;
3384 String[] args = null;
3385 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003386 for (int i=list.size()-1; i>=0; i--) {
3387 final ActivityRecord r = list.get(i);
3388 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3389 continue;
3390 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003391 if (innerPrefix == null) {
3392 innerPrefix = prefix + " ";
3393 args = new String[0];
3394 }
3395 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003396 final boolean full = !brief && (complete || !r.isInHistory());
3397 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003398 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003399 needNL = false;
3400 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003401 if (header1 != null) {
3402 pw.println(header1);
3403 header1 = null;
3404 }
3405 if (header2 != null) {
3406 pw.println(header2);
3407 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003408 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003409 if (lastTask != r.task) {
3410 lastTask = r.task;
3411 pw.print(prefix);
3412 pw.print(full ? "* " : " ");
3413 pw.println(lastTask);
3414 if (full) {
3415 lastTask.dump(pw, prefix + " ");
3416 } else if (complete) {
3417 // Complete + brief == give a summary. Isn't that obvious?!?
3418 if (lastTask.intent != null) {
3419 pw.print(prefix); pw.print(" ");
3420 pw.println(lastTask.intent.toInsecureStringWithClip());
3421 }
3422 }
3423 }
3424 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3425 pw.print(" #"); pw.print(i); pw.print(": ");
3426 pw.println(r);
3427 if (full) {
3428 r.dump(pw, innerPrefix);
3429 } else if (complete) {
3430 // Complete + brief == give a summary. Isn't that obvious?!?
3431 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3432 if (r.app != null) {
3433 pw.print(innerPrefix); pw.println(r.app);
3434 }
3435 }
3436 if (client && r.app != null && r.app.thread != null) {
3437 // flush anything that is already in the PrintWriter since the thread is going
3438 // to write to the file descriptor directly
3439 pw.flush();
3440 try {
3441 TransferPipe tp = new TransferPipe();
3442 try {
3443 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3444 r.appToken, innerPrefix, args);
3445 // Short timeout, since blocking here can
3446 // deadlock with the application.
3447 tp.go(fd, 2000);
3448 } finally {
3449 tp.kill();
3450 }
3451 } catch (IOException e) {
3452 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3453 } catch (RemoteException e) {
3454 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3455 }
3456 needNL = true;
3457 }
3458 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003459 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003460 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003461
Craig Mautnerf3333272013-04-22 10:55:53 -07003462 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003463 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003464 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3465 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003466 }
3467
3468 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003469 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003470 }
3471
3472 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003473 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003474 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3475 }
3476
Craig Mautner05d29032013-05-03 13:40:13 -07003477 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003478 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3479 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3480 }
Craig Mautner05d29032013-05-03 13:40:13 -07003481 }
3482
Craig Mautner0eea92c2013-05-16 13:35:39 -07003483 void removeSleepTimeouts() {
3484 mSleepTimeout = false;
3485 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3486 }
3487
3488 final void scheduleSleepTimeout() {
3489 removeSleepTimeouts();
3490 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3491 }
3492
Craig Mautner4a1cb222013-12-04 16:14:06 -08003493 @Override
3494 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003495 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003496 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3497 }
3498
3499 @Override
3500 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003501 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003502 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3503 }
3504
3505 @Override
3506 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003507 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003508 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3509 }
3510
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003511 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003512 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003513 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003514 newDisplay = mActivityDisplays.get(displayId) == null;
3515 if (newDisplay) {
3516 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003517 if (activityDisplay.mDisplay == null) {
3518 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3519 return;
3520 }
Craig Mautner4504de52013-12-20 09:06:56 -08003521 mActivityDisplays.put(displayId, activityDisplay);
3522 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003523 }
Craig Mautner4504de52013-12-20 09:06:56 -08003524 if (newDisplay) {
3525 mWindowManager.onDisplayAdded(displayId);
3526 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003527 }
3528
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003529 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003531 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3532 if (activityDisplay != null) {
3533 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003534 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003535 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003536 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003537 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003538 }
3539 }
3540 mWindowManager.onDisplayRemoved(displayId);
3541 }
3542
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003543 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003544 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003545 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3546 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003547 // TODO: Update the bounds.
3548 }
3549 }
3550 mWindowManager.onDisplayChanged(displayId);
3551 }
3552
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003553 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003554 StackInfo info = new StackInfo();
3555 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3556 info.displayId = Display.DEFAULT_DISPLAY;
3557 info.stackId = stack.mStackId;
3558
3559 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3560 final int numTasks = tasks.size();
3561 int[] taskIds = new int[numTasks];
3562 String[] taskNames = new String[numTasks];
3563 for (int i = 0; i < numTasks; ++i) {
3564 final TaskRecord task = tasks.get(i);
3565 taskIds[i] = task.taskId;
3566 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3567 : task.realActivity != null ? task.realActivity.flattenToString()
3568 : task.getTopActivity() != null ? task.getTopActivity().packageName
3569 : "unknown";
3570 }
3571 info.taskIds = taskIds;
3572 info.taskNames = taskNames;
3573 return info;
3574 }
3575
3576 StackInfo getStackInfoLocked(int stackId) {
3577 ActivityStack stack = getStack(stackId);
3578 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003579 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003580 }
3581 return null;
3582 }
3583
3584 ArrayList<StackInfo> getAllStackInfosLocked() {
3585 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003586 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3587 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003588 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003589 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003590 }
3591 }
3592 return list;
3593 }
3594
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003595 void showLockTaskToast() {
3596 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003597 }
3598
Benjamin Franz43261142015-02-11 15:59:44 +00003599 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003600 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003601 // Take out of lock task mode if necessary
3602 if (mLockTaskModeTask != null) {
3603 final Message lockTaskMsg = Message.obtain();
3604 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3605 lockTaskMsg.what = LOCK_TASK_END_MSG;
3606 mLockTaskModeTask = null;
3607 mHandler.sendMessage(lockTaskMsg);
3608 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003609 return;
3610 }
3611 if (isLockTaskModeViolation(task)) {
3612 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3613 return;
3614 }
3615 mLockTaskModeTask = task;
Craig Mautner299f9602015-01-26 09:47:33 -08003616 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003617 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003618
3619 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003620 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3621 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003622 lockTaskMsg.what = LOCK_TASK_START_MSG;
Benjamin Franz43261142015-02-11 15:59:44 +00003623 lockTaskMsg.arg2 = lockTaskModeState;
justinzhang5286d3f2014-05-12 17:06:01 -04003624 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003625 }
3626
3627 boolean isLockTaskModeViolation(TaskRecord task) {
3628 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3629 }
3630
3631 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3632 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003633 final Message lockTaskMsg = Message.obtain();
3634 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3635 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003636 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003637 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003638 }
3639 }
3640
Benjamin Franz43261142015-02-11 15:59:44 +00003641 int getLockTaskModeState() {
3642 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003643 }
3644
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003645 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003646
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003647 public ActivityStackSupervisorHandler(Looper looper) {
3648 super(looper);
3649 }
3650
Craig Mautnerf3333272013-04-22 10:55:53 -07003651 void activityIdleInternal(ActivityRecord r) {
3652 synchronized (mService) {
3653 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3654 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003655 }
3656
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003657 @Override
3658 public void handleMessage(Message msg) {
3659 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003660 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003661 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003662 if (mService.mDidDexOpt) {
3663 mService.mDidDexOpt = false;
3664 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3665 nmsg.obj = msg.obj;
3666 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3667 return;
3668 }
3669 // We don't at this point know if the activity is fullscreen,
3670 // so we need to be conservative and assume it isn't.
3671 activityIdleInternal((ActivityRecord)msg.obj);
3672 } break;
3673 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003674 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003675 activityIdleInternal((ActivityRecord)msg.obj);
3676 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003677 case RESUME_TOP_ACTIVITY_MSG: {
3678 synchronized (mService) {
3679 resumeTopActivitiesLocked();
3680 }
3681 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003682 case SLEEP_TIMEOUT_MSG: {
3683 synchronized (mService) {
3684 if (mService.isSleepingOrShuttingDown()) {
3685 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3686 mSleepTimeout = true;
3687 checkReadyForSleepLocked();
3688 }
3689 }
3690 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003691 case LAUNCH_TIMEOUT_MSG: {
3692 if (mService.mDidDexOpt) {
3693 mService.mDidDexOpt = false;
3694 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3695 return;
3696 }
3697 synchronized (mService) {
3698 if (mLaunchingActivity.isHeld()) {
3699 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3700 if (VALIDATE_WAKE_LOCK_CALLER
3701 && Binder.getCallingUid() != Process.myUid()) {
3702 throw new IllegalStateException("Calling must be system uid");
3703 }
3704 mLaunchingActivity.release();
3705 }
3706 }
3707 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003708 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003709 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003710 } break;
3711 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003712 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003713 } break;
3714 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003715 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003716 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003717 case CONTAINER_CALLBACK_VISIBILITY: {
3718 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003719 final IActivityContainerCallback callback = container.mCallback;
3720 if (callback != null) {
3721 try {
3722 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3723 } catch (RemoteException e) {
3724 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003725 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003726 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003727 case LOCK_TASK_START_MSG: {
3728 // When lock task starts, we disable the status bars.
3729 try {
Jason Monk62515be2014-05-21 16:06:19 -04003730 if (mLockTaskNotify == null) {
3731 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003732 }
Jason Monk62515be2014-05-21 16:06:19 -04003733 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003734 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003735 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003736 int flags = 0;
3737 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_LOCKED) {
3738 flags = StatusBarManager.DISABLE_MASK
3739 & (~StatusBarManager.DISABLE_BACK);
3740 } else if (mLockTaskModeState ==
3741 ActivityManager.LOCK_TASK_MODE_PINNED) {
3742 flags = StatusBarManager.DISABLE_MASK
3743 & (~StatusBarManager.DISABLE_BACK)
3744 & (~StatusBarManager.DISABLE_HOME)
3745 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003746 }
3747 getStatusBarService().disable(flags, mToken,
3748 mService.mContext.getPackageName());
3749 }
3750 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003751 if (getDevicePolicyManager() != null) {
3752 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003753 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003754 }
justinzhang5286d3f2014-05-12 17:06:01 -04003755 } catch (RemoteException ex) {
3756 throw new RuntimeException(ex);
3757 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003758 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003759 case LOCK_TASK_END_MSG: {
3760 // When lock task ends, we enable the status bars.
3761 try {
Jason Monk62515be2014-05-21 16:06:19 -04003762 if (getStatusBarService() != null) {
3763 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3764 mService.mContext.getPackageName());
3765 }
3766 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003767 if (getDevicePolicyManager() != null) {
3768 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3769 msg.arg1);
3770 }
Jason Monk62515be2014-05-21 16:06:19 -04003771 if (mLockTaskNotify == null) {
3772 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3773 }
3774 mLockTaskNotify.show(false);
3775 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003776 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003777 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003778 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Benjamin Franz43261142015-02-11 15:59:44 +00003779 if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_PINNED &&
3780 shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003781 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003782 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003783 new LockPatternUtils(mService.mContext)
3784 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003785 }
3786 } catch (SettingNotFoundException e) {
3787 // No setting, don't lock.
3788 }
justinzhang5286d3f2014-05-12 17:06:01 -04003789 } catch (RemoteException ex) {
3790 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003791 } finally {
3792 mLockTaskModeState = ActivityManager.LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003793 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003794 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003795 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3796 final ActivityContainer container = (ActivityContainer) msg.obj;
3797 final IActivityContainerCallback callback = container.mCallback;
3798 if (callback != null) {
3799 try {
3800 callback.onAllActivitiesComplete(container.asBinder());
3801 } catch (RemoteException e) {
3802 }
3803 }
3804 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003805 case LAUNCH_TASK_BEHIND_COMPLETE: {
3806 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003807 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003808 if (r != null) {
3809 handleLaunchTaskBehindCompleteLocked(r);
3810 }
3811 }
3812 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003813 }
3814 }
3815 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003816
Ying Wangb081a592014-04-22 15:20:16 -07003817 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003818 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003819 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003820 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003821 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003822 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003823 ActivityRecord mParentActivity = null;
3824 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003825
Craig Mautnere3a00d72014-04-16 08:31:19 -07003826 boolean mVisible = true;
3827
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003829 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003830
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003831 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3832 final static int CONTAINER_STATE_NO_SURFACE = 1;
3833 final static int CONTAINER_STATE_FINISHING = 2;
3834 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3835
3836 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003837 synchronized (mService) {
3838 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003839 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003840 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003841 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003842 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003843 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003844
Craig Mautnere0a38842013-12-16 16:14:02 -08003845 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003846 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08003847 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003848 mActivityDisplay = activityDisplay;
3849 mStack.mDisplayId = activityDisplay.mDisplayId;
3850 mStack.mStacks = activityDisplay.mStacks;
3851
3852 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003853 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003854 }
3855
3856 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003857 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003858 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003859 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3860 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003861 return;
3862 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003863 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003864 }
3865 }
3866
3867 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003868 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003869 synchronized (mService) {
3870 if (mActivityDisplay != null) {
3871 return mActivityDisplay.mDisplayId;
3872 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003873 }
3874 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003875 }
3876
Jeff Brownca9bc702014-02-11 14:32:56 -08003877 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003878 public int getStackId() {
3879 synchronized (mService) {
3880 return mStackId;
3881 }
3882 }
3883
3884 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003885 public boolean injectEvent(InputEvent event) {
3886 final long origId = Binder.clearCallingIdentity();
3887 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003888 synchronized (mService) {
3889 if (mActivityDisplay != null) {
3890 return mInputManagerInternal.injectInputEvent(event,
3891 mActivityDisplay.mDisplayId,
3892 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3893 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003894 }
3895 return false;
3896 } finally {
3897 Binder.restoreCallingIdentity(origId);
3898 }
3899 }
3900
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003901 @Override
3902 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003903 synchronized (mService) {
3904 if (mContainerState == CONTAINER_STATE_FINISHING) {
3905 return;
3906 }
3907 mContainerState = CONTAINER_STATE_FINISHING;
3908
Craig Mautnerd163e752014-06-13 17:18:47 -07003909 long origId = Binder.clearCallingIdentity();
3910 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003911 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003912 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003913 } finally {
3914 Binder.restoreCallingIdentity(origId);
3915 }
3916 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003917 }
3918
Craig Mautner60257702014-09-17 15:02:33 -07003919 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003920 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003921 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003922 if (mActivityDisplay != null) {
3923 mActivityDisplay.detachActivitiesLocked(mStack);
3924 mActivityDisplay = null;
3925 mStack.mDisplayId = -1;
3926 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003927 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003928 }
3929 }
3930
3931 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003932 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003933 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08003934 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003935 Binder.getCallingUid(), mCurrentUser, false,
3936 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003937
Craig Mautnere0a38842013-12-16 16:14:02 -08003938 // TODO: Switch to user app stacks here.
3939 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08003940 final Uri data = intent.getData();
3941 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
3942 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08003943 }
Craig Mautnerb9168362015-02-26 20:40:19 -08003944 checkEmbeddedAllowedInner(userId, intent, mimeType);
3945
3946 intent.addFlags(FORCE_NEW_TASK_FLAGS);
3947 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
3948 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003949 }
3950
3951 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003952 public final int startActivityIntentSender(IIntentSender intentSender)
3953 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003954 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3955
3956 if (!(intentSender instanceof PendingIntentRecord)) {
3957 throw new IllegalArgumentException("Bad PendingIntent object");
3958 }
3959
Craig Mautnerb9168362015-02-26 20:40:19 -08003960 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003961 Binder.getCallingUid(), mCurrentUser, false,
3962 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003963
3964 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3965 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3966 pendingIntent.key.requestResolvedType);
3967
3968 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3969 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3970 }
3971
3972 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003973 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003974 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003975 throw new SecurityException(
3976 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3977 }
3978 }
3979
Craig Mautnerdf88d732014-01-27 09:21:32 -08003980 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003981 public IBinder asBinder() {
3982 return this;
3983 }
3984
Craig Mautner4504de52013-12-20 09:06:56 -08003985 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003986 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003987 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003988 }
3989
Craig Mautner4a1cb222013-12-04 16:14:06 -08003990 ActivityStackSupervisor getOuter() {
3991 return ActivityStackSupervisor.this;
3992 }
3993
Craig Mautnerd163e752014-06-13 17:18:47 -07003994 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003995 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003996 }
3997
3998 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003999 synchronized (mService) {
4000 if (mActivityDisplay != null) {
4001 mActivityDisplay.getBounds(outBounds);
4002 } else {
4003 outBounds.set(0, 0);
4004 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004005 }
4006 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004007
Craig Mautner6985bad2014-04-21 15:22:06 -07004008 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004009 void setVisible(boolean visible) {
4010 if (mVisible != visible) {
4011 mVisible = visible;
4012 if (mCallback != null) {
4013 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4014 0 /* unused */, this).sendToTarget();
4015 }
4016 }
4017 }
4018
Craig Mautner6985bad2014-04-21 15:22:06 -07004019 void setDrawn() {
4020 }
4021
Craig Mautner1b4bf852014-05-26 15:06:32 -07004022 // You can always start a new task on a regular ActivityStack.
4023 boolean isEligibleForNewTasks() {
4024 return true;
4025 }
4026
Craig Mautnerd163e752014-06-13 17:18:47 -07004027 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004028 detachLocked();
4029 deleteActivityContainer(this);
4030 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004031 }
4032
Craig Mautner34b73df2014-01-12 21:11:08 -08004033 @Override
4034 public String toString() {
4035 return mIdString + (mActivityDisplay == null ? "N" : "A");
4036 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004037 }
4038
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004039 private class VirtualActivityContainer extends ActivityContainer {
4040 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004041 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004042
4043 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4044 super(getNextStackId());
4045 mParentActivity = parent;
4046 mCallback = callback;
4047 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004048 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004049 }
4050
4051 @Override
4052 public void setSurface(Surface surface, int width, int height, int density) {
4053 super.setSurface(surface, width, height, density);
4054
4055 synchronized (mService) {
4056 final long origId = Binder.clearCallingIdentity();
4057 try {
4058 setSurfaceLocked(surface, width, height, density);
4059 } finally {
4060 Binder.restoreCallingIdentity(origId);
4061 }
4062 }
4063 }
4064
4065 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4066 if (mContainerState == CONTAINER_STATE_FINISHING) {
4067 return;
4068 }
4069 VirtualActivityDisplay virtualActivityDisplay =
4070 (VirtualActivityDisplay) mActivityDisplay;
4071 if (virtualActivityDisplay == null) {
4072 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004073 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004074 mActivityDisplay = virtualActivityDisplay;
4075 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4076 attachToDisplayLocked(virtualActivityDisplay);
4077 }
4078
4079 if (mSurface != null) {
4080 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004081 }
4082
Craig Mautner6985bad2014-04-21 15:22:06 -07004083 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004084 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004085 mStack.resumeTopActivityLocked(null);
4086 } else {
4087 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004088 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004089 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004090 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004091 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004092 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004093
Craig Mautnerd163e752014-06-13 17:18:47 -07004094 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004095
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004096 if (DEBUG_STACK) Slog.d(TAG_STACK,
4097 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004098 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004099
Craig Mautner6985bad2014-04-21 15:22:06 -07004100 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004101 boolean isAttachedLocked() {
4102 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004103 }
4104
4105 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004106 void setDrawn() {
4107 synchronized (mService) {
4108 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004109 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004110 }
4111 }
4112
Craig Mautner1b4bf852014-05-26 15:06:32 -07004113 // Never start a new task on an ActivityView if it isn't explicitly specified.
4114 @Override
4115 boolean isEligibleForNewTasks() {
4116 return false;
4117 }
4118
Craig Mautnerd163e752014-06-13 17:18:47 -07004119 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004120 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004121 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4122 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4123 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4124 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4125 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004126 }
4127 }
4128
Craig Mautner4a1cb222013-12-04 16:14:06 -08004129 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4130 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004131 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004132 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004133 int mDisplayId;
4134 Display mDisplay;
4135 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004136
Craig Mautner4a1cb222013-12-04 16:14:06 -08004137 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4138 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004139 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004140
Jose Lima4b6c6692014-08-12 17:41:12 -07004141 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004142
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004143 ActivityDisplay() {
4144 }
Craig Mautner4504de52013-12-20 09:06:56 -08004145
Craig Mautner1a70a162014-09-13 12:09:31 -07004146 // After instantiation, check that mDisplay is not null before using this. The alternative
4147 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004148 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004149 final Display display = mDisplayManager.getDisplay(displayId);
4150 if (display == null) {
4151 return;
4152 }
4153 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004154 }
4155
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004156 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004157 mDisplay = display;
4158 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004159 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004160 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004161
4162 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004163 if (DEBUG_STACK) Slog.v(TAG_STACK,
4164 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004165 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004166 }
4167
Craig Mautnere0a38842013-12-16 16:14:02 -08004168 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004169 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004170 + " from displayId=" + mDisplayId);
4171 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004172 }
4173
4174 void getBounds(Point bounds) {
4175 mDisplay.getDisplayInfo(mDisplayInfo);
4176 bounds.x = mDisplayInfo.appWidth;
4177 bounds.y = mDisplayInfo.appHeight;
4178 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004179
Jose Lima4b6c6692014-08-12 17:41:12 -07004180 void setVisibleBehindActivity(ActivityRecord r) {
4181 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004182 }
4183
Jose Lima4b6c6692014-08-12 17:41:12 -07004184 boolean hasVisibleBehindActivity() {
4185 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004186 }
4187
Craig Mautner34b73df2014-01-12 21:11:08 -08004188 @Override
4189 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004190 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4191 }
4192 }
4193
4194 class VirtualActivityDisplay extends ActivityDisplay {
4195 VirtualDisplay mVirtualDisplay;
4196
Craig Mautner6985bad2014-04-21 15:22:06 -07004197 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004198 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004199 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4200 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4201 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4202 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004203
4204 init(mVirtualDisplay.getDisplay());
4205
4206 mWindowManager.handleDisplayAdded(mDisplayId);
4207 }
4208
4209 void setSurface(Surface surface) {
4210 if (mVirtualDisplay != null) {
4211 mVirtualDisplay.setSurface(surface);
4212 }
4213 }
4214
4215 @Override
4216 void detachActivitiesLocked(ActivityStack stack) {
4217 super.detachActivitiesLocked(stack);
4218 if (mVirtualDisplay != null) {
4219 mVirtualDisplay.release();
4220 mVirtualDisplay = null;
4221 }
4222 }
4223
4224 @Override
4225 public String toString() {
4226 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004227 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004228 }
Jose Lima58e66d62014-05-27 20:00:27 -07004229
4230 private boolean isLeanbackOnlyDevice() {
4231 boolean onLeanbackOnly = false;
4232 try {
4233 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4234 PackageManager.FEATURE_LEANBACK_ONLY);
4235 } catch (RemoteException e) {
4236 // noop
4237 }
4238
4239 return onLeanbackOnly;
4240 }
Craig Mautner27084302013-03-25 08:05:25 -07004241}