blob: bdb3201d66d74db066bda94ff6e173c87e8d0cce [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner15df08a2015-04-01 12:17:18 -070020import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070023import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070025import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
26import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070027import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner15df08a2015-04-01 12:17:18 -070028import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
Craig Mautner6170f732013-04-02 13:05:23 -070029import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080030import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070031import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070032import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
33import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070035import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
38import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
39import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070040
Craig Mautner2420ead2013-04-01 17:13:20 -070041import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080043import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.ActivityOptions;
45import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080046import android.app.IActivityContainer;
47import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070048import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070051import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070052import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070054import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040055import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040056import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070062import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.content.pm.ApplicationInfo;
64import android.content.pm.PackageManager;
65import android.content.pm.ResolveInfo;
66import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080067import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080068import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080069import android.hardware.display.DisplayManager;
70import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080071import android.hardware.display.DisplayManagerGlobal;
72import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080073import android.hardware.input.InputManager;
74import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080075import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070077import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070078import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070079import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070081import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070082import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070083import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070084import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070085import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070086import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040087import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070088import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070089import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070090import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070091import android.os.WorkSource;
Jason Monk62515be2014-05-21 16:06:19 -040092import android.provider.Settings;
93import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070094import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070095import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070096import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070097import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080098import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070099
Craig Mautner4a1cb222013-12-04 16:14:06 -0800100import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800101import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800103import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800104import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700105import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700106import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800107import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700108import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400109import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400110import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800111import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700112import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700113import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114
justinzhang5286d3f2014-05-12 17:06:01 -0400115
Craig Mautner8d341ef2013-03-26 09:03:27 -0700116import java.io.FileDescriptor;
117import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700118import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700119import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700120import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700121
Craig Mautner4a1cb222013-12-04 16:14:06 -0800122public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800123 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700124 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
125 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700126 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
127 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
128 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
129 private static final String TAG_STACK = TAG + POSTFIX_STACK;
130 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
131 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
132 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800133
Wale Ogunwaled57969f2014-11-15 19:37:29 -0800134 static final boolean DEBUG = DEBUG_ALL || false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700135 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
136 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700137 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700138 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700139 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700140 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
141 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
Craig Mautnereb0e38a2015-04-02 20:22:54 -0700142 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700143 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700144
Craig Mautner2219a1b2013-03-25 09:44:30 -0700145 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700146
Craig Mautnerf3333272013-04-22 10:55:53 -0700147 /** How long we wait until giving up on the last activity telling us it is idle. */
148 static final int IDLE_TIMEOUT = 10*1000;
149
Craig Mautner0eea92c2013-05-16 13:35:39 -0700150 /** How long we can hold the sleep wake lock before giving up. */
151 static final int SLEEP_TIMEOUT = 5*1000;
152
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700153 // How long we can hold the launch wake lock before giving up.
154 static final int LAUNCH_TIMEOUT = 10*1000;
155
Craig Mautner05d29032013-05-03 13:40:13 -0700156 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
157 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
158 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700159 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700160 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
162 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
163 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700164 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400165 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
166 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700167 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700168 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700169 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800170
Craig Mautner4504de52013-12-20 09:06:56 -0800171 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700172
Jason Monk62515be2014-05-21 16:06:19 -0400173 private static final String LOCK_TASK_TAG = "Lock-to-App";
174
justinzhang5286d3f2014-05-12 17:06:01 -0400175 /** Status Bar Service **/
176 private IBinder mToken = new Binder();
177 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400178 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400179
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700180 // For debugging to make sure the caller when acquiring/releasing our
181 // wake lock is the system process.
182 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700183
Craig Mautner27084302013-03-25 08:05:25 -0700184 final ActivityManagerService mService;
185
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800186 private final RecentTasks mRecentTasks;
187
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700188 final ActivityStackSupervisorHandler mHandler;
189
190 /** Short cut */
191 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800192 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700193
Craig Mautner8d341ef2013-03-26 09:03:27 -0700194 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700195 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700196
197 /** Task identifier that activities are currently being started in. Incremented each time a
198 * new task is created. */
199 private int mCurTaskId = 0;
200
Craig Mautner2420ead2013-04-01 17:13:20 -0700201 /** The current user */
202 private int mCurrentUser;
203
Craig Mautnere0a38842013-12-16 16:14:02 -0800204 /** The stack containing the launcher app. Assumed to always be attached to
205 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700206 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700207
Craig Mautnere0a38842013-12-16 16:14:02 -0800208 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700209 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700210
Craig Mautner4a1cb222013-12-04 16:14:06 -0800211 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
212 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800213 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800214 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700215
216 /** List of activities that are waiting for a new activity to become visible before completing
217 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800218 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700219
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700220 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800221 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700222
223 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800224 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700225
Craig Mautnerde4ef022013-04-07 19:01:33 -0700226 /** List of activities that are ready to be stopped, but waiting for the next activity to
227 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800228 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700229
Craig Mautnerf3333272013-04-22 10:55:53 -0700230 /** List of activities that are ready to be finished, but waiting for the previous activity to
231 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800232 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700233
Craig Mautner0eea92c2013-05-16 13:35:39 -0700234 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800235 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700236
Craig Mautnerf3333272013-04-22 10:55:53 -0700237 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800238 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700239
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700240 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800241 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700242
Craig Mautnerde4ef022013-04-07 19:01:33 -0700243 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
244 * is being brought in front of us. */
245 boolean mUserLeaving = false;
246
Craig Mautner0eea92c2013-05-16 13:35:39 -0700247 /** Set when we have taken too long waiting to go to sleep. */
248 boolean mSleepTimeout = false;
249
Jose Lima58e66d62014-05-27 20:00:27 -0700250 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
251 * setWindowManager is called. **/
252 private boolean mLeanbackOnlyDevice;
253
Craig Mautner0eea92c2013-05-16 13:35:39 -0700254 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700255 * We don't want to allow the device to go to sleep while in the process
256 * of launching an activity. This is primarily to allow alarm intent
257 * receivers to launch an activity and get that to run before the device
258 * goes back to sleep.
259 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700260 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700261
262 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700263 * Set when the system is going to sleep, until we have
264 * successfully paused the current activity and released our wake lock.
265 * At that point the system is allowed to actually sleep.
266 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700267 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700268
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700269 /** Stack id of the front stack when user switched, indexed by userId. */
270 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700271
Craig Mautner4504de52013-12-20 09:06:56 -0800272 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800273 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700274 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800275
276 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700277 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800278
Jeff Brownca9bc702014-02-11 14:32:56 -0800279 InputManagerInternal mInputManagerInternal;
280
Craig Mautner15df08a2015-04-01 12:17:18 -0700281 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
282 * may be finished until there is only one entry left. If this is empty the system is not
283 * in lockTask mode. */
284 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000285 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700286 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
287 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000288 */
289 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400290 /**
291 * Notifies the user when entering/exiting lock-task.
292 */
293 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800294
Craig Mautner15df08a2015-04-01 12:17:18 -0700295 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700296
Craig Mautner42d04db2014-11-06 12:13:23 -0800297 /** Used to keep resumeTopActivityLocked() from being entered recursively */
298 boolean inResumeTopActivity;
299
Craig Mautneree36c772014-07-16 14:56:05 -0700300 /**
301 * Description of a request to start a new activity, which has been held
302 * due to app switches being disabled.
303 */
304 static class PendingActivityLaunch {
305 final ActivityRecord r;
306 final ActivityRecord sourceRecord;
307 final int startFlags;
308 final ActivityStack stack;
309
310 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
311 int _startFlags, ActivityStack _stack) {
312 r = _r;
313 sourceRecord = _sourceRecord;
314 startFlags = _startFlags;
315 stack = _stack;
316 }
317 }
318
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800319 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700320 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800321 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700322 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
323 }
324
325 /**
326 * At the time when the constructor runs, the power manager has not yet been
327 * initialized. So we initialize our wakelocks afterwards.
328 */
329 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800330 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700331 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700332 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700333 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700334 }
335
justinzhang5286d3f2014-05-12 17:06:01 -0400336 // This function returns a IStatusBarService. The value is from ServiceManager.
337 // getService and is cached.
338 private IStatusBarService getStatusBarService() {
339 synchronized (mService) {
340 if (mStatusBarService == null) {
341 mStatusBarService = IStatusBarService.Stub.asInterface(
342 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
343 if (mStatusBarService == null) {
344 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
345 }
346 }
347 return mStatusBarService;
348 }
349 }
350
Jason Monk35c62a42014-06-17 10:24:47 -0400351 private IDevicePolicyManager getDevicePolicyManager() {
352 synchronized (mService) {
353 if (mDevicePolicyManager == null) {
354 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
355 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
356 if (mDevicePolicyManager == null) {
357 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
358 }
359 }
360 return mDevicePolicyManager;
361 }
362 }
363
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700364 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800365 synchronized (mService) {
366 mWindowManager = wm;
367
368 mDisplayManager =
369 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
370 mDisplayManager.registerDisplayListener(this, null);
371
372 Display[] displays = mDisplayManager.getDisplays();
373 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
374 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800375 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700376 if (activityDisplay.mDisplay == null) {
377 throw new IllegalStateException("Default Display does not exist");
378 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800379 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380 }
381
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700382 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800384
385 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700386
387 // Initialize this here, now that we can get a valid reference to PackageManager.
388 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800389 }
Craig Mautner27084302013-03-25 08:05:25 -0700390 }
391
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200392 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700393 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200394 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700395 }
396
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700397 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800398 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700399 }
400
Craig Mautnerde4ef022013-04-07 19:01:33 -0700401 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800402 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700403 }
404
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800405 /** Top of all visible stacks is/should always be equal to the focused stack.
406 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
407 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700408 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700409 if (stack == null) {
410 return false;
411 }
412
Craig Mautnerdf88d732014-01-27 09:21:32 -0800413 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
414 if (parent != null) {
415 stack = parent.task.stack;
416 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800417 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700418 }
419
Craig Mautner299f9602015-01-26 09:47:33 -0800420 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800421 moveHomeStack(toFront, reason, null);
422 }
423
424 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800425 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800426 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800427 if (topNdx <= 0) {
428 return;
429 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700430
431 // The home stack should either be at the top or bottom of the stack list.
432 if ((toFront && (stacks.get(topNdx) != mHomeStack))
433 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700434 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700435 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
436 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800437 stacks.remove(mHomeStack);
438 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700439 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800440
441 if (lastFocusedStack != null) {
442 mLastFocusedStack = lastFocusedStack;
443 }
444 mFocusedStack = stacks.get(topNdx);
445
Craig Mautnerde313752015-01-22 14:28:03 -0800446 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
447 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800448 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800449
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800450 if (mService.mBooting || !mService.mBooted) {
451 final ActivityRecord r = topRunningActivityLocked();
452 if (r != null && r.idle) {
453 checkFinishBootingLocked();
454 }
455 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700456 }
457
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700458 /** Returns true if the focus activity was adjusted to the home stack top activity. */
459 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700460 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
461 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700462 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700463 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700464
Craig Mautner84984fa2014-06-19 11:19:20 -0700465 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700466
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700467 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700468 if (top == null) {
469 return false;
470 }
471 mService.setFocusedActivityLocked(top, reason);
472 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700473 }
474
Craig Mautner299f9602015-01-26 09:47:33 -0800475 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700476 if (!mService.mBooting && !mService.mBooted) {
477 // Not ready yet!
478 return false;
479 }
480
Craig Mautner84984fa2014-06-19 11:19:20 -0700481 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
482 mWindowManager.showRecentApps();
483 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700484 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700485
Craig Mautner84984fa2014-06-19 11:19:20 -0700486 if (prev != null) {
487 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
488 }
489
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700490 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
491 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700492 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800493 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700494 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700495 }
Craig Mautner299f9602015-01-26 09:47:33 -0800496 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700497 }
498
Craig Mautner8d341ef2013-03-26 09:03:27 -0700499 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700500 return anyTaskForIdLocked(id, true);
501 }
502
503 /**
504 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
505 * @param id Id of the task we would like returned.
506 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
507 * restore the task from recents to the active list.
508 */
509 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800510 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800511 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800512 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800513 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
514 ActivityStack stack = stacks.get(stackNdx);
515 TaskRecord task = stack.taskForIdLocked(id);
516 if (task != null) {
517 return task;
518 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700519 }
520 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800521
522 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700523 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800524 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800525 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700526 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800527 return null;
528 }
529
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700530 if (!restoreFromRecents) {
531 return task;
532 }
533
Wale Ogunwale7de05352014-12-12 15:21:33 -0800534 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700535 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
536 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800537 return null;
538 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700539 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800540 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700541 }
542
Craig Mautner6170f732013-04-02 13:05:23 -0700543 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800544 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800546 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800547 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
548 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
549 if (r != null) {
550 return r;
551 }
Craig Mautner6170f732013-04-02 13:05:23 -0700552 }
553 }
554 return null;
555 }
556
Craig Mautneref73ee12014-04-23 11:45:37 -0700557 void setNextTaskId(int taskId) {
558 if (taskId > mCurTaskId) {
559 mCurTaskId = taskId;
560 }
561 }
562
Craig Mautner8d341ef2013-03-26 09:03:27 -0700563 int getNextTaskId() {
564 do {
565 mCurTaskId++;
566 if (mCurTaskId <= 0) {
567 mCurTaskId = 1;
568 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700569 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700570 return mCurTaskId;
571 }
572
Craig Mautnerde4ef022013-04-07 19:01:33 -0700573 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800574 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700575 if (stack == null) {
576 return null;
577 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700578 ActivityRecord resumedActivity = stack.mResumedActivity;
579 if (resumedActivity == null || resumedActivity.app == null) {
580 resumedActivity = stack.mPausingActivity;
581 if (resumedActivity == null || resumedActivity.app == null) {
582 resumedActivity = stack.topRunningActivityLocked(null);
583 }
584 }
585 return resumedActivity;
586 }
587
Dianne Hackbornff072722014-09-24 10:56:28 -0700588 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700589 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800591 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
592 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800593 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
594 final ActivityStack stack = stacks.get(stackNdx);
595 if (!isFrontStack(stack)) {
596 continue;
597 }
598 ActivityRecord hr = stack.topRunningActivityLocked(null);
599 if (hr != null) {
600 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
601 && processName.equals(hr.processName)) {
602 try {
George Mount2c92c972014-03-20 09:38:23 -0700603 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800604 didSomething = true;
605 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700606 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607 Slog.w(TAG, "Exception in new application when starting activity "
608 + hr.intent.getComponent().flattenToShortString(), e);
609 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700610 }
Craig Mautner20e72272013-04-01 13:45:53 -0700611 }
Craig Mautner20e72272013-04-01 13:45:53 -0700612 }
613 }
614 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700615 if (!didSomething) {
616 ensureActivitiesVisibleLocked(null, 0);
617 }
Craig Mautner20e72272013-04-01 13:45:53 -0700618 return didSomething;
619 }
620
621 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800622 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
623 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800624 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
625 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800626 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 continue;
628 }
629 final ActivityRecord resumedActivity = stack.mResumedActivity;
630 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800631 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
632 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800633 return false;
634 }
Craig Mautner20e72272013-04-01 13:45:53 -0700635 }
636 }
637 return true;
638 }
639
Craig Mautnerde4ef022013-04-07 19:01:33 -0700640 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800641 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
642 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800643 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
644 final ActivityStack stack = stacks.get(stackNdx);
645 if (isFrontStack(stack)) {
646 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700647 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800648 return false;
649 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700650 }
651 }
652 }
653 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700654 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
656 mLastFocusedStack + " to=" + mFocusedStack);
657 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700658 return true;
659 }
660
661 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800662 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800663 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
664 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
666 final ActivityStack stack = stacks.get(stackNdx);
667 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800668 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700669 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800670 return false;
671 }
672 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800673 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700674 }
675 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800676 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700677 }
678
Craig Mautner2acc3892013-09-23 10:28:14 -0700679 /**
680 * Pause all activities in either all of the stacks or just the back stacks.
681 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700682 * @return true if any activity was paused as a result of this call.
683 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700684 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700685 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800686 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
687 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800688 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
689 final ActivityStack stack = stacks.get(stackNdx);
690 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
691 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
692 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700693 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
694 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800695 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700696 }
697 }
698 return someActivityPaused;
699 }
700
Craig Mautnerde4ef022013-04-07 19:01:33 -0700701 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700702 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
704 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800705 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
706 final ActivityStack stack = stacks.get(stackNdx);
707 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700708 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800709 if (DEBUG_STATES) {
710 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
711 pausing = false;
712 } else {
713 return false;
714 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700715 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700716 }
717 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700718 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700719 }
720
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700721 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
722 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500723 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800724 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
725 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
726 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
727 final ActivityStack stack = stacks.get(stackNdx);
728 if (stack.mResumedActivity != null &&
729 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700730 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800731 }
732 }
733 }
734 }
735
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700736 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700737 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700738 }
739
740 void sendWaitingVisibleReportLocked(ActivityRecord r) {
741 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700742 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700743 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700744 if (w.who == null) {
745 changed = true;
746 w.timeout = false;
747 if (r != null) {
748 w.who = new ComponentName(r.info.packageName, r.info.name);
749 }
750 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
751 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700752 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700753 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700754 if (changed) {
755 mService.notifyAll();
756 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700757 }
758
759 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
760 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700761 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700762 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700763 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700764 if (w.who == null) {
765 changed = true;
766 w.timeout = timeout;
767 if (r != null) {
768 w.who = new ComponentName(r.info.packageName, r.info.name);
769 }
770 w.thisTime = thisTime;
771 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700772 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700773 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700774 if (changed) {
775 mService.notifyAll();
776 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700777 }
778
Craig Mautner29219d92013-04-16 20:19:12 -0700779 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800780 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700781 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
782 if (r != null) {
783 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700784 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700785
Craig Mautner4a1cb222013-12-04 16:14:06 -0800786 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800787 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800788 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
789 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700790 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700791 r = stack.topRunningActivityLocked(null);
792 if (r != null) {
793 return r;
794 }
795 }
796 }
797 return null;
798 }
799
Dianne Hackborn09233282014-04-30 11:33:59 -0700800 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700801 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800802 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
803 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800804 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800805 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800806 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800807 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
808 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700809 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800810 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700811 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700812 }
813 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700814
815 // The lists are already sorted from most recent to oldest. Just pull the most recent off
816 // each list and add it to list. Stop when all lists are empty or maxNum reached.
817 while (maxNum > 0) {
818 long mostRecentActiveTime = Long.MIN_VALUE;
819 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800820 final int numTaskLists = runningTaskLists.size();
821 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
822 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700823 if (!stackTaskList.isEmpty()) {
824 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
825 if (lastActiveTime > mostRecentActiveTime) {
826 mostRecentActiveTime = lastActiveTime;
827 selectedStackList = stackTaskList;
828 }
829 }
830 }
831 if (selectedStackList != null) {
832 list.add(selectedStackList.remove(0));
833 --maxNum;
834 } else {
835 break;
836 }
837 }
Craig Mautner20e72272013-04-01 13:45:53 -0700838 }
839
Craig Mautner23ac33b2013-04-01 16:26:35 -0700840 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700841 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700842 // Collect information about the target of the Intent.
843 ActivityInfo aInfo;
844 try {
845 ResolveInfo rInfo =
846 AppGlobals.getPackageManager().resolveIntent(
847 intent, resolvedType,
848 PackageManager.MATCH_DEFAULT_ONLY
849 | ActivityManagerService.STOCK_PM_FLAGS, userId);
850 aInfo = rInfo != null ? rInfo.activityInfo : null;
851 } catch (RemoteException e) {
852 aInfo = null;
853 }
854
855 if (aInfo != null) {
856 // Store the found target back into the intent, because now that
857 // we have it we never want to do this again. For example, if the
858 // user navigates back to this point in the history, we should
859 // always restart the exact same activity.
860 intent.setComponent(new ComponentName(
861 aInfo.applicationInfo.packageName, aInfo.name));
862
863 // Don't debug things in the system process
864 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
865 if (!aInfo.processName.equals("system")) {
866 mService.setDebugApp(aInfo.processName, true, false);
867 }
868 }
869
870 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
871 if (!aInfo.processName.equals("system")) {
872 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
873 }
874 }
875
Jeff Hao1b012d32014-08-20 10:35:34 -0700876 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700877 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700878 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700879 }
880 }
881 }
882 return aInfo;
883 }
884
Craig Mautner299f9602015-01-26 09:47:33 -0800885 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
886 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700887 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
888 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700889 }
890
Craig Mautner23ac33b2013-04-01 16:26:35 -0700891 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700892 String callingPackage, Intent intent, String resolvedType,
893 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700894 IBinder resultTo, String resultWho, int requestCode, int startFlags,
895 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700896 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700897 // Refuse possible leaked file descriptors
898 if (intent != null && intent.hasFileDescriptors()) {
899 throw new IllegalArgumentException("File descriptors passed in Intent");
900 }
901 boolean componentSpecified = intent.getComponent() != null;
902
903 // Don't modify the client's object!
904 intent = new Intent(intent);
905
906 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700907 ActivityInfo aInfo =
908 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700909
Craig Mautnere0a38842013-12-16 16:14:02 -0800910 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700911 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800912 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700913 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800914 // Cannot start a child activity if the parent is not resumed.
915 return ActivityManager.START_CANCELED;
916 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700917 final int realCallingPid = Binder.getCallingPid();
918 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700919 int callingPid;
920 if (callingUid >= 0) {
921 callingPid = -1;
922 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700923 callingPid = realCallingPid;
924 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700925 } else {
926 callingPid = callingUid = -1;
927 }
928
Craig Mautnere0a38842013-12-16 16:14:02 -0800929 final ActivityStack stack;
930 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800931 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800932 } else {
933 stack = container.mStack;
934 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800935 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700936 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700937 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700938
939 final long origId = Binder.clearCallingIdentity();
940
941 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800942 (aInfo.applicationInfo.privateFlags
943 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700944 // This may be a heavy-weight process! Check to see if we already
945 // have another, different heavy-weight process running.
946 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
947 if (mService.mHeavyWeightProcess != null &&
948 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
949 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700950 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700951 if (caller != null) {
952 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
953 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700954 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700955 } else {
956 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700957 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700958 + intent.toString());
959 ActivityOptions.abort(options);
960 return ActivityManager.START_PERMISSION_DENIED;
961 }
962 }
963
964 IIntentSender target = mService.getIntentSenderLocked(
965 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700966 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700967 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
968 | PendingIntent.FLAG_ONE_SHOT, null);
969
970 Intent newIntent = new Intent();
971 if (requestCode >= 0) {
972 // Caller is requesting a result.
973 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
974 }
975 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
976 new IntentSender(target));
977 if (mService.mHeavyWeightProcess.activities.size() > 0) {
978 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
979 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
980 hist.packageName);
981 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
982 hist.task.taskId);
983 }
984 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
985 aInfo.packageName);
986 newIntent.setFlags(intent.getFlags());
987 newIntent.setClassName("android",
988 HeavyWeightSwitcherActivity.class.getName());
989 intent = newIntent;
990 resolvedType = null;
991 caller = null;
992 callingUid = Binder.getCallingUid();
993 callingPid = Binder.getCallingPid();
994 componentSpecified = true;
995 try {
996 ResolveInfo rInfo =
997 AppGlobals.getPackageManager().resolveIntent(
998 intent, null,
999 PackageManager.MATCH_DEFAULT_ONLY
1000 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1001 aInfo = rInfo != null ? rInfo.activityInfo : null;
1002 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1003 } catch (RemoteException e) {
1004 aInfo = null;
1005 }
1006 }
1007 }
1008 }
1009
Dianne Hackborn91097de2014-04-04 18:02:06 -07001010 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1011 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001012 requestCode, callingPid, callingUid, callingPackage,
1013 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001014 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001015
Craig Mautner85c11a82014-07-17 12:38:18 -07001016 Binder.restoreCallingIdentity(origId);
1017
Craig Mautnerde4ef022013-04-07 19:01:33 -07001018 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001019 // If the caller also wants to switch to a new configuration,
1020 // do so now. This allows a clean switch, as we are waiting
1021 // for the current activity to pause (so we will not destroy
1022 // it), and have not yet started the next activity.
1023 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1024 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001025 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001026 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001027 "Updating to new configuration after starting activity.");
1028 mService.updateConfigurationLocked(config, null, false, false);
1029 }
1030
Craig Mautner23ac33b2013-04-01 16:26:35 -07001031 if (outResult != null) {
1032 outResult.result = res;
1033 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001034 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001035 do {
1036 try {
1037 mService.wait();
1038 } catch (InterruptedException e) {
1039 }
1040 } while (!outResult.timeout && outResult.who == null);
1041 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001042 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001043 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001044 outResult.timeout = false;
1045 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1046 outResult.totalTime = 0;
1047 outResult.thisTime = 0;
1048 } else {
1049 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001050 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001051 do {
1052 try {
1053 mService.wait();
1054 } catch (InterruptedException e) {
1055 }
1056 } while (!outResult.timeout && outResult.who == null);
1057 }
1058 }
1059 }
1060
1061 return res;
1062 }
1063 }
1064
1065 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1066 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1067 Bundle options, int userId) {
1068 if (intents == null) {
1069 throw new NullPointerException("intents is null");
1070 }
1071 if (resolvedTypes == null) {
1072 throw new NullPointerException("resolvedTypes is null");
1073 }
1074 if (intents.length != resolvedTypes.length) {
1075 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1076 }
1077
Craig Mautner23ac33b2013-04-01 16:26:35 -07001078
1079 int callingPid;
1080 if (callingUid >= 0) {
1081 callingPid = -1;
1082 } else if (caller == null) {
1083 callingPid = Binder.getCallingPid();
1084 callingUid = Binder.getCallingUid();
1085 } else {
1086 callingPid = callingUid = -1;
1087 }
1088 final long origId = Binder.clearCallingIdentity();
1089 try {
1090 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001091 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001092 for (int i=0; i<intents.length; i++) {
1093 Intent intent = intents[i];
1094 if (intent == null) {
1095 continue;
1096 }
1097
1098 // Refuse possible leaked file descriptors
1099 if (intent != null && intent.hasFileDescriptors()) {
1100 throw new IllegalArgumentException("File descriptors passed in Intent");
1101 }
1102
1103 boolean componentSpecified = intent.getComponent() != null;
1104
1105 // Don't modify the client's object!
1106 intent = new Intent(intent);
1107
1108 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001109 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001110 // TODO: New, check if this is correct
1111 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1112
1113 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001114 (aInfo.applicationInfo.privateFlags
1115 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001116 throw new IllegalArgumentException(
1117 "FLAG_CANT_SAVE_STATE not supported here");
1118 }
1119
1120 Bundle theseOptions;
1121 if (options != null && i == intents.length-1) {
1122 theseOptions = options;
1123 } else {
1124 theseOptions = null;
1125 }
Craig Mautner6170f732013-04-02 13:05:23 -07001126 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001127 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1128 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001129 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001130 if (res < 0) {
1131 return res;
1132 }
1133
1134 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1135 }
1136 }
1137 } finally {
1138 Binder.restoreCallingIdentity(origId);
1139 }
1140
1141 return ActivityManager.START_SUCCESS;
1142 }
1143
Craig Mautner2420ead2013-04-01 17:13:20 -07001144 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001145 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001146 throws RemoteException {
1147
Craig Mautner2568c3a2015-03-26 14:22:34 -07001148 if (andResume) {
1149 r.startFreezingScreenLocked(app, 0);
1150 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001151
Craig Mautner2568c3a2015-03-26 14:22:34 -07001152 // schedule launch ticks to collect information about slow apps.
1153 r.startLaunchTickingLocked();
1154 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001155
1156 // Have the window manager re-evaluate the orientation of
1157 // the screen based on the new activity order. Note that
1158 // as a result of this, it can call back into the activity
1159 // manager with a new orientation. We don't care about that,
1160 // because the activity is not currently running so we are
1161 // just restarting it anyway.
1162 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001163 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001164 mService.mConfiguration,
1165 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1166 mService.updateConfigurationLocked(config, r, false, false);
1167 }
1168
1169 r.app = app;
1170 app.waitingToKill = null;
1171 r.launchCount++;
1172 r.lastLaunchTime = SystemClock.uptimeMillis();
1173
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001174 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001175
1176 int idx = app.activities.indexOf(r);
1177 if (idx < 0) {
1178 app.activities.add(r);
1179 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001180 mService.updateLruProcessLocked(app, true, null);
1181 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001182
Craig Mautner15df08a2015-04-01 12:17:18 -07001183 final TaskRecord task = r.task;
1184 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
1185 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "lockTaskLaunchMode attribute");
1186 }
1187
1188 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001189 try {
1190 if (app.thread == null) {
1191 throw new RemoteException();
1192 }
1193 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001194 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001195 if (andResume) {
1196 results = r.results;
1197 newIntents = r.newIntents;
1198 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001199 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1200 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1201 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 if (andResume) {
1203 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1204 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001205 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001206 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001207 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001208 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001209 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001210 }
1211 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1212 r.sleeping = false;
1213 r.forceNewConfig = false;
1214 mService.showAskCompatModeDialogLocked(r);
1215 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001216 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001217 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1218 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1219 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001220 final String profileFile = mService.mProfileFile;
1221 if (profileFile != null) {
1222 ParcelFileDescriptor profileFd = mService.mProfileFd;
1223 if (profileFd != null) {
1224 try {
1225 profileFd = profileFd.dup();
1226 } catch (IOException e) {
1227 if (profileFd != null) {
1228 try {
1229 profileFd.close();
1230 } catch (IOException o) {
1231 }
1232 profileFd = null;
1233 }
1234 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001235 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001236
1237 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1238 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001239 }
1240 }
1241 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001242
Craig Mautner2568c3a2015-03-26 14:22:34 -07001243 if (andResume) {
1244 app.hasShownUi = true;
1245 app.pendingUiClean = true;
1246 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001247 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001248 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001249 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001250 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001251 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001252 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001253
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001254 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 // This may be a heavy-weight process! Note that the package
1256 // manager will ensure that only activity can run in the main
1257 // process of the .apk, which is the only thing that will be
1258 // considered heavy-weight.
1259 if (app.processName.equals(app.info.packageName)) {
1260 if (mService.mHeavyWeightProcess != null
1261 && mService.mHeavyWeightProcess != app) {
1262 Slog.w(TAG, "Starting new heavy weight process " + app
1263 + " when already running "
1264 + mService.mHeavyWeightProcess);
1265 }
1266 mService.mHeavyWeightProcess = app;
1267 Message msg = mService.mHandler.obtainMessage(
1268 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1269 msg.obj = r;
1270 mService.mHandler.sendMessage(msg);
1271 }
1272 }
1273
1274 } catch (RemoteException e) {
1275 if (r.launchFailed) {
1276 // This is the second time we failed -- finish activity
1277 // and give up.
1278 Slog.e(TAG, "Second failure launching "
1279 + r.intent.getComponent().flattenToShortString()
1280 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001281 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001282 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1283 "2nd-crash", false);
1284 return false;
1285 }
1286
1287 // This is the first time we failed -- restart process and
1288 // retry.
1289 app.activities.remove(r);
1290 throw e;
1291 }
1292
1293 r.launchFailed = false;
1294 if (stack.updateLRUListLocked(r)) {
1295 Slog.w(TAG, "Activity " + r
1296 + " being launched, but already in LRU list");
1297 }
1298
1299 if (andResume) {
1300 // As part of the process of launching, ActivityThread also performs
1301 // a resume.
1302 stack.minimalResumeActivityLocked(r);
1303 } else {
1304 // This activity is not starting in the resumed state... which
1305 // should look like we asked it to pause+stop (but remain visible),
1306 // and it has done so and reported back the current icicle and
1307 // other state.
1308 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1309 + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001310 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001311 r.stopped = true;
1312 }
1313
1314 // Launch the new version setup screen if needed. We do this -after-
1315 // launching the initial activity (that is, home), so that it can have
1316 // a chance to initialize itself while in the background, making the
1317 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001318 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001319 mService.startSetupActivityLocked();
1320 }
1321
Dianne Hackborn465fa392014-09-14 14:21:18 -07001322 // Update any services we are bound to that might care about whether
1323 // their client may have activities.
1324 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1325
Craig Mautner2420ead2013-04-01 17:13:20 -07001326 return true;
1327 }
1328
Craig Mautnere79d42682013-04-01 19:01:53 -07001329 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001330 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001331 // Is this activity's application already running?
1332 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001333 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001334
1335 r.task.stack.setLaunchTime(r);
1336
1337 if (app != null && app.thread != null) {
1338 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001339 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1340 || !"android".equals(r.info.packageName)) {
1341 // Don't add this if it is a platform component that is marked
1342 // to run in multiple processes, because this is actually
1343 // part of the framework so doesn't make sense to track as a
1344 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001345 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1346 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001347 }
George Mount2c92c972014-03-20 09:38:23 -07001348 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001349 return;
1350 } catch (RemoteException e) {
1351 Slog.w(TAG, "Exception when starting activity "
1352 + r.intent.getComponent().flattenToShortString(), e);
1353 }
1354
1355 // If a dead object exception was thrown -- fall through to
1356 // restart the application.
1357 }
1358
1359 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001360 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001361 }
1362
Craig Mautner6170f732013-04-02 13:05:23 -07001363 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001364 Intent intent, String resolvedType, ActivityInfo aInfo,
1365 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1366 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001367 int callingPid, int callingUid, String callingPackage,
1368 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001369 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1370 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001371 int err = ActivityManager.START_SUCCESS;
1372
1373 ProcessRecord callerApp = null;
1374 if (caller != null) {
1375 callerApp = mService.getRecordForAppLocked(caller);
1376 if (callerApp != null) {
1377 callingPid = callerApp.pid;
1378 callingUid = callerApp.info.uid;
1379 } else {
1380 Slog.w(TAG, "Unable to find app for caller " + caller
1381 + " (pid=" + callingPid + ") when starting: "
1382 + intent.toString());
1383 err = ActivityManager.START_PERMISSION_DENIED;
1384 }
1385 }
1386
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001387 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1388
Craig Mautner6170f732013-04-02 13:05:23 -07001389 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001390 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001391 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001392 + " on display " + (container == null ? (mFocusedStack == null ?
1393 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1394 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1395 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001396 }
1397
1398 ActivityRecord sourceRecord = null;
1399 ActivityRecord resultRecord = null;
1400 if (resultTo != null) {
1401 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001402 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1403 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001404 if (sourceRecord != null) {
1405 if (requestCode >= 0 && !sourceRecord.finishing) {
1406 resultRecord = sourceRecord;
1407 }
1408 }
1409 }
Craig Mautner6170f732013-04-02 13:05:23 -07001410
Dianne Hackborn91097de2014-04-04 18:02:06 -07001411 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001412
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001413 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001414 // Transfer the result target from the source activity to the new
1415 // one being started, including any failures.
1416 if (requestCode >= 0) {
1417 ActivityOptions.abort(options);
1418 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1419 }
1420 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001421 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1422 resultRecord = null;
1423 }
Craig Mautner6170f732013-04-02 13:05:23 -07001424 resultWho = sourceRecord.resultWho;
1425 requestCode = sourceRecord.requestCode;
1426 sourceRecord.resultTo = null;
1427 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001428 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001429 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001430 if (sourceRecord.launchedFromUid == callingUid) {
1431 // The new activity is being launched from the same uid as the previous
1432 // activity in the flow, and asking to forward its result back to the
1433 // previous. In this case the activity is serving as a trampoline between
1434 // the two, so we also want to update its launchedFromPackage to be the
1435 // same as the previous activity. Note that this is safe, since we know
1436 // these two packages come from the same uid; the caller could just as
1437 // well have supplied that same package name itself. This specifially
1438 // deals with the case of an intent picker/chooser being launched in the app
1439 // flow to redirect to an activity picked by the user, where we want the final
1440 // activity to consider it to have been launched by the previous app activity.
1441 callingPackage = sourceRecord.launchedFromPackage;
1442 }
Craig Mautner6170f732013-04-02 13:05:23 -07001443 }
1444
1445 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1446 // We couldn't find a class that can handle the given Intent.
1447 // That's the end of that!
1448 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1449 }
1450
1451 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1452 // We couldn't find the specific class specified in the Intent.
1453 // Also the end of the line.
1454 err = ActivityManager.START_CLASS_NOT_FOUND;
1455 }
1456
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001457 if (err == ActivityManager.START_SUCCESS
1458 && !isCurrentProfileLocked(userId)
1459 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1460 // Trying to launch a background activity that doesn't show for all users.
1461 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1462 }
1463
Dianne Hackborn91097de2014-04-04 18:02:06 -07001464 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1465 && sourceRecord.task.voiceSession != null) {
1466 // If this activity is being launched as part of a voice session, we need
1467 // to ensure that it is safe to do so. If the upcoming activity will also
1468 // be part of the voice session, we can only launch it if it has explicitly
1469 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001470 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001471 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1472 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001473 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1474 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001475 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1476 }
1477 } catch (RemoteException e) {
1478 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1479 }
1480 }
1481 }
1482
1483 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1484 // If the caller is starting a new voice session, just make sure the target
1485 // is actually allowing it to run this way.
1486 try {
1487 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1488 intent, resolvedType)) {
1489 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1490 }
1491 } catch (RemoteException e) {
1492 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1493 }
1494 }
1495
louis_changcd5d1982014-08-01 10:09:08 +08001496 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1497
Craig Mautner6170f732013-04-02 13:05:23 -07001498 if (err != ActivityManager.START_SUCCESS) {
1499 if (resultRecord != null) {
1500 resultStack.sendActivityResultLocked(-1,
1501 resultRecord, resultWho, requestCode,
1502 Activity.RESULT_CANCELED, null);
1503 }
Craig Mautner6170f732013-04-02 13:05:23 -07001504 ActivityOptions.abort(options);
1505 return err;
1506 }
1507
1508 final int startAnyPerm = mService.checkPermission(
1509 START_ANY_ACTIVITY, callingPid, callingUid);
1510 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1511 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1512 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1513 if (resultRecord != null) {
1514 resultStack.sendActivityResultLocked(-1,
1515 resultRecord, resultWho, requestCode,
1516 Activity.RESULT_CANCELED, null);
1517 }
Craig Mautner6170f732013-04-02 13:05:23 -07001518 String msg;
1519 if (!aInfo.exported) {
1520 msg = "Permission Denial: starting " + intent.toString()
1521 + " from " + callerApp + " (pid=" + callingPid
1522 + ", uid=" + callingUid + ")"
1523 + " not exported from uid " + aInfo.applicationInfo.uid;
1524 } else {
1525 msg = "Permission Denial: starting " + intent.toString()
1526 + " from " + callerApp + " (pid=" + callingPid
1527 + ", uid=" + callingUid + ")"
1528 + " requires " + aInfo.permission;
1529 }
1530 Slog.w(TAG, msg);
1531 throw new SecurityException(msg);
1532 }
1533
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001534 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001535 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001536
Craig Mautner6170f732013-04-02 13:05:23 -07001537 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001538 try {
1539 // The Intent we give to the watcher has the extra data
1540 // stripped off, since it can contain private information.
1541 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001542 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001543 aInfo.applicationInfo.packageName);
1544 } catch (RemoteException e) {
1545 mService.mController = null;
1546 }
Ben Gruver5e207332013-04-03 17:41:37 -07001547 }
Craig Mautner6170f732013-04-02 13:05:23 -07001548
Ben Gruver5e207332013-04-03 17:41:37 -07001549 if (abort) {
1550 if (resultRecord != null) {
1551 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001552 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001553 }
Ben Gruver5e207332013-04-03 17:41:37 -07001554 // We pretend to the caller that it was really started, but
1555 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001556 ActivityOptions.abort(options);
1557 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001558 }
1559
1560 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001561 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001562 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001563 if (outActivity != null) {
1564 outActivity[0] = r;
1565 }
1566
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001567 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001568 if (voiceSession == null && (stack.mResumedActivity == null
1569 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001570 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1571 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001572 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001573 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001574 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001575 ActivityOptions.abort(options);
1576 return ActivityManager.START_SWITCHES_CANCELED;
1577 }
1578 }
1579
1580 if (mService.mDidAppSwitch) {
1581 // This is the second allowed switch since we stopped switches,
1582 // so now just generally allow switches. Use case: user presses
1583 // home (switches disabled, switch to home, mDidAppSwitch now true);
1584 // user taps a home icon (coming from home so allowed, we hit here
1585 // and now allow anyone to switch again).
1586 mService.mAppSwitchesAllowedTime = 0;
1587 } else {
1588 mService.mDidAppSwitch = true;
1589 }
1590
Craig Mautneree36c772014-07-16 14:56:05 -07001591 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001592
Dianne Hackborn91097de2014-04-04 18:02:06 -07001593 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001594 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001595
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001596 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001597 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001598 // activity start, but we are not actually doing an activity
1599 // switch... just dismiss the keyguard now, because we
1600 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001601 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001602 }
1603 return err;
1604 }
1605
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001606 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001607 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001608
1609 // On leanback only devices we should keep all activities in the same stack.
1610 if (!mLeanbackOnlyDevice &&
1611 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001612
1613 ActivityStack stack;
1614
Wale Ogunwale7d701172015-03-11 15:36:30 -07001615 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001616 stack = task.stack;
1617 if (stack.isOnHomeDisplay()) {
1618 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001619 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1620 "computeStackFocus: Setting " + "focused stack to r=" + r
1621 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001622 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001623 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001624 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001625 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001626 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001627 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001628 }
1629
Craig Mautnere0a38842013-12-16 16:14:02 -08001630 final ActivityContainer container = r.mInitialActivityContainer;
1631 if (container != null) {
1632 // The first time put it on the desired stack, after this put on task stack.
1633 r.mInitialActivityContainer = null;
1634 return container.mStack;
1635 }
1636
Craig Mautner1b4bf852014-05-26 15:06:32 -07001637 if (mFocusedStack != mHomeStack && (!newTask ||
1638 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001639 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001640 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001641 return mFocusedStack;
1642 }
1643
Craig Mautnere0a38842013-12-16 16:14:02 -08001644 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1645 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001646 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001647 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001648 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001649 "computeStackFocus: Setting focused stack=" + stack);
1650 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001651 }
1652 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001653
Craig Mautner4a1cb222013-12-04 16:14:06 -08001654 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001655 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001656 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1657 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001658 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001659 }
1660 return mHomeStack;
1661 }
1662
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001663 boolean setFocusedStack(ActivityRecord r, String reason) {
1664 if (r == null) {
1665 // Not sure what you are trying to do, but it is not going to work...
1666 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001667 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001668 final TaskRecord task = r.task;
1669 if (task == null || task.stack == null) {
1670 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1671 return false;
1672 }
1673 task.stack.moveToFront(reason);
1674 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001675 }
1676
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001677 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001678 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001679 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001680 final Intent intent = r.intent;
1681 final int callingUid = r.launchedFromUid;
1682
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001683 // In some flows in to this function, we retrieve the task record and hold on to it
1684 // without a lock before calling back in to here... so the task at this point may
1685 // not actually be in recents. Check for that, and if it isn't in recents just
1686 // consider it invalid.
1687 if (inTask != null && !inTask.inRecents) {
1688 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1689 inTask = null;
1690 }
1691
Craig Mautnerad400af2014-08-13 16:41:36 -07001692 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001693 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1694 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001695
Craig Mautnera228ae92014-07-09 05:44:55 -07001696 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001697 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001698 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001699 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1700 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1701 "\"singleInstance\" or \"singleTask\"");
1702 launchFlags &=
1703 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1704 } else {
1705 switch (r.info.documentLaunchMode) {
1706 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1707 break;
1708 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1709 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1710 break;
1711 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1712 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1713 break;
1714 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1715 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1716 break;
1717 }
1718 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001719
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001720 final boolean launchTaskBehind = r.mLaunchTaskBehind
1721 && !launchSingleTask && !launchSingleInstance
1722 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001723
Wale Ogunwale7d701172015-03-11 15:36:30 -07001724 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1725 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001726 // For whatever reason this activity is being launched into a new
1727 // task... yet the caller has requested a result back. Well, that
1728 // is pretty messed up, so instead immediately send back a cancel
1729 // and let the new task continue launched as normal without a
1730 // dependency on its originator.
1731 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1732 r.resultTo.task.stack.sendActivityResultLocked(-1,
1733 r.resultTo, r.resultWho, r.requestCode,
1734 Activity.RESULT_CANCELED, null);
1735 r.resultTo = null;
1736 }
1737
1738 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1739 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1740 }
1741
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001742 // If we are actually going to launch in to a new task, there are some cases where
1743 // we further want to do multiple task.
1744 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1745 if (launchTaskBehind
1746 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1747 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1748 }
1749 }
1750
Craig Mautner8849a5e2013-04-02 16:41:03 -07001751 // We'll invoke onUserLeaving before onPause only if the launching
1752 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001753 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001754 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1755 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001756
1757 // If the caller has asked not to resume at this point, we make note
1758 // of this in the record so that we can skip it when trying to find
1759 // the top running activity.
1760 if (!doResume) {
1761 r.delayedResume = true;
1762 }
1763
Craig Mautnera254cd72014-05-25 16:47:39 -07001764 ActivityRecord notTop =
1765 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001766
1767 // If the onlyIfNeeded flag is set, then we can do this if the activity
1768 // being launched is the same as the one making the call... or, as
1769 // a special case, if we do not know the caller then we count the
1770 // current top activity as the caller.
1771 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1772 ActivityRecord checkedCaller = sourceRecord;
1773 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001774 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001775 }
1776 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1777 // Caller is not the same as launcher, so always needed.
1778 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1779 }
1780 }
1781
Craig Mautner8849a5e2013-04-02 16:41:03 -07001782 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001783 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001784
1785 // If the caller is not coming from another activity, but has given us an
1786 // explicit task into which they would like us to launch the new activity,
1787 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001788 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1789 final Intent baseIntent = inTask.getBaseIntent();
1790 final ActivityRecord root = inTask.getRootActivity();
1791 if (baseIntent == null) {
1792 ActivityOptions.abort(options);
1793 throw new IllegalArgumentException("Launching into task without base intent: "
1794 + inTask);
1795 }
1796
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001797 // If this task is empty, then we are adding the first activity -- it
1798 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001799 if (launchSingleInstance || launchSingleTask) {
1800 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1801 ActivityOptions.abort(options);
1802 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1803 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001804 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001805 if (root != null) {
1806 ActivityOptions.abort(options);
1807 throw new IllegalArgumentException("Caller with inTask " + inTask
1808 + " has root " + root + " but target is singleInstance/Task");
1809 }
1810 }
1811
1812 // If task is empty, then adopt the interesting intent launch flags in to the
1813 // activity being started.
1814 if (root == null) {
1815 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1816 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1817 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1818 launchFlags = (launchFlags&~flagsOfInterest)
1819 | (baseIntent.getFlags()&flagsOfInterest);
1820 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001821 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001822 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001823
Dianne Hackborn962d5352014-08-29 16:27:40 -07001824 // If the task is not empty and the caller is asking to start it as the root
1825 // of a new task, then we don't actually want to start this on the task. We
1826 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001827 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001828 addingToTask = false;
1829
1830 } else {
1831 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001832 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001833
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001834 reuseTask = inTask;
1835 } else {
1836 inTask = null;
1837 }
1838
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001839 if (inTask == null) {
1840 if (sourceRecord == null) {
1841 // This activity is not being started from another... in this
1842 // case we -always- start a new task.
1843 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1844 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1845 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1846 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1847 }
1848 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1849 // The original activity who is starting us is running as a single
1850 // instance... this new activity it is starting must go on its
1851 // own task.
1852 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1853 } else if (launchSingleInstance || launchSingleTask) {
1854 // The activity being started is a single instance... it always
1855 // gets launched into its own task.
1856 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1857 }
1858 }
1859
1860 ActivityInfo newTaskInfo = null;
1861 Intent newTaskIntent = null;
1862 ActivityStack sourceStack;
1863 if (sourceRecord != null) {
1864 if (sourceRecord.finishing) {
1865 // If the source is finishing, we can't further count it as our source. This
1866 // is because the task it is associated with may now be empty and on its way out,
1867 // so we don't want to blindly throw it in to that task. Instead we will take
1868 // the NEW_TASK flow and try to find a task for it. But save the task information
1869 // so it can be used when creating the new task.
1870 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1871 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1872 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1873 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1874 newTaskInfo = sourceRecord.info;
1875 newTaskIntent = sourceRecord.task.intent;
1876 }
1877 sourceRecord = null;
1878 sourceStack = null;
1879 } else {
1880 sourceStack = sourceRecord.task.stack;
1881 }
1882 } else {
1883 sourceStack = null;
1884 }
1885
1886 boolean movedHome = false;
1887 ActivityStack targetStack;
1888
1889 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001890 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001891
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001892 // We may want to try to place the new activity in to an existing task. We always
1893 // do this if the target activity is singleTask or singleInstance; we will also do
1894 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1895 // us to still place it in a new task: multi task, always doc mode, or being asked to
1896 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001897 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1898 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001899 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001900 // If bring to front is requested, and no result is requested and we have not
1901 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001902 // we can find a task that was started with this same
1903 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001904 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001905 // See if there is a task to bring to the front. If this is
1906 // a SINGLE_INSTANCE activity, there can be one and only one
1907 // instance of it in the history, and it is always in its own
1908 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001909 ActivityRecord intentActivity = !launchSingleInstance ?
1910 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001911 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001912 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001913 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001914 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001915 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1916 }
Craig Mautner29219d92013-04-16 20:19:12 -07001917 if (r.task == null) {
1918 r.task = intentActivity.task;
1919 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920 if (intentActivity.task.intent == null) {
1921 // This task was started because of movement of
1922 // the activity based on affinity... now that we
1923 // are actually launching it, we can assign the
1924 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001925 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001926 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001927 targetStack = intentActivity.task.stack;
1928 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001929 // If the target task is not in the front, then we need
1930 // to bring it to the front... except... well, with
1931 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1932 // to have the same behavior as if a new instance was
1933 // being started, which means not bringing it to the front
1934 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001935 final ActivityStack focusStack = getFocusedStack();
1936 ActivityRecord curTop = (focusStack == null)
1937 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001938 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001939 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001940 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001941 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001942 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1943 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001944 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001945 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001946 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1947 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001948 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001949 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1950 options, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001951 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001952 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001953 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1954 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001955 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001956 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001957 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001958 options = null;
1959 }
1960 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001961 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001962 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001963 + " from " + intentActivity);
1964 targetStack.moveToFront("intentActivityFound");
1965 }
1966
Craig Mautner8849a5e2013-04-02 16:41:03 -07001967 // If the caller has requested that the target task be
1968 // reset, then do so.
1969 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1970 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1971 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001972 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001973 // We don't need to start a new activity, and
1974 // the client said not to do anything if that
1975 // is the case, so this is it! And for paranoia, make
1976 // sure we have correctly resumed the top activity.
1977 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001978 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001979
1980 // Make sure to notify Keyguard as well if we are not running an app
1981 // transition later.
1982 if (!movedToFront) {
1983 notifyActivityDrawnForKeyguard();
1984 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001985 } else {
1986 ActivityOptions.abort(options);
1987 }
1988 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1989 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001990 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001991 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001992 // The caller has requested to completely replace any
1993 // existing task with its new activity. Well that should
1994 // not be too hard...
1995 reuseTask = intentActivity.task;
1996 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001997 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001998 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001999 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002000 // In this situation we want to remove all activities
2001 // from the task up to the one being started. In most
2002 // cases this means we are resetting the task to its
2003 // initial state.
2004 ActivityRecord top =
2005 intentActivity.task.performClearTaskLocked(r, launchFlags);
2006 if (top != null) {
2007 if (top.frontOfTask) {
2008 // Activity aliases may mean we use different
2009 // intents for the top activity, so make sure
2010 // the task now has the identity of the new
2011 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002012 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002013 }
2014 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2015 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002016 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002017 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002018 // A special case: we need to start the activity because it is not
2019 // currently running, and the caller has asked to clear the current
2020 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002021 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002022 // Now pretend like this activity is being started by the top of its
2023 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002024 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002025 TaskRecord task = sourceRecord.task;
2026 if (task != null && task.stack == null) {
2027 // Target stack got cleared when we all activities were removed
2028 // above. Go ahead and reset it.
2029 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2030 targetStack.addTask(
2031 task, !launchTaskBehind /* toTop */, false /* moving */);
2032 }
2033
Craig Mautner8849a5e2013-04-02 16:41:03 -07002034 }
2035 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2036 // In this case the top activity on the task is the
2037 // same as the one being launched, so we take that
2038 // as a request to bring the task to the foreground.
2039 // If the top activity in the task is the root
2040 // activity, deliver this new intent to it if it
2041 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002042 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002043 && intentActivity.realActivity.equals(r.realActivity)) {
2044 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2045 intentActivity.task);
2046 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002047 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002048 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002049 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2050 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002051 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2052 // In this case we are launching the root activity
2053 // of the task, but with a different intent. We
2054 // should start a new instance on top.
2055 addingToTask = true;
2056 sourceRecord = intentActivity;
2057 }
2058 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2059 // In this case an activity is being launched in to an
2060 // existing task, without resetting that task. This
2061 // is typically the situation of launching an activity
2062 // from a notification or shortcut. We want to place
2063 // the new activity on top of the current task.
2064 addingToTask = true;
2065 sourceRecord = intentActivity;
2066 } else if (!intentActivity.task.rootWasReset) {
2067 // In this case we are launching in to an existing task
2068 // that has not yet been started from its front door.
2069 // The current task has been brought to the front.
2070 // Ideally, we'd probably like to place this new task
2071 // at the bottom of its stack, but that's a little hard
2072 // to do with the current organization of the code so
2073 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002074 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002075 }
2076 if (!addingToTask && reuseTask == null) {
2077 // We didn't do anything... but it was needed (a.k.a., client
2078 // don't use that intent!) And for paranoia, make
2079 // sure we have correctly resumed the top activity.
2080 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002081 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002082 if (!movedToFront) {
2083 // Make sure to notify Keyguard as well if we are not running an app
2084 // transition later.
2085 notifyActivityDrawnForKeyguard();
2086 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002087 } else {
2088 ActivityOptions.abort(options);
2089 }
2090 return ActivityManager.START_TASK_TO_FRONT;
2091 }
2092 }
2093 }
2094 }
2095
2096 //String uri = r.intent.toURI();
2097 //Intent intent2 = new Intent(uri);
2098 //Slog.i(TAG, "Given intent: " + r.intent);
2099 //Slog.i(TAG, "URI is: " + uri);
2100 //Slog.i(TAG, "To intent: " + intent2);
2101
2102 if (r.packageName != null) {
2103 // If the activity being launched is the same as the one currently
2104 // at the top, then we need to check if it should only be launched
2105 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002106 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002107 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002108 if (top != null && r.resultTo == null) {
2109 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2110 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002111 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002112 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002113 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2114 top.task);
2115 // For paranoia, make sure we have correctly
2116 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002117 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002118 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002119 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002120 }
2121 ActivityOptions.abort(options);
2122 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2123 // We don't need to start a new activity, and
2124 // the client said not to do anything if that
2125 // is the case, so this is it!
2126 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2127 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002128 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002129 return ActivityManager.START_DELIVERED_TO_TOP;
2130 }
2131 }
2132 }
2133 }
2134
2135 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002136 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002137 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2138 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002139 }
2140 ActivityOptions.abort(options);
2141 return ActivityManager.START_CLASS_NOT_FOUND;
2142 }
2143
2144 boolean newTask = false;
2145 boolean keepCurTransition = false;
2146
Craig Mautnerbb742462014-07-07 15:28:55 -07002147 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002148 sourceRecord.task : null;
2149
Craig Mautner8849a5e2013-04-02 16:41:03 -07002150 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002151 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002152 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002153 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002154 targetStack = computeStackFocus(r, newTask);
2155 targetStack.moveToFront("startingNewTask");
2156
Craig Mautner8849a5e2013-04-02 16:41:03 -07002157 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002158 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2159 newTaskInfo != null ? newTaskInfo : r.info,
2160 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002161 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2162 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002163 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2164 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002165 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002166 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002167 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002168 if (isLockTaskModeViolation(r.task)) {
2169 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2170 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2171 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002172 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002173 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002174 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2175 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002176 // Caller wants to appear on home activity, so before starting
2177 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002178 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002179 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002180 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002181 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002182 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002183 if (isLockTaskModeViolation(sourceTask)) {
2184 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2185 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2186 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002187 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002188 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002189 final TaskRecord topTask = targetStack.topTask();
2190 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002191 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2192 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002193 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002194 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002195 // In this case, we are adding the activity to an existing
2196 // task, but the caller has asked to clear that task if the
2197 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002198 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002199 keepCurTransition = true;
2200 if (top != null) {
2201 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002202 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002203 // For paranoia, make sure we have correctly
2204 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002205 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002206 if (doResume) {
2207 targetStack.resumeTopActivityLocked(null);
2208 }
2209 ActivityOptions.abort(options);
2210 return ActivityManager.START_DELIVERED_TO_TOP;
2211 }
2212 } else if (!addingToTask &&
2213 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2214 // In this case, we are launching an activity in our own task
2215 // that may already be running somewhere in the history, and
2216 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002217 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002218 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002219 final TaskRecord task = top.task;
2220 task.moveActivityToFrontLocked(top);
2221 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002222 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002223 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002224 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002225 if (doResume) {
2226 targetStack.resumeTopActivityLocked(null);
2227 }
2228 return ActivityManager.START_DELIVERED_TO_TOP;
2229 }
2230 }
2231 // An existing activity is starting this new activity, so we want
2232 // to keep the new one in the same task as the one that is starting
2233 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002234 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002235 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002236 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002237
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002238 } else if (inTask != null) {
2239 // The calling is asking that the new activity be started in an explicit
2240 // task it has provided to us.
2241 if (isLockTaskModeViolation(inTask)) {
2242 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2243 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2244 }
2245 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002246 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002247
2248 // Check whether we should actually launch the new activity in to the task,
2249 // or just reuse the current activity on top.
2250 ActivityRecord top = inTask.getTopActivity();
2251 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2252 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2253 || launchSingleTop || launchSingleTask) {
2254 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2255 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2256 // We don't need to start a new activity, and
2257 // the client said not to do anything if that
2258 // is the case, so this is it!
2259 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2260 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002261 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002262 return ActivityManager.START_DELIVERED_TO_TOP;
2263 }
2264 }
2265
Dianne Hackborn962d5352014-08-29 16:27:40 -07002266 if (!addingToTask) {
2267 // We don't actually want to have this activity added to the task, so just
2268 // stop here but still tell the caller that we consumed the intent.
2269 ActivityOptions.abort(options);
2270 return ActivityManager.START_TASK_TO_FRONT;
2271 }
2272
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002273 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002274 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002275 + " in explicit task " + r.task);
2276
Craig Mautner8849a5e2013-04-02 16:41:03 -07002277 } else {
2278 // This not being started from an existing activity, and not part
2279 // of a new task... just put it in the top task, though these days
2280 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002281 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002282 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002283 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002284 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002285 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002286 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002287 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002288 + " in new guessed " + r.task);
2289 }
2290
2291 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002292 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002293
Craig Mautner76e2a762014-06-24 09:05:43 -07002294 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2295 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2296 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002297 if (newTask) {
2298 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2299 }
2300 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002301 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002302 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002303 if (!launchTaskBehind) {
2304 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002305 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002306 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002307 return ActivityManager.START_SUCCESS;
2308 }
2309
Craig Mautneree36c772014-07-16 14:56:05 -07002310 final void doPendingActivityLaunchesLocked(boolean doResume) {
2311 while (!mPendingActivityLaunches.isEmpty()) {
2312 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002313 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002314 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002315 }
2316 }
2317
Craig Mautner7f13ed32014-07-28 14:00:35 -07002318 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002319 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2320 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002321 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002322 mPendingActivityLaunches.remove(palNdx);
2323 }
2324 }
2325 }
2326
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002327 void setLaunchSource(int uid) {
2328 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2329 }
2330
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002331 void acquireLaunchWakelock() {
2332 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2333 throw new IllegalStateException("Calling must be system uid");
2334 }
2335 mLaunchingActivity.acquire();
2336 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2337 // To be safe, don't allow the wake lock to be held for too long.
2338 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2339 }
2340 }
2341
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002342 /**
2343 * Called when the frontmost task is idle.
2344 * @return the state of mService.mBooting before this was called.
2345 */
2346 private boolean checkFinishBootingLocked() {
2347 final boolean booting = mService.mBooting;
2348 boolean enableScreen = false;
2349 mService.mBooting = false;
2350 if (!mService.mBooted) {
2351 mService.mBooted = true;
2352 enableScreen = true;
2353 }
2354 if (booting || enableScreen) {
2355 mService.postFinishBooting(booting, enableScreen);
2356 }
2357 return booting;
2358 }
2359
Craig Mautnerf3333272013-04-22 10:55:53 -07002360 // Checked.
2361 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2362 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002363 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002364
Craig Mautnerf3333272013-04-22 10:55:53 -07002365 ArrayList<ActivityRecord> stops = null;
2366 ArrayList<ActivityRecord> finishes = null;
2367 ArrayList<UserStartedState> startingUsers = null;
2368 int NS = 0;
2369 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002370 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002371 boolean activityRemoved = false;
2372
Wale Ogunwale7d701172015-03-11 15:36:30 -07002373 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002374 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002375 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2376 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002377 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2378 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002379 if (fromTimeout) {
2380 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002381 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002382
2383 // This is a hack to semi-deal with a race condition
2384 // in the client where it can be constructed with a
2385 // newer configuration from when we asked it to launch.
2386 // We'll update with whatever configuration it now says
2387 // it used to launch.
2388 if (config != null) {
2389 r.configuration = config;
2390 }
2391
2392 // We are now idle. If someone is waiting for a thumbnail from
2393 // us, we can now deliver.
2394 r.idle = true;
2395
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002396 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002397 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002398 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002399 }
2400 }
2401
2402 if (allResumedActivitiesIdle()) {
2403 if (r != null) {
2404 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002405 }
2406
2407 if (mLaunchingActivity.isHeld()) {
2408 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2409 if (VALIDATE_WAKE_LOCK_CALLER &&
2410 Binder.getCallingUid() != Process.myUid()) {
2411 throw new IllegalStateException("Calling must be system uid");
2412 }
2413 mLaunchingActivity.release();
2414 }
2415 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002416 }
2417
2418 // Atomically retrieve all of the other things to do.
2419 stops = processStoppingActivitiesLocked(true);
2420 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002421 if ((NF = mFinishingActivities.size()) > 0) {
2422 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002423 mFinishingActivities.clear();
2424 }
2425
Craig Mautnerf3333272013-04-22 10:55:53 -07002426 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002427 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002428 mStartingUsers.clear();
2429 }
2430
Craig Mautnerf3333272013-04-22 10:55:53 -07002431 // Stop any activities that are scheduled to do so but have been
2432 // waiting for the next one to start.
2433 for (int i = 0; i < NS; i++) {
2434 r = stops.get(i);
2435 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002436 if (stack != null) {
2437 if (r.finishing) {
2438 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2439 } else {
2440 stack.stopActivityLocked(r);
2441 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002442 }
2443 }
2444
2445 // Finish any activities that are scheduled to do so but have been
2446 // waiting for the next one to start.
2447 for (int i = 0; i < NF; i++) {
2448 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002449 final ActivityStack stack = r.task.stack;
2450 if (stack != null) {
2451 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2452 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002453 }
2454
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002455 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002456 // Complete user switch
2457 if (startingUsers != null) {
2458 for (int i = 0; i < startingUsers.size(); i++) {
2459 mService.finishUserSwitch(startingUsers.get(i));
2460 }
2461 }
2462 // Complete starting up of background users
2463 if (mStartingBackgroundUsers.size() > 0) {
2464 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2465 mStartingBackgroundUsers.clear();
2466 for (int i = 0; i < startingUsers.size(); i++) {
2467 mService.finishUserBoot(startingUsers.get(i));
2468 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002469 }
2470 }
2471
2472 mService.trimApplications();
2473 //dump();
2474 //mWindowManager.dump();
2475
Craig Mautnerf3333272013-04-22 10:55:53 -07002476 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002477 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002478 }
2479
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002480 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002481 }
2482
Craig Mautner8e569572013-10-11 17:36:59 -07002483 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002484 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002485 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2486 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002487 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2488 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2489 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002490 }
Craig Mautner19091252013-10-05 00:03:53 -07002491 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002492 }
2493
2494 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002495 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2496 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002497 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2498 stacks.get(stackNdx).closeSystemDialogsLocked();
2499 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002500 }
2501 }
2502
Craig Mautner93529a42013-10-04 15:03:13 -07002503 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002504 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002505 }
2506
Craig Mautner8d341ef2013-03-26 09:03:27 -07002507 /**
2508 * @return true if some activity was finished (or would have finished if doit were true).
2509 */
2510 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2511 boolean didSomething = false;
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 final int numStacks = stacks.size();
2515 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2516 final ActivityStack stack = stacks.get(stackNdx);
2517 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2518 didSomething = true;
2519 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002520 }
2521 }
2522 return didSomething;
2523 }
2524
Dianne Hackborna413dc02013-07-12 12:02:55 -07002525 void updatePreviousProcessLocked(ActivityRecord r) {
2526 // Now that this process has stopped, we may want to consider
2527 // it to be the previous app to try to keep around in case
2528 // the user wants to return to it.
2529
2530 // First, found out what is currently the foreground app, so that
2531 // we don't blow away the previous app if this activity is being
2532 // hosted by the process that is actually still the foreground.
2533 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002534 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2535 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002536 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2537 final ActivityStack stack = stacks.get(stackNdx);
2538 if (isFrontStack(stack)) {
2539 if (stack.mResumedActivity != null) {
2540 fgApp = stack.mResumedActivity.app;
2541 } else if (stack.mPausingActivity != null) {
2542 fgApp = stack.mPausingActivity.app;
2543 }
2544 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002545 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002546 }
2547 }
2548
2549 // Now set this one as the previous process, only if that really
2550 // makes sense to.
2551 if (r.app != null && fgApp != null && r.app != fgApp
2552 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002553 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002554 mService.mPreviousProcess = r.app;
2555 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2556 }
2557 }
2558
Craig Mautner05d29032013-05-03 13:40:13 -07002559 boolean resumeTopActivitiesLocked() {
2560 return resumeTopActivitiesLocked(null, null, null);
2561 }
2562
2563 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2564 Bundle targetOptions) {
2565 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002566 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002567 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002568 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002569 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002570 if (isFrontStack(targetStack)) {
2571 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2572 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002573
Craig Mautnere0a38842013-12-16 16:14:02 -08002574 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2575 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002576 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2577 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002578 if (stack == targetStack) {
2579 // Already started above.
2580 continue;
2581 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002582 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002583 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002584 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002585 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002586 }
Craig Mautner05d29032013-05-03 13:40:13 -07002587 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002588 }
2589
Todd Kennedy539db512014-12-15 09:57:55 -08002590 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002591 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2592 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002593 final int numStacks = stacks.size();
2594 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2595 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002596 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002597 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002598 }
2599 }
2600
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002601 void finishVoiceTask(IVoiceInteractionSession session) {
2602 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2603 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2604 final int numStacks = stacks.size();
2605 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2606 final ActivityStack stack = stacks.get(stackNdx);
2607 stack.finishVoiceTask(session);
2608 }
2609 }
2610 }
2611
Craig Mautner299f9602015-01-26 09:47:33 -08002612 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002613 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2614 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002615 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002616 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2617 // Caller wants the home activity moved with it. To accomplish this,
2618 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002619 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002620 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002621 if (task.stack == null) {
2622 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2623 + task + " to front. Stack is null");
2624 return;
2625 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002626 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002627 if (DEBUG_STACK) Slog.d(TAG_STACK,
2628 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002629 }
2630
Craig Mautner967212c2013-04-13 21:10:58 -07002631 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002632 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2633 if (activityContainer != null) {
2634 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002635 }
2636 return null;
2637 }
2638
Craig Mautner967212c2013-04-13 21:10:58 -07002639 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002640 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002641 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2642 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002643 }
2644 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002645 }
2646
Craig Mautner4a1cb222013-12-04 16:14:06 -08002647 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002648 ActivityRecord homeActivity = getHomeActivity();
2649 if (homeActivity != null) {
2650 return homeActivity.appToken;
2651 }
2652 return null;
2653 }
2654
2655 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002656 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2657 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2658 final TaskRecord task = tasks.get(taskNdx);
2659 if (task.isHomeTask()) {
2660 final ArrayList<ActivityRecord> activities = task.mActivities;
2661 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2662 final ActivityRecord r = activities.get(activityNdx);
2663 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002664 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002665 }
2666 }
2667 }
2668 }
2669 return null;
2670 }
2671
Todd Kennedyca4d8422015-01-15 15:19:22 -08002672 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002673 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002674 ActivityContainer activityContainer =
2675 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002676 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002677 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002678 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002679 return activityContainer;
2680 }
2681
Craig Mautner34b73df2014-01-12 21:11:08 -08002682 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002683 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2684 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2685 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002686 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2687 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002688 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002689 }
2690 }
2691
Craig Mautner95da1082014-02-24 17:54:35 -08002692 void deleteActivityContainer(IActivityContainer container) {
2693 ActivityContainer activityContainer = (ActivityContainer)container;
2694 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002695 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2696 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002697 final int stackId = activityContainer.mStackId;
2698 mActivityContainers.remove(stackId);
2699 mWindowManager.removeStack(stackId);
2700 }
2701 }
2702
Wale Ogunwale60454db2015-01-23 16:05:07 -08002703 void resizeStackLocked(int stackId, Rect bounds) {
2704 final ActivityStack stack = getStack(stackId);
2705 if (stack == null) {
2706 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2707 return;
2708 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002709
2710 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002711 if (r != null && !r.task.mResizeable) {
2712 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002713 return;
2714 }
2715
Wale Ogunwale60454db2015-01-23 16:05:07 -08002716 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2717 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002718 if (r != null) {
2719 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2720 // And we need to make sure at this point that all other activities
2721 // are made visible with the correct configuration.
2722 ensureActivitiesVisibleLocked(r, 0);
2723 if (!updated) {
2724 resumeTopActivitiesLocked(stack, null, null);
2725 }
2726 }
2727 }
2728 }
2729
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002730 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2731 * current task stack if it only has one entry, moving the task to a stack that matches the
2732 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2733 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2734 task.mResizeable = true;
2735 final ActivityStack currentStack = task.stack;
2736 if (currentStack.isHomeStack()) {
2737 // Can't move task off the home stack. Sorry!
2738 return;
2739 }
2740
2741 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2742 if (matchingStackId != -1) {
2743 // There is already a stack with the right bounds!
2744 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2745 // Nothing to do here. Already in the right stack...
2746 return;
2747 }
2748 // Move task to stack with matching bounds.
2749 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2750 return;
2751 }
2752
2753 if (currentStack != null && currentStack.numTasks() == 1) {
2754 // Just resize the current stack since this is the task in it.
2755 resizeStackLocked(currentStack.mStackId, bounds);
2756 return;
2757 }
2758
2759 // Create new stack and move the task to it.
2760 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2761 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2762 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2763
2764 if (newStack == null) {
2765 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2766 return;
2767 }
2768 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2769 resizeStackLocked(newStack.mStackId, bounds);
2770 }
2771
Todd Kennedy4900bf92015-01-16 16:05:14 -08002772 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002773 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2774 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002775 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002776 }
2777
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002778 ActivityContainer activityContainer = new ActivityContainer(stackId);
2779 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002780 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002781 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002782 }
2783
2784 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002785 while (true) {
2786 if (++mLastStackId <= HOME_STACK_ID) {
2787 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002788 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002789 if (getStack(mLastStackId) == null) {
2790 break;
2791 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002792 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002793 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002794 }
2795
Wale Ogunwale7de05352014-12-12 15:21:33 -08002796 private boolean restoreRecentTaskLocked(TaskRecord task) {
2797 ActivityStack stack = null;
2798 // Determine stack to restore task to.
2799 if (mLeanbackOnlyDevice) {
2800 // There is only one stack for lean back devices.
2801 stack = mHomeStack;
2802 } else {
2803 // Look for the top stack on the home display that isn't the home stack.
2804 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2805 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2806 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002807 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002808 stack = tmpStack;
2809 break;
2810 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002811 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002812 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002813
2814 if (stack == null) {
2815 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2816 // before an application stack is created...Go ahead and create one on the default
2817 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002818 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002819 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002820 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002821 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2822 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002823 }
2824
2825 if (stack == null) {
2826 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002827 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2828 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002829 return false;
2830 }
2831
2832 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002833 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2834 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002835 final ArrayList<ActivityRecord> activities = task.mActivities;
2836 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2837 final ActivityRecord r = activities.get(activityNdx);
2838 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2839 r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002840 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Wale Ogunwale7de05352014-12-12 15:21:33 -08002841 r.userId, r.info.configChanges, task.voiceSession != null,
2842 r.mLaunchTaskBehind);
2843 }
2844 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002845 }
2846
Craig Mautner299f9602015-01-26 09:47:33 -08002847 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002848 final TaskRecord task = anyTaskForIdLocked(taskId);
2849 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002850 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002851 return;
2852 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002853 final ActivityStack stack = getStack(stackId);
2854 if (stack == null) {
2855 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2856 return;
2857 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002858 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2859 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07002860 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002861 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002862 stack.addTask(task, toTop, true);
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002863 // The task might have already been running and its visibility needs to be synchronized with
2864 // the visibility of the stack / windows.
2865 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07002866 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002867 }
2868
Craig Mautnerac6f8432013-07-17 13:24:59 -07002869 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002870 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002871 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2872 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002873 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2874 final ActivityStack stack = stacks.get(stackNdx);
2875 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002876 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002877 continue;
2878 }
2879 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002880 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2881 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002882 continue;
2883 }
2884 final ActivityRecord ar = stack.findTaskLocked(r);
2885 if (ar != null) {
2886 return ar;
2887 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002888 }
2889 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002890 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002891 return null;
2892 }
2893
2894 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002895 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2896 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002897 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2898 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2899 if (ar != null) {
2900 return ar;
2901 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002902 }
2903 }
2904 return null;
2905 }
2906
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002908 scheduleSleepTimeout();
2909 if (!mGoingToSleep.isHeld()) {
2910 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002911 if (mLaunchingActivity.isHeld()) {
2912 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2913 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002914 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002915 mLaunchingActivity.release();
2916 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002917 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002918 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002919 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002920 }
2921
2922 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002923 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002924
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002925 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002926 final long endTime = System.currentTimeMillis() + timeout;
2927 while (true) {
2928 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002929 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2930 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002931 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2932 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2933 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002934 }
2935 if (cantShutdown) {
2936 long timeRemaining = endTime - System.currentTimeMillis();
2937 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002938 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002939 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002940 } catch (InterruptedException e) {
2941 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002942 } else {
2943 Slog.w(TAG, "Activity manager shutdown timed out");
2944 timedout = true;
2945 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002946 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002947 } else {
2948 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002949 }
2950 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002951
2952 // Force checkReadyForSleep to complete.
2953 mSleepTimeout = true;
2954 checkReadyForSleepLocked();
2955
Craig Mautner8d341ef2013-03-26 09:03:27 -07002956 return timedout;
2957 }
2958
2959 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002960 removeSleepTimeouts();
2961 if (mGoingToSleep.isHeld()) {
2962 mGoingToSleep.release();
2963 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002964 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2965 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002966 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2967 final ActivityStack stack = stacks.get(stackNdx);
2968 stack.awakeFromSleepingLocked();
2969 if (isFrontStack(stack)) {
2970 resumeTopActivitiesLocked();
2971 }
Craig Mautner5314a402013-09-26 12:40:16 -07002972 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002973 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002974 mGoingToSleepActivities.clear();
2975 }
2976
2977 void activitySleptLocked(ActivityRecord r) {
2978 mGoingToSleepActivities.remove(r);
2979 checkReadyForSleepLocked();
2980 }
2981
2982 void checkReadyForSleepLocked() {
2983 if (!mService.isSleepingOrShuttingDown()) {
2984 // Do not care.
2985 return;
2986 }
2987
2988 if (!mSleepTimeout) {
2989 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2991 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2993 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2994 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002995 }
2996
2997 if (mStoppingActivities.size() > 0) {
2998 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002999 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003000 + mStoppingActivities.size() + " activities");
3001 scheduleIdleLocked();
3002 dontSleep = true;
3003 }
3004
3005 if (mGoingToSleepActivities.size() > 0) {
3006 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003007 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003008 + mGoingToSleepActivities.size() + " activities");
3009 dontSleep = true;
3010 }
3011
3012 if (dontSleep) {
3013 return;
3014 }
3015 }
3016
Craig Mautnere0a38842013-12-16 16:14:02 -08003017 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3018 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003019 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3020 stacks.get(stackNdx).goToSleep();
3021 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003022 }
3023
3024 removeSleepTimeouts();
3025
3026 if (mGoingToSleep.isHeld()) {
3027 mGoingToSleep.release();
3028 }
3029 if (mService.mShuttingDown) {
3030 mService.notifyAll();
3031 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003032 }
3033
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003034 boolean reportResumedActivityLocked(ActivityRecord r) {
3035 final ActivityStack stack = r.task.stack;
3036 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003037 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003038 }
3039 if (allResumedActivitiesComplete()) {
3040 ensureActivitiesVisibleLocked(null, 0);
3041 mWindowManager.executeAppTransition();
3042 return true;
3043 }
3044 return false;
3045 }
3046
Craig Mautner8d341ef2013-03-26 09:03:27 -07003047 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003048 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3049 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003050 final int numStacks = stacks.size();
3051 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3052 final ActivityStack stack = stacks.get(stackNdx);
3053 stack.handleAppCrashLocked(app);
3054 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003055 }
3056 }
3057
Jose Lima4b6c6692014-08-12 17:41:12 -07003058 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003059 final ActivityStack stack = r.task.stack;
3060 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003061 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3062 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003063 return false;
3064 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003065 final boolean isVisible = stack.hasVisibleBehindActivity();
3066 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3067 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003068
3069 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003070 if (top == null || top == r || (visible == isVisible)) {
3071 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3072 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003073 return true;
3074 }
3075
3076 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003077 if (visible && top.fullscreen) {
3078 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003079 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003080 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003081 " top.app.thread=" + top.app.thread);
3082 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003083 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3084 // Only the activity set as currently visible behind should actively reset its
3085 // visible behind state.
3086 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3087 + visible + " stack.getVisibleBehindActivity()=" +
3088 stack.getVisibleBehindActivity() + " r=" + r);
3089 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003090 }
3091
Jose Lima4b6c6692014-08-12 17:41:12 -07003092 stack.setVisibleBehindActivity(visible ? r : null);
3093 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003094 // Make the activity immediately above r opaque.
3095 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3096 if (next != null) {
3097 mService.convertFromTranslucent(next.appToken);
3098 }
3099 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003100 if (top.app != null && top.app.thread != null) {
3101 // Notify the top app of the change.
3102 try {
3103 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3104 } catch (RemoteException e) {
3105 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003106 }
3107 return true;
3108 }
3109
Craig Mautnerbb742462014-07-07 15:28:55 -07003110 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3111 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3112 r.mLaunchTaskBehind = false;
3113 final TaskRecord task = r.task;
3114 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003115 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003116 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003117 mWindowManager.setAppVisibility(r.appToken, false);
3118 }
3119
3120 void scheduleLaunchTaskBehindComplete(IBinder token) {
3121 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3122 }
3123
Craig Mautnerde4ef022013-04-07 19:01:33 -07003124 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003125 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003126 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3127 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003128 final int topStackNdx = stacks.size() - 1;
3129 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3130 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003131 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003132 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003133 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003134 }
3135
3136 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003137 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3138 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003139 final int numStacks = stacks.size();
3140 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3141 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003142 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003143 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003144 }
3145 }
3146
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003147 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3148 // Examine all activities currently running in the process.
3149 TaskRecord firstTask = null;
3150 // Tasks is non-null only if two or more tasks are found.
3151 ArraySet<TaskRecord> tasks = null;
3152 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3153 for (int i=0; i<app.activities.size(); i++) {
3154 ActivityRecord r = app.activities.get(i);
3155 // First, if we find an activity that is in the process of being destroyed,
3156 // then we just aren't going to do anything for now; we want things to settle
3157 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003158 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003159 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3160 return;
3161 }
3162 // Don't consider any activies that are currently not in a state where they
3163 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003164 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3165 || r.state == PAUSED || r.state == STOPPING) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003166 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3167 continue;
3168 }
3169 if (r.task != null) {
3170 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3171 + " from " + r);
3172 if (firstTask == null) {
3173 firstTask = r.task;
3174 } else if (firstTask != r.task) {
3175 if (tasks == null) {
3176 tasks = new ArraySet<>();
3177 tasks.add(firstTask);
3178 }
3179 tasks.add(r.task);
3180 }
3181 }
3182 }
3183 if (tasks == null) {
3184 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3185 return;
3186 }
3187 // If we have activities in multiple tasks that are in a position to be destroyed,
3188 // let's iterate through the tasks and release the oldest one.
3189 final int numDisplays = mActivityDisplays.size();
3190 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3191 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3192 // Step through all stacks starting from behind, to hit the oldest things first.
3193 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3194 final ActivityStack stack = stacks.get(stackNdx);
3195 // Try to release activities in this stack; if we manage to, we are done.
3196 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3197 return;
3198 }
3199 }
3200 }
3201 }
3202
Craig Mautner8d341ef2013-03-26 09:03:27 -07003203 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003204 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003205 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003206 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003207
Craig Mautner858d8a62013-04-23 17:08:34 -07003208 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003209 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3210 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003211 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003212 final ActivityStack stack = stacks.get(stackNdx);
3213 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003214 TaskRecord task = stack.topTask();
3215 if (task != null) {
3216 mWindowManager.moveTaskToTop(task.taskId);
3217 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003218 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003219 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003220
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003221 ActivityStack stack = getStack(restoreStackId);
3222 if (stack == null) {
3223 stack = mHomeStack;
3224 }
3225 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003226 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003227 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003228 TaskRecord task = stack.topTask();
3229 if (task != null) {
3230 mWindowManager.moveTaskToTop(task.taskId);
3231 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003232 } else {
3233 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003234 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003235 }
Craig Mautner93529a42013-10-04 15:03:13 -07003236 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003237 }
3238
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003239 /**
3240 * Add background users to send boot completed events to.
3241 * @param userId The user being started in the background
3242 * @param uss The state object for the user.
3243 */
3244 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3245 mStartingBackgroundUsers.add(uss);
3246 }
3247
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003248 /** Checks whether the userid is a profile of the current user. */
3249 boolean isCurrentProfileLocked(int userId) {
3250 if (userId == mCurrentUser) return true;
3251 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3252 if (mService.mCurrentProfileIds[i] == userId) return true;
3253 }
3254 return false;
3255 }
3256
Craig Mautnerde4ef022013-04-07 19:01:33 -07003257 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003258 ArrayList<ActivityRecord> stops = null;
3259
3260 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003261 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3262 ActivityRecord s = mStoppingActivities.get(activityNdx);
3263 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003264 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003265 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3266 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003267 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003268 if (s.finishing) {
3269 // If this activity is finishing, it is sitting on top of
3270 // everyone else but we now know it is no longer needed...
3271 // so get rid of it. Otherwise, we need to go through the
3272 // normal flow and hide it once we determine that it is
3273 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003274 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003275 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003276 }
3277 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003278 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003279 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003280 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003281 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003282 }
3283 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003284 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003285 }
3286 }
3287
3288 return stops;
3289 }
3290
Craig Mautnercf910b02013-04-23 11:23:27 -07003291 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003292 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3293 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3294 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3295 final ActivityStack stack = stacks.get(stackNdx);
3296 final ActivityRecord r = stack.topRunningActivityLocked(null);
3297 final ActivityState state = r == null ? DESTROYED : r.state;
3298 if (isFrontStack(stack)) {
3299 if (r == null) Slog.e(TAG,
3300 "validateTop...: null top activity, stack=" + stack);
3301 else {
3302 final ActivityRecord pausing = stack.mPausingActivity;
3303 if (pausing != null && pausing == r) Slog.e(TAG,
3304 "validateTop...: top stack has pausing activity r=" + r
3305 + " state=" + state);
3306 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3307 "validateTop...: activity in front not resumed r=" + r
3308 + " state=" + state);
3309 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003310 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003311 final ActivityRecord resumed = stack.mResumedActivity;
3312 if (resumed != null && resumed == r) Slog.e(TAG,
3313 "validateTop...: back stack has resumed activity r=" + r
3314 + " state=" + state);
3315 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3316 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003317 }
3318 }
3319 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003320 }
3321
Craig Mautner27084302013-03-25 08:05:25 -07003322 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003323 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003324 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003325 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3326 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3327 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003328 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner15df08a2015-04-01 12:17:18 -07003329 pw.print(prefix); pw.println("mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003330 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003331
Craig Mautner20e72272013-04-01 13:45:53 -07003332 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003333 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003334 }
3335
Dianne Hackborn390517b2013-05-30 15:03:32 -07003336 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3337 boolean needSep, String prefix) {
3338 if (activity != null) {
3339 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3340 if (needSep) {
3341 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003342 }
3343 pw.print(prefix);
3344 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003345 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003346 }
3347 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003348 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003349 }
3350
Craig Mautner8d341ef2013-03-26 09:03:27 -07003351 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3352 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003353 boolean printed = false;
3354 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003355 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3356 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003357 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003358 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003359 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003360 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003361 final ActivityStack stack = stacks.get(stackNdx);
3362 StringBuilder stackHeader = new StringBuilder(128);
3363 stackHeader.append(" Stack #");
3364 stackHeader.append(stack.mStackId);
3365 stackHeader.append(":");
3366 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3367 needSep, stackHeader.toString());
3368 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3369 !dumpAll, false, dumpPackage, true,
3370 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003371
Craig Mautner4a1cb222013-12-04 16:14:06 -08003372 needSep = printed;
3373 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3374 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003375 if (pr) {
3376 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003377 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003378 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003379 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3380 " mResumedActivity: ");
3381 if (pr) {
3382 printed = true;
3383 needSep = false;
3384 }
3385 if (dumpAll) {
3386 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3387 " mLastPausedActivity: ");
3388 if (pr) {
3389 printed = true;
3390 needSep = true;
3391 }
3392 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3393 needSep, " mLastNoHistoryActivity: ");
3394 }
3395 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003396 }
3397 }
3398
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003399 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3400 false, dumpPackage, true, " Activities waiting to finish:", null);
3401 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3402 false, dumpPackage, true, " Activities waiting to stop:", null);
3403 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3404 false, dumpPackage, true, " Activities waiting for another to become visible:",
3405 null);
3406 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3407 false, dumpPackage, true, " Activities waiting to sleep:", null);
3408 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3409 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003410
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003411 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003412 }
3413
Dianne Hackborn390517b2013-05-30 15:03:32 -07003414 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003415 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003416 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003417 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003418 String innerPrefix = null;
3419 String[] args = null;
3420 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003421 for (int i=list.size()-1; i>=0; i--) {
3422 final ActivityRecord r = list.get(i);
3423 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3424 continue;
3425 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003426 if (innerPrefix == null) {
3427 innerPrefix = prefix + " ";
3428 args = new String[0];
3429 }
3430 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003431 final boolean full = !brief && (complete || !r.isInHistory());
3432 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003433 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003434 needNL = false;
3435 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003436 if (header1 != null) {
3437 pw.println(header1);
3438 header1 = null;
3439 }
3440 if (header2 != null) {
3441 pw.println(header2);
3442 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003443 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003444 if (lastTask != r.task) {
3445 lastTask = r.task;
3446 pw.print(prefix);
3447 pw.print(full ? "* " : " ");
3448 pw.println(lastTask);
3449 if (full) {
3450 lastTask.dump(pw, prefix + " ");
3451 } else if (complete) {
3452 // Complete + brief == give a summary. Isn't that obvious?!?
3453 if (lastTask.intent != null) {
3454 pw.print(prefix); pw.print(" ");
3455 pw.println(lastTask.intent.toInsecureStringWithClip());
3456 }
3457 }
3458 }
3459 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3460 pw.print(" #"); pw.print(i); pw.print(": ");
3461 pw.println(r);
3462 if (full) {
3463 r.dump(pw, innerPrefix);
3464 } else if (complete) {
3465 // Complete + brief == give a summary. Isn't that obvious?!?
3466 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3467 if (r.app != null) {
3468 pw.print(innerPrefix); pw.println(r.app);
3469 }
3470 }
3471 if (client && r.app != null && r.app.thread != null) {
3472 // flush anything that is already in the PrintWriter since the thread is going
3473 // to write to the file descriptor directly
3474 pw.flush();
3475 try {
3476 TransferPipe tp = new TransferPipe();
3477 try {
3478 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3479 r.appToken, innerPrefix, args);
3480 // Short timeout, since blocking here can
3481 // deadlock with the application.
3482 tp.go(fd, 2000);
3483 } finally {
3484 tp.kill();
3485 }
3486 } catch (IOException e) {
3487 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3488 } catch (RemoteException e) {
3489 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3490 }
3491 needNL = true;
3492 }
3493 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003494 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003495 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003496
Craig Mautnerf3333272013-04-22 10:55:53 -07003497 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003498 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003499 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3500 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003501 }
3502
3503 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003504 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003505 }
3506
3507 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003508 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003509 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3510 }
3511
Craig Mautner05d29032013-05-03 13:40:13 -07003512 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003513 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3514 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3515 }
Craig Mautner05d29032013-05-03 13:40:13 -07003516 }
3517
Craig Mautner0eea92c2013-05-16 13:35:39 -07003518 void removeSleepTimeouts() {
3519 mSleepTimeout = false;
3520 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3521 }
3522
3523 final void scheduleSleepTimeout() {
3524 removeSleepTimeouts();
3525 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3526 }
3527
Craig Mautner4a1cb222013-12-04 16:14:06 -08003528 @Override
3529 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003530 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003531 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3532 }
3533
3534 @Override
3535 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003536 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003537 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3538 }
3539
3540 @Override
3541 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003542 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003543 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3544 }
3545
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003546 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003547 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003548 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003549 newDisplay = mActivityDisplays.get(displayId) == null;
3550 if (newDisplay) {
3551 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003552 if (activityDisplay.mDisplay == null) {
3553 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3554 return;
3555 }
Craig Mautner4504de52013-12-20 09:06:56 -08003556 mActivityDisplays.put(displayId, activityDisplay);
3557 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003558 }
Craig Mautner4504de52013-12-20 09:06:56 -08003559 if (newDisplay) {
3560 mWindowManager.onDisplayAdded(displayId);
3561 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003562 }
3563
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003564 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003565 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003566 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3567 if (activityDisplay != null) {
3568 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003569 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003570 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003571 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003572 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003573 }
3574 }
3575 mWindowManager.onDisplayRemoved(displayId);
3576 }
3577
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003578 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003579 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003580 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3581 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003582 // TODO: Update the bounds.
3583 }
3584 }
3585 mWindowManager.onDisplayChanged(displayId);
3586 }
3587
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003588 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003589 StackInfo info = new StackInfo();
3590 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3591 info.displayId = Display.DEFAULT_DISPLAY;
3592 info.stackId = stack.mStackId;
3593
3594 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3595 final int numTasks = tasks.size();
3596 int[] taskIds = new int[numTasks];
3597 String[] taskNames = new String[numTasks];
3598 for (int i = 0; i < numTasks; ++i) {
3599 final TaskRecord task = tasks.get(i);
3600 taskIds[i] = task.taskId;
3601 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3602 : task.realActivity != null ? task.realActivity.flattenToString()
3603 : task.getTopActivity() != null ? task.getTopActivity().packageName
3604 : "unknown";
3605 }
3606 info.taskIds = taskIds;
3607 info.taskNames = taskNames;
3608 return info;
3609 }
3610
3611 StackInfo getStackInfoLocked(int stackId) {
3612 ActivityStack stack = getStack(stackId);
3613 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003614 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003615 }
3616 return null;
3617 }
3618
3619 ArrayList<StackInfo> getAllStackInfosLocked() {
3620 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003621 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3622 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003623 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003624 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003625 }
3626 }
3627 return list;
3628 }
3629
Craig Mautner15df08a2015-04-01 12:17:18 -07003630 TaskRecord getLockedTaskLocked() {
3631 final int top = mLockTaskModeTasks.size() - 1;
3632 if (top >= 0) {
3633 return mLockTaskModeTasks.get(top);
3634 }
3635 return null;
3636 }
3637
3638 boolean isLockedTask(TaskRecord task) {
3639 return mLockTaskModeTasks.contains(task);
3640 }
3641
3642 boolean isLastLockedTask(TaskRecord task) {
3643 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3644 }
3645
3646 void removeLockedTaskLocked(final TaskRecord task) {
3647 if (mLockTaskModeTasks.remove(task) && mLockTaskModeTasks.isEmpty()) {
3648 // Last one.
3649 final Message lockTaskMsg = Message.obtain();
3650 lockTaskMsg.arg1 = task.userId;
3651 lockTaskMsg.what = LOCK_TASK_END_MSG;
3652 mHandler.sendMessage(lockTaskMsg);
3653 }
3654 }
3655
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003656 void showLockTaskToast() {
3657 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003658 }
3659
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003660 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3661 if (mLockTaskModeTasks.contains(task)) {
3662 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3663 }
3664 }
3665
Benjamin Franz43261142015-02-11 15:59:44 +00003666 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003667 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003668 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003669 final TaskRecord lockedTask = getLockedTaskLocked();
3670 if (lockedTask != null) {
3671 removeLockedTaskLocked(lockedTask);
3672 if (!mLockTaskModeTasks.isEmpty()) {
3673 // There are locked tasks remaining, can only finish this task, not unlock it.
3674 lockedTask.performClearTaskLocked();
3675 resumeTopActivitiesLocked();
3676 return;
3677 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003678 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003679 return;
3680 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003681
3682 // Should have already been checked, but do it again.
3683 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003684 return;
3685 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003686 if (isLockTaskModeViolation(task)) {
3687 Slog.e(TAG, "setLockTaskMode: Attempt to start an unauthorized lock task.");
3688 return;
3689 }
3690
3691 if (mLockTaskModeTasks.isEmpty()) {
3692 // First locktask.
3693 final Message lockTaskMsg = Message.obtain();
3694 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3695 lockTaskMsg.arg1 = task.userId;
3696 lockTaskMsg.what = LOCK_TASK_START_MSG;
3697 lockTaskMsg.arg2 = lockTaskModeState;
3698 mHandler.sendMessage(lockTaskMsg);
3699 }
3700 // Add it or move it to the top.
3701 mLockTaskModeTasks.remove(task);
3702 mLockTaskModeTasks.add(task);
3703
3704 if (task.mLockTaskUid == -1) {
3705 task.mLockTaskUid = task.mCallingUid;
3706 }
Craig Mautner299f9602015-01-26 09:47:33 -08003707 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003708 resumeTopActivitiesLocked();
3709 }
3710
3711 boolean isLockTaskModeViolation(TaskRecord task) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003712 if (getLockedTaskLocked() == task) {
3713 return false;
3714 }
3715 final int lockTaskAuth = task.mLockTaskAuth;
3716 switch (lockTaskAuth) {
3717 case LOCK_TASK_AUTH_DONT_LOCK:
3718 return !mLockTaskModeTasks.isEmpty();
3719 case LOCK_TASK_AUTH_LAUNCHABLE:
3720 case LOCK_TASK_AUTH_WHITELISTED:
3721 return false;
3722 case LOCK_TASK_AUTH_PINNABLE:
3723 // Pinnable tasks can't be launched on top of locktask tasks.
3724 return !mLockTaskModeTasks.isEmpty();
3725 default:
3726 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3727 return true;
3728 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003729 }
3730
Craig Mautner15df08a2015-04-01 12:17:18 -07003731 void onLockTaskPackagesUpdatedLocked() {
3732 boolean didSomething = false;
3733 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3734 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
3735 if (lockedTask.mLockTaskMode != LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED) {
3736 continue;
3737 }
3738 final boolean wasLaunchable = lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE;
3739 lockedTask.setLockTaskAuth();
3740 if (wasLaunchable && lockedTask.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE) {
3741 // Lost whitelisting authorization. End it now.
3742 removeLockedTaskLocked(lockedTask);
3743 lockedTask.performClearTaskLocked();
3744 didSomething = true;
3745 }
3746 }
3747 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3748 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3749 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3750 final ActivityStack stack = stacks.get(stackNdx);
3751 stack.onLockTaskPackagesUpdatedLocked();
3752 }
3753 }
3754 if (didSomething) {
3755 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003756 }
3757 }
3758
Benjamin Franz43261142015-02-11 15:59:44 +00003759 int getLockTaskModeState() {
3760 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003761 }
3762
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003763 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003764
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003765 public ActivityStackSupervisorHandler(Looper looper) {
3766 super(looper);
3767 }
3768
Craig Mautnerf3333272013-04-22 10:55:53 -07003769 void activityIdleInternal(ActivityRecord r) {
3770 synchronized (mService) {
3771 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3772 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003773 }
3774
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003775 @Override
3776 public void handleMessage(Message msg) {
3777 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003778 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003779 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003780 if (mService.mDidDexOpt) {
3781 mService.mDidDexOpt = false;
3782 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3783 nmsg.obj = msg.obj;
3784 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3785 return;
3786 }
3787 // We don't at this point know if the activity is fullscreen,
3788 // so we need to be conservative and assume it isn't.
3789 activityIdleInternal((ActivityRecord)msg.obj);
3790 } break;
3791 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003792 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003793 activityIdleInternal((ActivityRecord)msg.obj);
3794 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003795 case RESUME_TOP_ACTIVITY_MSG: {
3796 synchronized (mService) {
3797 resumeTopActivitiesLocked();
3798 }
3799 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003800 case SLEEP_TIMEOUT_MSG: {
3801 synchronized (mService) {
3802 if (mService.isSleepingOrShuttingDown()) {
3803 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3804 mSleepTimeout = true;
3805 checkReadyForSleepLocked();
3806 }
3807 }
3808 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003809 case LAUNCH_TIMEOUT_MSG: {
3810 if (mService.mDidDexOpt) {
3811 mService.mDidDexOpt = false;
3812 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3813 return;
3814 }
3815 synchronized (mService) {
3816 if (mLaunchingActivity.isHeld()) {
3817 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3818 if (VALIDATE_WAKE_LOCK_CALLER
3819 && Binder.getCallingUid() != Process.myUid()) {
3820 throw new IllegalStateException("Calling must be system uid");
3821 }
3822 mLaunchingActivity.release();
3823 }
3824 }
3825 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003826 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003827 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 } break;
3829 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003830 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 } break;
3832 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003833 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003834 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003835 case CONTAINER_CALLBACK_VISIBILITY: {
3836 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003837 final IActivityContainerCallback callback = container.mCallback;
3838 if (callback != null) {
3839 try {
3840 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3841 } catch (RemoteException e) {
3842 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003843 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003844 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003845 case LOCK_TASK_START_MSG: {
3846 // When lock task starts, we disable the status bars.
3847 try {
Jason Monk62515be2014-05-21 16:06:19 -04003848 if (mLockTaskNotify == null) {
3849 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003850 }
Jason Monk62515be2014-05-21 16:06:19 -04003851 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003852 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003853 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003854 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003855 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003856 flags = StatusBarManager.DISABLE_MASK
3857 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003858 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003859 flags = StatusBarManager.DISABLE_MASK
3860 & (~StatusBarManager.DISABLE_BACK)
3861 & (~StatusBarManager.DISABLE_HOME)
3862 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003863 }
3864 getStatusBarService().disable(flags, mToken,
3865 mService.mContext.getPackageName());
3866 }
3867 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003868 if (getDevicePolicyManager() != null) {
3869 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003870 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003871 }
justinzhang5286d3f2014-05-12 17:06:01 -04003872 } catch (RemoteException ex) {
3873 throw new RuntimeException(ex);
3874 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003875 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003876 case LOCK_TASK_END_MSG: {
3877 // When lock task ends, we enable the status bars.
3878 try {
Jason Monk62515be2014-05-21 16:06:19 -04003879 if (getStatusBarService() != null) {
3880 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3881 mService.mContext.getPackageName());
3882 }
3883 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003884 if (getDevicePolicyManager() != null) {
3885 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3886 msg.arg1);
3887 }
Jason Monk62515be2014-05-21 16:06:19 -04003888 if (mLockTaskNotify == null) {
3889 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3890 }
3891 mLockTaskNotify.show(false);
3892 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003893 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003894 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003895 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003896 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003897 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003898 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003899 new LockPatternUtils(mService.mContext)
3900 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003901 }
3902 } catch (SettingNotFoundException e) {
3903 // No setting, don't lock.
3904 }
justinzhang5286d3f2014-05-12 17:06:01 -04003905 } catch (RemoteException ex) {
3906 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003907 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003908 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003909 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003910 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003911 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3912 if (mLockTaskNotify == null) {
3913 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3914 }
3915 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3916 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003917 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3918 final ActivityContainer container = (ActivityContainer) msg.obj;
3919 final IActivityContainerCallback callback = container.mCallback;
3920 if (callback != null) {
3921 try {
3922 callback.onAllActivitiesComplete(container.asBinder());
3923 } catch (RemoteException e) {
3924 }
3925 }
3926 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003927 case LAUNCH_TASK_BEHIND_COMPLETE: {
3928 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003929 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003930 if (r != null) {
3931 handleLaunchTaskBehindCompleteLocked(r);
3932 }
3933 }
3934 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003935 }
3936 }
3937 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003938
Ying Wangb081a592014-04-22 15:20:16 -07003939 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003940 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003941 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003942 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003943 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003944 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003945 ActivityRecord mParentActivity = null;
3946 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003947
Craig Mautnere3a00d72014-04-16 08:31:19 -07003948 boolean mVisible = true;
3949
Craig Mautner4a1cb222013-12-04 16:14:06 -08003950 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003951 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003952
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003953 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3954 final static int CONTAINER_STATE_NO_SURFACE = 1;
3955 final static int CONTAINER_STATE_FINISHING = 2;
3956 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3957
3958 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003959 synchronized (mService) {
3960 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003961 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003962 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003963 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003964 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003965 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003966
Craig Mautnere0a38842013-12-16 16:14:02 -08003967 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003968 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08003969 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003970 mActivityDisplay = activityDisplay;
3971 mStack.mDisplayId = activityDisplay.mDisplayId;
3972 mStack.mStacks = activityDisplay.mStacks;
3973
3974 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003975 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003976 }
3977
3978 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003979 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003980 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003981 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3982 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003983 return;
3984 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003985 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003986 }
3987 }
3988
3989 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003990 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003991 synchronized (mService) {
3992 if (mActivityDisplay != null) {
3993 return mActivityDisplay.mDisplayId;
3994 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003995 }
3996 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003997 }
3998
Jeff Brownca9bc702014-02-11 14:32:56 -08003999 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004000 public int getStackId() {
4001 synchronized (mService) {
4002 return mStackId;
4003 }
4004 }
4005
4006 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004007 public boolean injectEvent(InputEvent event) {
4008 final long origId = Binder.clearCallingIdentity();
4009 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004010 synchronized (mService) {
4011 if (mActivityDisplay != null) {
4012 return mInputManagerInternal.injectInputEvent(event,
4013 mActivityDisplay.mDisplayId,
4014 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4015 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004016 }
4017 return false;
4018 } finally {
4019 Binder.restoreCallingIdentity(origId);
4020 }
4021 }
4022
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004023 @Override
4024 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004025 synchronized (mService) {
4026 if (mContainerState == CONTAINER_STATE_FINISHING) {
4027 return;
4028 }
4029 mContainerState = CONTAINER_STATE_FINISHING;
4030
Craig Mautnerd163e752014-06-13 17:18:47 -07004031 long origId = Binder.clearCallingIdentity();
4032 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004033 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004034 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004035 } finally {
4036 Binder.restoreCallingIdentity(origId);
4037 }
4038 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004039 }
4040
Craig Mautner60257702014-09-17 15:02:33 -07004041 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004042 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004043 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004044 if (mActivityDisplay != null) {
4045 mActivityDisplay.detachActivitiesLocked(mStack);
4046 mActivityDisplay = null;
4047 mStack.mDisplayId = -1;
4048 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004049 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004050 }
4051 }
4052
4053 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004054 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004055 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004056 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004057 Binder.getCallingUid(), mCurrentUser, false,
4058 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004059
Craig Mautnere0a38842013-12-16 16:14:02 -08004060 // TODO: Switch to user app stacks here.
4061 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004062 final Uri data = intent.getData();
4063 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4064 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004065 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004066 checkEmbeddedAllowedInner(userId, intent, mimeType);
4067
4068 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4069 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4070 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004071 }
4072
4073 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004074 public final int startActivityIntentSender(IIntentSender intentSender)
4075 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004076 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4077
4078 if (!(intentSender instanceof PendingIntentRecord)) {
4079 throw new IllegalArgumentException("Bad PendingIntent object");
4080 }
4081
Craig Mautnerb9168362015-02-26 20:40:19 -08004082 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004083 Binder.getCallingUid(), mCurrentUser, false,
4084 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004085
4086 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4087 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4088 pendingIntent.key.requestResolvedType);
4089
4090 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4091 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4092 }
4093
4094 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004095 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004096 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004097 throw new SecurityException(
4098 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4099 }
4100 }
4101
Craig Mautnerdf88d732014-01-27 09:21:32 -08004102 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004103 public IBinder asBinder() {
4104 return this;
4105 }
4106
Craig Mautner4504de52013-12-20 09:06:56 -08004107 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004108 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004109 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004110 }
4111
Craig Mautner4a1cb222013-12-04 16:14:06 -08004112 ActivityStackSupervisor getOuter() {
4113 return ActivityStackSupervisor.this;
4114 }
4115
Craig Mautnerd163e752014-06-13 17:18:47 -07004116 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004117 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004118 }
4119
4120 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004121 synchronized (mService) {
4122 if (mActivityDisplay != null) {
4123 mActivityDisplay.getBounds(outBounds);
4124 } else {
4125 outBounds.set(0, 0);
4126 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004127 }
4128 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004129
Craig Mautner6985bad2014-04-21 15:22:06 -07004130 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004131 void setVisible(boolean visible) {
4132 if (mVisible != visible) {
4133 mVisible = visible;
4134 if (mCallback != null) {
4135 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4136 0 /* unused */, this).sendToTarget();
4137 }
4138 }
4139 }
4140
Craig Mautner6985bad2014-04-21 15:22:06 -07004141 void setDrawn() {
4142 }
4143
Craig Mautner1b4bf852014-05-26 15:06:32 -07004144 // You can always start a new task on a regular ActivityStack.
4145 boolean isEligibleForNewTasks() {
4146 return true;
4147 }
4148
Craig Mautnerd163e752014-06-13 17:18:47 -07004149 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004150 detachLocked();
4151 deleteActivityContainer(this);
4152 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004153 }
4154
Craig Mautner34b73df2014-01-12 21:11:08 -08004155 @Override
4156 public String toString() {
4157 return mIdString + (mActivityDisplay == null ? "N" : "A");
4158 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004159 }
4160
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004161 private class VirtualActivityContainer extends ActivityContainer {
4162 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004163 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004164
4165 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4166 super(getNextStackId());
4167 mParentActivity = parent;
4168 mCallback = callback;
4169 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004170 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004171 }
4172
4173 @Override
4174 public void setSurface(Surface surface, int width, int height, int density) {
4175 super.setSurface(surface, width, height, density);
4176
4177 synchronized (mService) {
4178 final long origId = Binder.clearCallingIdentity();
4179 try {
4180 setSurfaceLocked(surface, width, height, density);
4181 } finally {
4182 Binder.restoreCallingIdentity(origId);
4183 }
4184 }
4185 }
4186
4187 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4188 if (mContainerState == CONTAINER_STATE_FINISHING) {
4189 return;
4190 }
4191 VirtualActivityDisplay virtualActivityDisplay =
4192 (VirtualActivityDisplay) mActivityDisplay;
4193 if (virtualActivityDisplay == null) {
4194 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004195 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004196 mActivityDisplay = virtualActivityDisplay;
4197 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4198 attachToDisplayLocked(virtualActivityDisplay);
4199 }
4200
4201 if (mSurface != null) {
4202 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004203 }
4204
Craig Mautner6985bad2014-04-21 15:22:06 -07004205 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004206 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004207 mStack.resumeTopActivityLocked(null);
4208 } else {
4209 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004210 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004211 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004212 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004213 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004214 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004215
Craig Mautnerd163e752014-06-13 17:18:47 -07004216 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004217
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004218 if (DEBUG_STACK) Slog.d(TAG_STACK,
4219 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004220 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004221
Craig Mautner6985bad2014-04-21 15:22:06 -07004222 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004223 boolean isAttachedLocked() {
4224 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004225 }
4226
4227 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004228 void setDrawn() {
4229 synchronized (mService) {
4230 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004231 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004232 }
4233 }
4234
Craig Mautner1b4bf852014-05-26 15:06:32 -07004235 // Never start a new task on an ActivityView if it isn't explicitly specified.
4236 @Override
4237 boolean isEligibleForNewTasks() {
4238 return false;
4239 }
4240
Craig Mautnerd163e752014-06-13 17:18:47 -07004241 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004242 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004243 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4244 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4245 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4246 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4247 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004248 }
4249 }
4250
Craig Mautner4a1cb222013-12-04 16:14:06 -08004251 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4252 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004253 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004254 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004255 int mDisplayId;
4256 Display mDisplay;
4257 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004258
Craig Mautner4a1cb222013-12-04 16:14:06 -08004259 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4260 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004261 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004262
Jose Lima4b6c6692014-08-12 17:41:12 -07004263 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004264
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004265 ActivityDisplay() {
4266 }
Craig Mautner4504de52013-12-20 09:06:56 -08004267
Craig Mautner1a70a162014-09-13 12:09:31 -07004268 // After instantiation, check that mDisplay is not null before using this. The alternative
4269 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004270 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004271 final Display display = mDisplayManager.getDisplay(displayId);
4272 if (display == null) {
4273 return;
4274 }
4275 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004276 }
4277
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004278 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004279 mDisplay = display;
4280 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004281 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004282 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004283
4284 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004285 if (DEBUG_STACK) Slog.v(TAG_STACK,
4286 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004287 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004288 }
4289
Craig Mautnere0a38842013-12-16 16:14:02 -08004290 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004291 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004292 + " from displayId=" + mDisplayId);
4293 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004294 }
4295
4296 void getBounds(Point bounds) {
4297 mDisplay.getDisplayInfo(mDisplayInfo);
4298 bounds.x = mDisplayInfo.appWidth;
4299 bounds.y = mDisplayInfo.appHeight;
4300 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004301
Jose Lima4b6c6692014-08-12 17:41:12 -07004302 void setVisibleBehindActivity(ActivityRecord r) {
4303 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004304 }
4305
Jose Lima4b6c6692014-08-12 17:41:12 -07004306 boolean hasVisibleBehindActivity() {
4307 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004308 }
4309
Craig Mautner34b73df2014-01-12 21:11:08 -08004310 @Override
4311 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004312 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4313 }
4314 }
4315
4316 class VirtualActivityDisplay extends ActivityDisplay {
4317 VirtualDisplay mVirtualDisplay;
4318
Craig Mautner6985bad2014-04-21 15:22:06 -07004319 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004320 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004321 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4322 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4323 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4324 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004325
4326 init(mVirtualDisplay.getDisplay());
4327
4328 mWindowManager.handleDisplayAdded(mDisplayId);
4329 }
4330
4331 void setSurface(Surface surface) {
4332 if (mVirtualDisplay != null) {
4333 mVirtualDisplay.setSurface(surface);
4334 }
4335 }
4336
4337 @Override
4338 void detachActivitiesLocked(ActivityStack stack) {
4339 super.detachActivitiesLocked(stack);
4340 if (mVirtualDisplay != null) {
4341 mVirtualDisplay.release();
4342 mVirtualDisplay = null;
4343 }
4344 }
4345
4346 @Override
4347 public String toString() {
4348 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004349 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004350 }
Jose Lima58e66d62014-05-27 20:00:27 -07004351
4352 private boolean isLeanbackOnlyDevice() {
4353 boolean onLeanbackOnly = false;
4354 try {
4355 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4356 PackageManager.FEATURE_LEANBACK_ONLY);
4357 } catch (RemoteException e) {
4358 // noop
4359 }
4360
4361 return onLeanbackOnly;
4362 }
Craig Mautner27084302013-03-25 08:05:25 -07004363}