blob: 56ea0f1e6b9d34785eb8f35294468a670d1f8d9d [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Wale Ogunwale868a5e12015-08-02 16:19:20 -070020import static android.app.ActivityManager.*;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070021import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
22import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070023import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070025import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070026import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080027import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070028import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070029import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
30import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
31import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070032import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070033import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
34import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010035import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070038
Svetoslav7008b512015-06-24 18:47:07 -070039import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070040import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080042import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityOptions;
44import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070045import android.app.AppOpsManager;
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;
Svetoslav7008b512015-06-24 18:47:07 -070064import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.content.pm.PackageManager;
66import android.content.pm.ResolveInfo;
67import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080068import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080069import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.hardware.display.DisplayManager;
71import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080072import android.hardware.display.DisplayManagerGlobal;
73import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080074import android.hardware.input.InputManager;
75import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080076import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070079import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070080import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070083import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070085import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070086import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070087import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040088import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070090import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070091import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070092import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070093import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040094import android.provider.Settings;
95import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070096import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070097import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070098import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070099import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800101import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700102
Craig Mautner4a1cb222013-12-04 16:14:06 -0800103import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800104import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800105import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800106import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800107import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700109import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800110import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700111import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400112import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700113import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400114import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800115import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700116import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700117import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118
justinzhang5286d3f2014-05-12 17:06:01 -0400119
Craig Mautner8d341ef2013-03-26 09:03:27 -0700120import java.io.FileDescriptor;
121import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700122import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700123import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700124import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700125import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700126import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700127
Craig Mautner4a1cb222013-12-04 16:14:06 -0800128public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800129 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700130 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700131 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700132 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700133 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700134 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700135 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700136 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700137 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
138 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700139 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700140 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700141 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
142 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700143 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700144 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800145
Craig Mautnerf3333272013-04-22 10:55:53 -0700146 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700147 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700148
Craig Mautner0eea92c2013-05-16 13:35:39 -0700149 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700150 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700151
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700152 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700153 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700154
Craig Mautner05d29032013-05-03 13:40:13 -0700155 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
156 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
157 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700158 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700159 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
161 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
162 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700163 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400164 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
165 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700166 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700167 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700168 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169
Wale Ogunwale040b4702015-08-06 18:10:50 -0700170 private static final 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
Wale Ogunwale040b4702015-08-06 18:10:50 -0700174 // Used to indicate if an object (e.g. stack) that we are trying to get
175 // should be created if it doesn't exist already.
176 private static final boolean CREATE_IF_NEEDED = true;
177
178 // Used to indicate if an object (e.g. task) should be moved/created
179 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700180 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700181
182 // Used to indicate that an objects (e.g. task) removal from its container
183 // (e.g. stack) is due to it moving to another container.
184 static final boolean MOVING = true;
185
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700186 // Force the focus to change to the stack we are moving a task to..
187 static final boolean FORCE_FOCUS = true;
188
Svetoslav7008b512015-06-24 18:47:07 -0700189 // Activity actions an app cannot start if it uses a permission which is not granted.
190 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
191 new ArrayMap<>();
192 static {
193 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
194 Manifest.permission.CAMERA);
195 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
196 Manifest.permission.CAMERA);
197 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
198 Manifest.permission.CALL_PHONE);
199 }
200
Svet Ganov99b60432015-06-27 13:15:22 -0700201 /** Action restriction: launching the activity is not restricted. */
202 private static final int ACTIVITY_RESTRICTION_NONE = 0;
203 /** Action restriction: launching the activity is restricted by a permission. */
204 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
205 /** Action restriction: launching the activity is restricted by an app op. */
206 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700207
justinzhang5286d3f2014-05-12 17:06:01 -0400208 /** Status Bar Service **/
209 private IBinder mToken = new Binder();
210 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400211 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400212
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700213 // For debugging to make sure the caller when acquiring/releasing our
214 // wake lock is the system process.
215 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700216
Craig Mautner27084302013-03-25 08:05:25 -0700217 final ActivityManagerService mService;
218
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800219 private final RecentTasks mRecentTasks;
220
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700221 final ActivityStackSupervisorHandler mHandler;
222
223 /** Short cut */
224 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800225 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700226
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700227 /** Counter for next free stack ID to use for dynamic activity stacks. */
228 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700229
230 /** Task identifier that activities are currently being started in. Incremented each time a
231 * new task is created. */
232 private int mCurTaskId = 0;
233
Craig Mautner2420ead2013-04-01 17:13:20 -0700234 /** The current user */
235 private int mCurrentUser;
236
Craig Mautnere0a38842013-12-16 16:14:02 -0800237 /** The stack containing the launcher app. Assumed to always be attached to
238 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700239 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700240
Craig Mautnere0a38842013-12-16 16:14:02 -0800241 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700242 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700243
Craig Mautner4a1cb222013-12-04 16:14:06 -0800244 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
245 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800246 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800247 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700248
249 /** List of activities that are waiting for a new activity to become visible before completing
250 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800251 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700252
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700253 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800254 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700255
256 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800257 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700258
Craig Mautnerde4ef022013-04-07 19:01:33 -0700259 /** List of activities that are ready to be stopped, but waiting for the next activity to
260 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800261 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700262
Craig Mautnerf3333272013-04-22 10:55:53 -0700263 /** List of activities that are ready to be finished, but waiting for the previous activity to
264 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800265 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700266
Craig Mautner0eea92c2013-05-16 13:35:39 -0700267 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800268 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700269
Craig Mautnerf3333272013-04-22 10:55:53 -0700270 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700271 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700272
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700273 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700274 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700275
Craig Mautnerde4ef022013-04-07 19:01:33 -0700276 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
277 * is being brought in front of us. */
278 boolean mUserLeaving = false;
279
Craig Mautner0eea92c2013-05-16 13:35:39 -0700280 /** Set when we have taken too long waiting to go to sleep. */
281 boolean mSleepTimeout = false;
282
Jose Lima58e66d62014-05-27 20:00:27 -0700283 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
284 * setWindowManager is called. **/
285 private boolean mLeanbackOnlyDevice;
286
Craig Mautner0eea92c2013-05-16 13:35:39 -0700287 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700288 * We don't want to allow the device to go to sleep while in the process
289 * of launching an activity. This is primarily to allow alarm intent
290 * receivers to launch an activity and get that to run before the device
291 * goes back to sleep.
292 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700293 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700294
295 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700296 * Set when the system is going to sleep, until we have
297 * successfully paused the current activity and released our wake lock.
298 * At that point the system is allowed to actually sleep.
299 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700300 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700301
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700302 /** Stack id of the front stack when user switched, indexed by userId. */
303 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700304
Craig Mautner4504de52013-12-20 09:06:56 -0800305 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800306 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700307 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800308
309 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700310 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800311
Jeff Brownca9bc702014-02-11 14:32:56 -0800312 InputManagerInternal mInputManagerInternal;
313
Craig Mautner15df08a2015-04-01 12:17:18 -0700314 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
315 * may be finished until there is only one entry left. If this is empty the system is not
316 * in lockTask mode. */
317 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000318 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700319 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
320 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000321 */
322 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400323 /**
324 * Notifies the user when entering/exiting lock-task.
325 */
326 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800327
Craig Mautner15df08a2015-04-01 12:17:18 -0700328 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700329
Craig Mautner42d04db2014-11-06 12:13:23 -0800330 /** Used to keep resumeTopActivityLocked() from being entered recursively */
331 boolean inResumeTopActivity;
332
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700333 // temp. rect used during resize calculation so we don't need to create a new object each time.
334 private final Rect tempRect = new Rect();
335
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700336 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
337 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
338
Craig Mautneree36c772014-07-16 14:56:05 -0700339 /**
340 * Description of a request to start a new activity, which has been held
341 * due to app switches being disabled.
342 */
343 static class PendingActivityLaunch {
344 final ActivityRecord r;
345 final ActivityRecord sourceRecord;
346 final int startFlags;
347 final ActivityStack stack;
348
349 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
350 int _startFlags, ActivityStack _stack) {
351 r = _r;
352 sourceRecord = _sourceRecord;
353 startFlags = _startFlags;
354 stack = _stack;
355 }
356 }
357
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800358 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700359 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800360 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700361 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
362 }
363
364 /**
365 * At the time when the constructor runs, the power manager has not yet been
366 * initialized. So we initialize our wakelocks afterwards.
367 */
368 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800369 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700370 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700371 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700372 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700373 }
374
justinzhang5286d3f2014-05-12 17:06:01 -0400375 // This function returns a IStatusBarService. The value is from ServiceManager.
376 // getService and is cached.
377 private IStatusBarService getStatusBarService() {
378 synchronized (mService) {
379 if (mStatusBarService == null) {
380 mStatusBarService = IStatusBarService.Stub.asInterface(
381 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
382 if (mStatusBarService == null) {
383 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
384 }
385 }
386 return mStatusBarService;
387 }
388 }
389
Jason Monk35c62a42014-06-17 10:24:47 -0400390 private IDevicePolicyManager getDevicePolicyManager() {
391 synchronized (mService) {
392 if (mDevicePolicyManager == null) {
393 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
394 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
395 if (mDevicePolicyManager == null) {
396 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
397 }
398 }
399 return mDevicePolicyManager;
400 }
401 }
402
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700403 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800404 synchronized (mService) {
405 mWindowManager = wm;
406
407 mDisplayManager =
408 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
409 mDisplayManager.registerDisplayListener(this, null);
410
411 Display[] displays = mDisplayManager.getDisplays();
412 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
413 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800414 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700415 if (activityDisplay.mDisplay == null) {
416 throw new IllegalStateException("Default Display does not exist");
417 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800418 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800419 }
420
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700421 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800422 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800423
424 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700425
426 // Initialize this here, now that we can get a valid reference to PackageManager.
427 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800428 }
Craig Mautner27084302013-03-25 08:05:25 -0700429 }
430
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200431 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700432 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200433 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700434 }
435
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700436 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800437 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700438 }
439
Craig Mautnerde4ef022013-04-07 19:01:33 -0700440 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800441 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700442 }
443
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800444 /** Top of all visible stacks is/should always be equal to the focused stack.
445 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
446 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700447 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700448 if (stack == null) {
449 return false;
450 }
451
Craig Mautnerdf88d732014-01-27 09:21:32 -0800452 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
453 if (parent != null) {
454 stack = parent.task.stack;
455 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800456 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700457 }
458
Craig Mautner299f9602015-01-26 09:47:33 -0800459 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800460 moveHomeStack(toFront, reason, null);
461 }
462
463 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800464 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800465 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800466 if (topNdx <= 0) {
467 return;
468 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700469
470 // The home stack should either be at the top or bottom of the stack list.
471 if ((toFront && (stacks.get(topNdx) != mHomeStack))
472 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700473 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700474 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
475 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800476 stacks.remove(mHomeStack);
477 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700478 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800479
480 if (lastFocusedStack != null) {
481 mLastFocusedStack = lastFocusedStack;
482 }
483 mFocusedStack = stacks.get(topNdx);
484
Craig Mautnerde313752015-01-22 14:28:03 -0800485 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
486 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800487 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800488
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800489 if (mService.mBooting || !mService.mBooted) {
490 final ActivityRecord r = topRunningActivityLocked();
491 if (r != null && r.idle) {
492 checkFinishBootingLocked();
493 }
494 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700495 }
496
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700497 /** Returns true if the focus activity was adjusted to the home stack top activity. */
498 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700499 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
500 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700501 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700502 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700503
Craig Mautner84984fa2014-06-19 11:19:20 -0700504 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700505
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700506 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700507 if (top == null) {
508 return false;
509 }
510 mService.setFocusedActivityLocked(top, reason);
511 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700512 }
513
Craig Mautner299f9602015-01-26 09:47:33 -0800514 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700515 if (!mService.mBooting && !mService.mBooted) {
516 // Not ready yet!
517 return false;
518 }
519
Craig Mautner84984fa2014-06-19 11:19:20 -0700520 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
521 mWindowManager.showRecentApps();
522 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700523 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700524
Craig Mautner84984fa2014-06-19 11:19:20 -0700525 if (prev != null) {
526 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
527 }
528
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700529 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
530 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700531 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800532 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700533 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700534 }
Craig Mautner299f9602015-01-26 09:47:33 -0800535 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700536 }
537
Craig Mautner8d341ef2013-03-26 09:03:27 -0700538 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700539 return anyTaskForIdLocked(id, true);
540 }
541
542 /**
543 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
544 * @param id Id of the task we would like returned.
545 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
546 * restore the task from recents to the active list.
547 */
548 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800549 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800551 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800552 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
553 ActivityStack stack = stacks.get(stackNdx);
554 TaskRecord task = stack.taskForIdLocked(id);
555 if (task != null) {
556 return task;
557 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700558 }
559 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800560
561 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700562 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800563 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800564 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700565 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800566 return null;
567 }
568
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700569 if (!restoreFromRecents) {
570 return task;
571 }
572
Chong Zhang5dcb2752015-08-18 13:50:26 -0700573 if (!restoreRecentTaskLocked(task, INVALID_STACK_ID)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700574 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
575 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800576 return null;
577 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700578 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800579 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700580 }
581
Craig Mautner6170f732013-04-02 13:05:23 -0700582 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800583 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800584 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800585 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800586 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
587 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
588 if (r != null) {
589 return r;
590 }
Craig Mautner6170f732013-04-02 13:05:23 -0700591 }
592 }
593 return null;
594 }
595
Craig Mautneref73ee12014-04-23 11:45:37 -0700596 void setNextTaskId(int taskId) {
597 if (taskId > mCurTaskId) {
598 mCurTaskId = taskId;
599 }
600 }
601
Craig Mautner8d341ef2013-03-26 09:03:27 -0700602 int getNextTaskId() {
603 do {
604 mCurTaskId++;
605 if (mCurTaskId <= 0) {
606 mCurTaskId = 1;
607 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700608 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700609 return mCurTaskId;
610 }
611
Craig Mautnerde4ef022013-04-07 19:01:33 -0700612 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800613 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700614 if (stack == null) {
615 return null;
616 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700617 ActivityRecord resumedActivity = stack.mResumedActivity;
618 if (resumedActivity == null || resumedActivity.app == null) {
619 resumedActivity = stack.mPausingActivity;
620 if (resumedActivity == null || resumedActivity.app == null) {
621 resumedActivity = stack.topRunningActivityLocked(null);
622 }
623 }
624 return resumedActivity;
625 }
626
Dianne Hackbornff072722014-09-24 10:56:28 -0700627 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700628 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800630 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
631 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800632 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
633 final ActivityStack stack = stacks.get(stackNdx);
634 if (!isFrontStack(stack)) {
635 continue;
636 }
637 ActivityRecord hr = stack.topRunningActivityLocked(null);
638 if (hr != null) {
639 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
640 && processName.equals(hr.processName)) {
641 try {
George Mount2c92c972014-03-20 09:38:23 -0700642 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800643 didSomething = true;
644 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700645 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800646 Slog.w(TAG, "Exception in new application when starting activity "
647 + hr.intent.getComponent().flattenToShortString(), e);
648 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700649 }
Craig Mautner20e72272013-04-01 13:45:53 -0700650 }
Craig Mautner20e72272013-04-01 13:45:53 -0700651 }
652 }
653 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700654 if (!didSomething) {
655 ensureActivitiesVisibleLocked(null, 0);
656 }
Craig Mautner20e72272013-04-01 13:45:53 -0700657 return didSomething;
658 }
659
660 boolean allResumedActivitiesIdle() {
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);
Craig Mautner34b73df2014-01-12 21:11:08 -0800665 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800666 continue;
667 }
668 final ActivityRecord resumedActivity = stack.mResumedActivity;
669 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700670 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800671 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672 return false;
673 }
Craig Mautner20e72272013-04-01 13:45:53 -0700674 }
675 }
676 return true;
677 }
678
Craig Mautnerde4ef022013-04-07 19:01:33 -0700679 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800680 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
681 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800682 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
683 final ActivityStack stack = stacks.get(stackNdx);
684 if (isFrontStack(stack)) {
685 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700686 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 return false;
688 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700689 }
690 }
691 }
692 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700693 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800694 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
695 mLastFocusedStack + " to=" + mFocusedStack);
696 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700697 return true;
698 }
699
700 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800701 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800702 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
703 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800704 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
705 final ActivityStack stack = stacks.get(stackNdx);
706 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800707 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700708 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800709 return false;
710 }
711 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700713 }
714 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800715 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700716 }
717
Craig Mautner2acc3892013-09-23 10:28:14 -0700718 /**
719 * Pause all activities in either all of the stacks or just the back stacks.
720 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700721 * @return true if any activity was paused as a result of this call.
722 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700723 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700724 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
726 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
728 final ActivityStack stack = stacks.get(stackNdx);
729 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700730 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800731 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700732 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
733 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800734 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700735 }
736 }
737 return someActivityPaused;
738 }
739
Craig Mautnerde4ef022013-04-07 19:01:33 -0700740 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700741 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800742 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
743 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800744 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
745 final ActivityStack stack = stacks.get(stackNdx);
746 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700747 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700749 Slog.d(TAG_STATES,
750 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800751 pausing = false;
752 } else {
753 return false;
754 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700755 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700756 }
757 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700758 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700759 }
760
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700761 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
762 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500763 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800764 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
765 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
766 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
767 final ActivityStack stack = stacks.get(stackNdx);
768 if (stack.mResumedActivity != null &&
769 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700770 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800771 }
772 }
773 }
774 }
775
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700776 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700777 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700778 }
779
780 void sendWaitingVisibleReportLocked(ActivityRecord r) {
781 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700782 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700783 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700784 if (w.who == null) {
785 changed = true;
786 w.timeout = false;
787 if (r != null) {
788 w.who = new ComponentName(r.info.packageName, r.info.name);
789 }
790 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
791 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700792 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700793 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700794 if (changed) {
795 mService.notifyAll();
796 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700797 }
798
799 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
800 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700801 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700802 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700803 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700804 if (w.who == null) {
805 changed = true;
806 w.timeout = timeout;
807 if (r != null) {
808 w.who = new ComponentName(r.info.packageName, r.info.name);
809 }
810 w.thisTime = thisTime;
811 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700812 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700813 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700814 if (changed) {
815 mService.notifyAll();
816 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700817 }
818
Craig Mautner29219d92013-04-16 20:19:12 -0700819 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800820 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700821 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
822 if (r != null) {
823 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700824 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700825
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800827 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
829 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700830 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700831 r = stack.topRunningActivityLocked(null);
832 if (r != null) {
833 return r;
834 }
835 }
836 }
837 return null;
838 }
839
Dianne Hackborn09233282014-04-30 11:33:59 -0700840 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700841 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800842 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
843 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800844 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800845 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800846 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800847 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
848 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700849 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800850 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700851 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700852 }
853 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700854
855 // The lists are already sorted from most recent to oldest. Just pull the most recent off
856 // each list and add it to list. Stop when all lists are empty or maxNum reached.
857 while (maxNum > 0) {
858 long mostRecentActiveTime = Long.MIN_VALUE;
859 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800860 final int numTaskLists = runningTaskLists.size();
861 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
862 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700863 if (!stackTaskList.isEmpty()) {
864 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
865 if (lastActiveTime > mostRecentActiveTime) {
866 mostRecentActiveTime = lastActiveTime;
867 selectedStackList = stackTaskList;
868 }
869 }
870 }
871 if (selectedStackList != null) {
872 list.add(selectedStackList.remove(0));
873 --maxNum;
874 } else {
875 break;
876 }
877 }
Craig Mautner20e72272013-04-01 13:45:53 -0700878 }
879
Craig Mautner23ac33b2013-04-01 16:26:35 -0700880 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700881 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700882 // Collect information about the target of the Intent.
883 ActivityInfo aInfo;
884 try {
885 ResolveInfo rInfo =
886 AppGlobals.getPackageManager().resolveIntent(
887 intent, resolvedType,
888 PackageManager.MATCH_DEFAULT_ONLY
889 | ActivityManagerService.STOCK_PM_FLAGS, userId);
890 aInfo = rInfo != null ? rInfo.activityInfo : null;
891 } catch (RemoteException e) {
892 aInfo = null;
893 }
894
895 if (aInfo != null) {
896 // Store the found target back into the intent, because now that
897 // we have it we never want to do this again. For example, if the
898 // user navigates back to this point in the history, we should
899 // always restart the exact same activity.
900 intent.setComponent(new ComponentName(
901 aInfo.applicationInfo.packageName, aInfo.name));
902
903 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700904 if (!aInfo.processName.equals("system")) {
905 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700906 mService.setDebugApp(aInfo.processName, true, false);
907 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700908
Man Caocfa78b22015-06-11 20:14:34 -0700909 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700910 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
911 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700912
Man Caocfa78b22015-06-11 20:14:34 -0700913 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
914 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
915 }
916
917 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700918 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700919 }
920 }
921 }
922 return aInfo;
923 }
924
Craig Mautner299f9602015-01-26 09:47:33 -0800925 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
926 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700927 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
928 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
929 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
930 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700931 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
932 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700933 null /* outActivity */, null /* container */, null /* inTask */);
934 if (inResumeTopActivity) {
935 // If we are in resume section already, home activity will be initialized, but not
936 // resumed (to avoid recursive resume) and will stay that way until something pokes it
937 // again. We need to schedule another resume.
938 scheduleResumeTopActivities();
939 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700940 }
941
Craig Mautner23ac33b2013-04-01 16:26:35 -0700942 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700943 String callingPackage, Intent intent, String resolvedType,
944 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700945 IBinder resultTo, String resultWho, int requestCode, int startFlags,
946 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700947 Bundle options, boolean ignoreTargetSecurity, int userId,
948 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700949 // Refuse possible leaked file descriptors
950 if (intent != null && intent.hasFileDescriptors()) {
951 throw new IllegalArgumentException("File descriptors passed in Intent");
952 }
953 boolean componentSpecified = intent.getComponent() != null;
954
955 // Don't modify the client's object!
956 intent = new Intent(intent);
957
958 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700959 ActivityInfo aInfo =
960 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700961
Craig Mautnere0a38842013-12-16 16:14:02 -0800962 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700963 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800964 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700965 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800966 // Cannot start a child activity if the parent is not resumed.
967 return ActivityManager.START_CANCELED;
968 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700969 final int realCallingPid = Binder.getCallingPid();
970 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700971 int callingPid;
972 if (callingUid >= 0) {
973 callingPid = -1;
974 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700975 callingPid = realCallingPid;
976 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700977 } else {
978 callingPid = callingUid = -1;
979 }
980
Craig Mautnere0a38842013-12-16 16:14:02 -0800981 final ActivityStack stack;
982 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800983 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800984 } else {
985 stack = container.mStack;
986 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800987 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700988 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700989 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700990
991 final long origId = Binder.clearCallingIdentity();
992
993 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800994 (aInfo.applicationInfo.privateFlags
995 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700996 // This may be a heavy-weight process! Check to see if we already
997 // have another, different heavy-weight process running.
998 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
999 if (mService.mHeavyWeightProcess != null &&
1000 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1001 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001002 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001003 if (caller != null) {
1004 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1005 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001006 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001007 } else {
1008 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001009 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001010 + intent.toString());
1011 ActivityOptions.abort(options);
1012 return ActivityManager.START_PERMISSION_DENIED;
1013 }
1014 }
1015
1016 IIntentSender target = mService.getIntentSenderLocked(
1017 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001018 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001019 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1020 | PendingIntent.FLAG_ONE_SHOT, null);
1021
1022 Intent newIntent = new Intent();
1023 if (requestCode >= 0) {
1024 // Caller is requesting a result.
1025 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1026 }
1027 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1028 new IntentSender(target));
1029 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1030 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1031 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1032 hist.packageName);
1033 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1034 hist.task.taskId);
1035 }
1036 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1037 aInfo.packageName);
1038 newIntent.setFlags(intent.getFlags());
1039 newIntent.setClassName("android",
1040 HeavyWeightSwitcherActivity.class.getName());
1041 intent = newIntent;
1042 resolvedType = null;
1043 caller = null;
1044 callingUid = Binder.getCallingUid();
1045 callingPid = Binder.getCallingPid();
1046 componentSpecified = true;
1047 try {
1048 ResolveInfo rInfo =
1049 AppGlobals.getPackageManager().resolveIntent(
1050 intent, null,
1051 PackageManager.MATCH_DEFAULT_ONLY
1052 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1053 aInfo = rInfo != null ? rInfo.activityInfo : null;
1054 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1055 } catch (RemoteException e) {
1056 aInfo = null;
1057 }
1058 }
1059 }
1060 }
1061
Dianne Hackborn91097de2014-04-04 18:02:06 -07001062 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1063 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001064 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001065 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001066 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001067
Craig Mautner85c11a82014-07-17 12:38:18 -07001068 Binder.restoreCallingIdentity(origId);
1069
Craig Mautnerde4ef022013-04-07 19:01:33 -07001070 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001071 // If the caller also wants to switch to a new configuration,
1072 // do so now. This allows a clean switch, as we are waiting
1073 // for the current activity to pause (so we will not destroy
1074 // it), and have not yet started the next activity.
1075 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1076 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001077 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001078 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001079 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001080 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001081 }
1082
Craig Mautner23ac33b2013-04-01 16:26:35 -07001083 if (outResult != null) {
1084 outResult.result = res;
1085 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001086 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001087 do {
1088 try {
1089 mService.wait();
1090 } catch (InterruptedException e) {
1091 }
1092 } while (!outResult.timeout && outResult.who == null);
1093 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001094 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001095 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001096 outResult.timeout = false;
1097 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1098 outResult.totalTime = 0;
1099 outResult.thisTime = 0;
1100 } else {
1101 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001102 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001103 do {
1104 try {
1105 mService.wait();
1106 } catch (InterruptedException e) {
1107 }
1108 } while (!outResult.timeout && outResult.who == null);
1109 }
1110 }
1111 }
1112
1113 return res;
1114 }
1115 }
1116
1117 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1118 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1119 Bundle options, int userId) {
1120 if (intents == null) {
1121 throw new NullPointerException("intents is null");
1122 }
1123 if (resolvedTypes == null) {
1124 throw new NullPointerException("resolvedTypes is null");
1125 }
1126 if (intents.length != resolvedTypes.length) {
1127 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1128 }
1129
Craig Mautner23ac33b2013-04-01 16:26:35 -07001130
1131 int callingPid;
1132 if (callingUid >= 0) {
1133 callingPid = -1;
1134 } else if (caller == null) {
1135 callingPid = Binder.getCallingPid();
1136 callingUid = Binder.getCallingUid();
1137 } else {
1138 callingPid = callingUid = -1;
1139 }
1140 final long origId = Binder.clearCallingIdentity();
1141 try {
1142 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001143 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001144 for (int i=0; i<intents.length; i++) {
1145 Intent intent = intents[i];
1146 if (intent == null) {
1147 continue;
1148 }
1149
1150 // Refuse possible leaked file descriptors
1151 if (intent != null && intent.hasFileDescriptors()) {
1152 throw new IllegalArgumentException("File descriptors passed in Intent");
1153 }
1154
1155 boolean componentSpecified = intent.getComponent() != null;
1156
1157 // Don't modify the client's object!
1158 intent = new Intent(intent);
1159
1160 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001161 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001162 // TODO: New, check if this is correct
1163 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1164
1165 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001166 (aInfo.applicationInfo.privateFlags
1167 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001168 throw new IllegalArgumentException(
1169 "FLAG_CANT_SAVE_STATE not supported here");
1170 }
1171
1172 Bundle theseOptions;
1173 if (options != null && i == intents.length-1) {
1174 theseOptions = options;
1175 } else {
1176 theseOptions = null;
1177 }
Craig Mautner6170f732013-04-02 13:05:23 -07001178 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001179 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1180 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001181 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001182 if (res < 0) {
1183 return res;
1184 }
1185
1186 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1187 }
1188 }
1189 } finally {
1190 Binder.restoreCallingIdentity(origId);
1191 }
1192
1193 return ActivityManager.START_SUCCESS;
1194 }
1195
Craig Mautner2420ead2013-04-01 17:13:20 -07001196 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001197 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001198 throws RemoteException {
1199
Craig Mautner2568c3a2015-03-26 14:22:34 -07001200 if (andResume) {
1201 r.startFreezingScreenLocked(app, 0);
1202 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001203
Craig Mautner2568c3a2015-03-26 14:22:34 -07001204 // schedule launch ticks to collect information about slow apps.
1205 r.startLaunchTickingLocked();
1206 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001207
1208 // Have the window manager re-evaluate the orientation of
1209 // the screen based on the new activity order. Note that
1210 // as a result of this, it can call back into the activity
1211 // manager with a new orientation. We don't care about that,
1212 // because the activity is not currently running so we are
1213 // just restarting it anyway.
1214 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001215 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001216 mService.mConfiguration,
1217 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001218 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001219 }
1220
1221 r.app = app;
1222 app.waitingToKill = null;
1223 r.launchCount++;
1224 r.lastLaunchTime = SystemClock.uptimeMillis();
1225
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001226 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001227
1228 int idx = app.activities.indexOf(r);
1229 if (idx < 0) {
1230 app.activities.add(r);
1231 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001232 mService.updateLruProcessLocked(app, true, null);
1233 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001234
Craig Mautner15df08a2015-04-01 12:17:18 -07001235 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001236 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1237 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001238 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001239 }
1240
1241 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001242 try {
1243 if (app.thread == null) {
1244 throw new RemoteException();
1245 }
1246 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001247 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001248 if (andResume) {
1249 results = r.results;
1250 newIntents = r.newIntents;
1251 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001252 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1253 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1254 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 if (andResume) {
1256 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1257 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001258 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001259 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001260 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001261 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001262 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001263 }
1264 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1265 r.sleeping = false;
1266 r.forceNewConfig = false;
1267 mService.showAskCompatModeDialogLocked(r);
1268 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001269 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001270 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1271 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1272 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001273 final String profileFile = mService.mProfileFile;
1274 if (profileFile != null) {
1275 ParcelFileDescriptor profileFd = mService.mProfileFd;
1276 if (profileFd != null) {
1277 try {
1278 profileFd = profileFd.dup();
1279 } catch (IOException e) {
1280 if (profileFd != null) {
1281 try {
1282 profileFd.close();
1283 } catch (IOException o) {
1284 }
1285 profileFd = null;
1286 }
1287 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001288 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001289
1290 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1291 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001292 }
1293 }
1294 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001295
Craig Mautner2568c3a2015-03-26 14:22:34 -07001296 if (andResume) {
1297 app.hasShownUi = true;
1298 app.pendingUiClean = true;
1299 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001300 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001301 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001302 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001303 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001304 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001305 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001306
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001307 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001308 // This may be a heavy-weight process! Note that the package
1309 // manager will ensure that only activity can run in the main
1310 // process of the .apk, which is the only thing that will be
1311 // considered heavy-weight.
1312 if (app.processName.equals(app.info.packageName)) {
1313 if (mService.mHeavyWeightProcess != null
1314 && mService.mHeavyWeightProcess != app) {
1315 Slog.w(TAG, "Starting new heavy weight process " + app
1316 + " when already running "
1317 + mService.mHeavyWeightProcess);
1318 }
1319 mService.mHeavyWeightProcess = app;
1320 Message msg = mService.mHandler.obtainMessage(
1321 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1322 msg.obj = r;
1323 mService.mHandler.sendMessage(msg);
1324 }
1325 }
1326
1327 } catch (RemoteException e) {
1328 if (r.launchFailed) {
1329 // This is the second time we failed -- finish activity
1330 // and give up.
1331 Slog.e(TAG, "Second failure launching "
1332 + r.intent.getComponent().flattenToShortString()
1333 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001334 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001335 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1336 "2nd-crash", false);
1337 return false;
1338 }
1339
1340 // This is the first time we failed -- restart process and
1341 // retry.
1342 app.activities.remove(r);
1343 throw e;
1344 }
1345
1346 r.launchFailed = false;
1347 if (stack.updateLRUListLocked(r)) {
1348 Slog.w(TAG, "Activity " + r
1349 + " being launched, but already in LRU list");
1350 }
1351
1352 if (andResume) {
1353 // As part of the process of launching, ActivityThread also performs
1354 // a resume.
1355 stack.minimalResumeActivityLocked(r);
1356 } else {
1357 // This activity is not starting in the resumed state... which
1358 // should look like we asked it to pause+stop (but remain visible),
1359 // and it has done so and reported back the current icicle and
1360 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001361 if (DEBUG_STATES) Slog.v(TAG_STATES,
1362 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001363 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001364 r.stopped = true;
1365 }
1366
1367 // Launch the new version setup screen if needed. We do this -after-
1368 // launching the initial activity (that is, home), so that it can have
1369 // a chance to initialize itself while in the background, making the
1370 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001371 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001372 mService.startSetupActivityLocked();
1373 }
1374
Dianne Hackborn465fa392014-09-14 14:21:18 -07001375 // Update any services we are bound to that might care about whether
1376 // their client may have activities.
1377 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1378
Craig Mautner2420ead2013-04-01 17:13:20 -07001379 return true;
1380 }
1381
Craig Mautnere79d42682013-04-01 19:01:53 -07001382 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001383 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001384 // Is this activity's application already running?
1385 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001386 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001387
1388 r.task.stack.setLaunchTime(r);
1389
1390 if (app != null && app.thread != null) {
1391 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001392 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1393 || !"android".equals(r.info.packageName)) {
1394 // Don't add this if it is a platform component that is marked
1395 // to run in multiple processes, because this is actually
1396 // part of the framework so doesn't make sense to track as a
1397 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001398 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1399 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001400 }
George Mount2c92c972014-03-20 09:38:23 -07001401 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001402 return;
1403 } catch (RemoteException e) {
1404 Slog.w(TAG, "Exception when starting activity "
1405 + r.intent.getComponent().flattenToShortString(), e);
1406 }
1407
1408 // If a dead object exception was thrown -- fall through to
1409 // restart the application.
1410 }
1411
1412 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001413 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001414 }
1415
Craig Mautner6170f732013-04-02 13:05:23 -07001416 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001417 Intent intent, String resolvedType, ActivityInfo aInfo,
1418 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1419 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001420 int callingPid, int callingUid, String callingPackage,
1421 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001422 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1423 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001424 int err = ActivityManager.START_SUCCESS;
1425
1426 ProcessRecord callerApp = null;
1427 if (caller != null) {
1428 callerApp = mService.getRecordForAppLocked(caller);
1429 if (callerApp != null) {
1430 callingPid = callerApp.pid;
1431 callingUid = callerApp.info.uid;
1432 } else {
1433 Slog.w(TAG, "Unable to find app for caller " + caller
1434 + " (pid=" + callingPid + ") when starting: "
1435 + intent.toString());
1436 err = ActivityManager.START_PERMISSION_DENIED;
1437 }
1438 }
1439
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001440 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1441
Craig Mautner6170f732013-04-02 13:05:23 -07001442 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001443 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001444 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001445 + " on display " + (container == null ? (mFocusedStack == null ?
1446 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1447 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1448 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001449 }
1450
1451 ActivityRecord sourceRecord = null;
1452 ActivityRecord resultRecord = null;
1453 if (resultTo != null) {
1454 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001455 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1456 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001457 if (sourceRecord != null) {
1458 if (requestCode >= 0 && !sourceRecord.finishing) {
1459 resultRecord = sourceRecord;
1460 }
1461 }
1462 }
Craig Mautner6170f732013-04-02 13:05:23 -07001463
Dianne Hackborn91097de2014-04-04 18:02:06 -07001464 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001465
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001466 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001467 // Transfer the result target from the source activity to the new
1468 // one being started, including any failures.
1469 if (requestCode >= 0) {
1470 ActivityOptions.abort(options);
1471 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1472 }
1473 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001474 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1475 resultRecord = null;
1476 }
Craig Mautner6170f732013-04-02 13:05:23 -07001477 resultWho = sourceRecord.resultWho;
1478 requestCode = sourceRecord.requestCode;
1479 sourceRecord.resultTo = null;
1480 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001481 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001482 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001483 if (sourceRecord.launchedFromUid == callingUid) {
1484 // The new activity is being launched from the same uid as the previous
1485 // activity in the flow, and asking to forward its result back to the
1486 // previous. In this case the activity is serving as a trampoline between
1487 // the two, so we also want to update its launchedFromPackage to be the
1488 // same as the previous activity. Note that this is safe, since we know
1489 // these two packages come from the same uid; the caller could just as
1490 // well have supplied that same package name itself. This specifially
1491 // deals with the case of an intent picker/chooser being launched in the app
1492 // flow to redirect to an activity picked by the user, where we want the final
1493 // activity to consider it to have been launched by the previous app activity.
1494 callingPackage = sourceRecord.launchedFromPackage;
1495 }
Craig Mautner6170f732013-04-02 13:05:23 -07001496 }
1497
1498 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1499 // We couldn't find a class that can handle the given Intent.
1500 // That's the end of that!
1501 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1502 }
1503
1504 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1505 // We couldn't find the specific class specified in the Intent.
1506 // Also the end of the line.
1507 err = ActivityManager.START_CLASS_NOT_FOUND;
1508 }
1509
Dianne Hackborn91097de2014-04-04 18:02:06 -07001510 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1511 && sourceRecord.task.voiceSession != null) {
1512 // If this activity is being launched as part of a voice session, we need
1513 // to ensure that it is safe to do so. If the upcoming activity will also
1514 // be part of the voice session, we can only launch it if it has explicitly
1515 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001516 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001517 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1518 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001519 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001520 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1521 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001522 Slog.w(TAG,
1523 "Activity being started in current voice task does not support voice: "
1524 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001525 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1526 }
1527 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001528 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001529 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1530 }
1531 }
1532 }
1533
1534 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1535 // If the caller is starting a new voice session, just make sure the target
1536 // is actually allowing it to run this way.
1537 try {
1538 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1539 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001540 Slog.w(TAG,
1541 "Activity being started in new voice task does not support: "
1542 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001543 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1544 }
1545 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001546 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001547 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1548 }
1549 }
1550
louis_changcd5d1982014-08-01 10:09:08 +08001551 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1552
Craig Mautner6170f732013-04-02 13:05:23 -07001553 if (err != ActivityManager.START_SUCCESS) {
1554 if (resultRecord != null) {
1555 resultStack.sendActivityResultLocked(-1,
1556 resultRecord, resultWho, requestCode,
1557 Activity.RESULT_CANCELED, null);
1558 }
Craig Mautner6170f732013-04-02 13:05:23 -07001559 ActivityOptions.abort(options);
1560 return err;
1561 }
1562
Svetoslav7008b512015-06-24 18:47:07 -07001563 boolean abort = false;
1564
Svet Ganov99b60432015-06-27 13:15:22 -07001565 final int startAnyPerm = mService.checkPermission(
1566 START_ANY_ACTIVITY, callingPid, callingUid);
1567
1568 if (startAnyPerm != PERMISSION_GRANTED) {
1569 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001570 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001571 final int actionRestriction = getActionRestrictionForCallingPackage(
1572 intent.getAction(), callingPackage, callingPid, callingUid);
1573
1574 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1575 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1576 if (resultRecord != null) {
1577 resultStack.sendActivityResultLocked(-1,
1578 resultRecord, resultWho, requestCode,
1579 Activity.RESULT_CANCELED, null);
1580 }
1581 String msg;
1582 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1583 msg = "Permission Denial: starting " + intent.toString()
1584 + " from " + callerApp + " (pid=" + callingPid
1585 + ", uid=" + callingUid + ")" + " with revoked permission "
1586 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1587 } else if (!aInfo.exported) {
1588 msg = "Permission Denial: starting " + intent.toString()
1589 + " from " + callerApp + " (pid=" + callingPid
1590 + ", uid=" + callingUid + ")"
1591 + " not exported from uid " + aInfo.applicationInfo.uid;
1592 } else {
1593 msg = "Permission Denial: starting " + intent.toString()
1594 + " from " + callerApp + " (pid=" + callingPid
1595 + ", uid=" + callingUid + ")"
1596 + " requires " + aInfo.permission;
1597 }
1598 Slog.w(TAG, msg);
1599 throw new SecurityException(msg);
1600 }
1601
1602 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1603 String message = "Appop Denial: starting " + intent.toString()
1604 + " from " + callerApp + " (pid=" + callingPid
1605 + ", uid=" + callingUid + ")"
1606 + " requires " + AppOpsManager.permissionToOp(
1607 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1608 Slog.w(TAG, message);
1609 abort = true;
1610 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1611 String message = "Appop Denial: starting " + intent.toString()
1612 + " from " + callerApp + " (pid=" + callingPid
1613 + ", uid=" + callingUid + ")"
1614 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1615 Slog.w(TAG, message);
1616 abort = true;
1617 }
Svetoslav7008b512015-06-24 18:47:07 -07001618 }
1619
1620 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001621 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001622
Craig Mautner6170f732013-04-02 13:05:23 -07001623 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001624 try {
1625 // The Intent we give to the watcher has the extra data
1626 // stripped off, since it can contain private information.
1627 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001628 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001629 aInfo.applicationInfo.packageName);
1630 } catch (RemoteException e) {
1631 mService.mController = null;
1632 }
Ben Gruver5e207332013-04-03 17:41:37 -07001633 }
Craig Mautner6170f732013-04-02 13:05:23 -07001634
Ben Gruver5e207332013-04-03 17:41:37 -07001635 if (abort) {
1636 if (resultRecord != null) {
1637 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001638 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001639 }
Ben Gruver5e207332013-04-03 17:41:37 -07001640 // We pretend to the caller that it was really started, but
1641 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001642 ActivityOptions.abort(options);
1643 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001644 }
1645
1646 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001647 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001648 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001649 if (outActivity != null) {
1650 outActivity[0] = r;
1651 }
1652
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001653 if (r.appTimeTracker == null && sourceRecord != null) {
1654 // If the caller didn't specify an explicit time tracker, we want to continue
1655 // tracking under any it has.
1656 r.appTimeTracker = sourceRecord.appTimeTracker;
1657 }
1658
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001659 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001660 if (voiceSession == null && (stack.mResumedActivity == null
1661 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001662 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1663 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001664 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001665 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001666 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001667 ActivityOptions.abort(options);
1668 return ActivityManager.START_SWITCHES_CANCELED;
1669 }
1670 }
1671
1672 if (mService.mDidAppSwitch) {
1673 // This is the second allowed switch since we stopped switches,
1674 // so now just generally allow switches. Use case: user presses
1675 // home (switches disabled, switch to home, mDidAppSwitch now true);
1676 // user taps a home icon (coming from home so allowed, we hit here
1677 // and now allow anyone to switch again).
1678 mService.mAppSwitchesAllowedTime = 0;
1679 } else {
1680 mService.mDidAppSwitch = true;
1681 }
1682
Craig Mautneree36c772014-07-16 14:56:05 -07001683 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001684
Dianne Hackborn91097de2014-04-04 18:02:06 -07001685 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001686 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001687
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001688 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001689 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001690 // activity start, but we are not actually doing an activity
1691 // switch... just dismiss the keyguard now, because we
1692 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001693 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001694 }
1695 return err;
1696 }
1697
Svet Ganov99b60432015-06-27 13:15:22 -07001698 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001699 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001700 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1701 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001702 == PackageManager.PERMISSION_DENIED) {
1703 return ACTIVITY_RESTRICTION_PERMISSION;
1704 }
1705
Christopher Tateff7add02015-08-17 10:23:22 -07001706 if (activityInfo.permission == null) {
1707 return ACTIVITY_RESTRICTION_NONE;
1708 }
1709
Svet Ganov99b60432015-06-27 13:15:22 -07001710 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1711 if (opCode == AppOpsManager.OP_NONE) {
1712 return ACTIVITY_RESTRICTION_NONE;
1713 }
1714
1715 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1716 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001717 if (!ignoreTargetSecurity) {
1718 return ACTIVITY_RESTRICTION_APPOP;
1719 }
Svet Ganov99b60432015-06-27 13:15:22 -07001720 }
1721
1722 return ACTIVITY_RESTRICTION_NONE;
1723 }
1724
Svetoslav7008b512015-06-24 18:47:07 -07001725 private int getActionRestrictionForCallingPackage(String action,
1726 String callingPackage, int callingPid, int callingUid) {
1727 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001728 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001729 }
1730
1731 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1732 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001733 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001734 }
1735
1736 final PackageInfo packageInfo;
1737 try {
1738 packageInfo = mService.mContext.getPackageManager()
1739 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1740 } catch (PackageManager.NameNotFoundException e) {
1741 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001742 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001743 }
1744
1745 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001746 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001747 }
1748
1749 if (mService.checkPermission(permission, callingPid, callingUid) ==
1750 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001751 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001752 }
1753
1754 final int opCode = AppOpsManager.permissionToOpCode(permission);
1755 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001756 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001757 }
1758
1759 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1760 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001761 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001762 }
1763
Svet Ganov99b60432015-06-27 13:15:22 -07001764 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001765 }
1766
Chong Zhang0fa656b2015-08-31 15:17:21 -07001767 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001768 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001769
1770 // On leanback only devices we should keep all activities in the same stack.
1771 if (!mLeanbackOnlyDevice &&
1772 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001773
1774 ActivityStack stack;
1775
Wale Ogunwale7d701172015-03-11 15:36:30 -07001776 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001777 stack = task.stack;
1778 if (stack.isOnHomeDisplay()) {
1779 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001780 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1781 "computeStackFocus: Setting " + "focused stack to r=" + r
1782 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001783 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001784 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001785 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001786 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001787 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001788 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001789 }
1790
Craig Mautnere0a38842013-12-16 16:14:02 -08001791 final ActivityContainer container = r.mInitialActivityContainer;
1792 if (container != null) {
1793 // The first time put it on the desired stack, after this put on task stack.
1794 r.mInitialActivityContainer = null;
1795 return container.mStack;
1796 }
1797
Craig Mautner1b4bf852014-05-26 15:06:32 -07001798 if (mFocusedStack != mHomeStack && (!newTask ||
1799 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001800 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001801 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001802 return mFocusedStack;
1803 }
1804
Wale Ogunwale706ed792015-08-02 10:29:44 -07001805 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001806 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1807 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001808 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001809 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1810 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001811 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001812 "computeStackFocus: Setting focused stack=" + stack);
1813 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001814 }
1815 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001816
Wale Ogunwale706ed792015-08-02 10:29:44 -07001817 // If there is no suitable dynamic stack then we figure out which static stack to use.
Chong Zhang0fa656b2015-08-31 15:17:21 -07001818 int stackId = task != null ? task.getLaunchStackId() :
1819 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1820 FULLSCREEN_WORKSPACE_STACK_ID;
1821 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001822 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1823 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001824 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001825 }
1826 return mHomeStack;
1827 }
1828
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001829 boolean setFocusedStack(ActivityRecord r, String reason) {
1830 if (r == null) {
1831 // Not sure what you are trying to do, but it is not going to work...
1832 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001833 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001834 final TaskRecord task = r.task;
1835 if (task == null || task.stack == null) {
1836 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1837 return false;
1838 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001839 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001840 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001841 }
1842
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001843 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001844 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001845 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001846 final Intent intent = r.intent;
1847 final int callingUid = r.launchedFromUid;
1848
Chong Zhang0fa656b2015-08-31 15:17:21 -07001849 boolean overrideBounds = false;
1850 Rect newBounds = null;
1851 if (r.info.resizeable || (inTask != null && inTask.mResizeable)) {
1852 if (intent.hasExtra(ActivityOptions.KEY_BOUNDS)) {
1853 overrideBounds = true;
1854 newBounds = Rect.unflattenFromString(
1855 intent.getStringExtra(ActivityOptions.KEY_BOUNDS));
1856 } else if (options != null) {
1857 ActivityOptions opts = new ActivityOptions(options);
1858 if (opts.hasBounds()) {
1859 overrideBounds = true;
1860 newBounds = opts.getBounds();
1861 }
1862 }
1863 }
1864
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001865 // In some flows in to this function, we retrieve the task record and hold on to it
1866 // without a lock before calling back in to here... so the task at this point may
1867 // not actually be in recents. Check for that, and if it isn't in recents just
1868 // consider it invalid.
1869 if (inTask != null && !inTask.inRecents) {
1870 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1871 inTask = null;
1872 }
1873
Craig Mautnerad400af2014-08-13 16:41:36 -07001874 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001875 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1876 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001877
Craig Mautnera228ae92014-07-09 05:44:55 -07001878 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001879 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001880 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001881 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1882 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1883 "\"singleInstance\" or \"singleTask\"");
1884 launchFlags &=
1885 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1886 } else {
1887 switch (r.info.documentLaunchMode) {
1888 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1889 break;
1890 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1891 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1892 break;
1893 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1894 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1895 break;
1896 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1897 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1898 break;
1899 }
1900 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001901
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001902 final boolean launchTaskBehind = r.mLaunchTaskBehind
1903 && !launchSingleTask && !launchSingleInstance
1904 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001905
Wale Ogunwale7d701172015-03-11 15:36:30 -07001906 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1907 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001908 // For whatever reason this activity is being launched into a new
1909 // task... yet the caller has requested a result back. Well, that
1910 // is pretty messed up, so instead immediately send back a cancel
1911 // and let the new task continue launched as normal without a
1912 // dependency on its originator.
1913 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1914 r.resultTo.task.stack.sendActivityResultLocked(-1,
1915 r.resultTo, r.resultWho, r.requestCode,
1916 Activity.RESULT_CANCELED, null);
1917 r.resultTo = null;
1918 }
1919
1920 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1921 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1922 }
1923
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001924 // If we are actually going to launch in to a new task, there are some cases where
1925 // we further want to do multiple task.
1926 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1927 if (launchTaskBehind
1928 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1929 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1930 }
1931 }
1932
Craig Mautner8849a5e2013-04-02 16:41:03 -07001933 // We'll invoke onUserLeaving before onPause only if the launching
1934 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001935 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001936 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1937 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001938
1939 // If the caller has asked not to resume at this point, we make note
1940 // of this in the record so that we can skip it when trying to find
1941 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001942 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001943 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001944 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001945 }
1946
Craig Mautnera254cd72014-05-25 16:47:39 -07001947 ActivityRecord notTop =
1948 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001949
1950 // If the onlyIfNeeded flag is set, then we can do this if the activity
1951 // being launched is the same as the one making the call... or, as
1952 // a special case, if we do not know the caller then we count the
1953 // current top activity as the caller.
1954 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1955 ActivityRecord checkedCaller = sourceRecord;
1956 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001957 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001958 }
1959 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1960 // Caller is not the same as launcher, so always needed.
1961 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1962 }
1963 }
1964
Craig Mautner8849a5e2013-04-02 16:41:03 -07001965 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001966 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001967
1968 // If the caller is not coming from another activity, but has given us an
1969 // explicit task into which they would like us to launch the new activity,
1970 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001971 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1972 final Intent baseIntent = inTask.getBaseIntent();
1973 final ActivityRecord root = inTask.getRootActivity();
1974 if (baseIntent == null) {
1975 ActivityOptions.abort(options);
1976 throw new IllegalArgumentException("Launching into task without base intent: "
1977 + inTask);
1978 }
1979
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001980 // If this task is empty, then we are adding the first activity -- it
1981 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001982 if (launchSingleInstance || launchSingleTask) {
1983 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1984 ActivityOptions.abort(options);
1985 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1986 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001987 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001988 if (root != null) {
1989 ActivityOptions.abort(options);
1990 throw new IllegalArgumentException("Caller with inTask " + inTask
1991 + " has root " + root + " but target is singleInstance/Task");
1992 }
1993 }
1994
1995 // If task is empty, then adopt the interesting intent launch flags in to the
1996 // activity being started.
1997 if (root == null) {
1998 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1999 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2000 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2001 launchFlags = (launchFlags&~flagsOfInterest)
2002 | (baseIntent.getFlags()&flagsOfInterest);
2003 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002004 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002005 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002006
Dianne Hackborn962d5352014-08-29 16:27:40 -07002007 // If the task is not empty and the caller is asking to start it as the root
2008 // of a new task, then we don't actually want to start this on the task. We
2009 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002010 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002011 addingToTask = false;
2012
2013 } else {
2014 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002015 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002016
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002017 reuseTask = inTask;
2018 } else {
2019 inTask = null;
2020 }
2021
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002022 if (inTask == null) {
2023 if (sourceRecord == null) {
2024 // This activity is not being started from another... in this
2025 // case we -always- start a new task.
2026 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2027 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2028 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2029 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2030 }
2031 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2032 // The original activity who is starting us is running as a single
2033 // instance... this new activity it is starting must go on its
2034 // own task.
2035 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2036 } else if (launchSingleInstance || launchSingleTask) {
2037 // The activity being started is a single instance... it always
2038 // gets launched into its own task.
2039 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2040 }
2041 }
2042
2043 ActivityInfo newTaskInfo = null;
2044 Intent newTaskIntent = null;
2045 ActivityStack sourceStack;
2046 if (sourceRecord != null) {
2047 if (sourceRecord.finishing) {
2048 // If the source is finishing, we can't further count it as our source. This
2049 // is because the task it is associated with may now be empty and on its way out,
2050 // so we don't want to blindly throw it in to that task. Instead we will take
2051 // the NEW_TASK flow and try to find a task for it. But save the task information
2052 // so it can be used when creating the new task.
2053 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2054 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2055 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2056 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2057 newTaskInfo = sourceRecord.info;
2058 newTaskIntent = sourceRecord.task.intent;
2059 }
2060 sourceRecord = null;
2061 sourceStack = null;
2062 } else {
2063 sourceStack = sourceRecord.task.stack;
2064 }
2065 } else {
2066 sourceStack = null;
2067 }
2068
2069 boolean movedHome = false;
2070 ActivityStack targetStack;
2071
2072 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002073 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002074
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002075 // We may want to try to place the new activity in to an existing task. We always
2076 // do this if the target activity is singleTask or singleInstance; we will also do
2077 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2078 // us to still place it in a new task: multi task, always doc mode, or being asked to
2079 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002080 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2081 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002082 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002083 // If bring to front is requested, and no result is requested and we have not
2084 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002085 // we can find a task that was started with this same
2086 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002087 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002088 // See if there is a task to bring to the front. If this is
2089 // a SINGLE_INSTANCE activity, there can be one and only one
2090 // instance of it in the history, and it is always in its own
2091 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002092 ActivityRecord intentActivity = !launchSingleInstance ?
2093 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002094 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002095 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2096 // but still needs to be a lock task mode violation since the task gets
2097 // cleared out and the device would otherwise leave the locked task.
2098 if (isLockTaskModeViolation(intentActivity.task,
2099 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2100 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002101 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002102 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002103 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2104 }
Craig Mautner29219d92013-04-16 20:19:12 -07002105 if (r.task == null) {
2106 r.task = intentActivity.task;
2107 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002108 if (intentActivity.task.intent == null) {
2109 // This task was started because of movement of
2110 // the activity based on affinity... now that we
2111 // are actually launching it, we can assign the
2112 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002113 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002114 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002115 targetStack = intentActivity.task.stack;
2116 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002117 // If the target task is not in the front, then we need
2118 // to bring it to the front... except... well, with
2119 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2120 // to have the same behavior as if a new instance was
2121 // being started, which means not bringing it to the front
2122 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002123 final ActivityStack focusStack = getFocusedStack();
2124 ActivityRecord curTop = (focusStack == null)
2125 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002126 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002127 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002128 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002129 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002130 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2131 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002132 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002133 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002134 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2135 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002136 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002137 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002138 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002139 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002140 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002141 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2142 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002143 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002144 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002145 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002146 options = null;
2147 }
2148 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002149 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002150 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002151 + " from " + intentActivity);
2152 targetStack.moveToFront("intentActivityFound");
2153 }
2154
Craig Mautner8849a5e2013-04-02 16:41:03 -07002155 // If the caller has requested that the target task be
2156 // reset, then do so.
2157 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2158 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2159 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002160 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002161 // We don't need to start a new activity, and
2162 // the client said not to do anything if that
2163 // is the case, so this is it! And for paranoia, make
2164 // sure we have correctly resumed the top activity.
2165 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002166 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002167
2168 // Make sure to notify Keyguard as well if we are not running an app
2169 // transition later.
2170 if (!movedToFront) {
2171 notifyActivityDrawnForKeyguard();
2172 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002173 } else {
2174 ActivityOptions.abort(options);
2175 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002176 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002177 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2178 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002179 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002180 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002181 // The caller has requested to completely replace any
2182 // existing task with its new activity. Well that should
2183 // not be too hard...
2184 reuseTask = intentActivity.task;
2185 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002186 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002187 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002188 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002189 // In this situation we want to remove all activities
2190 // from the task up to the one being started. In most
2191 // cases this means we are resetting the task to its
2192 // initial state.
2193 ActivityRecord top =
2194 intentActivity.task.performClearTaskLocked(r, launchFlags);
2195 if (top != null) {
2196 if (top.frontOfTask) {
2197 // Activity aliases may mean we use different
2198 // intents for the top activity, so make sure
2199 // the task now has the identity of the new
2200 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002201 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002202 }
2203 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2204 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002205 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002206 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002207 // A special case: we need to start the activity because it is not
2208 // currently running, and the caller has asked to clear the current
2209 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002210 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002211 // Now pretend like this activity is being started by the top of its
2212 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002213 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002214 TaskRecord task = sourceRecord.task;
2215 if (task != null && task.stack == null) {
2216 // Target stack got cleared when we all activities were removed
2217 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002218 targetStack = computeStackFocus(
2219 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002220 targetStack.addTask(
2221 task, !launchTaskBehind /* toTop */, false /* moving */);
2222 }
2223
Craig Mautner8849a5e2013-04-02 16:41:03 -07002224 }
2225 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2226 // In this case the top activity on the task is the
2227 // same as the one being launched, so we take that
2228 // as a request to bring the task to the foreground.
2229 // If the top activity in the task is the root
2230 // activity, deliver this new intent to it if it
2231 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002232 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002233 && intentActivity.realActivity.equals(r.realActivity)) {
2234 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2235 intentActivity.task);
2236 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002237 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002238 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002239 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2240 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002241 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2242 // In this case we are launching the root activity
2243 // of the task, but with a different intent. We
2244 // should start a new instance on top.
2245 addingToTask = true;
2246 sourceRecord = intentActivity;
2247 }
2248 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2249 // In this case an activity is being launched in to an
2250 // existing task, without resetting that task. This
2251 // is typically the situation of launching an activity
2252 // from a notification or shortcut. We want to place
2253 // the new activity on top of the current task.
2254 addingToTask = true;
2255 sourceRecord = intentActivity;
2256 } else if (!intentActivity.task.rootWasReset) {
2257 // In this case we are launching in to an existing task
2258 // that has not yet been started from its front door.
2259 // The current task has been brought to the front.
2260 // Ideally, we'd probably like to place this new task
2261 // at the bottom of its stack, but that's a little hard
2262 // to do with the current organization of the code so
2263 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002264 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002265 }
2266 if (!addingToTask && reuseTask == null) {
2267 // We didn't do anything... but it was needed (a.k.a., client
2268 // don't use that intent!) And for paranoia, make
2269 // sure we have correctly resumed the top activity.
2270 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002271 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002272 if (!movedToFront) {
2273 // Make sure to notify Keyguard as well if we are not running an app
2274 // transition later.
2275 notifyActivityDrawnForKeyguard();
2276 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002277 } else {
2278 ActivityOptions.abort(options);
2279 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002280 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002281 return ActivityManager.START_TASK_TO_FRONT;
2282 }
2283 }
2284 }
2285 }
2286
2287 //String uri = r.intent.toURI();
2288 //Intent intent2 = new Intent(uri);
2289 //Slog.i(TAG, "Given intent: " + r.intent);
2290 //Slog.i(TAG, "URI is: " + uri);
2291 //Slog.i(TAG, "To intent: " + intent2);
2292
2293 if (r.packageName != null) {
2294 // If the activity being launched is the same as the one currently
2295 // at the top, then we need to check if it should only be launched
2296 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002297 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002298 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002299 if (top != null && r.resultTo == null) {
2300 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2301 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002302 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002303 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002304 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2305 top.task);
2306 // For paranoia, make sure we have correctly
2307 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002308 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002309 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002310 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002311 }
2312 ActivityOptions.abort(options);
2313 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2314 // We don't need to start a new activity, and
2315 // the client said not to do anything if that
2316 // is the case, so this is it!
2317 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2318 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002319 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002320 return ActivityManager.START_DELIVERED_TO_TOP;
2321 }
2322 }
2323 }
2324 }
2325
2326 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002327 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002328 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2329 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002330 }
2331 ActivityOptions.abort(options);
2332 return ActivityManager.START_CLASS_NOT_FOUND;
2333 }
2334
2335 boolean newTask = false;
2336 boolean keepCurTransition = false;
2337
Craig Mautnerbb742462014-07-07 15:28:55 -07002338 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002339 sourceRecord.task : null;
2340
Craig Mautner8849a5e2013-04-02 16:41:03 -07002341 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002342 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002343 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002344 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002345 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002346 if (doResume) {
2347 targetStack.moveToFront("startingNewTask");
2348 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002349
Craig Mautner8849a5e2013-04-02 16:41:03 -07002350 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002351 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2352 newTaskInfo != null ? newTaskInfo : r.info,
2353 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002354 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2355 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002356 if (overrideBounds) {
2357 r.task.updateOverrideConfiguration(newBounds);
2358 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002359 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2360 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002361 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002362 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002363 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002364 if (isLockTaskModeViolation(r.task)) {
2365 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2366 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2367 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002368 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002369 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002370 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2371 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002372 // Caller wants to appear on home activity, so before starting
2373 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002374 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002375 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002376 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002377 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002378 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002379 if (isLockTaskModeViolation(sourceTask)) {
2380 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2381 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2382 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002383 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002384 if (doResume) {
2385 targetStack.moveToFront("sourceStackToFront");
2386 }
Craig Mautner737fae22014-10-15 12:52:10 -07002387 final TaskRecord topTask = targetStack.topTask();
2388 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002389 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002390 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002391 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002392 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002393 // In this case, we are adding the activity to an existing
2394 // task, but the caller has asked to clear that task if the
2395 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002396 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002397 keepCurTransition = true;
2398 if (top != null) {
2399 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002400 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002401 // For paranoia, make sure we have correctly
2402 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002403 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002404 if (doResume) {
2405 targetStack.resumeTopActivityLocked(null);
2406 }
2407 ActivityOptions.abort(options);
2408 return ActivityManager.START_DELIVERED_TO_TOP;
2409 }
2410 } else if (!addingToTask &&
2411 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2412 // In this case, we are launching an activity in our own task
2413 // that may already be running somewhere in the history, and
2414 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002415 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002416 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002417 final TaskRecord task = top.task;
2418 task.moveActivityToFrontLocked(top);
2419 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002420 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002421 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002422 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002423 if (doResume) {
2424 targetStack.resumeTopActivityLocked(null);
2425 }
2426 return ActivityManager.START_DELIVERED_TO_TOP;
2427 }
2428 }
2429 // An existing activity is starting this new activity, so we want
2430 // to keep the new one in the same task as the one that is starting
2431 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002432 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002433 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002434 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002435
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002436 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002437 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002438 // task it has provided to us.
2439 if (isLockTaskModeViolation(inTask)) {
2440 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2441 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2442 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002443 if (overrideBounds) {
2444 inTask.updateOverrideConfiguration(newBounds);
2445 int stackId = inTask.getLaunchStackId();
2446 if (stackId != inTask.stack.mStackId) {
2447 moveTaskToStackUncheckedLocked(
2448 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2449 }
2450 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002451 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002452 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2453 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002454
2455 // Check whether we should actually launch the new activity in to the task,
2456 // or just reuse the current activity on top.
2457 ActivityRecord top = inTask.getTopActivity();
2458 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2459 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2460 || launchSingleTop || launchSingleTask) {
2461 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2462 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2463 // We don't need to start a new activity, and
2464 // the client said not to do anything if that
2465 // is the case, so this is it!
2466 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2467 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002468 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002469 return ActivityManager.START_DELIVERED_TO_TOP;
2470 }
2471 }
2472
Dianne Hackborn962d5352014-08-29 16:27:40 -07002473 if (!addingToTask) {
2474 // We don't actually want to have this activity added to the task, so just
2475 // stop here but still tell the caller that we consumed the intent.
2476 ActivityOptions.abort(options);
2477 return ActivityManager.START_TASK_TO_FRONT;
2478 }
2479
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002480 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002481 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002482 + " in explicit task " + r.task);
2483
Craig Mautner8849a5e2013-04-02 16:41:03 -07002484 } else {
2485 // This not being started from an existing activity, and not part
2486 // of a new task... just put it in the top task, though these days
2487 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002488 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002489 if (doResume) {
2490 targetStack.moveToFront("addingToTopTask");
2491 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002492 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002493 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002494 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002495 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002496 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002497 + " in new guessed " + r.task);
2498 }
2499
2500 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002501 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002502
Craig Mautner76e2a762014-06-24 09:05:43 -07002503 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2504 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2505 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002506 if (newTask) {
2507 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2508 }
2509 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002510 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002511 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002512 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002513 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002514 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002515 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002516 return ActivityManager.START_SUCCESS;
2517 }
2518
Craig Mautneree36c772014-07-16 14:56:05 -07002519 final void doPendingActivityLaunchesLocked(boolean doResume) {
2520 while (!mPendingActivityLaunches.isEmpty()) {
2521 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002522 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002523 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002524 }
2525 }
2526
Craig Mautner7f13ed32014-07-28 14:00:35 -07002527 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002528 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2529 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002530 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002531 mPendingActivityLaunches.remove(palNdx);
2532 }
2533 }
2534 }
2535
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002536 void setLaunchSource(int uid) {
2537 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2538 }
2539
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002540 void acquireLaunchWakelock() {
2541 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2542 throw new IllegalStateException("Calling must be system uid");
2543 }
2544 mLaunchingActivity.acquire();
2545 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2546 // To be safe, don't allow the wake lock to be held for too long.
2547 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2548 }
2549 }
2550
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002551 /**
2552 * Called when the frontmost task is idle.
2553 * @return the state of mService.mBooting before this was called.
2554 */
2555 private boolean checkFinishBootingLocked() {
2556 final boolean booting = mService.mBooting;
2557 boolean enableScreen = false;
2558 mService.mBooting = false;
2559 if (!mService.mBooted) {
2560 mService.mBooted = true;
2561 enableScreen = true;
2562 }
2563 if (booting || enableScreen) {
2564 mService.postFinishBooting(booting, enableScreen);
2565 }
2566 return booting;
2567 }
2568
Craig Mautnerf3333272013-04-22 10:55:53 -07002569 // Checked.
2570 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2571 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002572 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002573
Craig Mautnerf3333272013-04-22 10:55:53 -07002574 ArrayList<ActivityRecord> stops = null;
2575 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002576 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002577 int NS = 0;
2578 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002579 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002580 boolean activityRemoved = false;
2581
Wale Ogunwale7d701172015-03-11 15:36:30 -07002582 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002583 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002584 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2585 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002586 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2587 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002588 if (fromTimeout) {
2589 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002590 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002591
2592 // This is a hack to semi-deal with a race condition
2593 // in the client where it can be constructed with a
2594 // newer configuration from when we asked it to launch.
2595 // We'll update with whatever configuration it now says
2596 // it used to launch.
2597 if (config != null) {
2598 r.configuration = config;
2599 }
2600
2601 // We are now idle. If someone is waiting for a thumbnail from
2602 // us, we can now deliver.
2603 r.idle = true;
2604
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002605 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002606 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002607 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002608 }
2609 }
2610
2611 if (allResumedActivitiesIdle()) {
2612 if (r != null) {
2613 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002614 }
2615
2616 if (mLaunchingActivity.isHeld()) {
2617 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2618 if (VALIDATE_WAKE_LOCK_CALLER &&
2619 Binder.getCallingUid() != Process.myUid()) {
2620 throw new IllegalStateException("Calling must be system uid");
2621 }
2622 mLaunchingActivity.release();
2623 }
2624 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002625 }
2626
2627 // Atomically retrieve all of the other things to do.
2628 stops = processStoppingActivitiesLocked(true);
2629 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002630 if ((NF = mFinishingActivities.size()) > 0) {
2631 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002632 mFinishingActivities.clear();
2633 }
2634
Craig Mautnerf3333272013-04-22 10:55:53 -07002635 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002636 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002637 mStartingUsers.clear();
2638 }
2639
Craig Mautnerf3333272013-04-22 10:55:53 -07002640 // Stop any activities that are scheduled to do so but have been
2641 // waiting for the next one to start.
2642 for (int i = 0; i < NS; i++) {
2643 r = stops.get(i);
2644 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002645 if (stack != null) {
2646 if (r.finishing) {
2647 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2648 } else {
2649 stack.stopActivityLocked(r);
2650 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002651 }
2652 }
2653
2654 // Finish any activities that are scheduled to do so but have been
2655 // waiting for the next one to start.
2656 for (int i = 0; i < NF; i++) {
2657 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002658 final ActivityStack stack = r.task.stack;
2659 if (stack != null) {
2660 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2661 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002662 }
2663
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002664 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002665 // Complete user switch
2666 if (startingUsers != null) {
2667 for (int i = 0; i < startingUsers.size(); i++) {
2668 mService.finishUserSwitch(startingUsers.get(i));
2669 }
2670 }
2671 // Complete starting up of background users
2672 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002673 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002674 mStartingBackgroundUsers.clear();
2675 for (int i = 0; i < startingUsers.size(); i++) {
2676 mService.finishUserBoot(startingUsers.get(i));
2677 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002678 }
2679 }
2680
2681 mService.trimApplications();
2682 //dump();
2683 //mWindowManager.dump();
2684
Craig Mautnerf3333272013-04-22 10:55:53 -07002685 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002686 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002687 }
2688
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002689 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002690 }
2691
Craig Mautner8e569572013-10-11 17:36:59 -07002692 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002693 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002694 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2695 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002696 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2697 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2698 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002699 }
Craig Mautner19091252013-10-05 00:03:53 -07002700 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002701 }
2702
2703 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002704 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2705 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002706 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2707 stacks.get(stackNdx).closeSystemDialogsLocked();
2708 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002709 }
2710 }
2711
Craig Mautner93529a42013-10-04 15:03:13 -07002712 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002713 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002714 }
2715
Craig Mautner8d341ef2013-03-26 09:03:27 -07002716 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002717 * Update the last used stack id for non-current user (current user's last
2718 * used stack is the focused stack)
2719 */
2720 void updateUserStackLocked(int userId, ActivityStack stack) {
2721 if (userId != mCurrentUser) {
2722 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2723 }
2724 }
2725
2726 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002727 * @return true if some activity was finished (or would have finished if doit were true).
2728 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002729 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2730 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002731 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002732 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2733 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002734 final int numStacks = stacks.size();
2735 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2736 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002737 if (stack.finishDisabledPackageActivitiesLocked(
2738 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002739 didSomething = true;
2740 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002741 }
2742 }
2743 return didSomething;
2744 }
2745
Dianne Hackborna413dc02013-07-12 12:02:55 -07002746 void updatePreviousProcessLocked(ActivityRecord r) {
2747 // Now that this process has stopped, we may want to consider
2748 // it to be the previous app to try to keep around in case
2749 // the user wants to return to it.
2750
2751 // First, found out what is currently the foreground app, so that
2752 // we don't blow away the previous app if this activity is being
2753 // hosted by the process that is actually still the foreground.
2754 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002755 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2756 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002757 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2758 final ActivityStack stack = stacks.get(stackNdx);
2759 if (isFrontStack(stack)) {
2760 if (stack.mResumedActivity != null) {
2761 fgApp = stack.mResumedActivity.app;
2762 } else if (stack.mPausingActivity != null) {
2763 fgApp = stack.mPausingActivity.app;
2764 }
2765 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002766 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002767 }
2768 }
2769
2770 // Now set this one as the previous process, only if that really
2771 // makes sense to.
2772 if (r.app != null && fgApp != null && r.app != fgApp
2773 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002774 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002775 mService.mPreviousProcess = r.app;
2776 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2777 }
2778 }
2779
Craig Mautner05d29032013-05-03 13:40:13 -07002780 boolean resumeTopActivitiesLocked() {
2781 return resumeTopActivitiesLocked(null, null, null);
2782 }
2783
2784 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2785 Bundle targetOptions) {
2786 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002787 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002788 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002789 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002790 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002791 if (isFrontStack(targetStack)) {
2792 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2793 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002794
Craig Mautnere0a38842013-12-16 16:14:02 -08002795 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2796 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002797 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2798 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002799 if (stack == targetStack) {
2800 // Already started above.
2801 continue;
2802 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002803 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002804 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002805 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002806 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002807 }
Craig Mautner05d29032013-05-03 13:40:13 -07002808 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002809 }
2810
Todd Kennedy539db512014-12-15 09:57:55 -08002811 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002812 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2813 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002814 final int numStacks = stacks.size();
2815 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2816 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002817 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002818 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002819 }
2820 }
2821
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002822 void finishVoiceTask(IVoiceInteractionSession session) {
2823 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2824 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2825 final int numStacks = stacks.size();
2826 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2827 final ActivityStack stack = stacks.get(stackNdx);
2828 stack.finishVoiceTask(session);
2829 }
2830 }
2831 }
2832
Craig Mautner299f9602015-01-26 09:47:33 -08002833 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002834 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2835 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002836 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002837 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2838 // Caller wants the home activity moved with it. To accomplish this,
2839 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002840 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002841 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002842 if (task.stack == null) {
2843 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2844 + task + " to front. Stack is null");
2845 return;
2846 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002847
2848 int stackId = task.stack.mStackId;
2849 if (task.mResizeable && options != null) {
2850 ActivityOptions opts = new ActivityOptions(options);
2851 if (opts.hasBounds()) {
2852 Rect bounds = opts.getBounds();
2853 task.updateOverrideConfiguration(bounds);
2854 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, false);
2855 stackId = task.getLaunchStackId();
2856 }
2857 }
2858
2859 if (stackId != task.stack.mStackId) {
2860 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, FORCE_FOCUS, reason);
2861 } else {
2862 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002863 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2864 reason);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002865 }
2866
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002867 if (DEBUG_STACK) Slog.d(TAG_STACK,
2868 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002869 }
2870
Craig Mautner967212c2013-04-13 21:10:58 -07002871 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002872 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002873 }
2874
2875 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002876 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2877 if (activityContainer != null) {
2878 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002879 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002880 if (!createStaticStackIfNeeded
2881 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2882 return null;
2883 }
2884 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002885 }
2886
Craig Mautner967212c2013-04-13 21:10:58 -07002887 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002888 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002889 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2890 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002891 }
2892 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002893 }
2894
Craig Mautner4a1cb222013-12-04 16:14:06 -08002895 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002896 ActivityRecord homeActivity = getHomeActivity();
2897 if (homeActivity != null) {
2898 return homeActivity.appToken;
2899 }
2900 return null;
2901 }
2902
2903 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002904 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002905 }
2906
2907 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002908 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2909 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2910 final TaskRecord task = tasks.get(taskNdx);
2911 if (task.isHomeTask()) {
2912 final ArrayList<ActivityRecord> activities = task.mActivities;
2913 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2914 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002915 if (r.isHomeActivity()
2916 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002917 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002918 }
2919 }
2920 }
2921 }
2922 return null;
2923 }
2924
Todd Kennedyca4d8422015-01-15 15:19:22 -08002925 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002927 ActivityContainer activityContainer =
2928 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002929 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002930 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2931 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002932 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002933 return activityContainer;
2934 }
2935
Craig Mautner34b73df2014-01-12 21:11:08 -08002936 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002937 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2938 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2939 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002940 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2941 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002942 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002943 }
2944 }
2945
Craig Mautner95da1082014-02-24 17:54:35 -08002946 void deleteActivityContainer(IActivityContainer container) {
2947 ActivityContainer activityContainer = (ActivityContainer)container;
2948 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002949 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2950 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002951 final int stackId = activityContainer.mStackId;
2952 mActivityContainers.remove(stackId);
2953 mWindowManager.removeStack(stackId);
2954 }
2955 }
2956
Wale Ogunwale60454db2015-01-23 16:05:07 -08002957 void resizeStackLocked(int stackId, Rect bounds) {
2958 final ActivityStack stack = getStack(stackId);
2959 if (stack == null) {
2960 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2961 return;
2962 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002963
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002964 ActivityRecord r = stack.topRunningActivityLocked(null);
2965 final boolean resizeTasks = r != null && r.task.mResizeable;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002966
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002967 mTmpBounds.clear();
2968 mTmpConfigs.clear();
2969 if (resizeTasks) {
2970 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2971 for (int i = tasks.size() - 1; i >= 0; i--) {
2972 TaskRecord task = tasks.get(i);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002973 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002974 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2975 mTmpBounds.put(task.taskId, task.mBounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002976 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002977 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002978 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, resizeTasks, mTmpConfigs,
2979 mTmpBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002980 if (stack.mStackId == DOCKED_STACK_ID) {
2981 // Dock stack funness...Yay!
2982 if (stack.mFullscreen) {
2983 // The dock stack went fullscreen which is kinda like dismissing it.
2984 // In this case we make all other static stacks fullscreen and move all
2985 // docked stack tasks to the fullscreen stack.
2986 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2987 if (i != DOCKED_STACK_ID) {
2988 resizeStackLocked(i, null);
2989 }
2990 }
2991
2992 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2993 final int count = tasks.size();
2994 for (int i = 0; i < count; i++) {
2995 moveTaskToStackLocked(tasks.get(i).taskId,
2996 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
2997 }
2998
2999 // stack shouldn't contain anymore activities, so nothing to resume.
3000 r = null;
3001 } else {
3002 // Docked stacks occupy a dedicated region on screen so the size of all other
3003 // static stacks need to be adjusted so they don't overlap with the docked stack.
3004 final int leftChange = stack.mBounds.left - bounds.left;
3005 final int rightChange = stack.mBounds.right - bounds.right;
3006 final int topChange = stack.mBounds.top - bounds.top;
3007 final int bottomChange = stack.mBounds.bottom - bounds.bottom;
3008
3009 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3010 if (i != DOCKED_STACK_ID) {
3011 ActivityStack otherStack = getStack(i);
3012 if (otherStack != null) {
3013 tempRect.set(otherStack.mBounds);
3014 // We adjust the opposing sides of the other stacks to
3015 // the side in the dock stack that changed.
3016 tempRect.left -= rightChange;
3017 tempRect.right -= leftChange;
3018 tempRect.top -= bottomChange;
3019 tempRect.bottom -= topChange;
3020 resizeStackLocked(i, tempRect);
3021 }
3022 }
3023 }
3024
3025 }
3026 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003027 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003028
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003029 if (r != null) {
3030 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
3031 // And we need to make sure at this point that all other activities
3032 // are made visible with the correct configuration.
3033 ensureActivitiesVisibleLocked(r, 0);
3034 if (!updated) {
3035 resumeTopActivitiesLocked(stack, null, null);
3036 }
3037 }
3038 }
3039
3040 void resizeTaskLocked(TaskRecord task, Rect bounds) {
3041 if (!task.mResizeable) {
3042 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3043 return;
3044 }
3045
3046 if (task.mBounds != null && task.mBounds.equals(bounds)) {
3047 // Nothing to do here...
3048 return;
3049 }
3050
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003051 if (!mWindowManager.isValidTaskId(task.taskId)) {
3052 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003053 // All we can do for now is update the bounds so it can be used when the task is
3054 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003055 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003056 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3057 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003058 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003059 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003060 return;
3061 }
3062
Wale Ogunwale040b4702015-08-06 18:10:50 -07003063 // The stack of a task is determined by its size (fullscreen vs non-fullscreen).
3064 // Place the task in the right stack if it isn't there already based on the requested
3065 // bounds.
3066 int stackId = task.stack.mStackId;
3067 if (bounds == null && stackId != FULLSCREEN_WORKSPACE_STACK_ID) {
3068 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003069 } else if (bounds != null
3070 && stackId != FREEFORM_WORKSPACE_STACK_ID && stackId != DOCKED_STACK_ID) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003071 stackId = FREEFORM_WORKSPACE_STACK_ID;
3072 }
3073 if (stackId != task.stack.mStackId) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003074 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, "resizeTask");
Wale Ogunwale040b4702015-08-06 18:10:50 -07003075 }
3076
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003077 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003078 // This variable holds information whether the configuration didn't change in a signficant
3079 // way and the activity was kept the way it was. If it's false, it means the activity had
3080 // to be relaunched due to configuration change.
3081 boolean kept = true;
3082 if (overrideConfig != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003083 ActivityRecord r = task.topRunningActivityLocked(null);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003084 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003085 final ActivityStack stack = task.stack;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003086 kept = stack.ensureActivityConfigurationLocked(r, 0);
3087 // All other activities must be made visible with their correct configuration.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003088 ensureActivitiesVisibleLocked(r, 0);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003089 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003090 resumeTopActivitiesLocked(stack, null, null);
3091 }
3092 }
3093 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003094 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003095 }
3096
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003097 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003098 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3099 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003100 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003101 }
3102
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003103 ActivityContainer activityContainer = new ActivityContainer(stackId);
3104 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003105 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003106 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003107 }
3108
3109 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003110 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003111 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3112 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003113 break;
3114 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003115 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003116 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003117 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003118 }
3119
Chong Zhang5dcb2752015-08-18 13:50:26 -07003120 /**
3121 * Restores a recent task to a stack
3122 * @param task The recent task to be restored.
3123 * @param stackId The stack to restore the task to (default launch stack will be used
3124 * if stackId is invalid).
3125 * @return true if the task has been restored successfully.
3126 */
3127 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3128 if (stackId == INVALID_STACK_ID) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07003129 stackId = mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003130 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003131 if (task.stack != null) {
3132 // Task has already been restored once. See if we need to do anything more
3133 if (task.stack.mStackId == stackId) {
3134 // Nothing else to do since it is already restored in the right stack.
3135 return true;
3136 }
3137 // Remove current stack association, so we can re-associate the task with the
3138 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003139 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003140 }
3141
3142 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003143 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003144
3145 if (stack == null) {
3146 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003147 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3148 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003149 return false;
3150 }
3151
3152 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003153 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3154 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003155 final ArrayList<ActivityRecord> activities = task.mActivities;
3156 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003157 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003158 }
3159 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003160 }
3161
Wale Ogunwale040b4702015-08-06 18:10:50 -07003162 /**
3163 * Moves the specified task record to the input stack id.
3164 * WARNING: This method performs an unchecked/raw move of the task and
3165 * can leave the system in an unstable state if used incorrectly.
3166 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3167 * to a stack.
3168 * @param task Task to move.
3169 * @param stackId Id of stack to move task to.
3170 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003171 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003172 * @param reason Reason the task is been moved.
3173 * @return The stack the task was moved to.
3174 */
3175 private ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003176 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3177 final ActivityRecord r = task.getTopActivity();
3178 final boolean wasFocused = isFrontStack(task.stack) && (topRunningActivityLocked() == r);
3179 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3180
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003181 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003182 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3183 if (task.stack != null) {
3184 task.stack.removeTask(task, reason, MOVING);
3185 }
3186 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003187
3188 // If the task had focus before (or we're requested to move focus),
3189 // move focus to the new stack.
3190 if (forceFocus || wasFocused) {
3191 // If the task owns the last resumed activity, transfer that together,
3192 // so that we don't resume the same activity again in the new stack.
3193 // Apps may depend on onResume()/onPause() being called in pairs.
3194 if (wasResumed) {
3195 stack.mResumedActivity = r;
3196 }
3197 // move the stack in which we are placing the task to the front.
3198 stack.moveToFront(reason);
3199 }
3200
Wale Ogunwale040b4702015-08-06 18:10:50 -07003201 return stack;
3202 }
3203
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003204 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003205 final TaskRecord task = anyTaskForIdLocked(taskId);
3206 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003207 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003208 return;
3209 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003210 final String reason = "moveTaskToStack";
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003211 final ActivityStack stack =
Chong Zhang02898352015-08-21 17:27:14 -07003212 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003213
3214 // Make sure the task has the appropriate bounds/size for the stack it is in.
3215 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003216 resizeTaskLocked(task, stack.mBounds);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003217 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3218 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
3219 resizeTaskLocked(task, task.mLastNonFullscreenBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003220 } else if (stackId == DOCKED_STACK_ID) {
3221 resizeTaskLocked(task, stack.mBounds);
3222 }
3223
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003224 // The task might have already been running and its visibility needs to be synchronized with
3225 // the visibility of the stack / windows.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003226 ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07003227 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003228 }
3229
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003230 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3231 final TaskRecord task = anyTaskForIdLocked(taskId);
3232 if (task == null) {
3233 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3234 return;
3235 }
3236 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003237 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003238 mWindowManager.positionTaskInStack(taskId, stackId, position);
3239 final boolean stackChanged = task.stack != null && task.stack != stack;
3240 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003241 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003242 }
3243 stack.positionTask(task, position, stackChanged);
3244 // The task might have already been running and its visibility needs to be synchronized with
3245 // the visibility of the stack / windows.
3246 stack.ensureActivitiesVisibleLocked(null, 0);
3247 resumeTopActivitiesLocked();
3248 }
3249
Craig Mautnerac6f8432013-07-17 13:24:59 -07003250 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003251 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003252 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3253 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003254 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3255 final ActivityStack stack = stacks.get(stackNdx);
3256 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003257 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003258 continue;
3259 }
3260 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003261 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3262 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003263 continue;
3264 }
3265 final ActivityRecord ar = stack.findTaskLocked(r);
3266 if (ar != null) {
3267 return ar;
3268 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003269 }
3270 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003271 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003272 return null;
3273 }
3274
3275 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003276 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3277 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003278 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3279 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3280 if (ar != null) {
3281 return ar;
3282 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003283 }
3284 }
3285 return null;
3286 }
3287
Craig Mautner8d341ef2013-03-26 09:03:27 -07003288 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003289 scheduleSleepTimeout();
3290 if (!mGoingToSleep.isHeld()) {
3291 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003292 if (mLaunchingActivity.isHeld()) {
3293 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3294 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003295 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003296 mLaunchingActivity.release();
3297 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003298 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003299 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003300 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003301 }
3302
3303 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003304 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003305
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003306 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003307 final long endTime = System.currentTimeMillis() + timeout;
3308 while (true) {
3309 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003310 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3311 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003312 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3313 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3314 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003315 }
3316 if (cantShutdown) {
3317 long timeRemaining = endTime - System.currentTimeMillis();
3318 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003319 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003320 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003321 } catch (InterruptedException e) {
3322 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003323 } else {
3324 Slog.w(TAG, "Activity manager shutdown timed out");
3325 timedout = true;
3326 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003327 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003328 } else {
3329 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003330 }
3331 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003332
3333 // Force checkReadyForSleep to complete.
3334 mSleepTimeout = true;
3335 checkReadyForSleepLocked();
3336
Craig Mautner8d341ef2013-03-26 09:03:27 -07003337 return timedout;
3338 }
3339
3340 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003341 removeSleepTimeouts();
3342 if (mGoingToSleep.isHeld()) {
3343 mGoingToSleep.release();
3344 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003345 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3346 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003347 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3348 final ActivityStack stack = stacks.get(stackNdx);
3349 stack.awakeFromSleepingLocked();
3350 if (isFrontStack(stack)) {
3351 resumeTopActivitiesLocked();
3352 }
Craig Mautner5314a402013-09-26 12:40:16 -07003353 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003354 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003355 mGoingToSleepActivities.clear();
3356 }
3357
3358 void activitySleptLocked(ActivityRecord r) {
3359 mGoingToSleepActivities.remove(r);
3360 checkReadyForSleepLocked();
3361 }
3362
3363 void checkReadyForSleepLocked() {
3364 if (!mService.isSleepingOrShuttingDown()) {
3365 // Do not care.
3366 return;
3367 }
3368
3369 if (!mSleepTimeout) {
3370 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003371 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3372 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003373 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3374 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3375 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003376 }
3377
3378 if (mStoppingActivities.size() > 0) {
3379 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003380 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003381 + mStoppingActivities.size() + " activities");
3382 scheduleIdleLocked();
3383 dontSleep = true;
3384 }
3385
3386 if (mGoingToSleepActivities.size() > 0) {
3387 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003388 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003389 + mGoingToSleepActivities.size() + " activities");
3390 dontSleep = true;
3391 }
3392
3393 if (dontSleep) {
3394 return;
3395 }
3396 }
3397
Craig Mautnere0a38842013-12-16 16:14:02 -08003398 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3399 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003400 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3401 stacks.get(stackNdx).goToSleep();
3402 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003403 }
3404
3405 removeSleepTimeouts();
3406
3407 if (mGoingToSleep.isHeld()) {
3408 mGoingToSleep.release();
3409 }
3410 if (mService.mShuttingDown) {
3411 mService.notifyAll();
3412 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003413 }
3414
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003415 boolean reportResumedActivityLocked(ActivityRecord r) {
3416 final ActivityStack stack = r.task.stack;
3417 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003418 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003419 }
3420 if (allResumedActivitiesComplete()) {
3421 ensureActivitiesVisibleLocked(null, 0);
3422 mWindowManager.executeAppTransition();
3423 return true;
3424 }
3425 return false;
3426 }
3427
Craig Mautner8d341ef2013-03-26 09:03:27 -07003428 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003429 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3430 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003431 int stackNdx = stacks.size() - 1;
3432 while (stackNdx >= 0) {
3433 stacks.get(stackNdx).handleAppCrashLocked(app);
3434 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003435 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003436 }
3437 }
3438
Jose Lima4b6c6692014-08-12 17:41:12 -07003439 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003440 final ActivityStack stack = r.task.stack;
3441 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003442 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3443 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003444 return false;
3445 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003446 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003447 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3448 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003449
3450 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003451 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003452 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003453 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003454 return true;
3455 }
3456
3457 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003458 if (visible && top.fullscreen) {
3459 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003460 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3461 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3462 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3463 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003464 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003465 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3466 // Only the activity set as currently visible behind should actively reset its
3467 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003468 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3469 "requestVisibleBehind: returning visible=" + visible
3470 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3471 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003472 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003473 }
3474
Jose Lima4b6c6692014-08-12 17:41:12 -07003475 stack.setVisibleBehindActivity(visible ? r : null);
3476 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003477 // Make the activity immediately above r opaque.
3478 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3479 if (next != null) {
3480 mService.convertFromTranslucent(next.appToken);
3481 }
3482 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003483 if (top.app != null && top.app.thread != null) {
3484 // Notify the top app of the change.
3485 try {
3486 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3487 } catch (RemoteException e) {
3488 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003489 }
3490 return true;
3491 }
3492
Craig Mautnerbb742462014-07-07 15:28:55 -07003493 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3494 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3495 r.mLaunchTaskBehind = false;
3496 final TaskRecord task = r.task;
3497 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003498 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003499 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003500 mWindowManager.setAppVisibility(r.appToken, false);
3501 }
3502
3503 void scheduleLaunchTaskBehindComplete(IBinder token) {
3504 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3505 }
3506
Craig Mautnerde4ef022013-04-07 19:01:33 -07003507 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003508 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003509 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3510 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003511 final int topStackNdx = stacks.size() - 1;
3512 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3513 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003514 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003515 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003516 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003517 }
3518
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003519 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3520 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3521 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3522 final int topStackNdx = stacks.size() - 1;
3523 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3524 final ActivityStack stack = stacks.get(stackNdx);
3525 stack.clearOtherAppTimeTrackers(except);
3526 }
3527 }
3528 }
3529
Craig Mautner8d341ef2013-03-26 09:03:27 -07003530 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003531 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3532 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003533 final int numStacks = stacks.size();
3534 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3535 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003536 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003537 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003538 }
3539 }
3540
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003541 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3542 // Examine all activities currently running in the process.
3543 TaskRecord firstTask = null;
3544 // Tasks is non-null only if two or more tasks are found.
3545 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003546 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3547 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003548 ActivityRecord r = app.activities.get(i);
3549 // First, if we find an activity that is in the process of being destroyed,
3550 // then we just aren't going to do anything for now; we want things to settle
3551 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003552 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003553 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003554 return;
3555 }
3556 // Don't consider any activies that are currently not in a state where they
3557 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003558 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3559 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003560 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003561 continue;
3562 }
3563 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003564 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003565 + " from " + r);
3566 if (firstTask == null) {
3567 firstTask = r.task;
3568 } else if (firstTask != r.task) {
3569 if (tasks == null) {
3570 tasks = new ArraySet<>();
3571 tasks.add(firstTask);
3572 }
3573 tasks.add(r.task);
3574 }
3575 }
3576 }
3577 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003578 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003579 return;
3580 }
3581 // If we have activities in multiple tasks that are in a position to be destroyed,
3582 // let's iterate through the tasks and release the oldest one.
3583 final int numDisplays = mActivityDisplays.size();
3584 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3585 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3586 // Step through all stacks starting from behind, to hit the oldest things first.
3587 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3588 final ActivityStack stack = stacks.get(stackNdx);
3589 // Try to release activities in this stack; if we manage to, we are done.
3590 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3591 return;
3592 }
3593 }
3594 }
3595 }
3596
Amith Yamasani37a40c22015-06-17 13:25:42 -07003597 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003598 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003599 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003600 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003601
Craig Mautner858d8a62013-04-23 17:08:34 -07003602 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003603 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3604 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003605 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003606 final ActivityStack stack = stacks.get(stackNdx);
3607 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003608 TaskRecord task = stack.topTask();
3609 if (task != null) {
3610 mWindowManager.moveTaskToTop(task.taskId);
3611 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003612 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003613 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003614
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003615 ActivityStack stack = getStack(restoreStackId);
3616 if (stack == null) {
3617 stack = mHomeStack;
3618 }
3619 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003620 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003621 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003622 TaskRecord task = stack.topTask();
3623 if (task != null) {
3624 mWindowManager.moveTaskToTop(task.taskId);
3625 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003626 } else {
3627 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003628 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003629 }
Craig Mautner93529a42013-10-04 15:03:13 -07003630 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003631 }
3632
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003633 /**
3634 * Add background users to send boot completed events to.
3635 * @param userId The user being started in the background
3636 * @param uss The state object for the user.
3637 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003638 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003639 mStartingBackgroundUsers.add(uss);
3640 }
3641
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003642 /** Checks whether the userid is a profile of the current user. */
3643 boolean isCurrentProfileLocked(int userId) {
3644 if (userId == mCurrentUser) return true;
3645 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3646 if (mService.mCurrentProfileIds[i] == userId) return true;
3647 }
3648 return false;
3649 }
3650
Chong Zhang45c25ce2015-08-10 22:18:26 -07003651 /** Checks whether the activity should be shown for current user. */
3652 boolean okToShowLocked(ActivityRecord r) {
3653 return r != null && (isCurrentProfileLocked(r.userId)
3654 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3655 }
3656
Craig Mautnerde4ef022013-04-07 19:01:33 -07003657 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003658 ArrayList<ActivityRecord> stops = null;
3659
3660 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003661 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3662 ActivityRecord s = mStoppingActivities.get(activityNdx);
3663 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003664 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003665 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3666 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003667 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003668 if (s.finishing) {
3669 // If this activity is finishing, it is sitting on top of
3670 // everyone else but we now know it is no longer needed...
3671 // so get rid of it. Otherwise, we need to go through the
3672 // normal flow and hide it once we determine that it is
3673 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003674 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003675 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003676 }
3677 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003678 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003679 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003680 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003681 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003682 }
3683 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003684 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003685 }
3686 }
3687
3688 return stops;
3689 }
3690
Craig Mautnercf910b02013-04-23 11:23:27 -07003691 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003692 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3693 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3694 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3695 final ActivityStack stack = stacks.get(stackNdx);
3696 final ActivityRecord r = stack.topRunningActivityLocked(null);
3697 final ActivityState state = r == null ? DESTROYED : r.state;
3698 if (isFrontStack(stack)) {
3699 if (r == null) Slog.e(TAG,
3700 "validateTop...: null top activity, stack=" + stack);
3701 else {
3702 final ActivityRecord pausing = stack.mPausingActivity;
3703 if (pausing != null && pausing == r) Slog.e(TAG,
3704 "validateTop...: top stack has pausing activity r=" + r
3705 + " state=" + state);
3706 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3707 "validateTop...: activity in front not resumed r=" + r
3708 + " state=" + state);
3709 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003710 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003711 final ActivityRecord resumed = stack.mResumedActivity;
3712 if (resumed != null && resumed == r) Slog.e(TAG,
3713 "validateTop...: back stack has resumed activity r=" + r
3714 + " state=" + state);
3715 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3716 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003717 }
3718 }
3719 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003720 }
3721
Craig Mautnere0570202015-05-13 13:06:11 -07003722 private String lockTaskModeToString() {
3723 switch (mLockTaskModeState) {
3724 case LOCK_TASK_MODE_LOCKED:
3725 return "LOCKED";
3726 case LOCK_TASK_MODE_PINNED:
3727 return "PINNED";
3728 case LOCK_TASK_MODE_NONE:
3729 return "NONE";
3730 default: return "unknown=" + mLockTaskModeState;
3731 }
3732 }
3733
Craig Mautner27084302013-03-25 08:05:25 -07003734 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003735 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003736 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003737 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3738 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3739 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003740 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003741 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3742 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3743 if (packages.size() > 0) {
3744 pw.println(" mLockTaskPackages (userId:packages)=");
3745 for (int i = 0; i < packages.size(); ++i) {
3746 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3747 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3748 }
3749 }
3750 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003751 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003752
Craig Mautner20e72272013-04-01 13:45:53 -07003753 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003754 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003755 }
3756
Dianne Hackborn390517b2013-05-30 15:03:32 -07003757 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3758 boolean needSep, String prefix) {
3759 if (activity != null) {
3760 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3761 if (needSep) {
3762 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003763 }
3764 pw.print(prefix);
3765 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003766 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003767 }
3768 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003769 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003770 }
3771
Craig Mautner8d341ef2013-03-26 09:03:27 -07003772 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3773 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003774 boolean printed = false;
3775 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003776 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3777 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003778 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003779 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003780 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003781 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003782 final ActivityStack stack = stacks.get(stackNdx);
3783 StringBuilder stackHeader = new StringBuilder(128);
3784 stackHeader.append(" Stack #");
3785 stackHeader.append(stack.mStackId);
3786 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003787 stackHeader.append("\n");
3788 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3789 stackHeader.append("\n");
3790 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003791 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3792 needSep, stackHeader.toString());
3793 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3794 !dumpAll, false, dumpPackage, true,
3795 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003796
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 needSep = printed;
3798 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3799 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003800 if (pr) {
3801 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003802 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003803 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003804 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3805 " mResumedActivity: ");
3806 if (pr) {
3807 printed = true;
3808 needSep = false;
3809 }
3810 if (dumpAll) {
3811 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3812 " mLastPausedActivity: ");
3813 if (pr) {
3814 printed = true;
3815 needSep = true;
3816 }
3817 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3818 needSep, " mLastNoHistoryActivity: ");
3819 }
3820 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003821 }
3822 }
3823
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003824 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3825 false, dumpPackage, true, " Activities waiting to finish:", null);
3826 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3827 false, dumpPackage, true, " Activities waiting to stop:", null);
3828 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3829 false, dumpPackage, true, " Activities waiting for another to become visible:",
3830 null);
3831 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3832 false, dumpPackage, true, " Activities waiting to sleep:", null);
3833 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3834 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003835
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003836 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003837 }
3838
Dianne Hackborn390517b2013-05-30 15:03:32 -07003839 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003840 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003841 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003842 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003843 String innerPrefix = null;
3844 String[] args = null;
3845 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003846 for (int i=list.size()-1; i>=0; i--) {
3847 final ActivityRecord r = list.get(i);
3848 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3849 continue;
3850 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003851 if (innerPrefix == null) {
3852 innerPrefix = prefix + " ";
3853 args = new String[0];
3854 }
3855 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003856 final boolean full = !brief && (complete || !r.isInHistory());
3857 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003858 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003859 needNL = false;
3860 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003861 if (header1 != null) {
3862 pw.println(header1);
3863 header1 = null;
3864 }
3865 if (header2 != null) {
3866 pw.println(header2);
3867 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003868 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003869 if (lastTask != r.task) {
3870 lastTask = r.task;
3871 pw.print(prefix);
3872 pw.print(full ? "* " : " ");
3873 pw.println(lastTask);
3874 if (full) {
3875 lastTask.dump(pw, prefix + " ");
3876 } else if (complete) {
3877 // Complete + brief == give a summary. Isn't that obvious?!?
3878 if (lastTask.intent != null) {
3879 pw.print(prefix); pw.print(" ");
3880 pw.println(lastTask.intent.toInsecureStringWithClip());
3881 }
3882 }
3883 }
3884 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3885 pw.print(" #"); pw.print(i); pw.print(": ");
3886 pw.println(r);
3887 if (full) {
3888 r.dump(pw, innerPrefix);
3889 } else if (complete) {
3890 // Complete + brief == give a summary. Isn't that obvious?!?
3891 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3892 if (r.app != null) {
3893 pw.print(innerPrefix); pw.println(r.app);
3894 }
3895 }
3896 if (client && r.app != null && r.app.thread != null) {
3897 // flush anything that is already in the PrintWriter since the thread is going
3898 // to write to the file descriptor directly
3899 pw.flush();
3900 try {
3901 TransferPipe tp = new TransferPipe();
3902 try {
3903 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3904 r.appToken, innerPrefix, args);
3905 // Short timeout, since blocking here can
3906 // deadlock with the application.
3907 tp.go(fd, 2000);
3908 } finally {
3909 tp.kill();
3910 }
3911 } catch (IOException e) {
3912 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3913 } catch (RemoteException e) {
3914 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3915 }
3916 needNL = true;
3917 }
3918 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003919 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003920 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003921
Craig Mautnerf3333272013-04-22 10:55:53 -07003922 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003923 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3924 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003925 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3926 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003927 }
3928
3929 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003930 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003931 }
3932
3933 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003934 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3935 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003936 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3937 }
3938
Craig Mautner05d29032013-05-03 13:40:13 -07003939 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003940 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3941 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3942 }
Craig Mautner05d29032013-05-03 13:40:13 -07003943 }
3944
Craig Mautner0eea92c2013-05-16 13:35:39 -07003945 void removeSleepTimeouts() {
3946 mSleepTimeout = false;
3947 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3948 }
3949
3950 final void scheduleSleepTimeout() {
3951 removeSleepTimeouts();
3952 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3953 }
3954
Craig Mautner4a1cb222013-12-04 16:14:06 -08003955 @Override
3956 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003957 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003958 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3959 }
3960
3961 @Override
3962 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003963 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003964 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3965 }
3966
3967 @Override
3968 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003969 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003970 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3971 }
3972
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003973 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003974 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003975 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003976 newDisplay = mActivityDisplays.get(displayId) == null;
3977 if (newDisplay) {
3978 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003979 if (activityDisplay.mDisplay == null) {
3980 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3981 return;
3982 }
Craig Mautner4504de52013-12-20 09:06:56 -08003983 mActivityDisplays.put(displayId, activityDisplay);
3984 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003985 }
Craig Mautner4504de52013-12-20 09:06:56 -08003986 if (newDisplay) {
3987 mWindowManager.onDisplayAdded(displayId);
3988 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003989 }
3990
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003991 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003992 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003993 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3994 if (activityDisplay != null) {
3995 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003996 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003997 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003998 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003999 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004000 }
4001 }
4002 mWindowManager.onDisplayRemoved(displayId);
4003 }
4004
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004005 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004006 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004007 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4008 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 // TODO: Update the bounds.
4010 }
4011 }
4012 mWindowManager.onDisplayChanged(displayId);
4013 }
4014
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004015 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004016 StackInfo info = new StackInfo();
4017 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4018 info.displayId = Display.DEFAULT_DISPLAY;
4019 info.stackId = stack.mStackId;
4020
4021 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4022 final int numTasks = tasks.size();
4023 int[] taskIds = new int[numTasks];
4024 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004025 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004026 for (int i = 0; i < numTasks; ++i) {
4027 final TaskRecord task = tasks.get(i);
4028 taskIds[i] = task.taskId;
4029 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4030 : task.realActivity != null ? task.realActivity.flattenToString()
4031 : task.getTopActivity() != null ? task.getTopActivity().packageName
4032 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004033 taskBounds[i] = new Rect();
4034 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004035 }
4036 info.taskIds = taskIds;
4037 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004038 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004039 return info;
4040 }
4041
4042 StackInfo getStackInfoLocked(int stackId) {
4043 ActivityStack stack = getStack(stackId);
4044 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004045 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004046 }
4047 return null;
4048 }
4049
4050 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004051 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004052 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4053 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004054 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004055 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004056 }
4057 }
4058 return list;
4059 }
4060
Craig Mautner15df08a2015-04-01 12:17:18 -07004061 TaskRecord getLockedTaskLocked() {
4062 final int top = mLockTaskModeTasks.size() - 1;
4063 if (top >= 0) {
4064 return mLockTaskModeTasks.get(top);
4065 }
4066 return null;
4067 }
4068
4069 boolean isLockedTask(TaskRecord task) {
4070 return mLockTaskModeTasks.contains(task);
4071 }
4072
4073 boolean isLastLockedTask(TaskRecord task) {
4074 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4075 }
4076
4077 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004078 if (!mLockTaskModeTasks.remove(task)) {
4079 return;
4080 }
4081 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4082 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004083 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004084 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4085 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004086 final Message lockTaskMsg = Message.obtain();
4087 lockTaskMsg.arg1 = task.userId;
4088 lockTaskMsg.what = LOCK_TASK_END_MSG;
4089 mHandler.sendMessage(lockTaskMsg);
4090 }
4091 }
4092
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004093 void showLockTaskToast() {
4094 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004095 }
4096
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004097 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4098 if (mLockTaskModeTasks.contains(task)) {
4099 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4100 }
4101 }
4102
Craig Mautner432f64e2015-05-20 14:59:57 -07004103 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4104 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004105 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004106 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004107 final TaskRecord lockedTask = getLockedTaskLocked();
4108 if (lockedTask != null) {
4109 removeLockedTaskLocked(lockedTask);
4110 if (!mLockTaskModeTasks.isEmpty()) {
4111 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004112 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4113 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004114 lockedTask.performClearTaskLocked();
4115 resumeTopActivitiesLocked();
4116 return;
4117 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004118 }
Craig Mautnere0570202015-05-13 13:06:11 -07004119 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4120 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004121 return;
4122 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004123
4124 // Should have already been checked, but do it again.
4125 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004126 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4127 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004128 return;
4129 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004130 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004131 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004132 return;
4133 }
4134
4135 if (mLockTaskModeTasks.isEmpty()) {
4136 // First locktask.
4137 final Message lockTaskMsg = Message.obtain();
4138 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4139 lockTaskMsg.arg1 = task.userId;
4140 lockTaskMsg.what = LOCK_TASK_START_MSG;
4141 lockTaskMsg.arg2 = lockTaskModeState;
4142 mHandler.sendMessage(lockTaskMsg);
4143 }
4144 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004145 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4146 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004147 mLockTaskModeTasks.remove(task);
4148 mLockTaskModeTasks.add(task);
4149
4150 if (task.mLockTaskUid == -1) {
4151 task.mLockTaskUid = task.mCallingUid;
4152 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004153
4154 if (andResume) {
4155 findTaskToMoveToFrontLocked(task, 0, null, reason);
4156 resumeTopActivitiesLocked();
4157 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004158 }
4159
4160 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004161 return isLockTaskModeViolation(task, false);
4162 }
4163
4164 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4165 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004166 return false;
4167 }
4168 final int lockTaskAuth = task.mLockTaskAuth;
4169 switch (lockTaskAuth) {
4170 case LOCK_TASK_AUTH_DONT_LOCK:
4171 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004172 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004173 case LOCK_TASK_AUTH_LAUNCHABLE:
4174 case LOCK_TASK_AUTH_WHITELISTED:
4175 return false;
4176 case LOCK_TASK_AUTH_PINNABLE:
4177 // Pinnable tasks can't be launched on top of locktask tasks.
4178 return !mLockTaskModeTasks.isEmpty();
4179 default:
4180 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4181 return true;
4182 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004183 }
4184
Craig Mautner15df08a2015-04-01 12:17:18 -07004185 void onLockTaskPackagesUpdatedLocked() {
4186 boolean didSomething = false;
4187 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4188 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004189 final boolean wasWhitelisted =
4190 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4191 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004192 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004193 final boolean isWhitelisted =
4194 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4195 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4196 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004197 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004198 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4199 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004200 removeLockedTaskLocked(lockedTask);
4201 lockedTask.performClearTaskLocked();
4202 didSomething = true;
4203 }
4204 }
4205 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4206 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4207 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4208 final ActivityStack stack = stacks.get(stackNdx);
4209 stack.onLockTaskPackagesUpdatedLocked();
4210 }
4211 }
Craig Mautnere0570202015-05-13 13:06:11 -07004212 final ActivityRecord r = topRunningActivityLocked();
4213 final TaskRecord task = r != null ? r.task : null;
4214 if (mLockTaskModeTasks.isEmpty() && task != null
4215 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4216 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004217 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4218 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4219 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4220 false);
4221 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004222 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004223 if (didSomething) {
4224 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004225 }
4226 }
4227
Benjamin Franz43261142015-02-11 15:59:44 +00004228 int getLockTaskModeState() {
4229 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004230 }
4231
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004232 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004233
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004234 public ActivityStackSupervisorHandler(Looper looper) {
4235 super(looper);
4236 }
4237
Craig Mautnerf3333272013-04-22 10:55:53 -07004238 void activityIdleInternal(ActivityRecord r) {
4239 synchronized (mService) {
4240 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4241 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004242 }
4243
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004244 @Override
4245 public void handleMessage(Message msg) {
4246 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004247 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004248 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4249 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004250 if (mService.mDidDexOpt) {
4251 mService.mDidDexOpt = false;
4252 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4253 nmsg.obj = msg.obj;
4254 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4255 return;
4256 }
4257 // We don't at this point know if the activity is fullscreen,
4258 // so we need to be conservative and assume it isn't.
4259 activityIdleInternal((ActivityRecord)msg.obj);
4260 } break;
4261 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004262 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004263 activityIdleInternal((ActivityRecord)msg.obj);
4264 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004265 case RESUME_TOP_ACTIVITY_MSG: {
4266 synchronized (mService) {
4267 resumeTopActivitiesLocked();
4268 }
4269 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004270 case SLEEP_TIMEOUT_MSG: {
4271 synchronized (mService) {
4272 if (mService.isSleepingOrShuttingDown()) {
4273 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4274 mSleepTimeout = true;
4275 checkReadyForSleepLocked();
4276 }
4277 }
4278 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004279 case LAUNCH_TIMEOUT_MSG: {
4280 if (mService.mDidDexOpt) {
4281 mService.mDidDexOpt = false;
4282 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4283 return;
4284 }
4285 synchronized (mService) {
4286 if (mLaunchingActivity.isHeld()) {
4287 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4288 if (VALIDATE_WAKE_LOCK_CALLER
4289 && Binder.getCallingUid() != Process.myUid()) {
4290 throw new IllegalStateException("Calling must be system uid");
4291 }
4292 mLaunchingActivity.release();
4293 }
4294 }
4295 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004296 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004297 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004298 } break;
4299 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004300 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004301 } break;
4302 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004303 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004304 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004305 case CONTAINER_CALLBACK_VISIBILITY: {
4306 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004307 final IActivityContainerCallback callback = container.mCallback;
4308 if (callback != null) {
4309 try {
4310 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4311 } catch (RemoteException e) {
4312 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004313 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004314 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004315 case LOCK_TASK_START_MSG: {
4316 // When lock task starts, we disable the status bars.
4317 try {
Jason Monk62515be2014-05-21 16:06:19 -04004318 if (mLockTaskNotify == null) {
4319 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004320 }
Jason Monk62515be2014-05-21 16:06:19 -04004321 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004322 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004323 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004324 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004325 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004326 flags = StatusBarManager.DISABLE_MASK
4327 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004328 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004329 flags = StatusBarManager.DISABLE_MASK
4330 & (~StatusBarManager.DISABLE_BACK)
4331 & (~StatusBarManager.DISABLE_HOME)
4332 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004333 }
4334 getStatusBarService().disable(flags, mToken,
4335 mService.mContext.getPackageName());
4336 }
4337 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004338 if (getDevicePolicyManager() != null) {
4339 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004340 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004341 }
justinzhang5286d3f2014-05-12 17:06:01 -04004342 } catch (RemoteException ex) {
4343 throw new RuntimeException(ex);
4344 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004345 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004346 case LOCK_TASK_END_MSG: {
4347 // When lock task ends, we enable the status bars.
4348 try {
Jason Monk62515be2014-05-21 16:06:19 -04004349 if (getStatusBarService() != null) {
4350 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4351 mService.mContext.getPackageName());
4352 }
4353 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004354 if (getDevicePolicyManager() != null) {
4355 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4356 msg.arg1);
4357 }
Jason Monk62515be2014-05-21 16:06:19 -04004358 if (mLockTaskNotify == null) {
4359 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4360 }
4361 mLockTaskNotify.show(false);
4362 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004363 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004364 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004365 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004366 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004367 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004368 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004369 new LockPatternUtils(mService.mContext)
4370 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004371 }
4372 } catch (SettingNotFoundException e) {
4373 // No setting, don't lock.
4374 }
justinzhang5286d3f2014-05-12 17:06:01 -04004375 } catch (RemoteException ex) {
4376 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004377 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004378 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004379 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004380 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004381 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4382 if (mLockTaskNotify == null) {
4383 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4384 }
4385 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4386 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004387 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4388 final ActivityContainer container = (ActivityContainer) msg.obj;
4389 final IActivityContainerCallback callback = container.mCallback;
4390 if (callback != null) {
4391 try {
4392 callback.onAllActivitiesComplete(container.asBinder());
4393 } catch (RemoteException e) {
4394 }
4395 }
4396 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004397 case LAUNCH_TASK_BEHIND_COMPLETE: {
4398 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004399 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004400 if (r != null) {
4401 handleLaunchTaskBehindCompleteLocked(r);
4402 }
4403 }
4404 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004405 }
4406 }
4407 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004408
Ying Wangb081a592014-04-22 15:20:16 -07004409 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004410 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004411 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004412 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004413 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004414 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004415 ActivityRecord mParentActivity = null;
4416 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004417
Craig Mautnere3a00d72014-04-16 08:31:19 -07004418 boolean mVisible = true;
4419
Craig Mautner4a1cb222013-12-04 16:14:06 -08004420 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004421 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004422
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004423 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4424 final static int CONTAINER_STATE_NO_SURFACE = 1;
4425 final static int CONTAINER_STATE_FINISHING = 2;
4426 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4427
4428 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004429 synchronized (mService) {
4430 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004431 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004432 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004433 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004434 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004435 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004436
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004437 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004438 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004439 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004440 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004441 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004442 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004443 }
4444
4445 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004446 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004447 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004448 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4449 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004450 return;
4451 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004452 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004453 }
4454 }
4455
4456 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004457 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004458 synchronized (mService) {
4459 if (mActivityDisplay != null) {
4460 return mActivityDisplay.mDisplayId;
4461 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004462 }
4463 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004464 }
4465
Jeff Brownca9bc702014-02-11 14:32:56 -08004466 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004467 public int getStackId() {
4468 synchronized (mService) {
4469 return mStackId;
4470 }
4471 }
4472
4473 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004474 public boolean injectEvent(InputEvent event) {
4475 final long origId = Binder.clearCallingIdentity();
4476 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004477 synchronized (mService) {
4478 if (mActivityDisplay != null) {
4479 return mInputManagerInternal.injectInputEvent(event,
4480 mActivityDisplay.mDisplayId,
4481 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4482 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004483 }
4484 return false;
4485 } finally {
4486 Binder.restoreCallingIdentity(origId);
4487 }
4488 }
4489
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004490 @Override
4491 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004492 synchronized (mService) {
4493 if (mContainerState == CONTAINER_STATE_FINISHING) {
4494 return;
4495 }
4496 mContainerState = CONTAINER_STATE_FINISHING;
4497
Craig Mautnerd163e752014-06-13 17:18:47 -07004498 long origId = Binder.clearCallingIdentity();
4499 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004500 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004501 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004502 } finally {
4503 Binder.restoreCallingIdentity(origId);
4504 }
4505 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004506 }
4507
Craig Mautner60257702014-09-17 15:02:33 -07004508 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004509 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004510 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004511 if (mActivityDisplay != null) {
4512 mActivityDisplay.detachActivitiesLocked(mStack);
4513 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004514 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004515 }
4516 }
4517
4518 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004519 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004520 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004521 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004522 Binder.getCallingUid(), mCurrentUser, false,
4523 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004524
Craig Mautnere0a38842013-12-16 16:14:02 -08004525 // TODO: Switch to user app stacks here.
4526 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004527 final Uri data = intent.getData();
4528 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4529 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004530 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004531 checkEmbeddedAllowedInner(userId, intent, mimeType);
4532
4533 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4534 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004535 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004536 }
4537
4538 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004539 public final int startActivityIntentSender(IIntentSender intentSender)
4540 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004541 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4542
4543 if (!(intentSender instanceof PendingIntentRecord)) {
4544 throw new IllegalArgumentException("Bad PendingIntent object");
4545 }
4546
Craig Mautnerb9168362015-02-26 20:40:19 -08004547 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004548 Binder.getCallingUid(), mCurrentUser, false,
4549 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004550
4551 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4552 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4553 pendingIntent.key.requestResolvedType);
4554
4555 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4556 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4557 }
4558
4559 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004560 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004561 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004562 throw new SecurityException(
4563 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4564 }
4565 }
4566
Craig Mautnerdf88d732014-01-27 09:21:32 -08004567 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004568 public IBinder asBinder() {
4569 return this;
4570 }
4571
Craig Mautner4504de52013-12-20 09:06:56 -08004572 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004573 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004574 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004575 }
4576
Craig Mautner4a1cb222013-12-04 16:14:06 -08004577 ActivityStackSupervisor getOuter() {
4578 return ActivityStackSupervisor.this;
4579 }
4580
Craig Mautnerd163e752014-06-13 17:18:47 -07004581 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004582 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004583 }
4584
4585 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004586 synchronized (mService) {
4587 if (mActivityDisplay != null) {
4588 mActivityDisplay.getBounds(outBounds);
4589 } else {
4590 outBounds.set(0, 0);
4591 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004592 }
4593 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004594
Craig Mautner6985bad2014-04-21 15:22:06 -07004595 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004596 void setVisible(boolean visible) {
4597 if (mVisible != visible) {
4598 mVisible = visible;
4599 if (mCallback != null) {
4600 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4601 0 /* unused */, this).sendToTarget();
4602 }
4603 }
4604 }
4605
Craig Mautner6985bad2014-04-21 15:22:06 -07004606 void setDrawn() {
4607 }
4608
Craig Mautner1b4bf852014-05-26 15:06:32 -07004609 // You can always start a new task on a regular ActivityStack.
4610 boolean isEligibleForNewTasks() {
4611 return true;
4612 }
4613
Craig Mautnerd163e752014-06-13 17:18:47 -07004614 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004615 detachLocked();
4616 deleteActivityContainer(this);
4617 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004618 }
4619
Craig Mautner34b73df2014-01-12 21:11:08 -08004620 @Override
4621 public String toString() {
4622 return mIdString + (mActivityDisplay == null ? "N" : "A");
4623 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004624 }
4625
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004626 private class VirtualActivityContainer extends ActivityContainer {
4627 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004628 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004629
4630 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4631 super(getNextStackId());
4632 mParentActivity = parent;
4633 mCallback = callback;
4634 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004635 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004636 }
4637
4638 @Override
4639 public void setSurface(Surface surface, int width, int height, int density) {
4640 super.setSurface(surface, width, height, density);
4641
4642 synchronized (mService) {
4643 final long origId = Binder.clearCallingIdentity();
4644 try {
4645 setSurfaceLocked(surface, width, height, density);
4646 } finally {
4647 Binder.restoreCallingIdentity(origId);
4648 }
4649 }
4650 }
4651
4652 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4653 if (mContainerState == CONTAINER_STATE_FINISHING) {
4654 return;
4655 }
4656 VirtualActivityDisplay virtualActivityDisplay =
4657 (VirtualActivityDisplay) mActivityDisplay;
4658 if (virtualActivityDisplay == null) {
4659 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004660 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004661 mActivityDisplay = virtualActivityDisplay;
4662 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004663 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004664 }
4665
4666 if (mSurface != null) {
4667 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004668 }
4669
Craig Mautner6985bad2014-04-21 15:22:06 -07004670 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004671 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004672 mStack.resumeTopActivityLocked(null);
4673 } else {
4674 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004675 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004676 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004677 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004678 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004679 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004680
Craig Mautnerd163e752014-06-13 17:18:47 -07004681 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004682
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004683 if (DEBUG_STACK) Slog.d(TAG_STACK,
4684 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004685 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004686
Craig Mautner6985bad2014-04-21 15:22:06 -07004687 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004688 boolean isAttachedLocked() {
4689 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004690 }
4691
4692 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004693 void setDrawn() {
4694 synchronized (mService) {
4695 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004696 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004697 }
4698 }
4699
Craig Mautner1b4bf852014-05-26 15:06:32 -07004700 // Never start a new task on an ActivityView if it isn't explicitly specified.
4701 @Override
4702 boolean isEligibleForNewTasks() {
4703 return false;
4704 }
4705
Craig Mautnerd163e752014-06-13 17:18:47 -07004706 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004707 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004708 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4709 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4710 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4711 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4712 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004713 }
4714 }
4715
Craig Mautner4a1cb222013-12-04 16:14:06 -08004716 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4717 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004718 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004719 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004720 int mDisplayId;
4721 Display mDisplay;
4722 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004723
Craig Mautner4a1cb222013-12-04 16:14:06 -08004724 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4725 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004726 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004727
Jose Lima4b6c6692014-08-12 17:41:12 -07004728 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004729
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004730 ActivityDisplay() {
4731 }
Craig Mautner4504de52013-12-20 09:06:56 -08004732
Craig Mautner1a70a162014-09-13 12:09:31 -07004733 // After instantiation, check that mDisplay is not null before using this. The alternative
4734 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004735 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004736 final Display display = mDisplayManager.getDisplay(displayId);
4737 if (display == null) {
4738 return;
4739 }
4740 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004741 }
4742
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004743 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004744 mDisplay = display;
4745 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004746 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004747 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004748
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004749 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004750 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004751 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4752 + " onTop=" + onTop);
4753 if (onTop) {
4754 mStacks.add(stack);
4755 } else {
4756 mStacks.add(0, stack);
4757 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004758 }
4759
Craig Mautnere0a38842013-12-16 16:14:02 -08004760 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004761 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004762 + " from displayId=" + mDisplayId);
4763 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004764 }
4765
4766 void getBounds(Point bounds) {
4767 mDisplay.getDisplayInfo(mDisplayInfo);
4768 bounds.x = mDisplayInfo.appWidth;
4769 bounds.y = mDisplayInfo.appHeight;
4770 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004771
Jose Lima4b6c6692014-08-12 17:41:12 -07004772 void setVisibleBehindActivity(ActivityRecord r) {
4773 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004774 }
4775
Jose Lima4b6c6692014-08-12 17:41:12 -07004776 boolean hasVisibleBehindActivity() {
4777 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004778 }
4779
Craig Mautner34b73df2014-01-12 21:11:08 -08004780 @Override
4781 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004782 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4783 }
4784 }
4785
4786 class VirtualActivityDisplay extends ActivityDisplay {
4787 VirtualDisplay mVirtualDisplay;
4788
Craig Mautner6985bad2014-04-21 15:22:06 -07004789 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004790 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004791 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4792 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4793 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4794 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004795
4796 init(mVirtualDisplay.getDisplay());
4797
4798 mWindowManager.handleDisplayAdded(mDisplayId);
4799 }
4800
4801 void setSurface(Surface surface) {
4802 if (mVirtualDisplay != null) {
4803 mVirtualDisplay.setSurface(surface);
4804 }
4805 }
4806
4807 @Override
4808 void detachActivitiesLocked(ActivityStack stack) {
4809 super.detachActivitiesLocked(stack);
4810 if (mVirtualDisplay != null) {
4811 mVirtualDisplay.release();
4812 mVirtualDisplay = null;
4813 }
4814 }
4815
4816 @Override
4817 public String toString() {
4818 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004819 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004820 }
Jose Lima58e66d62014-05-27 20:00:27 -07004821
4822 private boolean isLeanbackOnlyDevice() {
4823 boolean onLeanbackOnly = false;
4824 try {
4825 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4826 PackageManager.FEATURE_LEANBACK_ONLY);
4827 } catch (RemoteException e) {
4828 // noop
4829 }
4830
4831 return onLeanbackOnly;
4832 }
Craig Mautner27084302013-03-25 08:05:25 -07004833}