blob: 1585f6114647ba5e922cc9185c31aee50b1e3e45 [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 Mautner4a1cb222013-12-04 16:14:06 -0800169
Craig Mautner4504de52013-12-20 09:06:56 -0800170 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700171
Jason Monk62515be2014-05-21 16:06:19 -0400172 private static final String LOCK_TASK_TAG = "Lock-to-App";
173
justinzhang5286d3f2014-05-12 17:06:01 -0400174 /** Status Bar Service **/
175 private IBinder mToken = new Binder();
176 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400177 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400178
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700179 // For debugging to make sure the caller when acquiring/releasing our
180 // wake lock is the system process.
181 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700182
Craig Mautner27084302013-03-25 08:05:25 -0700183 final ActivityManagerService mService;
184
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800185 private final RecentTasks mRecentTasks;
186
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700187 final ActivityStackSupervisorHandler mHandler;
188
189 /** Short cut */
190 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800191 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700192
Craig Mautner8d341ef2013-03-26 09:03:27 -0700193 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700194 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700195
196 /** Task identifier that activities are currently being started in. Incremented each time a
197 * new task is created. */
198 private int mCurTaskId = 0;
199
Craig Mautner2420ead2013-04-01 17:13:20 -0700200 /** The current user */
201 private int mCurrentUser;
202
Craig Mautnere0a38842013-12-16 16:14:02 -0800203 /** The stack containing the launcher app. Assumed to always be attached to
204 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700205 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700206
Craig Mautnere0a38842013-12-16 16:14:02 -0800207 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700208 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700209
Craig Mautner4a1cb222013-12-04 16:14:06 -0800210 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
211 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800212 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800213 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700214
215 /** List of activities that are waiting for a new activity to become visible before completing
216 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800217 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700218
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700219 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800220 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700221
222 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800223 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700224
Craig Mautnerde4ef022013-04-07 19:01:33 -0700225 /** List of activities that are ready to be stopped, but waiting for the next activity to
226 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800227 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700228
Craig Mautnerf3333272013-04-22 10:55:53 -0700229 /** List of activities that are ready to be finished, but waiting for the previous activity to
230 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800231 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700232
Craig Mautner0eea92c2013-05-16 13:35:39 -0700233 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800234 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700235
Craig Mautnerf3333272013-04-22 10:55:53 -0700236 /** Used on user changes */
Craig Mautner8c14c152015-01-15 17:32:07 -0800237 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700238
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700239 /** Used to queue up any background users being started */
Craig Mautner8c14c152015-01-15 17:32:07 -0800240 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700241
Craig Mautnerde4ef022013-04-07 19:01:33 -0700242 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
243 * is being brought in front of us. */
244 boolean mUserLeaving = false;
245
Craig Mautner0eea92c2013-05-16 13:35:39 -0700246 /** Set when we have taken too long waiting to go to sleep. */
247 boolean mSleepTimeout = false;
248
Jose Lima58e66d62014-05-27 20:00:27 -0700249 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
250 * setWindowManager is called. **/
251 private boolean mLeanbackOnlyDevice;
252
Craig Mautner0eea92c2013-05-16 13:35:39 -0700253 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700254 * We don't want to allow the device to go to sleep while in the process
255 * of launching an activity. This is primarily to allow alarm intent
256 * receivers to launch an activity and get that to run before the device
257 * goes back to sleep.
258 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700259 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700260
261 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700262 * Set when the system is going to sleep, until we have
263 * successfully paused the current activity and released our wake lock.
264 * At that point the system is allowed to actually sleep.
265 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700266 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700267
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700268 /** Stack id of the front stack when user switched, indexed by userId. */
269 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700270
Craig Mautner4504de52013-12-20 09:06:56 -0800271 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800272 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700273 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800274
275 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700276 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800277
Jeff Brownca9bc702014-02-11 14:32:56 -0800278 InputManagerInternal mInputManagerInternal;
279
Craig Mautner15df08a2015-04-01 12:17:18 -0700280 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
281 * may be finished until there is only one entry left. If this is empty the system is not
282 * in lockTask mode. */
283 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000284 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700285 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
286 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000287 */
288 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400289 /**
290 * Notifies the user when entering/exiting lock-task.
291 */
292 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800293
Craig Mautner15df08a2015-04-01 12:17:18 -0700294 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700295
Craig Mautner42d04db2014-11-06 12:13:23 -0800296 /** Used to keep resumeTopActivityLocked() from being entered recursively */
297 boolean inResumeTopActivity;
298
Craig Mautneree36c772014-07-16 14:56:05 -0700299 /**
300 * Description of a request to start a new activity, which has been held
301 * due to app switches being disabled.
302 */
303 static class PendingActivityLaunch {
304 final ActivityRecord r;
305 final ActivityRecord sourceRecord;
306 final int startFlags;
307 final ActivityStack stack;
308
309 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
310 int _startFlags, ActivityStack _stack) {
311 r = _r;
312 sourceRecord = _sourceRecord;
313 startFlags = _startFlags;
314 stack = _stack;
315 }
316 }
317
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800318 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700319 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800320 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700321 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
322 }
323
324 /**
325 * At the time when the constructor runs, the power manager has not yet been
326 * initialized. So we initialize our wakelocks afterwards.
327 */
328 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800329 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700330 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700331 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700332 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700333 }
334
justinzhang5286d3f2014-05-12 17:06:01 -0400335 // This function returns a IStatusBarService. The value is from ServiceManager.
336 // getService and is cached.
337 private IStatusBarService getStatusBarService() {
338 synchronized (mService) {
339 if (mStatusBarService == null) {
340 mStatusBarService = IStatusBarService.Stub.asInterface(
341 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
342 if (mStatusBarService == null) {
343 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
344 }
345 }
346 return mStatusBarService;
347 }
348 }
349
Jason Monk35c62a42014-06-17 10:24:47 -0400350 private IDevicePolicyManager getDevicePolicyManager() {
351 synchronized (mService) {
352 if (mDevicePolicyManager == null) {
353 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
354 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
355 if (mDevicePolicyManager == null) {
356 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
357 }
358 }
359 return mDevicePolicyManager;
360 }
361 }
362
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700363 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800364 synchronized (mService) {
365 mWindowManager = wm;
366
367 mDisplayManager =
368 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
369 mDisplayManager.registerDisplayListener(this, null);
370
371 Display[] displays = mDisplayManager.getDisplays();
372 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
373 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800374 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700375 if (activityDisplay.mDisplay == null) {
376 throw new IllegalStateException("Default Display does not exist");
377 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800378 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 }
380
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700381 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800382 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800383
384 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700385
386 // Initialize this here, now that we can get a valid reference to PackageManager.
387 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800388 }
Craig Mautner27084302013-03-25 08:05:25 -0700389 }
390
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200391 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700392 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200393 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700394 }
395
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700396 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800397 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700398 }
399
Craig Mautnerde4ef022013-04-07 19:01:33 -0700400 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800401 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700402 }
403
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800404 /** Top of all visible stacks is/should always be equal to the focused stack.
405 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
406 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700407 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700408 if (stack == null) {
409 return false;
410 }
411
Craig Mautnerdf88d732014-01-27 09:21:32 -0800412 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
413 if (parent != null) {
414 stack = parent.task.stack;
415 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800416 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700417 }
418
Craig Mautner299f9602015-01-26 09:47:33 -0800419 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800420 moveHomeStack(toFront, reason, null);
421 }
422
423 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800424 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800425 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800426 if (topNdx <= 0) {
427 return;
428 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700429
430 // The home stack should either be at the top or bottom of the stack list.
431 if ((toFront && (stacks.get(topNdx) != mHomeStack))
432 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700433 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700434 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
435 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800436 stacks.remove(mHomeStack);
437 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700438 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800439
440 if (lastFocusedStack != null) {
441 mLastFocusedStack = lastFocusedStack;
442 }
443 mFocusedStack = stacks.get(topNdx);
444
Craig Mautnerde313752015-01-22 14:28:03 -0800445 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
446 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800447 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800448
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800449 if (mService.mBooting || !mService.mBooted) {
450 final ActivityRecord r = topRunningActivityLocked();
451 if (r != null && r.idle) {
452 checkFinishBootingLocked();
453 }
454 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700455 }
456
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700457 /** Returns true if the focus activity was adjusted to the home stack top activity. */
458 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700459 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
460 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700461 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700462 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700463
Craig Mautner84984fa2014-06-19 11:19:20 -0700464 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700465
466 final ActivityRecord top = mHomeStack.topRunningActivityLocked(null);
467 if (top == null) {
468 return false;
469 }
470 mService.setFocusedActivityLocked(top, reason);
471 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700472 }
473
Craig Mautner299f9602015-01-26 09:47:33 -0800474 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700475 if (!mService.mBooting && !mService.mBooted) {
476 // Not ready yet!
477 return false;
478 }
479
Craig Mautner84984fa2014-06-19 11:19:20 -0700480 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
481 mWindowManager.showRecentApps();
482 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700483 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700484
Craig Mautner84984fa2014-06-19 11:19:20 -0700485 if (prev != null) {
486 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
487 }
488
Craig Mautnera8a90e02013-06-28 15:24:50 -0700489 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700490 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800491 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700492 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700493 }
Craig Mautner299f9602015-01-26 09:47:33 -0800494 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700495 }
496
Craig Mautner8d341ef2013-03-26 09:03:27 -0700497 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700498 return anyTaskForIdLocked(id, true);
499 }
500
501 /**
502 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
503 * @param id Id of the task we would like returned.
504 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
505 * restore the task from recents to the active list.
506 */
507 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800508 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800509 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800510 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800511 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
512 ActivityStack stack = stacks.get(stackNdx);
513 TaskRecord task = stack.taskForIdLocked(id);
514 if (task != null) {
515 return task;
516 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700517 }
518 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800519
520 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700521 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800522 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800523 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700524 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800525 return null;
526 }
527
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700528 if (!restoreFromRecents) {
529 return task;
530 }
531
Wale Ogunwale7de05352014-12-12 15:21:33 -0800532 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700533 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
534 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800535 return null;
536 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700537 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800538 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700539 }
540
Craig Mautner6170f732013-04-02 13:05:23 -0700541 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800542 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800543 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800544 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
546 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
547 if (r != null) {
548 return r;
549 }
Craig Mautner6170f732013-04-02 13:05:23 -0700550 }
551 }
552 return null;
553 }
554
Craig Mautneref73ee12014-04-23 11:45:37 -0700555 void setNextTaskId(int taskId) {
556 if (taskId > mCurTaskId) {
557 mCurTaskId = taskId;
558 }
559 }
560
Craig Mautner8d341ef2013-03-26 09:03:27 -0700561 int getNextTaskId() {
562 do {
563 mCurTaskId++;
564 if (mCurTaskId <= 0) {
565 mCurTaskId = 1;
566 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700567 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700568 return mCurTaskId;
569 }
570
Craig Mautnerde4ef022013-04-07 19:01:33 -0700571 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800572 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700573 if (stack == null) {
574 return null;
575 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700576 ActivityRecord resumedActivity = stack.mResumedActivity;
577 if (resumedActivity == null || resumedActivity.app == null) {
578 resumedActivity = stack.mPausingActivity;
579 if (resumedActivity == null || resumedActivity.app == null) {
580 resumedActivity = stack.topRunningActivityLocked(null);
581 }
582 }
583 return resumedActivity;
584 }
585
Dianne Hackbornff072722014-09-24 10:56:28 -0700586 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700587 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800588 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800589 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
590 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
592 final ActivityStack stack = stacks.get(stackNdx);
593 if (!isFrontStack(stack)) {
594 continue;
595 }
596 ActivityRecord hr = stack.topRunningActivityLocked(null);
597 if (hr != null) {
598 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
599 && processName.equals(hr.processName)) {
600 try {
George Mount2c92c972014-03-20 09:38:23 -0700601 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 didSomething = true;
603 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700604 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605 Slog.w(TAG, "Exception in new application when starting activity "
606 + hr.intent.getComponent().flattenToShortString(), e);
607 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700608 }
Craig Mautner20e72272013-04-01 13:45:53 -0700609 }
Craig Mautner20e72272013-04-01 13:45:53 -0700610 }
611 }
612 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700613 if (!didSomething) {
614 ensureActivitiesVisibleLocked(null, 0);
615 }
Craig Mautner20e72272013-04-01 13:45:53 -0700616 return didSomething;
617 }
618
619 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800620 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
621 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800622 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
623 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800624 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 continue;
626 }
627 final ActivityRecord resumedActivity = stack.mResumedActivity;
628 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800629 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
630 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 return false;
632 }
Craig Mautner20e72272013-04-01 13:45:53 -0700633 }
634 }
635 return true;
636 }
637
Craig Mautnerde4ef022013-04-07 19:01:33 -0700638 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
640 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
642 final ActivityStack stack = stacks.get(stackNdx);
643 if (isFrontStack(stack)) {
644 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700645 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800646 return false;
647 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700648 }
649 }
650 }
651 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700652 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800653 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
654 mLastFocusedStack + " to=" + mFocusedStack);
655 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700656 return true;
657 }
658
659 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800660 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800661 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
662 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
664 final ActivityStack stack = stacks.get(stackNdx);
665 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800666 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700667 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800668 return false;
669 }
670 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700672 }
673 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800674 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700675 }
676
Craig Mautner2acc3892013-09-23 10:28:14 -0700677 /**
678 * Pause all activities in either all of the stacks or just the back stacks.
679 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700680 * @return true if any activity was paused as a result of this call.
681 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700682 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700683 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800684 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
685 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
687 final ActivityStack stack = stacks.get(stackNdx);
688 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
689 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
690 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700691 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
692 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800693 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700694 }
695 }
696 return someActivityPaused;
697 }
698
Craig Mautnerde4ef022013-04-07 19:01:33 -0700699 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700700 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800701 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
702 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
704 final ActivityStack stack = stacks.get(stackNdx);
705 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700706 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 if (DEBUG_STATES) {
708 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
709 pausing = false;
710 } else {
711 return false;
712 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700713 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700714 }
715 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700716 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700717 }
718
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700719 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
720 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500721 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
723 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 final ActivityStack stack = stacks.get(stackNdx);
726 if (stack.mResumedActivity != null &&
727 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700728 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800729 }
730 }
731 }
732 }
733
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700734 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700735 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700736 }
737
738 void sendWaitingVisibleReportLocked(ActivityRecord r) {
739 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700740 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700741 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700742 if (w.who == null) {
743 changed = true;
744 w.timeout = false;
745 if (r != null) {
746 w.who = new ComponentName(r.info.packageName, r.info.name);
747 }
748 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
749 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700750 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700751 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700752 if (changed) {
753 mService.notifyAll();
754 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700755 }
756
757 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
758 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700759 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700760 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700761 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700762 if (w.who == null) {
763 changed = true;
764 w.timeout = timeout;
765 if (r != null) {
766 w.who = new ComponentName(r.info.packageName, r.info.name);
767 }
768 w.thisTime = thisTime;
769 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700770 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700771 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700772 if (changed) {
773 mService.notifyAll();
774 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700775 }
776
Craig Mautner29219d92013-04-16 20:19:12 -0700777 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800778 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700779 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
780 if (r != null) {
781 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700782 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700783
Craig Mautner4a1cb222013-12-04 16:14:06 -0800784 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800785 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800786 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
787 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700788 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700789 r = stack.topRunningActivityLocked(null);
790 if (r != null) {
791 return r;
792 }
793 }
794 }
795 return null;
796 }
797
Dianne Hackborn09233282014-04-30 11:33:59 -0700798 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700799 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800800 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
801 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800802 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800803 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800804 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800805 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
806 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700807 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800808 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700809 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700810 }
811 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700812
813 // The lists are already sorted from most recent to oldest. Just pull the most recent off
814 // each list and add it to list. Stop when all lists are empty or maxNum reached.
815 while (maxNum > 0) {
816 long mostRecentActiveTime = Long.MIN_VALUE;
817 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800818 final int numTaskLists = runningTaskLists.size();
819 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
820 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700821 if (!stackTaskList.isEmpty()) {
822 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
823 if (lastActiveTime > mostRecentActiveTime) {
824 mostRecentActiveTime = lastActiveTime;
825 selectedStackList = stackTaskList;
826 }
827 }
828 }
829 if (selectedStackList != null) {
830 list.add(selectedStackList.remove(0));
831 --maxNum;
832 } else {
833 break;
834 }
835 }
Craig Mautner20e72272013-04-01 13:45:53 -0700836 }
837
Craig Mautner23ac33b2013-04-01 16:26:35 -0700838 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700839 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700840 // Collect information about the target of the Intent.
841 ActivityInfo aInfo;
842 try {
843 ResolveInfo rInfo =
844 AppGlobals.getPackageManager().resolveIntent(
845 intent, resolvedType,
846 PackageManager.MATCH_DEFAULT_ONLY
847 | ActivityManagerService.STOCK_PM_FLAGS, userId);
848 aInfo = rInfo != null ? rInfo.activityInfo : null;
849 } catch (RemoteException e) {
850 aInfo = null;
851 }
852
853 if (aInfo != null) {
854 // Store the found target back into the intent, because now that
855 // we have it we never want to do this again. For example, if the
856 // user navigates back to this point in the history, we should
857 // always restart the exact same activity.
858 intent.setComponent(new ComponentName(
859 aInfo.applicationInfo.packageName, aInfo.name));
860
861 // Don't debug things in the system process
862 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
863 if (!aInfo.processName.equals("system")) {
864 mService.setDebugApp(aInfo.processName, true, false);
865 }
866 }
867
868 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
869 if (!aInfo.processName.equals("system")) {
870 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
871 }
872 }
873
Jeff Hao1b012d32014-08-20 10:35:34 -0700874 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700875 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700876 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700877 }
878 }
879 }
880 return aInfo;
881 }
882
Craig Mautner299f9602015-01-26 09:47:33 -0800883 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
884 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Dianne Hackborn95465202014-09-15 16:21:55 -0700885 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
886 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700887 }
888
Craig Mautner23ac33b2013-04-01 16:26:35 -0700889 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700890 String callingPackage, Intent intent, String resolvedType,
891 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700892 IBinder resultTo, String resultWho, int requestCode, int startFlags,
893 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700894 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700895 // Refuse possible leaked file descriptors
896 if (intent != null && intent.hasFileDescriptors()) {
897 throw new IllegalArgumentException("File descriptors passed in Intent");
898 }
899 boolean componentSpecified = intent.getComponent() != null;
900
901 // Don't modify the client's object!
902 intent = new Intent(intent);
903
904 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700905 ActivityInfo aInfo =
906 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700907
Craig Mautnere0a38842013-12-16 16:14:02 -0800908 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700909 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800910 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700911 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800912 // Cannot start a child activity if the parent is not resumed.
913 return ActivityManager.START_CANCELED;
914 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700915 final int realCallingPid = Binder.getCallingPid();
916 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700917 int callingPid;
918 if (callingUid >= 0) {
919 callingPid = -1;
920 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700921 callingPid = realCallingPid;
922 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700923 } else {
924 callingPid = callingUid = -1;
925 }
926
Craig Mautnere0a38842013-12-16 16:14:02 -0800927 final ActivityStack stack;
928 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800929 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800930 } else {
931 stack = container.mStack;
932 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800933 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700934 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700935 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700936
937 final long origId = Binder.clearCallingIdentity();
938
939 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800940 (aInfo.applicationInfo.privateFlags
941 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700942 // This may be a heavy-weight process! Check to see if we already
943 // have another, different heavy-weight process running.
944 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
945 if (mService.mHeavyWeightProcess != null &&
946 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
947 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700948 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700949 if (caller != null) {
950 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
951 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700952 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700953 } else {
954 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700955 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700956 + intent.toString());
957 ActivityOptions.abort(options);
958 return ActivityManager.START_PERMISSION_DENIED;
959 }
960 }
961
962 IIntentSender target = mService.getIntentSenderLocked(
963 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700964 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700965 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
966 | PendingIntent.FLAG_ONE_SHOT, null);
967
968 Intent newIntent = new Intent();
969 if (requestCode >= 0) {
970 // Caller is requesting a result.
971 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
972 }
973 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
974 new IntentSender(target));
975 if (mService.mHeavyWeightProcess.activities.size() > 0) {
976 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
977 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
978 hist.packageName);
979 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
980 hist.task.taskId);
981 }
982 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
983 aInfo.packageName);
984 newIntent.setFlags(intent.getFlags());
985 newIntent.setClassName("android",
986 HeavyWeightSwitcherActivity.class.getName());
987 intent = newIntent;
988 resolvedType = null;
989 caller = null;
990 callingUid = Binder.getCallingUid();
991 callingPid = Binder.getCallingPid();
992 componentSpecified = true;
993 try {
994 ResolveInfo rInfo =
995 AppGlobals.getPackageManager().resolveIntent(
996 intent, null,
997 PackageManager.MATCH_DEFAULT_ONLY
998 | ActivityManagerService.STOCK_PM_FLAGS, userId);
999 aInfo = rInfo != null ? rInfo.activityInfo : null;
1000 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1001 } catch (RemoteException e) {
1002 aInfo = null;
1003 }
1004 }
1005 }
1006 }
1007
Dianne Hackborn91097de2014-04-04 18:02:06 -07001008 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1009 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001010 requestCode, callingPid, callingUid, callingPackage,
1011 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001012 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001013
Craig Mautner85c11a82014-07-17 12:38:18 -07001014 Binder.restoreCallingIdentity(origId);
1015
Craig Mautnerde4ef022013-04-07 19:01:33 -07001016 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 // If the caller also wants to switch to a new configuration,
1018 // do so now. This allows a clean switch, as we are waiting
1019 // for the current activity to pause (so we will not destroy
1020 // it), and have not yet started the next activity.
1021 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1022 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001023 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001024 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001025 "Updating to new configuration after starting activity.");
1026 mService.updateConfigurationLocked(config, null, false, false);
1027 }
1028
Craig Mautner23ac33b2013-04-01 16:26:35 -07001029 if (outResult != null) {
1030 outResult.result = res;
1031 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001032 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001033 do {
1034 try {
1035 mService.wait();
1036 } catch (InterruptedException e) {
1037 }
1038 } while (!outResult.timeout && outResult.who == null);
1039 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001040 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001041 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001042 outResult.timeout = false;
1043 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1044 outResult.totalTime = 0;
1045 outResult.thisTime = 0;
1046 } else {
1047 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001048 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001049 do {
1050 try {
1051 mService.wait();
1052 } catch (InterruptedException e) {
1053 }
1054 } while (!outResult.timeout && outResult.who == null);
1055 }
1056 }
1057 }
1058
1059 return res;
1060 }
1061 }
1062
1063 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1064 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1065 Bundle options, int userId) {
1066 if (intents == null) {
1067 throw new NullPointerException("intents is null");
1068 }
1069 if (resolvedTypes == null) {
1070 throw new NullPointerException("resolvedTypes is null");
1071 }
1072 if (intents.length != resolvedTypes.length) {
1073 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1074 }
1075
Craig Mautner23ac33b2013-04-01 16:26:35 -07001076
1077 int callingPid;
1078 if (callingUid >= 0) {
1079 callingPid = -1;
1080 } else if (caller == null) {
1081 callingPid = Binder.getCallingPid();
1082 callingUid = Binder.getCallingUid();
1083 } else {
1084 callingPid = callingUid = -1;
1085 }
1086 final long origId = Binder.clearCallingIdentity();
1087 try {
1088 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001089 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001090 for (int i=0; i<intents.length; i++) {
1091 Intent intent = intents[i];
1092 if (intent == null) {
1093 continue;
1094 }
1095
1096 // Refuse possible leaked file descriptors
1097 if (intent != null && intent.hasFileDescriptors()) {
1098 throw new IllegalArgumentException("File descriptors passed in Intent");
1099 }
1100
1101 boolean componentSpecified = intent.getComponent() != null;
1102
1103 // Don't modify the client's object!
1104 intent = new Intent(intent);
1105
1106 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001107 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001108 // TODO: New, check if this is correct
1109 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1110
1111 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001112 (aInfo.applicationInfo.privateFlags
1113 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001114 throw new IllegalArgumentException(
1115 "FLAG_CANT_SAVE_STATE not supported here");
1116 }
1117
1118 Bundle theseOptions;
1119 if (options != null && i == intents.length-1) {
1120 theseOptions = options;
1121 } else {
1122 theseOptions = null;
1123 }
Craig Mautner6170f732013-04-02 13:05:23 -07001124 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001125 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1126 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001127 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001128 if (res < 0) {
1129 return res;
1130 }
1131
1132 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1133 }
1134 }
1135 } finally {
1136 Binder.restoreCallingIdentity(origId);
1137 }
1138
1139 return ActivityManager.START_SUCCESS;
1140 }
1141
Craig Mautner2420ead2013-04-01 17:13:20 -07001142 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001143 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001144 throws RemoteException {
1145
Craig Mautner2568c3a2015-03-26 14:22:34 -07001146 if (andResume) {
1147 r.startFreezingScreenLocked(app, 0);
1148 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001149
Craig Mautner2568c3a2015-03-26 14:22:34 -07001150 // schedule launch ticks to collect information about slow apps.
1151 r.startLaunchTickingLocked();
1152 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001153
1154 // Have the window manager re-evaluate the orientation of
1155 // the screen based on the new activity order. Note that
1156 // as a result of this, it can call back into the activity
1157 // manager with a new orientation. We don't care about that,
1158 // because the activity is not currently running so we are
1159 // just restarting it anyway.
1160 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001161 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001162 mService.mConfiguration,
1163 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1164 mService.updateConfigurationLocked(config, r, false, false);
1165 }
1166
1167 r.app = app;
1168 app.waitingToKill = null;
1169 r.launchCount++;
1170 r.lastLaunchTime = SystemClock.uptimeMillis();
1171
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001172 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001173
1174 int idx = app.activities.indexOf(r);
1175 if (idx < 0) {
1176 app.activities.add(r);
1177 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001178 mService.updateLruProcessLocked(app, true, null);
1179 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001180
Craig Mautner15df08a2015-04-01 12:17:18 -07001181 final TaskRecord task = r.task;
1182 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
1183 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "lockTaskLaunchMode attribute");
1184 }
1185
1186 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001187 try {
1188 if (app.thread == null) {
1189 throw new RemoteException();
1190 }
1191 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001192 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001193 if (andResume) {
1194 results = r.results;
1195 newIntents = r.newIntents;
1196 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001197 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1198 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1199 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001200 if (andResume) {
1201 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1202 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001203 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001204 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001205 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001206 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001207 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 }
1209 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1210 r.sleeping = false;
1211 r.forceNewConfig = false;
1212 mService.showAskCompatModeDialogLocked(r);
1213 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001214 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1216 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1217 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001218 final String profileFile = mService.mProfileFile;
1219 if (profileFile != null) {
1220 ParcelFileDescriptor profileFd = mService.mProfileFd;
1221 if (profileFd != null) {
1222 try {
1223 profileFd = profileFd.dup();
1224 } catch (IOException e) {
1225 if (profileFd != null) {
1226 try {
1227 profileFd.close();
1228 } catch (IOException o) {
1229 }
1230 profileFd = null;
1231 }
1232 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001234
1235 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1236 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001237 }
1238 }
1239 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001240
Craig Mautner2568c3a2015-03-26 14:22:34 -07001241 if (andResume) {
1242 app.hasShownUi = true;
1243 app.pendingUiClean = true;
1244 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001245 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001246 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001247 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwale60454db2015-01-23 16:05:07 -08001248 new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001249 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001250 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001251
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001252 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 // This may be a heavy-weight process! Note that the package
1254 // manager will ensure that only activity can run in the main
1255 // process of the .apk, which is the only thing that will be
1256 // considered heavy-weight.
1257 if (app.processName.equals(app.info.packageName)) {
1258 if (mService.mHeavyWeightProcess != null
1259 && mService.mHeavyWeightProcess != app) {
1260 Slog.w(TAG, "Starting new heavy weight process " + app
1261 + " when already running "
1262 + mService.mHeavyWeightProcess);
1263 }
1264 mService.mHeavyWeightProcess = app;
1265 Message msg = mService.mHandler.obtainMessage(
1266 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1267 msg.obj = r;
1268 mService.mHandler.sendMessage(msg);
1269 }
1270 }
1271
1272 } catch (RemoteException e) {
1273 if (r.launchFailed) {
1274 // This is the second time we failed -- finish activity
1275 // and give up.
1276 Slog.e(TAG, "Second failure launching "
1277 + r.intent.getComponent().flattenToShortString()
1278 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001279 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1281 "2nd-crash", false);
1282 return false;
1283 }
1284
1285 // This is the first time we failed -- restart process and
1286 // retry.
1287 app.activities.remove(r);
1288 throw e;
1289 }
1290
1291 r.launchFailed = false;
1292 if (stack.updateLRUListLocked(r)) {
1293 Slog.w(TAG, "Activity " + r
1294 + " being launched, but already in LRU list");
1295 }
1296
1297 if (andResume) {
1298 // As part of the process of launching, ActivityThread also performs
1299 // a resume.
1300 stack.minimalResumeActivityLocked(r);
1301 } else {
1302 // This activity is not starting in the resumed state... which
1303 // should look like we asked it to pause+stop (but remain visible),
1304 // and it has done so and reported back the current icicle and
1305 // other state.
1306 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1307 + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001308 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001309 r.stopped = true;
1310 }
1311
1312 // Launch the new version setup screen if needed. We do this -after-
1313 // launching the initial activity (that is, home), so that it can have
1314 // a chance to initialize itself while in the background, making the
1315 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001316 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001317 mService.startSetupActivityLocked();
1318 }
1319
Dianne Hackborn465fa392014-09-14 14:21:18 -07001320 // Update any services we are bound to that might care about whether
1321 // their client may have activities.
1322 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1323
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 return true;
1325 }
1326
Craig Mautnere79d42682013-04-01 19:01:53 -07001327 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001328 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001329 // Is this activity's application already running?
1330 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001331 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001332
1333 r.task.stack.setLaunchTime(r);
1334
1335 if (app != null && app.thread != null) {
1336 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001337 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1338 || !"android".equals(r.info.packageName)) {
1339 // Don't add this if it is a platform component that is marked
1340 // to run in multiple processes, because this is actually
1341 // part of the framework so doesn't make sense to track as a
1342 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001343 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1344 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001345 }
George Mount2c92c972014-03-20 09:38:23 -07001346 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001347 return;
1348 } catch (RemoteException e) {
1349 Slog.w(TAG, "Exception when starting activity "
1350 + r.intent.getComponent().flattenToShortString(), e);
1351 }
1352
1353 // If a dead object exception was thrown -- fall through to
1354 // restart the application.
1355 }
1356
1357 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001358 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001359 }
1360
Craig Mautner6170f732013-04-02 13:05:23 -07001361 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001362 Intent intent, String resolvedType, ActivityInfo aInfo,
1363 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1364 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001365 int callingPid, int callingUid, String callingPackage,
1366 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001367 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1368 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001369 int err = ActivityManager.START_SUCCESS;
1370
1371 ProcessRecord callerApp = null;
1372 if (caller != null) {
1373 callerApp = mService.getRecordForAppLocked(caller);
1374 if (callerApp != null) {
1375 callingPid = callerApp.pid;
1376 callingUid = callerApp.info.uid;
1377 } else {
1378 Slog.w(TAG, "Unable to find app for caller " + caller
1379 + " (pid=" + callingPid + ") when starting: "
1380 + intent.toString());
1381 err = ActivityManager.START_PERMISSION_DENIED;
1382 }
1383 }
1384
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001385 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1386
Craig Mautner6170f732013-04-02 13:05:23 -07001387 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001388 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001389 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001390 + " on display " + (container == null ? (mFocusedStack == null ?
1391 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1392 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1393 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001394 }
1395
1396 ActivityRecord sourceRecord = null;
1397 ActivityRecord resultRecord = null;
1398 if (resultTo != null) {
1399 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001400 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1401 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001402 if (sourceRecord != null) {
1403 if (requestCode >= 0 && !sourceRecord.finishing) {
1404 resultRecord = sourceRecord;
1405 }
1406 }
1407 }
Craig Mautner6170f732013-04-02 13:05:23 -07001408
Dianne Hackborn91097de2014-04-04 18:02:06 -07001409 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001410
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001411 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001412 // Transfer the result target from the source activity to the new
1413 // one being started, including any failures.
1414 if (requestCode >= 0) {
1415 ActivityOptions.abort(options);
1416 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1417 }
1418 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001419 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1420 resultRecord = null;
1421 }
Craig Mautner6170f732013-04-02 13:05:23 -07001422 resultWho = sourceRecord.resultWho;
1423 requestCode = sourceRecord.requestCode;
1424 sourceRecord.resultTo = null;
1425 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001426 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001427 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001428 if (sourceRecord.launchedFromUid == callingUid) {
1429 // The new activity is being launched from the same uid as the previous
1430 // activity in the flow, and asking to forward its result back to the
1431 // previous. In this case the activity is serving as a trampoline between
1432 // the two, so we also want to update its launchedFromPackage to be the
1433 // same as the previous activity. Note that this is safe, since we know
1434 // these two packages come from the same uid; the caller could just as
1435 // well have supplied that same package name itself. This specifially
1436 // deals with the case of an intent picker/chooser being launched in the app
1437 // flow to redirect to an activity picked by the user, where we want the final
1438 // activity to consider it to have been launched by the previous app activity.
1439 callingPackage = sourceRecord.launchedFromPackage;
1440 }
Craig Mautner6170f732013-04-02 13:05:23 -07001441 }
1442
1443 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1444 // We couldn't find a class that can handle the given Intent.
1445 // That's the end of that!
1446 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1447 }
1448
1449 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1450 // We couldn't find the specific class specified in the Intent.
1451 // Also the end of the line.
1452 err = ActivityManager.START_CLASS_NOT_FOUND;
1453 }
1454
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001455 if (err == ActivityManager.START_SUCCESS
1456 && !isCurrentProfileLocked(userId)
1457 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1458 // Trying to launch a background activity that doesn't show for all users.
1459 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1460 }
1461
Dianne Hackborn91097de2014-04-04 18:02:06 -07001462 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1463 && sourceRecord.task.voiceSession != null) {
1464 // If this activity is being launched as part of a voice session, we need
1465 // to ensure that it is safe to do so. If the upcoming activity will also
1466 // be part of the voice session, we can only launch it if it has explicitly
1467 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001468 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001469 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1470 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001471 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1472 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001473 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1474 }
1475 } catch (RemoteException e) {
1476 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1477 }
1478 }
1479 }
1480
1481 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1482 // If the caller is starting a new voice session, just make sure the target
1483 // is actually allowing it to run this way.
1484 try {
1485 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1486 intent, resolvedType)) {
1487 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1488 }
1489 } catch (RemoteException e) {
1490 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1491 }
1492 }
1493
louis_changcd5d1982014-08-01 10:09:08 +08001494 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1495
Craig Mautner6170f732013-04-02 13:05:23 -07001496 if (err != ActivityManager.START_SUCCESS) {
1497 if (resultRecord != null) {
1498 resultStack.sendActivityResultLocked(-1,
1499 resultRecord, resultWho, requestCode,
1500 Activity.RESULT_CANCELED, null);
1501 }
Craig Mautner6170f732013-04-02 13:05:23 -07001502 ActivityOptions.abort(options);
1503 return err;
1504 }
1505
1506 final int startAnyPerm = mService.checkPermission(
1507 START_ANY_ACTIVITY, callingPid, callingUid);
1508 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1509 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1510 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1511 if (resultRecord != null) {
1512 resultStack.sendActivityResultLocked(-1,
1513 resultRecord, resultWho, requestCode,
1514 Activity.RESULT_CANCELED, null);
1515 }
Craig Mautner6170f732013-04-02 13:05:23 -07001516 String msg;
1517 if (!aInfo.exported) {
1518 msg = "Permission Denial: starting " + intent.toString()
1519 + " from " + callerApp + " (pid=" + callingPid
1520 + ", uid=" + callingUid + ")"
1521 + " not exported from uid " + aInfo.applicationInfo.uid;
1522 } else {
1523 msg = "Permission Denial: starting " + intent.toString()
1524 + " from " + callerApp + " (pid=" + callingPid
1525 + ", uid=" + callingUid + ")"
1526 + " requires " + aInfo.permission;
1527 }
1528 Slog.w(TAG, msg);
1529 throw new SecurityException(msg);
1530 }
1531
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001532 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001533 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001534
Craig Mautner6170f732013-04-02 13:05:23 -07001535 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001536 try {
1537 // The Intent we give to the watcher has the extra data
1538 // stripped off, since it can contain private information.
1539 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001540 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001541 aInfo.applicationInfo.packageName);
1542 } catch (RemoteException e) {
1543 mService.mController = null;
1544 }
Ben Gruver5e207332013-04-03 17:41:37 -07001545 }
Craig Mautner6170f732013-04-02 13:05:23 -07001546
Ben Gruver5e207332013-04-03 17:41:37 -07001547 if (abort) {
1548 if (resultRecord != null) {
1549 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001550 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001551 }
Ben Gruver5e207332013-04-03 17:41:37 -07001552 // We pretend to the caller that it was really started, but
1553 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001554 ActivityOptions.abort(options);
1555 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001556 }
1557
1558 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001559 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001560 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001561 if (outActivity != null) {
1562 outActivity[0] = r;
1563 }
1564
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001565 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001566 if (voiceSession == null && (stack.mResumedActivity == null
1567 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001568 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1569 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001570 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001571 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001572 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001573 ActivityOptions.abort(options);
1574 return ActivityManager.START_SWITCHES_CANCELED;
1575 }
1576 }
1577
1578 if (mService.mDidAppSwitch) {
1579 // This is the second allowed switch since we stopped switches,
1580 // so now just generally allow switches. Use case: user presses
1581 // home (switches disabled, switch to home, mDidAppSwitch now true);
1582 // user taps a home icon (coming from home so allowed, we hit here
1583 // and now allow anyone to switch again).
1584 mService.mAppSwitchesAllowedTime = 0;
1585 } else {
1586 mService.mDidAppSwitch = true;
1587 }
1588
Craig Mautneree36c772014-07-16 14:56:05 -07001589 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001590
Dianne Hackborn91097de2014-04-04 18:02:06 -07001591 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001592 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001593
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001594 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001595 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001596 // activity start, but we are not actually doing an activity
1597 // switch... just dismiss the keyguard now, because we
1598 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001599 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001600 }
1601 return err;
1602 }
1603
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001604 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001605 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001606
1607 // On leanback only devices we should keep all activities in the same stack.
1608 if (!mLeanbackOnlyDevice &&
1609 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001610
1611 ActivityStack stack;
1612
Wale Ogunwale7d701172015-03-11 15:36:30 -07001613 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001614 stack = task.stack;
1615 if (stack.isOnHomeDisplay()) {
1616 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001617 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1618 "computeStackFocus: Setting " + "focused stack to r=" + r
1619 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001620 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001621 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001622 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001623 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001624 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001625 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001626 }
1627
Craig Mautnere0a38842013-12-16 16:14:02 -08001628 final ActivityContainer container = r.mInitialActivityContainer;
1629 if (container != null) {
1630 // The first time put it on the desired stack, after this put on task stack.
1631 r.mInitialActivityContainer = null;
1632 return container.mStack;
1633 }
1634
Craig Mautner1b4bf852014-05-26 15:06:32 -07001635 if (mFocusedStack != mHomeStack && (!newTask ||
1636 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001637 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001638 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001639 return mFocusedStack;
1640 }
1641
Craig Mautnere0a38842013-12-16 16:14:02 -08001642 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1643 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001644 stack = homeDisplayStacks.get(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08001645 if (!stack.isHomeStack()) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001646 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001647 "computeStackFocus: Setting focused stack=" + stack);
1648 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001649 }
1650 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001651
Craig Mautner4a1cb222013-12-04 16:14:06 -08001652 // Need to create an app stack for this user.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001653 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001654 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1655 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001656 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001657 }
1658 return mHomeStack;
1659 }
1660
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001661 boolean setFocusedStack(ActivityRecord r, String reason) {
1662 if (r == null) {
1663 // Not sure what you are trying to do, but it is not going to work...
1664 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001665 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001666 final TaskRecord task = r.task;
1667 if (task == null || task.stack == null) {
1668 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1669 return false;
1670 }
1671 task.stack.moveToFront(reason);
1672 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001673 }
1674
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001675 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001676 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001677 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001678 final Intent intent = r.intent;
1679 final int callingUid = r.launchedFromUid;
1680
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001681 // In some flows in to this function, we retrieve the task record and hold on to it
1682 // without a lock before calling back in to here... so the task at this point may
1683 // not actually be in recents. Check for that, and if it isn't in recents just
1684 // consider it invalid.
1685 if (inTask != null && !inTask.inRecents) {
1686 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1687 inTask = null;
1688 }
1689
Craig Mautnerad400af2014-08-13 16:41:36 -07001690 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001691 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1692 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001693
Craig Mautnera228ae92014-07-09 05:44:55 -07001694 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001695 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001696 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001697 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1698 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1699 "\"singleInstance\" or \"singleTask\"");
1700 launchFlags &=
1701 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1702 } else {
1703 switch (r.info.documentLaunchMode) {
1704 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1705 break;
1706 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1707 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1708 break;
1709 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1710 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1711 break;
1712 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1713 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1714 break;
1715 }
1716 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001717
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001718 final boolean launchTaskBehind = r.mLaunchTaskBehind
1719 && !launchSingleTask && !launchSingleInstance
1720 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001721
Wale Ogunwale7d701172015-03-11 15:36:30 -07001722 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1723 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001724 // For whatever reason this activity is being launched into a new
1725 // task... yet the caller has requested a result back. Well, that
1726 // is pretty messed up, so instead immediately send back a cancel
1727 // and let the new task continue launched as normal without a
1728 // dependency on its originator.
1729 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1730 r.resultTo.task.stack.sendActivityResultLocked(-1,
1731 r.resultTo, r.resultWho, r.requestCode,
1732 Activity.RESULT_CANCELED, null);
1733 r.resultTo = null;
1734 }
1735
1736 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1737 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1738 }
1739
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001740 // If we are actually going to launch in to a new task, there are some cases where
1741 // we further want to do multiple task.
1742 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1743 if (launchTaskBehind
1744 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1745 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1746 }
1747 }
1748
Craig Mautner8849a5e2013-04-02 16:41:03 -07001749 // We'll invoke onUserLeaving before onPause only if the launching
1750 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001751 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001752 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1753 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001754
1755 // If the caller has asked not to resume at this point, we make note
1756 // of this in the record so that we can skip it when trying to find
1757 // the top running activity.
1758 if (!doResume) {
1759 r.delayedResume = true;
1760 }
1761
Craig Mautnera254cd72014-05-25 16:47:39 -07001762 ActivityRecord notTop =
1763 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001764
1765 // If the onlyIfNeeded flag is set, then we can do this if the activity
1766 // being launched is the same as the one making the call... or, as
1767 // a special case, if we do not know the caller then we count the
1768 // current top activity as the caller.
1769 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1770 ActivityRecord checkedCaller = sourceRecord;
1771 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001772 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001773 }
1774 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1775 // Caller is not the same as launcher, so always needed.
1776 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1777 }
1778 }
1779
Craig Mautner8849a5e2013-04-02 16:41:03 -07001780 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001781 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001782
1783 // If the caller is not coming from another activity, but has given us an
1784 // explicit task into which they would like us to launch the new activity,
1785 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001786 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1787 final Intent baseIntent = inTask.getBaseIntent();
1788 final ActivityRecord root = inTask.getRootActivity();
1789 if (baseIntent == null) {
1790 ActivityOptions.abort(options);
1791 throw new IllegalArgumentException("Launching into task without base intent: "
1792 + inTask);
1793 }
1794
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001795 // If this task is empty, then we are adding the first activity -- it
1796 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001797 if (launchSingleInstance || launchSingleTask) {
1798 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1799 ActivityOptions.abort(options);
1800 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1801 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001802 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001803 if (root != null) {
1804 ActivityOptions.abort(options);
1805 throw new IllegalArgumentException("Caller with inTask " + inTask
1806 + " has root " + root + " but target is singleInstance/Task");
1807 }
1808 }
1809
1810 // If task is empty, then adopt the interesting intent launch flags in to the
1811 // activity being started.
1812 if (root == null) {
1813 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1814 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1815 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1816 launchFlags = (launchFlags&~flagsOfInterest)
1817 | (baseIntent.getFlags()&flagsOfInterest);
1818 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001819 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001820 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001821
Dianne Hackborn962d5352014-08-29 16:27:40 -07001822 // If the task is not empty and the caller is asking to start it as the root
1823 // of a new task, then we don't actually want to start this on the task. We
1824 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001825 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001826 addingToTask = false;
1827
1828 } else {
1829 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001830 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001831
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001832 reuseTask = inTask;
1833 } else {
1834 inTask = null;
1835 }
1836
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001837 if (inTask == null) {
1838 if (sourceRecord == null) {
1839 // This activity is not being started from another... in this
1840 // case we -always- start a new task.
1841 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1842 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1843 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1844 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1845 }
1846 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1847 // The original activity who is starting us is running as a single
1848 // instance... this new activity it is starting must go on its
1849 // own task.
1850 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1851 } else if (launchSingleInstance || launchSingleTask) {
1852 // The activity being started is a single instance... it always
1853 // gets launched into its own task.
1854 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1855 }
1856 }
1857
1858 ActivityInfo newTaskInfo = null;
1859 Intent newTaskIntent = null;
1860 ActivityStack sourceStack;
1861 if (sourceRecord != null) {
1862 if (sourceRecord.finishing) {
1863 // If the source is finishing, we can't further count it as our source. This
1864 // is because the task it is associated with may now be empty and on its way out,
1865 // so we don't want to blindly throw it in to that task. Instead we will take
1866 // the NEW_TASK flow and try to find a task for it. But save the task information
1867 // so it can be used when creating the new task.
1868 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1869 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1870 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1871 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1872 newTaskInfo = sourceRecord.info;
1873 newTaskIntent = sourceRecord.task.intent;
1874 }
1875 sourceRecord = null;
1876 sourceStack = null;
1877 } else {
1878 sourceStack = sourceRecord.task.stack;
1879 }
1880 } else {
1881 sourceStack = null;
1882 }
1883
1884 boolean movedHome = false;
1885 ActivityStack targetStack;
1886
1887 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001888 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001889
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001890 // We may want to try to place the new activity in to an existing task. We always
1891 // do this if the target activity is singleTask or singleInstance; we will also do
1892 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1893 // us to still place it in a new task: multi task, always doc mode, or being asked to
1894 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001895 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1896 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001897 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001898 // If bring to front is requested, and no result is requested and we have not
1899 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001900 // we can find a task that was started with this same
1901 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001902 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001903 // See if there is a task to bring to the front. If this is
1904 // a SINGLE_INSTANCE activity, there can be one and only one
1905 // instance of it in the history, and it is always in its own
1906 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001907 ActivityRecord intentActivity = !launchSingleInstance ?
1908 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001909 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001910 if (isLockTaskModeViolation(intentActivity.task)) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07001911 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001912 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001913 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1914 }
Craig Mautner29219d92013-04-16 20:19:12 -07001915 if (r.task == null) {
1916 r.task = intentActivity.task;
1917 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001918 if (intentActivity.task.intent == null) {
1919 // This task was started because of movement of
1920 // the activity based on affinity... now that we
1921 // are actually launching it, we can assign the
1922 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001923 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001924 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001925 targetStack = intentActivity.task.stack;
1926 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001927 // If the target task is not in the front, then we need
1928 // to bring it to the front... except... well, with
1929 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1930 // to have the same behavior as if a new instance was
1931 // being started, which means not bringing it to the front
1932 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001933 final ActivityStack focusStack = getFocusedStack();
1934 ActivityRecord curTop = (focusStack == null)
1935 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001936 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07001937 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001938 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001939 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001940 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1941 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001942 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001943 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001944 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1945 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001946 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001947 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
1948 options, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001949 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001950 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001951 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1952 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001953 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001954 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001955 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001956 options = null;
1957 }
1958 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001959 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001960 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001961 + " from " + intentActivity);
1962 targetStack.moveToFront("intentActivityFound");
1963 }
1964
Craig Mautner8849a5e2013-04-02 16:41:03 -07001965 // If the caller has requested that the target task be
1966 // reset, then do so.
1967 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1968 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1969 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001970 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001971 // We don't need to start a new activity, and
1972 // the client said not to do anything if that
1973 // is the case, so this is it! And for paranoia, make
1974 // sure we have correctly resumed the top activity.
1975 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001976 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01001977
1978 // Make sure to notify Keyguard as well if we are not running an app
1979 // transition later.
1980 if (!movedToFront) {
1981 notifyActivityDrawnForKeyguard();
1982 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001983 } else {
1984 ActivityOptions.abort(options);
1985 }
1986 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1987 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001988 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001989 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001990 // The caller has requested to completely replace any
1991 // existing task with its new activity. Well that should
1992 // not be too hard...
1993 reuseTask = intentActivity.task;
1994 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001995 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07001996 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001997 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001998 // In this situation we want to remove all activities
1999 // from the task up to the one being started. In most
2000 // cases this means we are resetting the task to its
2001 // initial state.
2002 ActivityRecord top =
2003 intentActivity.task.performClearTaskLocked(r, launchFlags);
2004 if (top != null) {
2005 if (top.frontOfTask) {
2006 // Activity aliases may mean we use different
2007 // intents for the top activity, so make sure
2008 // the task now has the identity of the new
2009 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002010 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002011 }
2012 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2013 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002014 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002015 } else {
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002016 // A special case: we need to start the activity because it is not
2017 // currently running, and the caller has asked to clear the current
2018 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002019 addingToTask = true;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002020 // Now pretend like this activity is being started by the top of its
2021 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002022 sourceRecord = intentActivity;
Wale Ogunwale86920fe2015-03-17 11:36:24 -07002023 TaskRecord task = sourceRecord.task;
2024 if (task != null && task.stack == null) {
2025 // Target stack got cleared when we all activities were removed
2026 // above. Go ahead and reset it.
2027 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2028 targetStack.addTask(
2029 task, !launchTaskBehind /* toTop */, false /* moving */);
2030 }
2031
Craig Mautner8849a5e2013-04-02 16:41:03 -07002032 }
2033 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2034 // In this case the top activity on the task is the
2035 // same as the one being launched, so we take that
2036 // as a request to bring the task to the foreground.
2037 // If the top activity in the task is the root
2038 // activity, deliver this new intent to it if it
2039 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002040 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002041 && intentActivity.realActivity.equals(r.realActivity)) {
2042 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2043 intentActivity.task);
2044 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002045 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002046 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002047 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2048 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002049 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2050 // In this case we are launching the root activity
2051 // of the task, but with a different intent. We
2052 // should start a new instance on top.
2053 addingToTask = true;
2054 sourceRecord = intentActivity;
2055 }
2056 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2057 // In this case an activity is being launched in to an
2058 // existing task, without resetting that task. This
2059 // is typically the situation of launching an activity
2060 // from a notification or shortcut. We want to place
2061 // the new activity on top of the current task.
2062 addingToTask = true;
2063 sourceRecord = intentActivity;
2064 } else if (!intentActivity.task.rootWasReset) {
2065 // In this case we are launching in to an existing task
2066 // that has not yet been started from its front door.
2067 // The current task has been brought to the front.
2068 // Ideally, we'd probably like to place this new task
2069 // at the bottom of its stack, but that's a little hard
2070 // to do with the current organization of the code so
2071 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002072 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002073 }
2074 if (!addingToTask && reuseTask == null) {
2075 // We didn't do anything... but it was needed (a.k.a., client
2076 // don't use that intent!) And for paranoia, make
2077 // sure we have correctly resumed the top activity.
2078 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002079 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002080 if (!movedToFront) {
2081 // Make sure to notify Keyguard as well if we are not running an app
2082 // transition later.
2083 notifyActivityDrawnForKeyguard();
2084 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002085 } else {
2086 ActivityOptions.abort(options);
2087 }
2088 return ActivityManager.START_TASK_TO_FRONT;
2089 }
2090 }
2091 }
2092 }
2093
2094 //String uri = r.intent.toURI();
2095 //Intent intent2 = new Intent(uri);
2096 //Slog.i(TAG, "Given intent: " + r.intent);
2097 //Slog.i(TAG, "URI is: " + uri);
2098 //Slog.i(TAG, "To intent: " + intent2);
2099
2100 if (r.packageName != null) {
2101 // If the activity being launched is the same as the one currently
2102 // at the top, then we need to check if it should only be launched
2103 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002104 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002105 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002106 if (top != null && r.resultTo == null) {
2107 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2108 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002109 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002110 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002111 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2112 top.task);
2113 // For paranoia, make sure we have correctly
2114 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002115 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002116 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002117 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002118 }
2119 ActivityOptions.abort(options);
2120 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2121 // We don't need to start a new activity, and
2122 // the client said not to do anything if that
2123 // is the case, so this is it!
2124 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2125 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002126 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002127 return ActivityManager.START_DELIVERED_TO_TOP;
2128 }
2129 }
2130 }
2131 }
2132
2133 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002134 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002135 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2136 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002137 }
2138 ActivityOptions.abort(options);
2139 return ActivityManager.START_CLASS_NOT_FOUND;
2140 }
2141
2142 boolean newTask = false;
2143 boolean keepCurTransition = false;
2144
Craig Mautnerbb742462014-07-07 15:28:55 -07002145 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002146 sourceRecord.task : null;
2147
Craig Mautner8849a5e2013-04-02 16:41:03 -07002148 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002149 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002150 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002151 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002152 targetStack = computeStackFocus(r, newTask);
2153 targetStack.moveToFront("startingNewTask");
2154
Craig Mautner8849a5e2013-04-02 16:41:03 -07002155 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002156 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2157 newTaskInfo != null ? newTaskInfo : r.info,
2158 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002159 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2160 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002161 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2162 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002163 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002164 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002165 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002166 if (isLockTaskModeViolation(r.task)) {
2167 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2168 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2169 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002170 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002171 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002172 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2173 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002174 // Caller wants to appear on home activity, so before starting
2175 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002176 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002177 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002178 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002179 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002180 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002181 if (isLockTaskModeViolation(sourceTask)) {
2182 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2183 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2184 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002185 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002186 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002187 final TaskRecord topTask = targetStack.topTask();
2188 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002189 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
2190 "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002191 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002192 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002193 // In this case, we are adding the activity to an existing
2194 // task, but the caller has asked to clear that task if the
2195 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002196 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002197 keepCurTransition = true;
2198 if (top != null) {
2199 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002200 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002201 // For paranoia, make sure we have correctly
2202 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002203 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002204 if (doResume) {
2205 targetStack.resumeTopActivityLocked(null);
2206 }
2207 ActivityOptions.abort(options);
2208 return ActivityManager.START_DELIVERED_TO_TOP;
2209 }
2210 } else if (!addingToTask &&
2211 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2212 // In this case, we are launching an activity in our own task
2213 // that may already be running somewhere in the history, and
2214 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002215 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002216 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002217 final TaskRecord task = top.task;
2218 task.moveActivityToFrontLocked(top);
2219 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002220 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002221 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002222 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002223 if (doResume) {
2224 targetStack.resumeTopActivityLocked(null);
2225 }
2226 return ActivityManager.START_DELIVERED_TO_TOP;
2227 }
2228 }
2229 // An existing activity is starting this new activity, so we want
2230 // to keep the new one in the same task as the one that is starting
2231 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002232 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002233 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002234 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002235
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002236 } else if (inTask != null) {
2237 // The calling is asking that the new activity be started in an explicit
2238 // task it has provided to us.
2239 if (isLockTaskModeViolation(inTask)) {
2240 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2241 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2242 }
2243 targetStack = inTask.stack;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002244 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002245
2246 // Check whether we should actually launch the new activity in to the task,
2247 // or just reuse the current activity on top.
2248 ActivityRecord top = inTask.getTopActivity();
2249 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2250 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2251 || launchSingleTop || launchSingleTask) {
2252 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2253 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2254 // We don't need to start a new activity, and
2255 // the client said not to do anything if that
2256 // is the case, so this is it!
2257 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2258 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002259 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002260 return ActivityManager.START_DELIVERED_TO_TOP;
2261 }
2262 }
2263
Dianne Hackborn962d5352014-08-29 16:27:40 -07002264 if (!addingToTask) {
2265 // We don't actually want to have this activity added to the task, so just
2266 // stop here but still tell the caller that we consumed the intent.
2267 ActivityOptions.abort(options);
2268 return ActivityManager.START_TASK_TO_FRONT;
2269 }
2270
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002271 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002272 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002273 + " in explicit task " + r.task);
2274
Craig Mautner8849a5e2013-04-02 16:41:03 -07002275 } else {
2276 // This not being started from an existing activity, and not part
2277 // of a new task... just put it in the top task, though these days
2278 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002279 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002280 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002281 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002282 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002283 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002284 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002285 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002286 + " in new guessed " + r.task);
2287 }
2288
2289 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002290 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002291
Craig Mautner76e2a762014-06-24 09:05:43 -07002292 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2293 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2294 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002295 if (newTask) {
2296 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2297 }
2298 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002299 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002300 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002301 if (!launchTaskBehind) {
2302 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002303 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002304 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002305 return ActivityManager.START_SUCCESS;
2306 }
2307
Craig Mautneree36c772014-07-16 14:56:05 -07002308 final void doPendingActivityLaunchesLocked(boolean doResume) {
2309 while (!mPendingActivityLaunches.isEmpty()) {
2310 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002311 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002312 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002313 }
2314 }
2315
Craig Mautner7f13ed32014-07-28 14:00:35 -07002316 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002317 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2318 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002319 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002320 mPendingActivityLaunches.remove(palNdx);
2321 }
2322 }
2323 }
2324
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002325 void setLaunchSource(int uid) {
2326 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2327 }
2328
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002329 void acquireLaunchWakelock() {
2330 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2331 throw new IllegalStateException("Calling must be system uid");
2332 }
2333 mLaunchingActivity.acquire();
2334 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2335 // To be safe, don't allow the wake lock to be held for too long.
2336 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2337 }
2338 }
2339
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002340 /**
2341 * Called when the frontmost task is idle.
2342 * @return the state of mService.mBooting before this was called.
2343 */
2344 private boolean checkFinishBootingLocked() {
2345 final boolean booting = mService.mBooting;
2346 boolean enableScreen = false;
2347 mService.mBooting = false;
2348 if (!mService.mBooted) {
2349 mService.mBooted = true;
2350 enableScreen = true;
2351 }
2352 if (booting || enableScreen) {
2353 mService.postFinishBooting(booting, enableScreen);
2354 }
2355 return booting;
2356 }
2357
Craig Mautnerf3333272013-04-22 10:55:53 -07002358 // Checked.
2359 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2360 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002361 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002362
Craig Mautnerf3333272013-04-22 10:55:53 -07002363 ArrayList<ActivityRecord> stops = null;
2364 ArrayList<ActivityRecord> finishes = null;
2365 ArrayList<UserStartedState> startingUsers = null;
2366 int NS = 0;
2367 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002368 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002369 boolean activityRemoved = false;
2370
Wale Ogunwale7d701172015-03-11 15:36:30 -07002371 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002372 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002373 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2374 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002375 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2376 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002377 if (fromTimeout) {
2378 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002379 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002380
2381 // This is a hack to semi-deal with a race condition
2382 // in the client where it can be constructed with a
2383 // newer configuration from when we asked it to launch.
2384 // We'll update with whatever configuration it now says
2385 // it used to launch.
2386 if (config != null) {
2387 r.configuration = config;
2388 }
2389
2390 // We are now idle. If someone is waiting for a thumbnail from
2391 // us, we can now deliver.
2392 r.idle = true;
2393
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002394 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002395 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002396 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002397 }
2398 }
2399
2400 if (allResumedActivitiesIdle()) {
2401 if (r != null) {
2402 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002403 }
2404
2405 if (mLaunchingActivity.isHeld()) {
2406 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2407 if (VALIDATE_WAKE_LOCK_CALLER &&
2408 Binder.getCallingUid() != Process.myUid()) {
2409 throw new IllegalStateException("Calling must be system uid");
2410 }
2411 mLaunchingActivity.release();
2412 }
2413 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002414 }
2415
2416 // Atomically retrieve all of the other things to do.
2417 stops = processStoppingActivitiesLocked(true);
2418 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002419 if ((NF = mFinishingActivities.size()) > 0) {
2420 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002421 mFinishingActivities.clear();
2422 }
2423
Craig Mautnerf3333272013-04-22 10:55:53 -07002424 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002425 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002426 mStartingUsers.clear();
2427 }
2428
Craig Mautnerf3333272013-04-22 10:55:53 -07002429 // Stop any activities that are scheduled to do so but have been
2430 // waiting for the next one to start.
2431 for (int i = 0; i < NS; i++) {
2432 r = stops.get(i);
2433 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002434 if (stack != null) {
2435 if (r.finishing) {
2436 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2437 } else {
2438 stack.stopActivityLocked(r);
2439 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002440 }
2441 }
2442
2443 // Finish any activities that are scheduled to do so but have been
2444 // waiting for the next one to start.
2445 for (int i = 0; i < NF; i++) {
2446 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002447 final ActivityStack stack = r.task.stack;
2448 if (stack != null) {
2449 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2450 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002451 }
2452
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002453 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002454 // Complete user switch
2455 if (startingUsers != null) {
2456 for (int i = 0; i < startingUsers.size(); i++) {
2457 mService.finishUserSwitch(startingUsers.get(i));
2458 }
2459 }
2460 // Complete starting up of background users
2461 if (mStartingBackgroundUsers.size() > 0) {
2462 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2463 mStartingBackgroundUsers.clear();
2464 for (int i = 0; i < startingUsers.size(); i++) {
2465 mService.finishUserBoot(startingUsers.get(i));
2466 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002467 }
2468 }
2469
2470 mService.trimApplications();
2471 //dump();
2472 //mWindowManager.dump();
2473
Craig Mautnerf3333272013-04-22 10:55:53 -07002474 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002475 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002476 }
2477
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002478 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002479 }
2480
Craig Mautner8e569572013-10-11 17:36:59 -07002481 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002482 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002483 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2484 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002485 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2486 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2487 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002488 }
Craig Mautner19091252013-10-05 00:03:53 -07002489 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002490 }
2491
2492 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002493 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2494 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002495 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2496 stacks.get(stackNdx).closeSystemDialogsLocked();
2497 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002498 }
2499 }
2500
Craig Mautner93529a42013-10-04 15:03:13 -07002501 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002502 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002503 }
2504
Craig Mautner8d341ef2013-03-26 09:03:27 -07002505 /**
2506 * @return true if some activity was finished (or would have finished if doit were true).
2507 */
2508 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2509 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002510 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2511 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002512 final int numStacks = stacks.size();
2513 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2514 final ActivityStack stack = stacks.get(stackNdx);
2515 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2516 didSomething = true;
2517 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002518 }
2519 }
2520 return didSomething;
2521 }
2522
Dianne Hackborna413dc02013-07-12 12:02:55 -07002523 void updatePreviousProcessLocked(ActivityRecord r) {
2524 // Now that this process has stopped, we may want to consider
2525 // it to be the previous app to try to keep around in case
2526 // the user wants to return to it.
2527
2528 // First, found out what is currently the foreground app, so that
2529 // we don't blow away the previous app if this activity is being
2530 // hosted by the process that is actually still the foreground.
2531 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002532 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2533 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002534 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2535 final ActivityStack stack = stacks.get(stackNdx);
2536 if (isFrontStack(stack)) {
2537 if (stack.mResumedActivity != null) {
2538 fgApp = stack.mResumedActivity.app;
2539 } else if (stack.mPausingActivity != null) {
2540 fgApp = stack.mPausingActivity.app;
2541 }
2542 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002543 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002544 }
2545 }
2546
2547 // Now set this one as the previous process, only if that really
2548 // makes sense to.
2549 if (r.app != null && fgApp != null && r.app != fgApp
2550 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002551 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002552 mService.mPreviousProcess = r.app;
2553 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2554 }
2555 }
2556
Craig Mautner05d29032013-05-03 13:40:13 -07002557 boolean resumeTopActivitiesLocked() {
2558 return resumeTopActivitiesLocked(null, null, null);
2559 }
2560
2561 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2562 Bundle targetOptions) {
2563 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002564 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002565 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002566 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002567 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002568 if (isFrontStack(targetStack)) {
2569 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2570 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002571
Craig Mautnere0a38842013-12-16 16:14:02 -08002572 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2573 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002574 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2575 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002576 if (stack == targetStack) {
2577 // Already started above.
2578 continue;
2579 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002580 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002581 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002582 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002583 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002584 }
Craig Mautner05d29032013-05-03 13:40:13 -07002585 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002586 }
2587
Todd Kennedy539db512014-12-15 09:57:55 -08002588 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002589 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2590 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002591 final int numStacks = stacks.size();
2592 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2593 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002594 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002595 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002596 }
2597 }
2598
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002599 void finishVoiceTask(IVoiceInteractionSession session) {
2600 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2601 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2602 final int numStacks = stacks.size();
2603 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2604 final ActivityStack stack = stacks.get(stackNdx);
2605 stack.finishVoiceTask(session);
2606 }
2607 }
2608 }
2609
Craig Mautner299f9602015-01-26 09:47:33 -08002610 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002611 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2612 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002613 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002614 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2615 // Caller wants the home activity moved with it. To accomplish this,
2616 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002617 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002618 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002619 if (task.stack == null) {
2620 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2621 + task + " to front. Stack is null");
2622 return;
2623 }
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002624 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options, reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002625 if (DEBUG_STACK) Slog.d(TAG_STACK,
2626 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002627 }
2628
Craig Mautner967212c2013-04-13 21:10:58 -07002629 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002630 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2631 if (activityContainer != null) {
2632 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002633 }
2634 return null;
2635 }
2636
Craig Mautner967212c2013-04-13 21:10:58 -07002637 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002638 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2640 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002641 }
2642 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002643 }
2644
Craig Mautner4a1cb222013-12-04 16:14:06 -08002645 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002646 ActivityRecord homeActivity = getHomeActivity();
2647 if (homeActivity != null) {
2648 return homeActivity.appToken;
2649 }
2650 return null;
2651 }
2652
2653 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002654 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2655 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2656 final TaskRecord task = tasks.get(taskNdx);
2657 if (task.isHomeTask()) {
2658 final ArrayList<ActivityRecord> activities = task.mActivities;
2659 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2660 final ActivityRecord r = activities.get(activityNdx);
2661 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002662 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002663 }
2664 }
2665 }
2666 }
2667 return null;
2668 }
2669
Todd Kennedyca4d8422015-01-15 15:19:22 -08002670 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002671 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002672 ActivityContainer activityContainer =
2673 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002674 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002675 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002676 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002677 return activityContainer;
2678 }
2679
Craig Mautner34b73df2014-01-12 21:11:08 -08002680 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002681 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2682 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2683 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002684 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2685 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002686 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002687 }
2688 }
2689
Craig Mautner95da1082014-02-24 17:54:35 -08002690 void deleteActivityContainer(IActivityContainer container) {
2691 ActivityContainer activityContainer = (ActivityContainer)container;
2692 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002693 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2694 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002695 final int stackId = activityContainer.mStackId;
2696 mActivityContainers.remove(stackId);
2697 mWindowManager.removeStack(stackId);
2698 }
2699 }
2700
Wale Ogunwale60454db2015-01-23 16:05:07 -08002701 void resizeStackLocked(int stackId, Rect bounds) {
2702 final ActivityStack stack = getStack(stackId);
2703 if (stack == null) {
2704 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2705 return;
2706 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002707
2708 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002709 if (r != null && !r.task.mResizeable) {
2710 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002711 return;
2712 }
2713
Wale Ogunwale60454db2015-01-23 16:05:07 -08002714 final Configuration overrideConfig = mWindowManager.resizeStack(stackId, bounds);
2715 if (stack.updateOverrideConfiguration(overrideConfig)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002716 if (r != null) {
2717 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2718 // And we need to make sure at this point that all other activities
2719 // are made visible with the correct configuration.
2720 ensureActivitiesVisibleLocked(r, 0);
2721 if (!updated) {
2722 resumeTopActivitiesLocked(stack, null, null);
2723 }
2724 }
2725 }
2726 }
2727
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002728 /** Makes sure the input task is in a stack with the specified bounds by either resizing the
2729 * current task stack if it only has one entry, moving the task to a stack that matches the
2730 * bounds, or creating a new stack with the required bounds. Also, makes the task resizeable.*/
2731 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2732 task.mResizeable = true;
2733 final ActivityStack currentStack = task.stack;
2734 if (currentStack.isHomeStack()) {
2735 // Can't move task off the home stack. Sorry!
2736 return;
2737 }
2738
2739 final int matchingStackId = mWindowManager.getStackIdWithBounds(bounds);
2740 if (matchingStackId != -1) {
2741 // There is already a stack with the right bounds!
2742 if (currentStack != null && currentStack.mStackId == matchingStackId) {
2743 // Nothing to do here. Already in the right stack...
2744 return;
2745 }
2746 // Move task to stack with matching bounds.
2747 moveTaskToStackLocked(task.taskId, matchingStackId, true);
2748 return;
2749 }
2750
2751 if (currentStack != null && currentStack.numTasks() == 1) {
2752 // Just resize the current stack since this is the task in it.
2753 resizeStackLocked(currentStack.mStackId, bounds);
2754 return;
2755 }
2756
2757 // Create new stack and move the task to it.
2758 final int displayId = (currentStack != null && currentStack.mDisplayId != -1)
2759 ? currentStack.mDisplayId : Display.DEFAULT_DISPLAY;
2760 ActivityStack newStack = createStackOnDisplay(getNextStackId(), displayId);
2761
2762 if (newStack == null) {
2763 Slog.e(TAG, "resizeTaskLocked: Can't create stack for task=" + task);
2764 return;
2765 }
2766 moveTaskToStackLocked(task.taskId, newStack.mStackId, true);
2767 resizeStackLocked(newStack.mStackId, bounds);
2768 }
2769
Todd Kennedy4900bf92015-01-16 16:05:14 -08002770 ActivityStack createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002771 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2772 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002773 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002774 }
2775
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002776 ActivityContainer activityContainer = new ActivityContainer(stackId);
2777 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002778 activityContainer.attachToDisplayLocked(activityDisplay);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002779 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002780 }
2781
2782 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002783 while (true) {
2784 if (++mLastStackId <= HOME_STACK_ID) {
2785 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002786 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002787 if (getStack(mLastStackId) == null) {
2788 break;
2789 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002790 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002791 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002792 }
2793
Wale Ogunwale7de05352014-12-12 15:21:33 -08002794 private boolean restoreRecentTaskLocked(TaskRecord task) {
2795 ActivityStack stack = null;
2796 // Determine stack to restore task to.
2797 if (mLeanbackOnlyDevice) {
2798 // There is only one stack for lean back devices.
2799 stack = mHomeStack;
2800 } else {
2801 // Look for the top stack on the home display that isn't the home stack.
2802 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2803 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2804 final ActivityStack tmpStack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07002805 if (!tmpStack.isHomeStack() && tmpStack.mFullscreen) {
Wale Ogunwale7de05352014-12-12 15:21:33 -08002806 stack = tmpStack;
2807 break;
2808 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002809 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002810 }
Wale Ogunwale7de05352014-12-12 15:21:33 -08002811
2812 if (stack == null) {
2813 // We couldn't find a stack to restore the task to. Possible if are restoring recents
2814 // before an application stack is created...Go ahead and create one on the default
2815 // display.
Todd Kennedy4900bf92015-01-16 16:05:14 -08002816 stack = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002817 // Restore home stack to top.
Craig Mautner299f9602015-01-26 09:47:33 -08002818 moveHomeStack(true, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002819 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2820 "Created stack=" + stack + " for recents restoration.");
Wale Ogunwale7de05352014-12-12 15:21:33 -08002821 }
2822
2823 if (stack == null) {
2824 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002825 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2826 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002827 return false;
2828 }
2829
2830 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002831 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2832 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002833 final ArrayList<ActivityRecord> activities = task.mActivities;
2834 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2835 final ActivityRecord r = activities.get(activityNdx);
2836 mWindowManager.addAppToken(0, r.appToken, task.taskId, stack.mStackId,
2837 r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002838 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0,
Wale Ogunwale7de05352014-12-12 15:21:33 -08002839 r.userId, r.info.configChanges, task.voiceSession != null,
2840 r.mLaunchTaskBehind);
2841 }
2842 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002843 }
2844
Craig Mautner299f9602015-01-26 09:47:33 -08002845 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002846 final TaskRecord task = anyTaskForIdLocked(taskId);
2847 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002848 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002849 return;
2850 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002851 final ActivityStack stack = getStack(stackId);
2852 if (stack == null) {
2853 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2854 return;
2855 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002856 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
2857 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07002858 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002859 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002860 stack.addTask(task, toTop, true);
Craig Mautner05d29032013-05-03 13:40:13 -07002861 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002862 }
2863
Craig Mautnerac6f8432013-07-17 13:24:59 -07002864 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002865 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002866 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2867 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002868 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2869 final ActivityStack stack = stacks.get(stackNdx);
2870 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002871 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002872 continue;
2873 }
2874 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002875 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2876 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002877 continue;
2878 }
2879 final ActivityRecord ar = stack.findTaskLocked(r);
2880 if (ar != null) {
2881 return ar;
2882 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002883 }
2884 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002885 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002886 return null;
2887 }
2888
2889 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002890 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2891 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002892 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2893 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2894 if (ar != null) {
2895 return ar;
2896 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002897 }
2898 }
2899 return null;
2900 }
2901
Craig Mautner8d341ef2013-03-26 09:03:27 -07002902 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002903 scheduleSleepTimeout();
2904 if (!mGoingToSleep.isHeld()) {
2905 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002906 if (mLaunchingActivity.isHeld()) {
2907 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2908 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002909 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002910 mLaunchingActivity.release();
2911 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002912 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002913 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002914 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002915 }
2916
2917 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002918 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002919
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002920 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002921 final long endTime = System.currentTimeMillis() + timeout;
2922 while (true) {
2923 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2925 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2927 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2928 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002929 }
2930 if (cantShutdown) {
2931 long timeRemaining = endTime - System.currentTimeMillis();
2932 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002933 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002934 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002935 } catch (InterruptedException e) {
2936 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002937 } else {
2938 Slog.w(TAG, "Activity manager shutdown timed out");
2939 timedout = true;
2940 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002941 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002942 } else {
2943 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002944 }
2945 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002946
2947 // Force checkReadyForSleep to complete.
2948 mSleepTimeout = true;
2949 checkReadyForSleepLocked();
2950
Craig Mautner8d341ef2013-03-26 09:03:27 -07002951 return timedout;
2952 }
2953
2954 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002955 removeSleepTimeouts();
2956 if (mGoingToSleep.isHeld()) {
2957 mGoingToSleep.release();
2958 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002959 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2960 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002961 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2962 final ActivityStack stack = stacks.get(stackNdx);
2963 stack.awakeFromSleepingLocked();
2964 if (isFrontStack(stack)) {
2965 resumeTopActivitiesLocked();
2966 }
Craig Mautner5314a402013-09-26 12:40:16 -07002967 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002968 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002969 mGoingToSleepActivities.clear();
2970 }
2971
2972 void activitySleptLocked(ActivityRecord r) {
2973 mGoingToSleepActivities.remove(r);
2974 checkReadyForSleepLocked();
2975 }
2976
2977 void checkReadyForSleepLocked() {
2978 if (!mService.isSleepingOrShuttingDown()) {
2979 // Do not care.
2980 return;
2981 }
2982
2983 if (!mSleepTimeout) {
2984 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002985 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2986 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002987 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2988 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2989 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002990 }
2991
2992 if (mStoppingActivities.size() > 0) {
2993 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002994 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002995 + mStoppingActivities.size() + " activities");
2996 scheduleIdleLocked();
2997 dontSleep = true;
2998 }
2999
3000 if (mGoingToSleepActivities.size() > 0) {
3001 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003002 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003003 + mGoingToSleepActivities.size() + " activities");
3004 dontSleep = true;
3005 }
3006
3007 if (dontSleep) {
3008 return;
3009 }
3010 }
3011
Craig Mautnere0a38842013-12-16 16:14:02 -08003012 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3013 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003014 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3015 stacks.get(stackNdx).goToSleep();
3016 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003017 }
3018
3019 removeSleepTimeouts();
3020
3021 if (mGoingToSleep.isHeld()) {
3022 mGoingToSleep.release();
3023 }
3024 if (mService.mShuttingDown) {
3025 mService.notifyAll();
3026 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003027 }
3028
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003029 boolean reportResumedActivityLocked(ActivityRecord r) {
3030 final ActivityStack stack = r.task.stack;
3031 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003032 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003033 }
3034 if (allResumedActivitiesComplete()) {
3035 ensureActivitiesVisibleLocked(null, 0);
3036 mWindowManager.executeAppTransition();
3037 return true;
3038 }
3039 return false;
3040 }
3041
Craig Mautner8d341ef2013-03-26 09:03:27 -07003042 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003043 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3044 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003045 final int numStacks = stacks.size();
3046 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3047 final ActivityStack stack = stacks.get(stackNdx);
3048 stack.handleAppCrashLocked(app);
3049 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003050 }
3051 }
3052
Jose Lima4b6c6692014-08-12 17:41:12 -07003053 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003054 final ActivityStack stack = r.task.stack;
3055 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003056 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
3057 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003058 return false;
3059 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003060 final boolean isVisible = stack.hasVisibleBehindActivity();
3061 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
3062 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003063
3064 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003065 if (top == null || top == r || (visible == isVisible)) {
3066 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
3067 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003068 return true;
3069 }
3070
3071 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003072 if (visible && top.fullscreen) {
3073 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07003074 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07003075 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003076 " top.app.thread=" + top.app.thread);
3077 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003078 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3079 // Only the activity set as currently visible behind should actively reset its
3080 // visible behind state.
3081 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
3082 + visible + " stack.getVisibleBehindActivity()=" +
3083 stack.getVisibleBehindActivity() + " r=" + r);
3084 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003085 }
3086
Jose Lima4b6c6692014-08-12 17:41:12 -07003087 stack.setVisibleBehindActivity(visible ? r : null);
3088 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003089 // Make the activity immediately above r opaque.
3090 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3091 if (next != null) {
3092 mService.convertFromTranslucent(next.appToken);
3093 }
3094 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003095 if (top.app != null && top.app.thread != null) {
3096 // Notify the top app of the change.
3097 try {
3098 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3099 } catch (RemoteException e) {
3100 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003101 }
3102 return true;
3103 }
3104
Craig Mautnerbb742462014-07-07 15:28:55 -07003105 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3106 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3107 r.mLaunchTaskBehind = false;
3108 final TaskRecord task = r.task;
3109 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003110 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003111 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003112 mWindowManager.setAppVisibility(r.appToken, false);
3113 }
3114
3115 void scheduleLaunchTaskBehindComplete(IBinder token) {
3116 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3117 }
3118
Craig Mautnerde4ef022013-04-07 19:01:33 -07003119 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003120 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003121 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3122 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003123 final int topStackNdx = stacks.size() - 1;
3124 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3125 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003126 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003127 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003128 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003129 }
3130
3131 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003132 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3133 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003134 final int numStacks = stacks.size();
3135 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3136 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003137 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003138 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003139 }
3140 }
3141
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003142 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3143 // Examine all activities currently running in the process.
3144 TaskRecord firstTask = null;
3145 // Tasks is non-null only if two or more tasks are found.
3146 ArraySet<TaskRecord> tasks = null;
3147 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3148 for (int i=0; i<app.activities.size(); i++) {
3149 ActivityRecord r = app.activities.get(i);
3150 // First, if we find an activity that is in the process of being destroyed,
3151 // then we just aren't going to do anything for now; we want things to settle
3152 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003153 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003154 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
3155 return;
3156 }
3157 // Don't consider any activies that are currently not in a state where they
3158 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003159 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3160 || r.state == PAUSED || r.state == STOPPING) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003161 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
3162 continue;
3163 }
3164 if (r.task != null) {
3165 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
3166 + " from " + r);
3167 if (firstTask == null) {
3168 firstTask = r.task;
3169 } else if (firstTask != r.task) {
3170 if (tasks == null) {
3171 tasks = new ArraySet<>();
3172 tasks.add(firstTask);
3173 }
3174 tasks.add(r.task);
3175 }
3176 }
3177 }
3178 if (tasks == null) {
3179 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
3180 return;
3181 }
3182 // If we have activities in multiple tasks that are in a position to be destroyed,
3183 // let's iterate through the tasks and release the oldest one.
3184 final int numDisplays = mActivityDisplays.size();
3185 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3186 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3187 // Step through all stacks starting from behind, to hit the oldest things first.
3188 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3189 final ActivityStack stack = stacks.get(stackNdx);
3190 // Try to release activities in this stack; if we manage to, we are done.
3191 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3192 return;
3193 }
3194 }
3195 }
3196 }
3197
Craig Mautner8d341ef2013-03-26 09:03:27 -07003198 boolean switchUserLocked(int userId, UserStartedState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003199 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003200 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003201 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003202
Craig Mautner858d8a62013-04-23 17:08:34 -07003203 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003204 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3205 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003206 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003207 final ActivityStack stack = stacks.get(stackNdx);
3208 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003209 TaskRecord task = stack.topTask();
3210 if (task != null) {
3211 mWindowManager.moveTaskToTop(task.taskId);
3212 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003213 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003214 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003215
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003216 ActivityStack stack = getStack(restoreStackId);
3217 if (stack == null) {
3218 stack = mHomeStack;
3219 }
3220 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003221 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003222 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003223 TaskRecord task = stack.topTask();
3224 if (task != null) {
3225 mWindowManager.moveTaskToTop(task.taskId);
3226 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003227 } else {
3228 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003229 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003230 }
Craig Mautner93529a42013-10-04 15:03:13 -07003231 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003232 }
3233
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003234 /**
3235 * Add background users to send boot completed events to.
3236 * @param userId The user being started in the background
3237 * @param uss The state object for the user.
3238 */
3239 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
3240 mStartingBackgroundUsers.add(uss);
3241 }
3242
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003243 /** Checks whether the userid is a profile of the current user. */
3244 boolean isCurrentProfileLocked(int userId) {
3245 if (userId == mCurrentUser) return true;
3246 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3247 if (mService.mCurrentProfileIds[i] == userId) return true;
3248 }
3249 return false;
3250 }
3251
Craig Mautnerde4ef022013-04-07 19:01:33 -07003252 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003253 ArrayList<ActivityRecord> stops = null;
3254
3255 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003256 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3257 ActivityRecord s = mStoppingActivities.get(activityNdx);
3258 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003259 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003260 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3261 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003262 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003263 if (s.finishing) {
3264 // If this activity is finishing, it is sitting on top of
3265 // everyone else but we now know it is no longer needed...
3266 // so get rid of it. Otherwise, we need to go through the
3267 // normal flow and hide it once we determine that it is
3268 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003269 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003270 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003271 }
3272 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003273 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003274 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003275 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003276 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003277 }
3278 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003279 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003280 }
3281 }
3282
3283 return stops;
3284 }
3285
Craig Mautnercf910b02013-04-23 11:23:27 -07003286 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003287 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3288 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3289 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3290 final ActivityStack stack = stacks.get(stackNdx);
3291 final ActivityRecord r = stack.topRunningActivityLocked(null);
3292 final ActivityState state = r == null ? DESTROYED : r.state;
3293 if (isFrontStack(stack)) {
3294 if (r == null) Slog.e(TAG,
3295 "validateTop...: null top activity, stack=" + stack);
3296 else {
3297 final ActivityRecord pausing = stack.mPausingActivity;
3298 if (pausing != null && pausing == r) Slog.e(TAG,
3299 "validateTop...: top stack has pausing activity r=" + r
3300 + " state=" + state);
3301 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3302 "validateTop...: activity in front not resumed r=" + r
3303 + " state=" + state);
3304 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003305 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003306 final ActivityRecord resumed = stack.mResumedActivity;
3307 if (resumed != null && resumed == r) Slog.e(TAG,
3308 "validateTop...: back stack has resumed activity r=" + r
3309 + " state=" + state);
3310 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3311 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003312 }
3313 }
3314 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003315 }
3316
Craig Mautner27084302013-03-25 08:05:25 -07003317 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003318 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003319 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003320 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3321 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3322 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003323 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner15df08a2015-04-01 12:17:18 -07003324 pw.print(prefix); pw.println("mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003325 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003326
Craig Mautner20e72272013-04-01 13:45:53 -07003327 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003328 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003329 }
3330
Dianne Hackborn390517b2013-05-30 15:03:32 -07003331 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3332 boolean needSep, String prefix) {
3333 if (activity != null) {
3334 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3335 if (needSep) {
3336 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003337 }
3338 pw.print(prefix);
3339 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003340 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003341 }
3342 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003343 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003344 }
3345
Craig Mautner8d341ef2013-03-26 09:03:27 -07003346 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3347 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003348 boolean printed = false;
3349 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003350 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3351 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003352 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003353 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003354 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003355 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003356 final ActivityStack stack = stacks.get(stackNdx);
3357 StringBuilder stackHeader = new StringBuilder(128);
3358 stackHeader.append(" Stack #");
3359 stackHeader.append(stack.mStackId);
3360 stackHeader.append(":");
3361 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3362 needSep, stackHeader.toString());
3363 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3364 !dumpAll, false, dumpPackage, true,
3365 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003366
Craig Mautner4a1cb222013-12-04 16:14:06 -08003367 needSep = printed;
3368 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3369 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003370 if (pr) {
3371 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003372 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003373 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003374 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3375 " mResumedActivity: ");
3376 if (pr) {
3377 printed = true;
3378 needSep = false;
3379 }
3380 if (dumpAll) {
3381 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3382 " mLastPausedActivity: ");
3383 if (pr) {
3384 printed = true;
3385 needSep = true;
3386 }
3387 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3388 needSep, " mLastNoHistoryActivity: ");
3389 }
3390 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003391 }
3392 }
3393
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003394 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3395 false, dumpPackage, true, " Activities waiting to finish:", null);
3396 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3397 false, dumpPackage, true, " Activities waiting to stop:", null);
3398 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3399 false, dumpPackage, true, " Activities waiting for another to become visible:",
3400 null);
3401 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3402 false, dumpPackage, true, " Activities waiting to sleep:", null);
3403 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3404 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003405
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003406 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003407 }
3408
Dianne Hackborn390517b2013-05-30 15:03:32 -07003409 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003410 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003411 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003412 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003413 String innerPrefix = null;
3414 String[] args = null;
3415 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003416 for (int i=list.size()-1; i>=0; i--) {
3417 final ActivityRecord r = list.get(i);
3418 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3419 continue;
3420 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003421 if (innerPrefix == null) {
3422 innerPrefix = prefix + " ";
3423 args = new String[0];
3424 }
3425 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003426 final boolean full = !brief && (complete || !r.isInHistory());
3427 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003428 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003429 needNL = false;
3430 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003431 if (header1 != null) {
3432 pw.println(header1);
3433 header1 = null;
3434 }
3435 if (header2 != null) {
3436 pw.println(header2);
3437 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003438 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003439 if (lastTask != r.task) {
3440 lastTask = r.task;
3441 pw.print(prefix);
3442 pw.print(full ? "* " : " ");
3443 pw.println(lastTask);
3444 if (full) {
3445 lastTask.dump(pw, prefix + " ");
3446 } else if (complete) {
3447 // Complete + brief == give a summary. Isn't that obvious?!?
3448 if (lastTask.intent != null) {
3449 pw.print(prefix); pw.print(" ");
3450 pw.println(lastTask.intent.toInsecureStringWithClip());
3451 }
3452 }
3453 }
3454 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3455 pw.print(" #"); pw.print(i); pw.print(": ");
3456 pw.println(r);
3457 if (full) {
3458 r.dump(pw, innerPrefix);
3459 } else if (complete) {
3460 // Complete + brief == give a summary. Isn't that obvious?!?
3461 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3462 if (r.app != null) {
3463 pw.print(innerPrefix); pw.println(r.app);
3464 }
3465 }
3466 if (client && r.app != null && r.app.thread != null) {
3467 // flush anything that is already in the PrintWriter since the thread is going
3468 // to write to the file descriptor directly
3469 pw.flush();
3470 try {
3471 TransferPipe tp = new TransferPipe();
3472 try {
3473 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3474 r.appToken, innerPrefix, args);
3475 // Short timeout, since blocking here can
3476 // deadlock with the application.
3477 tp.go(fd, 2000);
3478 } finally {
3479 tp.kill();
3480 }
3481 } catch (IOException e) {
3482 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3483 } catch (RemoteException e) {
3484 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3485 }
3486 needNL = true;
3487 }
3488 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003489 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003490 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003491
Craig Mautnerf3333272013-04-22 10:55:53 -07003492 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003493 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003494 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3495 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003496 }
3497
3498 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003499 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003500 }
3501
3502 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003503 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003504 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3505 }
3506
Craig Mautner05d29032013-05-03 13:40:13 -07003507 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003508 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3509 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3510 }
Craig Mautner05d29032013-05-03 13:40:13 -07003511 }
3512
Craig Mautner0eea92c2013-05-16 13:35:39 -07003513 void removeSleepTimeouts() {
3514 mSleepTimeout = false;
3515 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3516 }
3517
3518 final void scheduleSleepTimeout() {
3519 removeSleepTimeouts();
3520 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3521 }
3522
Craig Mautner4a1cb222013-12-04 16:14:06 -08003523 @Override
3524 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003525 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003526 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3527 }
3528
3529 @Override
3530 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003531 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003532 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3533 }
3534
3535 @Override
3536 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003537 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003538 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3539 }
3540
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003541 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003542 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003543 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003544 newDisplay = mActivityDisplays.get(displayId) == null;
3545 if (newDisplay) {
3546 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003547 if (activityDisplay.mDisplay == null) {
3548 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3549 return;
3550 }
Craig Mautner4504de52013-12-20 09:06:56 -08003551 mActivityDisplays.put(displayId, activityDisplay);
3552 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003553 }
Craig Mautner4504de52013-12-20 09:06:56 -08003554 if (newDisplay) {
3555 mWindowManager.onDisplayAdded(displayId);
3556 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003557 }
3558
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003559 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003560 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003561 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3562 if (activityDisplay != null) {
3563 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003564 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003565 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003566 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003567 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003568 }
3569 }
3570 mWindowManager.onDisplayRemoved(displayId);
3571 }
3572
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003573 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003574 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003575 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3576 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003577 // TODO: Update the bounds.
3578 }
3579 }
3580 mWindowManager.onDisplayChanged(displayId);
3581 }
3582
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003583 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003584 StackInfo info = new StackInfo();
3585 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3586 info.displayId = Display.DEFAULT_DISPLAY;
3587 info.stackId = stack.mStackId;
3588
3589 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3590 final int numTasks = tasks.size();
3591 int[] taskIds = new int[numTasks];
3592 String[] taskNames = new String[numTasks];
3593 for (int i = 0; i < numTasks; ++i) {
3594 final TaskRecord task = tasks.get(i);
3595 taskIds[i] = task.taskId;
3596 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3597 : task.realActivity != null ? task.realActivity.flattenToString()
3598 : task.getTopActivity() != null ? task.getTopActivity().packageName
3599 : "unknown";
3600 }
3601 info.taskIds = taskIds;
3602 info.taskNames = taskNames;
3603 return info;
3604 }
3605
3606 StackInfo getStackInfoLocked(int stackId) {
3607 ActivityStack stack = getStack(stackId);
3608 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003609 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003610 }
3611 return null;
3612 }
3613
3614 ArrayList<StackInfo> getAllStackInfosLocked() {
3615 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003616 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3617 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003618 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003619 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 }
3621 }
3622 return list;
3623 }
3624
Craig Mautner15df08a2015-04-01 12:17:18 -07003625 TaskRecord getLockedTaskLocked() {
3626 final int top = mLockTaskModeTasks.size() - 1;
3627 if (top >= 0) {
3628 return mLockTaskModeTasks.get(top);
3629 }
3630 return null;
3631 }
3632
3633 boolean isLockedTask(TaskRecord task) {
3634 return mLockTaskModeTasks.contains(task);
3635 }
3636
3637 boolean isLastLockedTask(TaskRecord task) {
3638 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3639 }
3640
3641 void removeLockedTaskLocked(final TaskRecord task) {
3642 if (mLockTaskModeTasks.remove(task) && mLockTaskModeTasks.isEmpty()) {
3643 // Last one.
3644 final Message lockTaskMsg = Message.obtain();
3645 lockTaskMsg.arg1 = task.userId;
3646 lockTaskMsg.what = LOCK_TASK_END_MSG;
3647 mHandler.sendMessage(lockTaskMsg);
3648 }
3649 }
3650
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003651 void showLockTaskToast() {
3652 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003653 }
3654
Benjamin Franz43261142015-02-11 15:59:44 +00003655 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003656 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003657 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003658 final TaskRecord lockedTask = getLockedTaskLocked();
3659 if (lockedTask != null) {
3660 removeLockedTaskLocked(lockedTask);
3661 if (!mLockTaskModeTasks.isEmpty()) {
3662 // There are locked tasks remaining, can only finish this task, not unlock it.
3663 lockedTask.performClearTaskLocked();
3664 resumeTopActivitiesLocked();
3665 return;
3666 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003667 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003668 return;
3669 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003670
3671 // Should have already been checked, but do it again.
3672 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003673 return;
3674 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003675 if (isLockTaskModeViolation(task)) {
3676 Slog.e(TAG, "setLockTaskMode: Attempt to start an unauthorized lock task.");
3677 return;
3678 }
3679
3680 if (mLockTaskModeTasks.isEmpty()) {
3681 // First locktask.
3682 final Message lockTaskMsg = Message.obtain();
3683 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3684 lockTaskMsg.arg1 = task.userId;
3685 lockTaskMsg.what = LOCK_TASK_START_MSG;
3686 lockTaskMsg.arg2 = lockTaskModeState;
3687 mHandler.sendMessage(lockTaskMsg);
3688 }
3689 // Add it or move it to the top.
3690 mLockTaskModeTasks.remove(task);
3691 mLockTaskModeTasks.add(task);
3692
3693 if (task.mLockTaskUid == -1) {
3694 task.mLockTaskUid = task.mCallingUid;
3695 }
Craig Mautner299f9602015-01-26 09:47:33 -08003696 findTaskToMoveToFrontLocked(task, 0, null, reason);
Craig Mautneraea74a52014-03-08 14:23:10 -08003697 resumeTopActivitiesLocked();
3698 }
3699
3700 boolean isLockTaskModeViolation(TaskRecord task) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003701 if (getLockedTaskLocked() == task) {
3702 return false;
3703 }
3704 final int lockTaskAuth = task.mLockTaskAuth;
3705 switch (lockTaskAuth) {
3706 case LOCK_TASK_AUTH_DONT_LOCK:
3707 return !mLockTaskModeTasks.isEmpty();
3708 case LOCK_TASK_AUTH_LAUNCHABLE:
3709 case LOCK_TASK_AUTH_WHITELISTED:
3710 return false;
3711 case LOCK_TASK_AUTH_PINNABLE:
3712 // Pinnable tasks can't be launched on top of locktask tasks.
3713 return !mLockTaskModeTasks.isEmpty();
3714 default:
3715 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3716 return true;
3717 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003718 }
3719
Craig Mautner15df08a2015-04-01 12:17:18 -07003720 void onLockTaskPackagesUpdatedLocked() {
3721 boolean didSomething = false;
3722 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3723 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
3724 if (lockedTask.mLockTaskMode != LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED) {
3725 continue;
3726 }
3727 final boolean wasLaunchable = lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE;
3728 lockedTask.setLockTaskAuth();
3729 if (wasLaunchable && lockedTask.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE) {
3730 // Lost whitelisting authorization. End it now.
3731 removeLockedTaskLocked(lockedTask);
3732 lockedTask.performClearTaskLocked();
3733 didSomething = true;
3734 }
3735 }
3736 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3737 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3738 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3739 final ActivityStack stack = stacks.get(stackNdx);
3740 stack.onLockTaskPackagesUpdatedLocked();
3741 }
3742 }
3743 if (didSomething) {
3744 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003745 }
3746 }
3747
Benjamin Franz43261142015-02-11 15:59:44 +00003748 int getLockTaskModeState() {
3749 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003750 }
3751
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003752 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003753
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003754 public ActivityStackSupervisorHandler(Looper looper) {
3755 super(looper);
3756 }
3757
Craig Mautnerf3333272013-04-22 10:55:53 -07003758 void activityIdleInternal(ActivityRecord r) {
3759 synchronized (mService) {
3760 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3761 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003762 }
3763
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003764 @Override
3765 public void handleMessage(Message msg) {
3766 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003767 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003768 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003769 if (mService.mDidDexOpt) {
3770 mService.mDidDexOpt = false;
3771 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3772 nmsg.obj = msg.obj;
3773 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3774 return;
3775 }
3776 // We don't at this point know if the activity is fullscreen,
3777 // so we need to be conservative and assume it isn't.
3778 activityIdleInternal((ActivityRecord)msg.obj);
3779 } break;
3780 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003781 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003782 activityIdleInternal((ActivityRecord)msg.obj);
3783 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003784 case RESUME_TOP_ACTIVITY_MSG: {
3785 synchronized (mService) {
3786 resumeTopActivitiesLocked();
3787 }
3788 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003789 case SLEEP_TIMEOUT_MSG: {
3790 synchronized (mService) {
3791 if (mService.isSleepingOrShuttingDown()) {
3792 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3793 mSleepTimeout = true;
3794 checkReadyForSleepLocked();
3795 }
3796 }
3797 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003798 case LAUNCH_TIMEOUT_MSG: {
3799 if (mService.mDidDexOpt) {
3800 mService.mDidDexOpt = false;
3801 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3802 return;
3803 }
3804 synchronized (mService) {
3805 if (mLaunchingActivity.isHeld()) {
3806 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3807 if (VALIDATE_WAKE_LOCK_CALLER
3808 && Binder.getCallingUid() != Process.myUid()) {
3809 throw new IllegalStateException("Calling must be system uid");
3810 }
3811 mLaunchingActivity.release();
3812 }
3813 }
3814 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003816 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003817 } break;
3818 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003819 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003820 } break;
3821 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003822 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003823 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003824 case CONTAINER_CALLBACK_VISIBILITY: {
3825 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003826 final IActivityContainerCallback callback = container.mCallback;
3827 if (callback != null) {
3828 try {
3829 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3830 } catch (RemoteException e) {
3831 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003832 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003833 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003834 case LOCK_TASK_START_MSG: {
3835 // When lock task starts, we disable the status bars.
3836 try {
Jason Monk62515be2014-05-21 16:06:19 -04003837 if (mLockTaskNotify == null) {
3838 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003839 }
Jason Monk62515be2014-05-21 16:06:19 -04003840 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003841 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003842 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003843 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003844 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003845 flags = StatusBarManager.DISABLE_MASK
3846 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003847 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003848 flags = StatusBarManager.DISABLE_MASK
3849 & (~StatusBarManager.DISABLE_BACK)
3850 & (~StatusBarManager.DISABLE_HOME)
3851 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003852 }
3853 getStatusBarService().disable(flags, mToken,
3854 mService.mContext.getPackageName());
3855 }
3856 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003857 if (getDevicePolicyManager() != null) {
3858 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003859 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003860 }
justinzhang5286d3f2014-05-12 17:06:01 -04003861 } catch (RemoteException ex) {
3862 throw new RuntimeException(ex);
3863 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003864 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003865 case LOCK_TASK_END_MSG: {
3866 // When lock task ends, we enable the status bars.
3867 try {
Jason Monk62515be2014-05-21 16:06:19 -04003868 if (getStatusBarService() != null) {
3869 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3870 mService.mContext.getPackageName());
3871 }
3872 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003873 if (getDevicePolicyManager() != null) {
3874 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3875 msg.arg1);
3876 }
Jason Monk62515be2014-05-21 16:06:19 -04003877 if (mLockTaskNotify == null) {
3878 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3879 }
3880 mLockTaskNotify.show(false);
3881 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003882 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003883 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003884 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003885 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003886 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003887 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003888 new LockPatternUtils(mService.mContext)
3889 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003890 }
3891 } catch (SettingNotFoundException e) {
3892 // No setting, don't lock.
3893 }
justinzhang5286d3f2014-05-12 17:06:01 -04003894 } catch (RemoteException ex) {
3895 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003896 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003897 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003898 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003899 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003900 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3901 final ActivityContainer container = (ActivityContainer) msg.obj;
3902 final IActivityContainerCallback callback = container.mCallback;
3903 if (callback != null) {
3904 try {
3905 callback.onAllActivitiesComplete(container.asBinder());
3906 } catch (RemoteException e) {
3907 }
3908 }
3909 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003910 case LAUNCH_TASK_BEHIND_COMPLETE: {
3911 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003912 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003913 if (r != null) {
3914 handleLaunchTaskBehindCompleteLocked(r);
3915 }
3916 }
3917 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003918 }
3919 }
3920 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003921
Ying Wangb081a592014-04-22 15:20:16 -07003922 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003923 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003924 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003925 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003926 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003927 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003928 ActivityRecord mParentActivity = null;
3929 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003930
Craig Mautnere3a00d72014-04-16 08:31:19 -07003931 boolean mVisible = true;
3932
Craig Mautner4a1cb222013-12-04 16:14:06 -08003933 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003934 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003935
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003936 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3937 final static int CONTAINER_STATE_NO_SURFACE = 1;
3938 final static int CONTAINER_STATE_FINISHING = 2;
3939 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3940
3941 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003942 synchronized (mService) {
3943 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003944 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003945 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003946 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003947 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003948 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003949
Craig Mautnere0a38842013-12-16 16:14:02 -08003950 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003951 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Craig Mautner34b73df2014-01-12 21:11:08 -08003952 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003953 mActivityDisplay = activityDisplay;
3954 mStack.mDisplayId = activityDisplay.mDisplayId;
3955 mStack.mStacks = activityDisplay.mStacks;
3956
3957 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003958 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003959 }
3960
3961 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003962 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003963 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003964 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3965 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003966 return;
3967 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003968 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003969 }
3970 }
3971
3972 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003973 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003974 synchronized (mService) {
3975 if (mActivityDisplay != null) {
3976 return mActivityDisplay.mDisplayId;
3977 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003978 }
3979 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003980 }
3981
Jeff Brownca9bc702014-02-11 14:32:56 -08003982 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003983 public int getStackId() {
3984 synchronized (mService) {
3985 return mStackId;
3986 }
3987 }
3988
3989 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003990 public boolean injectEvent(InputEvent event) {
3991 final long origId = Binder.clearCallingIdentity();
3992 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003993 synchronized (mService) {
3994 if (mActivityDisplay != null) {
3995 return mInputManagerInternal.injectInputEvent(event,
3996 mActivityDisplay.mDisplayId,
3997 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3998 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003999 }
4000 return false;
4001 } finally {
4002 Binder.restoreCallingIdentity(origId);
4003 }
4004 }
4005
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004006 @Override
4007 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004008 synchronized (mService) {
4009 if (mContainerState == CONTAINER_STATE_FINISHING) {
4010 return;
4011 }
4012 mContainerState = CONTAINER_STATE_FINISHING;
4013
Craig Mautnerd163e752014-06-13 17:18:47 -07004014 long origId = Binder.clearCallingIdentity();
4015 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004016 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004017 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004018 } finally {
4019 Binder.restoreCallingIdentity(origId);
4020 }
4021 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004022 }
4023
Craig Mautner60257702014-09-17 15:02:33 -07004024 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004025 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004026 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004027 if (mActivityDisplay != null) {
4028 mActivityDisplay.detachActivitiesLocked(mStack);
4029 mActivityDisplay = null;
4030 mStack.mDisplayId = -1;
4031 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004032 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004033 }
4034 }
4035
4036 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004037 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004038 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004039 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004040 Binder.getCallingUid(), mCurrentUser, false,
4041 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004042
Craig Mautnere0a38842013-12-16 16:14:02 -08004043 // TODO: Switch to user app stacks here.
4044 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004045 final Uri data = intent.getData();
4046 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4047 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004048 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004049 checkEmbeddedAllowedInner(userId, intent, mimeType);
4050
4051 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4052 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
4053 0, 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004054 }
4055
4056 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004057 public final int startActivityIntentSender(IIntentSender intentSender)
4058 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004059 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4060
4061 if (!(intentSender instanceof PendingIntentRecord)) {
4062 throw new IllegalArgumentException("Bad PendingIntent object");
4063 }
4064
Craig Mautnerb9168362015-02-26 20:40:19 -08004065 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004066 Binder.getCallingUid(), mCurrentUser, false,
4067 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004068
4069 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4070 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4071 pendingIntent.key.requestResolvedType);
4072
4073 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4074 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4075 }
4076
4077 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004078 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004079 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004080 throw new SecurityException(
4081 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4082 }
4083 }
4084
Craig Mautnerdf88d732014-01-27 09:21:32 -08004085 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004086 public IBinder asBinder() {
4087 return this;
4088 }
4089
Craig Mautner4504de52013-12-20 09:06:56 -08004090 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004091 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004092 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004093 }
4094
Craig Mautner4a1cb222013-12-04 16:14:06 -08004095 ActivityStackSupervisor getOuter() {
4096 return ActivityStackSupervisor.this;
4097 }
4098
Craig Mautnerd163e752014-06-13 17:18:47 -07004099 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004100 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004101 }
4102
4103 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004104 synchronized (mService) {
4105 if (mActivityDisplay != null) {
4106 mActivityDisplay.getBounds(outBounds);
4107 } else {
4108 outBounds.set(0, 0);
4109 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004110 }
4111 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004112
Craig Mautner6985bad2014-04-21 15:22:06 -07004113 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004114 void setVisible(boolean visible) {
4115 if (mVisible != visible) {
4116 mVisible = visible;
4117 if (mCallback != null) {
4118 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4119 0 /* unused */, this).sendToTarget();
4120 }
4121 }
4122 }
4123
Craig Mautner6985bad2014-04-21 15:22:06 -07004124 void setDrawn() {
4125 }
4126
Craig Mautner1b4bf852014-05-26 15:06:32 -07004127 // You can always start a new task on a regular ActivityStack.
4128 boolean isEligibleForNewTasks() {
4129 return true;
4130 }
4131
Craig Mautnerd163e752014-06-13 17:18:47 -07004132 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004133 detachLocked();
4134 deleteActivityContainer(this);
4135 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004136 }
4137
Craig Mautner34b73df2014-01-12 21:11:08 -08004138 @Override
4139 public String toString() {
4140 return mIdString + (mActivityDisplay == null ? "N" : "A");
4141 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004142 }
4143
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004144 private class VirtualActivityContainer extends ActivityContainer {
4145 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004146 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004147
4148 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4149 super(getNextStackId());
4150 mParentActivity = parent;
4151 mCallback = callback;
4152 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004153 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004154 }
4155
4156 @Override
4157 public void setSurface(Surface surface, int width, int height, int density) {
4158 super.setSurface(surface, width, height, density);
4159
4160 synchronized (mService) {
4161 final long origId = Binder.clearCallingIdentity();
4162 try {
4163 setSurfaceLocked(surface, width, height, density);
4164 } finally {
4165 Binder.restoreCallingIdentity(origId);
4166 }
4167 }
4168 }
4169
4170 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4171 if (mContainerState == CONTAINER_STATE_FINISHING) {
4172 return;
4173 }
4174 VirtualActivityDisplay virtualActivityDisplay =
4175 (VirtualActivityDisplay) mActivityDisplay;
4176 if (virtualActivityDisplay == null) {
4177 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004178 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004179 mActivityDisplay = virtualActivityDisplay;
4180 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
4181 attachToDisplayLocked(virtualActivityDisplay);
4182 }
4183
4184 if (mSurface != null) {
4185 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004186 }
4187
Craig Mautner6985bad2014-04-21 15:22:06 -07004188 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004189 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004190 mStack.resumeTopActivityLocked(null);
4191 } else {
4192 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004193 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004194 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004195 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004196 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004197 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004198
Craig Mautnerd163e752014-06-13 17:18:47 -07004199 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004200
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004201 if (DEBUG_STACK) Slog.d(TAG_STACK,
4202 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004203 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004204
Craig Mautner6985bad2014-04-21 15:22:06 -07004205 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004206 boolean isAttachedLocked() {
4207 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004208 }
4209
4210 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004211 void setDrawn() {
4212 synchronized (mService) {
4213 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004214 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004215 }
4216 }
4217
Craig Mautner1b4bf852014-05-26 15:06:32 -07004218 // Never start a new task on an ActivityView if it isn't explicitly specified.
4219 @Override
4220 boolean isEligibleForNewTasks() {
4221 return false;
4222 }
4223
Craig Mautnerd163e752014-06-13 17:18:47 -07004224 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004225 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004226 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4227 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4228 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4229 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4230 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004231 }
4232 }
4233
Craig Mautner4a1cb222013-12-04 16:14:06 -08004234 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4235 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004236 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004237 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004238 int mDisplayId;
4239 Display mDisplay;
4240 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004241
Craig Mautner4a1cb222013-12-04 16:14:06 -08004242 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4243 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004244 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004245
Jose Lima4b6c6692014-08-12 17:41:12 -07004246 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004247
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004248 ActivityDisplay() {
4249 }
Craig Mautner4504de52013-12-20 09:06:56 -08004250
Craig Mautner1a70a162014-09-13 12:09:31 -07004251 // After instantiation, check that mDisplay is not null before using this. The alternative
4252 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004253 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004254 final Display display = mDisplayManager.getDisplay(displayId);
4255 if (display == null) {
4256 return;
4257 }
4258 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004259 }
4260
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004261 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004262 mDisplay = display;
4263 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004264 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004265 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004266
4267 void attachActivities(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004268 if (DEBUG_STACK) Slog.v(TAG_STACK,
4269 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004270 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004271 }
4272
Craig Mautnere0a38842013-12-16 16:14:02 -08004273 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004274 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004275 + " from displayId=" + mDisplayId);
4276 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004277 }
4278
4279 void getBounds(Point bounds) {
4280 mDisplay.getDisplayInfo(mDisplayInfo);
4281 bounds.x = mDisplayInfo.appWidth;
4282 bounds.y = mDisplayInfo.appHeight;
4283 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004284
Jose Lima4b6c6692014-08-12 17:41:12 -07004285 void setVisibleBehindActivity(ActivityRecord r) {
4286 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004287 }
4288
Jose Lima4b6c6692014-08-12 17:41:12 -07004289 boolean hasVisibleBehindActivity() {
4290 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004291 }
4292
Craig Mautner34b73df2014-01-12 21:11:08 -08004293 @Override
4294 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004295 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4296 }
4297 }
4298
4299 class VirtualActivityDisplay extends ActivityDisplay {
4300 VirtualDisplay mVirtualDisplay;
4301
Craig Mautner6985bad2014-04-21 15:22:06 -07004302 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004303 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004304 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4305 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4306 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4307 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004308
4309 init(mVirtualDisplay.getDisplay());
4310
4311 mWindowManager.handleDisplayAdded(mDisplayId);
4312 }
4313
4314 void setSurface(Surface surface) {
4315 if (mVirtualDisplay != null) {
4316 mVirtualDisplay.setSurface(surface);
4317 }
4318 }
4319
4320 @Override
4321 void detachActivitiesLocked(ActivityStack stack) {
4322 super.detachActivitiesLocked(stack);
4323 if (mVirtualDisplay != null) {
4324 mVirtualDisplay.release();
4325 mVirtualDisplay = null;
4326 }
4327 }
4328
4329 @Override
4330 public String toString() {
4331 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004332 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004333 }
Jose Lima58e66d62014-05-27 20:00:27 -07004334
4335 private boolean isLeanbackOnlyDevice() {
4336 boolean onLeanbackOnly = false;
4337 try {
4338 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4339 PackageManager.FEATURE_LEANBACK_ONLY);
4340 } catch (RemoteException e) {
4341 // noop
4342 }
4343
4344 return onLeanbackOnly;
4345 }
Craig Mautner27084302013-03-25 08:05:25 -07004346}