blob: c7cdb2e46d62cc06f12b608f8dcf48e3cad056bd [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 Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
22import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
23import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
24import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
25import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
26import static android.app.ActivityManager.StackId.HOME_STACK_ID;
27import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
28import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
29import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070030import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
31import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070032import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
33import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070034import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070035import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070036import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Chong Zhangb15758a2015-11-17 12:12:03 -080037import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080038import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070039import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070040import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
41import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
42import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070043import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070044import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
45import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010046import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070047import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
48import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070049
Svetoslav7008b512015-06-24 18:47:07 -070050import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070051import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070053import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080054import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.app.ActivityOptions;
56import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070057import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080058import android.app.IActivityContainer;
59import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070060import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.app.IApplicationThread;
Clara Bayarrif7fea162015-10-22 16:09:52 +010062import android.app.KeyguardManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070064import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070065import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070066import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070067import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040068import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040069import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070070import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070071import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070072import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070073import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070074import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070075import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070077import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070078import android.content.pm.PackageManager;
79import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010080import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080082import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080083import android.hardware.display.DisplayManager;
84import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080085import android.hardware.display.DisplayManagerGlobal;
86import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080087import android.hardware.input.InputManager;
88import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080089import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070090import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070091import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070092import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070093import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070094import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070095import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070096import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070097import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070098import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070099import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400101import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700103import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700104import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700105import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100106import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700107import android.os.WorkSource;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100108import android.os.storage.StorageManager;
Svetoslav7008b512015-06-24 18:47:07 -0700109import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400110import android.provider.Settings;
111import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700112import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700113import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700114import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700115import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700116import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800117import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118
Craig Mautner4a1cb222013-12-04 16:14:06 -0800119import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800120import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800121import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800122import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800123import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800124import android.widget.Toast;
125
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700127import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800128import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700129import com.android.internal.os.TransferPipe;
Chong Zhangb15758a2015-11-17 12:12:03 -0800130import com.android.internal.R;
justinzhang5286d3f2014-05-12 17:06:01 -0400131import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700132import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400133import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800134import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700135import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700136import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700137
Craig Mautner8d341ef2013-03-26 09:03:27 -0700138import java.io.FileDescriptor;
139import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700140import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700141import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700142import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700143import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700144import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700145
Craig Mautner4a1cb222013-12-04 16:14:06 -0800146public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800147 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700148 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700149 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700150 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700152 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700153 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700154 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700155 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
156 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700157 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700158 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700159 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
160 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700161 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700162 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800163
Craig Mautnerf3333272013-04-22 10:55:53 -0700164 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700165 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700166
Craig Mautner0eea92c2013-05-16 13:35:39 -0700167 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700168 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700169
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700170 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700171 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700172
Craig Mautner05d29032013-05-03 13:40:13 -0700173 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
174 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
175 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700176 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700177 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800178 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
179 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
180 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700181 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400182 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
183 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700184 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700185 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700186 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800187
Wale Ogunwale040b4702015-08-06 18:10:50 -0700188 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700189
Jason Monk62515be2014-05-21 16:06:19 -0400190 private static final String LOCK_TASK_TAG = "Lock-to-App";
191
Wale Ogunwale040b4702015-08-06 18:10:50 -0700192 // Used to indicate if an object (e.g. stack) that we are trying to get
193 // should be created if it doesn't exist already.
194 private static final boolean CREATE_IF_NEEDED = true;
195
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700196 // Used to indicate that windows of activities should be preserved during the resize.
197 static final boolean PRESERVE_WINDOWS = true;
198
Wale Ogunwale040b4702015-08-06 18:10:50 -0700199 // Used to indicate if an object (e.g. task) should be moved/created
200 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700201 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700202
203 // Used to indicate that an objects (e.g. task) removal from its container
204 // (e.g. stack) is due to it moving to another container.
205 static final boolean MOVING = true;
206
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700207 // Force the focus to change to the stack we are moving a task to..
208 static final boolean FORCE_FOCUS = true;
209
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700210 // Restore task from the saved recents if it can't be found in any live stack.
211 static final boolean RESTORE_FROM_RECENTS = true;
212
Svetoslav7008b512015-06-24 18:47:07 -0700213 // Activity actions an app cannot start if it uses a permission which is not granted.
214 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
215 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700216
Svetoslav7008b512015-06-24 18:47:07 -0700217 static {
218 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
219 Manifest.permission.CAMERA);
220 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
221 Manifest.permission.CAMERA);
222 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
223 Manifest.permission.CALL_PHONE);
224 }
225
Svet Ganov99b60432015-06-27 13:15:22 -0700226 /** Action restriction: launching the activity is not restricted. */
227 private static final int ACTIVITY_RESTRICTION_NONE = 0;
228 /** Action restriction: launching the activity is restricted by a permission. */
229 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
230 /** Action restriction: launching the activity is restricted by an app op. */
231 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700232
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700233 // The height/width divide used when fitting a task within a bounds with method
234 // {@link #fitWithinBounds}.
235 // We always want the task to to be visible in the bounds without affecting its size when
236 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
237 // the input bounds right or bottom side minus the width or height divided by this value.
238 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
239
justinzhang5286d3f2014-05-12 17:06:01 -0400240 /** Status Bar Service **/
241 private IBinder mToken = new Binder();
242 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400243 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400244
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700245 // For debugging to make sure the caller when acquiring/releasing our
246 // wake lock is the system process.
247 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700248
Craig Mautner27084302013-03-25 08:05:25 -0700249 final ActivityManagerService mService;
250
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800251 private final RecentTasks mRecentTasks;
252
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700253 final ActivityStackSupervisorHandler mHandler;
254
255 /** Short cut */
256 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800257 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700258
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700259 /** Counter for next free stack ID to use for dynamic activity stacks. */
260 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700261
262 /** Task identifier that activities are currently being started in. Incremented each time a
263 * new task is created. */
264 private int mCurTaskId = 0;
265
Craig Mautner2420ead2013-04-01 17:13:20 -0700266 /** The current user */
267 private int mCurrentUser;
268
Craig Mautnere0a38842013-12-16 16:14:02 -0800269 /** The stack containing the launcher app. Assumed to always be attached to
270 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700271 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700272
Craig Mautnere0a38842013-12-16 16:14:02 -0800273 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700274 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700275
Craig Mautner4a1cb222013-12-04 16:14:06 -0800276 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
277 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800278 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800279 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700280
281 /** List of activities that are waiting for a new activity to become visible before completing
282 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800283 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700284
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700285 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800286 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700287
288 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800289 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700290
Craig Mautnerde4ef022013-04-07 19:01:33 -0700291 /** List of activities that are ready to be stopped, but waiting for the next activity to
292 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800293 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700294
Craig Mautnerf3333272013-04-22 10:55:53 -0700295 /** List of activities that are ready to be finished, but waiting for the previous activity to
296 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800297 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700298
Craig Mautner0eea92c2013-05-16 13:35:39 -0700299 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800300 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700301
Craig Mautnerf3333272013-04-22 10:55:53 -0700302 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700303 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700304
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700305 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700306 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700307
Craig Mautnerde4ef022013-04-07 19:01:33 -0700308 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
309 * is being brought in front of us. */
310 boolean mUserLeaving = false;
311
Craig Mautner0eea92c2013-05-16 13:35:39 -0700312 /** Set when we have taken too long waiting to go to sleep. */
313 boolean mSleepTimeout = false;
314
315 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700316 * We don't want to allow the device to go to sleep while in the process
317 * of launching an activity. This is primarily to allow alarm intent
318 * receivers to launch an activity and get that to run before the device
319 * goes back to sleep.
320 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700321 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700322
323 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700324 * Set when the system is going to sleep, until we have
325 * successfully paused the current activity and released our wake lock.
326 * At that point the system is allowed to actually sleep.
327 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700328 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700329
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700330 /** Stack id of the front stack when user switched, indexed by userId. */
331 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700332
Craig Mautner4504de52013-12-20 09:06:56 -0800333 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800334 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700335 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800336
337 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700338 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800339
Jeff Brownca9bc702014-02-11 14:32:56 -0800340 InputManagerInternal mInputManagerInternal;
341
Craig Mautner15df08a2015-04-01 12:17:18 -0700342 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
343 * may be finished until there is only one entry left. If this is empty the system is not
344 * in lockTask mode. */
345 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000346 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700347 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
348 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000349 */
350 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400351 /**
352 * Notifies the user when entering/exiting lock-task.
353 */
354 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800355
Craig Mautner15df08a2015-04-01 12:17:18 -0700356 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700357
Craig Mautner42d04db2014-11-06 12:13:23 -0800358 /** Used to keep resumeTopActivityLocked() from being entered recursively */
359 boolean inResumeTopActivity;
360
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700361 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700362 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700363 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700364
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700365 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
366 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
367
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700368 // The default minimal size that will be used if the activity doesn't specify its minimal size.
369 // It will be calculated when the default display gets added.
370 private int mDefaultMinimalSizeOfResizeableTask = -1;
371
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700372 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
373 private boolean mTaskLayersChanged = true;
374
Craig Mautneree36c772014-07-16 14:56:05 -0700375 /**
376 * Description of a request to start a new activity, which has been held
377 * due to app switches being disabled.
378 */
379 static class PendingActivityLaunch {
380 final ActivityRecord r;
381 final ActivityRecord sourceRecord;
382 final int startFlags;
383 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700384 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700385
386 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700387 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700388 r = _r;
389 sourceRecord = _sourceRecord;
390 startFlags = _startFlags;
391 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700392 callerApp = _callerApp;
393 }
394
395 void sendErrorResult(String message) {
396 try {
397 if (callerApp.thread != null) {
398 callerApp.thread.scheduleCrash(message);
399 }
400 } catch (RemoteException e) {
401 Slog.e(TAG, "Exception scheduling crash of failed "
402 + "activity launcher sourceRecord=" + sourceRecord, e);
403 }
Craig Mautneree36c772014-07-16 14:56:05 -0700404 }
405 }
406
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800407 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700408 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800409 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700410 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
411 }
412
413 /**
414 * At the time when the constructor runs, the power manager has not yet been
415 * initialized. So we initialize our wakelocks afterwards.
416 */
417 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800418 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700419 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700420 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700421 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700422 }
423
justinzhang5286d3f2014-05-12 17:06:01 -0400424 // This function returns a IStatusBarService. The value is from ServiceManager.
425 // getService and is cached.
426 private IStatusBarService getStatusBarService() {
427 synchronized (mService) {
428 if (mStatusBarService == null) {
429 mStatusBarService = IStatusBarService.Stub.asInterface(
430 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
431 if (mStatusBarService == null) {
432 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
433 }
434 }
435 return mStatusBarService;
436 }
437 }
438
Jason Monk35c62a42014-06-17 10:24:47 -0400439 private IDevicePolicyManager getDevicePolicyManager() {
440 synchronized (mService) {
441 if (mDevicePolicyManager == null) {
442 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
443 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
444 if (mDevicePolicyManager == null) {
445 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
446 }
447 }
448 return mDevicePolicyManager;
449 }
450 }
451
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700452 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800453 synchronized (mService) {
454 mWindowManager = wm;
455
456 mDisplayManager =
457 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
458 mDisplayManager.registerDisplayListener(this, null);
459
460 Display[] displays = mDisplayManager.getDisplays();
461 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
462 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800463 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700464 if (activityDisplay.mDisplay == null) {
465 throw new IllegalStateException("Default Display does not exist");
466 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800467 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700468 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800469 }
470
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700471 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800472 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800473
474 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800475 }
Craig Mautner27084302013-03-25 08:05:25 -0700476 }
477
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200478 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700479 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200480 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700481 }
482
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700483 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800484 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700485 }
486
Craig Mautnerde4ef022013-04-07 19:01:33 -0700487 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800488 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700489 }
490
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700491 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700492 if (stack == null) {
493 return false;
494 }
495
Craig Mautnerdf88d732014-01-27 09:21:32 -0800496 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
497 if (parent != null) {
498 stack = parent.task.stack;
499 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800500 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700501 }
502
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700503 /** The top most stack. */
504 boolean isFrontStack(ActivityStack stack) {
505 if (stack == null) {
506 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800507 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700508
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700509 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
510 if (parent != null) {
511 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800512 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700513 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
514 }
515
516 void setFocusStack(String reason, ActivityStack focusedStack) {
517 mLastFocusedStack = mFocusedStack;
518 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800519
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700520 EventLogTags.writeAmFocusedStack(
521 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
522 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800523
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800524 if (mService.mBooting || !mService.mBooted) {
525 final ActivityRecord r = topRunningActivityLocked();
526 if (r != null && r.idle) {
527 checkFinishBootingLocked();
528 }
529 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700530 }
531
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700532 void moveHomeStackToFront(String reason) {
533 mHomeStack.moveToFront(reason);
534 }
535
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700536 /** Returns true if the focus activity was adjusted to the home stack top activity. */
537 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700538 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
539 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700540 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700541 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700542
Craig Mautner84984fa2014-06-19 11:19:20 -0700543 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700544
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700545 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700546 if (top == null) {
547 return false;
548 }
549 mService.setFocusedActivityLocked(top, reason);
550 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700551 }
552
Craig Mautner299f9602015-01-26 09:47:33 -0800553 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700554 if (!mService.mBooting && !mService.mBooted) {
555 // Not ready yet!
556 return false;
557 }
558
Craig Mautner84984fa2014-06-19 11:19:20 -0700559 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
560 mWindowManager.showRecentApps();
561 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700562 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700563
Craig Mautner84984fa2014-06-19 11:19:20 -0700564 if (prev != null) {
565 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
566 }
567
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700568 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
569 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800570 // Only resume home activity if isn't finishing.
571 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800572 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700573 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700574 }
Craig Mautner299f9602015-01-26 09:47:33 -0800575 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700576 }
577
Craig Mautner8d341ef2013-03-26 09:03:27 -0700578 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700579 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700580 }
581
582 /**
583 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
584 * @param id Id of the task we would like returned.
585 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
586 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700587 * @param stackId The stack to restore the task to (default launch stack will be used if
588 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700589 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700590 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800591 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800593 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
595 ActivityStack stack = stacks.get(stackNdx);
596 TaskRecord task = stack.taskForIdLocked(id);
597 if (task != null) {
598 return task;
599 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700600 }
601 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800602
603 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700604 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800605 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800606 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700607 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800608 return null;
609 }
610
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700611 if (!restoreFromRecents) {
612 return task;
613 }
614
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700615 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700616 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
617 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800618 return null;
619 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700620 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800621 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700622 }
623
Craig Mautner6170f732013-04-02 13:05:23 -0700624 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800625 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800627 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800628 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
629 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
630 if (r != null) {
631 return r;
632 }
Craig Mautner6170f732013-04-02 13:05:23 -0700633 }
634 }
635 return null;
636 }
637
Craig Mautneref73ee12014-04-23 11:45:37 -0700638 void setNextTaskId(int taskId) {
639 if (taskId > mCurTaskId) {
640 mCurTaskId = taskId;
641 }
642 }
643
Craig Mautner8d341ef2013-03-26 09:03:27 -0700644 int getNextTaskId() {
645 do {
646 mCurTaskId++;
647 if (mCurTaskId <= 0) {
648 mCurTaskId = 1;
649 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700650 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700651 return mCurTaskId;
652 }
653
Craig Mautnerde4ef022013-04-07 19:01:33 -0700654 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800655 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700656 if (stack == null) {
657 return null;
658 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700659 ActivityRecord resumedActivity = stack.mResumedActivity;
660 if (resumedActivity == null || resumedActivity.app == null) {
661 resumedActivity = stack.mPausingActivity;
662 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700663 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700664 }
665 }
666 return resumedActivity;
667 }
668
Dianne Hackbornff072722014-09-24 10:56:28 -0700669 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700670 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800672 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
673 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
675 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700676 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677 continue;
678 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700679 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800680 if (hr != null) {
681 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
682 && processName.equals(hr.processName)) {
683 try {
George Mount2c92c972014-03-20 09:38:23 -0700684 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 didSomething = true;
686 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700687 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800688 Slog.w(TAG, "Exception in new application when starting activity "
689 + hr.intent.getComponent().flattenToShortString(), e);
690 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700691 }
Craig Mautner20e72272013-04-01 13:45:53 -0700692 }
Craig Mautner20e72272013-04-01 13:45:53 -0700693 }
694 }
695 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700696 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700697 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700698 }
Craig Mautner20e72272013-04-01 13:45:53 -0700699 return didSomething;
700 }
701
702 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
704 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800705 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
706 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700707 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800708 continue;
709 }
710 final ActivityRecord resumedActivity = stack.mResumedActivity;
711 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700712 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800713 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800714 return false;
715 }
Craig Mautner20e72272013-04-01 13:45:53 -0700716 }
717 }
718 return true;
719 }
720
Craig Mautnerde4ef022013-04-07 19:01:33 -0700721 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
723 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700726 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700728 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800729 return false;
730 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700731 }
732 }
733 }
734 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700735 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800736 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
737 mLastFocusedStack + " to=" + mFocusedStack);
738 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700739 return true;
740 }
741
742 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800743 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800744 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
745 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800746 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
747 final ActivityStack stack = stacks.get(stackNdx);
748 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800749 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700750 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800751 return false;
752 }
753 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800754 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700755 }
756 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800757 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700758 }
759
Craig Mautner2acc3892013-09-23 10:28:14 -0700760 /**
761 * Pause all activities in either all of the stacks or just the back stacks.
762 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700763 * @return true if any activity was paused as a result of this call.
764 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700765 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700766 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
768 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
770 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700771 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700772 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700774 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
775 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700777 }
778 }
779 return someActivityPaused;
780 }
781
Craig Mautnerde4ef022013-04-07 19:01:33 -0700782 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700783 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800784 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
785 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800786 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
787 final ActivityStack stack = stacks.get(stackNdx);
788 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700789 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700791 Slog.d(TAG_STATES,
792 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800793 pausing = false;
794 } else {
795 return false;
796 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700797 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700798 }
799 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700800 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700801 }
802
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700803 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
804 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500805 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800806 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
807 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
808 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
809 final ActivityStack stack = stacks.get(stackNdx);
810 if (stack.mResumedActivity != null &&
811 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700812 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800813 }
814 }
815 }
816 }
817
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700818 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700819 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700820 }
821
822 void sendWaitingVisibleReportLocked(ActivityRecord r) {
823 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700824 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700825 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700826 if (w.who == null) {
827 changed = true;
828 w.timeout = false;
829 if (r != null) {
830 w.who = new ComponentName(r.info.packageName, r.info.name);
831 }
832 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
833 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700834 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700835 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700836 if (changed) {
837 mService.notifyAll();
838 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700839 }
840
841 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
842 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700843 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700844 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700845 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700846 if (w.who == null) {
847 changed = true;
848 w.timeout = timeout;
849 if (r != null) {
850 w.who = new ComponentName(r.info.packageName, r.info.name);
851 }
852 w.thisTime = thisTime;
853 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700854 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700855 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700856 if (changed) {
857 mService.notifyAll();
858 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700859 }
860
Craig Mautner29219d92013-04-16 20:19:12 -0700861 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800862 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700863 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700864 if (r != null) {
865 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700866 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700867
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800869 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
871 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700872 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700873 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700874 if (r != null) {
875 return r;
876 }
877 }
878 }
879 return null;
880 }
881
Dianne Hackborn09233282014-04-30 11:33:59 -0700882 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700883 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800884 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
885 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800886 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800888 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
890 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700891 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800892 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700893 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700894 }
895 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700896
897 // The lists are already sorted from most recent to oldest. Just pull the most recent off
898 // each list and add it to list. Stop when all lists are empty or maxNum reached.
899 while (maxNum > 0) {
900 long mostRecentActiveTime = Long.MIN_VALUE;
901 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800902 final int numTaskLists = runningTaskLists.size();
903 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
904 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700905 if (!stackTaskList.isEmpty()) {
906 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
907 if (lastActiveTime > mostRecentActiveTime) {
908 mostRecentActiveTime = lastActiveTime;
909 selectedStackList = stackTaskList;
910 }
911 }
912 }
913 if (selectedStackList != null) {
914 list.add(selectedStackList.remove(0));
915 --maxNum;
916 } else {
917 break;
918 }
919 }
Craig Mautner20e72272013-04-01 13:45:53 -0700920 }
921
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700923 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924 // Collect information about the target of the Intent.
925 ActivityInfo aInfo;
926 try {
927 ResolveInfo rInfo =
928 AppGlobals.getPackageManager().resolveIntent(
929 intent, resolvedType,
930 PackageManager.MATCH_DEFAULT_ONLY
931 | ActivityManagerService.STOCK_PM_FLAGS, userId);
932 aInfo = rInfo != null ? rInfo.activityInfo : null;
933 } catch (RemoteException e) {
934 aInfo = null;
935 }
936
937 if (aInfo != null) {
938 // Store the found target back into the intent, because now that
939 // we have it we never want to do this again. For example, if the
940 // user navigates back to this point in the history, we should
941 // always restart the exact same activity.
942 intent.setComponent(new ComponentName(
943 aInfo.applicationInfo.packageName, aInfo.name));
944
945 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700946 if (!aInfo.processName.equals("system")) {
947 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700948 mService.setDebugApp(aInfo.processName, true, false);
949 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700950
Man Caocfa78b22015-06-11 20:14:34 -0700951 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
952 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
953 }
954
955 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700956 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700957 }
958 }
959 }
960 return aInfo;
961 }
962
Craig Mautner299f9602015-01-26 09:47:33 -0800963 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
964 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700965 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
966 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
967 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
968 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700969 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
970 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700971 null /* outActivity */, null /* container */, null /* inTask */);
972 if (inResumeTopActivity) {
973 // If we are in resume section already, home activity will be initialized, but not
974 // resumed (to avoid recursive resume) and will stay that way until something pokes it
975 // again. We need to schedule another resume.
976 scheduleResumeTopActivities();
977 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700978 }
979
Craig Mautner23ac33b2013-04-01 16:26:35 -0700980 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700981 String callingPackage, Intent intent, String resolvedType,
982 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700983 IBinder resultTo, String resultWho, int requestCode, int startFlags,
984 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Chong Zhang280d3322015-11-03 17:27:26 -0800985 Bundle bOptions, boolean ignoreTargetSecurity, int userId,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700986 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700987 // Refuse possible leaked file descriptors
988 if (intent != null && intent.hasFileDescriptors()) {
989 throw new IllegalArgumentException("File descriptors passed in Intent");
990 }
991 boolean componentSpecified = intent.getComponent() != null;
992
993 // Don't modify the client's object!
994 intent = new Intent(intent);
995
996 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700997 ActivityInfo aInfo =
998 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700999
Chong Zhang280d3322015-11-03 17:27:26 -08001000 ActivityOptions options = ActivityOptions.fromBundle(bOptions);
Craig Mautnere0a38842013-12-16 16:14:02 -08001001 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001002 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001003 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001004 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001005 // Cannot start a child activity if the parent is not resumed.
1006 return ActivityManager.START_CANCELED;
1007 }
Dianne Hackborn95465202014-09-15 16:21:55 -07001008 final int realCallingPid = Binder.getCallingPid();
1009 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -07001010 int callingPid;
1011 if (callingUid >= 0) {
1012 callingPid = -1;
1013 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001014 callingPid = realCallingPid;
1015 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001016 } else {
1017 callingPid = callingUid = -1;
1018 }
1019
Craig Mautnere0a38842013-12-16 16:14:02 -08001020 final ActivityStack stack;
1021 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001022 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001023 } else {
1024 stack = container.mStack;
1025 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001026 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001027 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001028 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001029
1030 final long origId = Binder.clearCallingIdentity();
1031
1032 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001033 (aInfo.applicationInfo.privateFlags
1034 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001035 // This may be a heavy-weight process! Check to see if we already
1036 // have another, different heavy-weight process running.
1037 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1038 if (mService.mHeavyWeightProcess != null &&
1039 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1040 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001041 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001042 if (caller != null) {
1043 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1044 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001045 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001046 } else {
1047 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001048 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001049 + intent.toString());
1050 ActivityOptions.abort(options);
1051 return ActivityManager.START_PERMISSION_DENIED;
1052 }
1053 }
1054
1055 IIntentSender target = mService.getIntentSenderLocked(
1056 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001057 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001058 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1059 | PendingIntent.FLAG_ONE_SHOT, null);
1060
1061 Intent newIntent = new Intent();
1062 if (requestCode >= 0) {
1063 // Caller is requesting a result.
1064 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1065 }
1066 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1067 new IntentSender(target));
1068 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1069 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1070 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1071 hist.packageName);
1072 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1073 hist.task.taskId);
1074 }
1075 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1076 aInfo.packageName);
1077 newIntent.setFlags(intent.getFlags());
1078 newIntent.setClassName("android",
1079 HeavyWeightSwitcherActivity.class.getName());
1080 intent = newIntent;
1081 resolvedType = null;
1082 caller = null;
1083 callingUid = Binder.getCallingUid();
1084 callingPid = Binder.getCallingPid();
1085 componentSpecified = true;
1086 try {
1087 ResolveInfo rInfo =
1088 AppGlobals.getPackageManager().resolveIntent(
1089 intent, null,
1090 PackageManager.MATCH_DEFAULT_ONLY
1091 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1092 aInfo = rInfo != null ? rInfo.activityInfo : null;
1093 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1094 } catch (RemoteException e) {
1095 aInfo = null;
1096 }
1097 }
1098 }
1099 }
1100
Dianne Hackborn91097de2014-04-04 18:02:06 -07001101 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1102 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001103 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001104 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001105 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001106
Craig Mautner85c11a82014-07-17 12:38:18 -07001107 Binder.restoreCallingIdentity(origId);
1108
Craig Mautnerde4ef022013-04-07 19:01:33 -07001109 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001110 // If the caller also wants to switch to a new configuration,
1111 // do so now. This allows a clean switch, as we are waiting
1112 // for the current activity to pause (so we will not destroy
1113 // it), and have not yet started the next activity.
1114 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1115 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001116 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001117 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001118 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001119 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001120 }
1121
Craig Mautner23ac33b2013-04-01 16:26:35 -07001122 if (outResult != null) {
1123 outResult.result = res;
1124 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001125 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001126 do {
1127 try {
1128 mService.wait();
1129 } catch (InterruptedException e) {
1130 }
1131 } while (!outResult.timeout && outResult.who == null);
1132 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001133 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001134 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001135 outResult.timeout = false;
1136 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1137 outResult.totalTime = 0;
1138 outResult.thisTime = 0;
1139 } else {
1140 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001141 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001142 do {
1143 try {
1144 mService.wait();
1145 } catch (InterruptedException e) {
1146 }
1147 } while (!outResult.timeout && outResult.who == null);
1148 }
1149 }
1150 }
1151
1152 return res;
1153 }
1154 }
1155
1156 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1157 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
Chong Zhang280d3322015-11-03 17:27:26 -08001158 Bundle bOptions, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001159 if (intents == null) {
1160 throw new NullPointerException("intents is null");
1161 }
1162 if (resolvedTypes == null) {
1163 throw new NullPointerException("resolvedTypes is null");
1164 }
1165 if (intents.length != resolvedTypes.length) {
1166 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1167 }
1168
Craig Mautner23ac33b2013-04-01 16:26:35 -07001169
1170 int callingPid;
1171 if (callingUid >= 0) {
1172 callingPid = -1;
1173 } else if (caller == null) {
1174 callingPid = Binder.getCallingPid();
1175 callingUid = Binder.getCallingUid();
1176 } else {
1177 callingPid = callingUid = -1;
1178 }
1179 final long origId = Binder.clearCallingIdentity();
1180 try {
1181 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001182 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001183 for (int i=0; i<intents.length; i++) {
1184 Intent intent = intents[i];
1185 if (intent == null) {
1186 continue;
1187 }
1188
1189 // Refuse possible leaked file descriptors
1190 if (intent != null && intent.hasFileDescriptors()) {
1191 throw new IllegalArgumentException("File descriptors passed in Intent");
1192 }
1193
1194 boolean componentSpecified = intent.getComponent() != null;
1195
1196 // Don't modify the client's object!
1197 intent = new Intent(intent);
1198
1199 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001200 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001201 // TODO: New, check if this is correct
1202 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1203
1204 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001205 (aInfo.applicationInfo.privateFlags
1206 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001207 throw new IllegalArgumentException(
1208 "FLAG_CANT_SAVE_STATE not supported here");
1209 }
1210
Chong Zhang280d3322015-11-03 17:27:26 -08001211 ActivityOptions options = ActivityOptions.fromBundle(
1212 i == intents.length - 1 ? bOptions : null);
Craig Mautner6170f732013-04-02 13:05:23 -07001213 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001214 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1215 callingPackage, callingPid, callingUid,
Chong Zhang280d3322015-11-03 17:27:26 -08001216 0, options, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001217 if (res < 0) {
1218 return res;
1219 }
1220
1221 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1222 }
1223 }
1224 } finally {
1225 Binder.restoreCallingIdentity(origId);
1226 }
1227
1228 return ActivityManager.START_SUCCESS;
1229 }
1230
Craig Mautner2420ead2013-04-01 17:13:20 -07001231 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001232 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 throws RemoteException {
1234
Craig Mautner2568c3a2015-03-26 14:22:34 -07001235 if (andResume) {
1236 r.startFreezingScreenLocked(app, 0);
1237 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001238
Craig Mautner2568c3a2015-03-26 14:22:34 -07001239 // schedule launch ticks to collect information about slow apps.
1240 r.startLaunchTickingLocked();
1241 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001242
1243 // Have the window manager re-evaluate the orientation of
1244 // the screen based on the new activity order. Note that
1245 // as a result of this, it can call back into the activity
1246 // manager with a new orientation. We don't care about that,
1247 // because the activity is not currently running so we are
1248 // just restarting it anyway.
1249 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001250 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 mService.mConfiguration,
1252 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001253 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 }
1255
1256 r.app = app;
1257 app.waitingToKill = null;
1258 r.launchCount++;
1259 r.lastLaunchTime = SystemClock.uptimeMillis();
1260
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001261 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001262
1263 int idx = app.activities.indexOf(r);
1264 if (idx < 0) {
1265 app.activities.add(r);
1266 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001267 mService.updateLruProcessLocked(app, true, null);
1268 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001269
Craig Mautner15df08a2015-04-01 12:17:18 -07001270 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001271 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1272 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001273 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001274 }
1275
1276 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001277 try {
1278 if (app.thread == null) {
1279 throw new RemoteException();
1280 }
1281 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001282 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001283 if (andResume) {
1284 results = r.results;
1285 newIntents = r.newIntents;
1286 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001287 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1288 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1289 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001290 if (andResume) {
1291 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1292 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001293 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001294 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001295 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001296 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001297 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001298 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001299 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001300 r.sleeping = false;
1301 r.forceNewConfig = false;
1302 mService.showAskCompatModeDialogLocked(r);
1303 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001304 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001305 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1306 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1307 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001308 final String profileFile = mService.mProfileFile;
1309 if (profileFile != null) {
1310 ParcelFileDescriptor profileFd = mService.mProfileFd;
1311 if (profileFd != null) {
1312 try {
1313 profileFd = profileFd.dup();
1314 } catch (IOException e) {
1315 if (profileFd != null) {
1316 try {
1317 profileFd.close();
1318 } catch (IOException o) {
1319 }
1320 profileFd = null;
1321 }
1322 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001323 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001324
1325 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1326 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001327 }
1328 }
1329 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001330
Craig Mautner2568c3a2015-03-26 14:22:34 -07001331 if (andResume) {
1332 app.hasShownUi = true;
1333 app.pendingUiClean = true;
1334 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001335 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001336 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001337 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001338 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001339 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001340 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001341
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001342 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001343 // This may be a heavy-weight process! Note that the package
1344 // manager will ensure that only activity can run in the main
1345 // process of the .apk, which is the only thing that will be
1346 // considered heavy-weight.
1347 if (app.processName.equals(app.info.packageName)) {
1348 if (mService.mHeavyWeightProcess != null
1349 && mService.mHeavyWeightProcess != app) {
1350 Slog.w(TAG, "Starting new heavy weight process " + app
1351 + " when already running "
1352 + mService.mHeavyWeightProcess);
1353 }
1354 mService.mHeavyWeightProcess = app;
1355 Message msg = mService.mHandler.obtainMessage(
1356 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1357 msg.obj = r;
1358 mService.mHandler.sendMessage(msg);
1359 }
1360 }
1361
1362 } catch (RemoteException e) {
1363 if (r.launchFailed) {
1364 // This is the second time we failed -- finish activity
1365 // and give up.
1366 Slog.e(TAG, "Second failure launching "
1367 + r.intent.getComponent().flattenToShortString()
1368 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001369 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001370 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1371 "2nd-crash", false);
1372 return false;
1373 }
1374
1375 // This is the first time we failed -- restart process and
1376 // retry.
1377 app.activities.remove(r);
1378 throw e;
1379 }
1380
1381 r.launchFailed = false;
1382 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001383 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001384 }
1385
1386 if (andResume) {
1387 // As part of the process of launching, ActivityThread also performs
1388 // a resume.
1389 stack.minimalResumeActivityLocked(r);
1390 } else {
1391 // This activity is not starting in the resumed state... which
1392 // should look like we asked it to pause+stop (but remain visible),
1393 // and it has done so and reported back the current icicle and
1394 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001395 if (DEBUG_STATES) Slog.v(TAG_STATES,
1396 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001397 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001398 r.stopped = true;
1399 }
1400
1401 // Launch the new version setup screen if needed. We do this -after-
1402 // launching the initial activity (that is, home), so that it can have
1403 // a chance to initialize itself while in the background, making the
1404 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001405 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001406 mService.startSetupActivityLocked();
1407 }
1408
Dianne Hackborn465fa392014-09-14 14:21:18 -07001409 // Update any services we are bound to that might care about whether
1410 // their client may have activities.
1411 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1412
Craig Mautner2420ead2013-04-01 17:13:20 -07001413 return true;
1414 }
1415
Craig Mautnere79d42682013-04-01 19:01:53 -07001416 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001417 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001418 // Is this activity's application already running?
1419 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001420 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001421
1422 r.task.stack.setLaunchTime(r);
1423
1424 if (app != null && app.thread != null) {
1425 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001426 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1427 || !"android".equals(r.info.packageName)) {
1428 // Don't add this if it is a platform component that is marked
1429 // to run in multiple processes, because this is actually
1430 // part of the framework so doesn't make sense to track as a
1431 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001432 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1433 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001434 }
George Mount2c92c972014-03-20 09:38:23 -07001435 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001436 return;
1437 } catch (RemoteException e) {
1438 Slog.w(TAG, "Exception when starting activity "
1439 + r.intent.getComponent().flattenToShortString(), e);
1440 }
1441
1442 // If a dead object exception was thrown -- fall through to
1443 // restart the application.
1444 }
1445
1446 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001447 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001448 }
1449
Craig Mautner6170f732013-04-02 13:05:23 -07001450 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001451 Intent intent, String resolvedType, ActivityInfo aInfo,
1452 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1453 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001454 int callingPid, int callingUid, String callingPackage,
Chong Zhang280d3322015-11-03 17:27:26 -08001455 int realCallingPid, int realCallingUid, int startFlags, ActivityOptions options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001456 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1457 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001458 int err = ActivityManager.START_SUCCESS;
1459
1460 ProcessRecord callerApp = null;
1461 if (caller != null) {
1462 callerApp = mService.getRecordForAppLocked(caller);
1463 if (callerApp != null) {
1464 callingPid = callerApp.pid;
1465 callingUid = callerApp.info.uid;
1466 } else {
1467 Slog.w(TAG, "Unable to find app for caller " + caller
1468 + " (pid=" + callingPid + ") when starting: "
1469 + intent.toString());
1470 err = ActivityManager.START_PERMISSION_DENIED;
1471 }
1472 }
1473
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001474 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1475
Craig Mautner6170f732013-04-02 13:05:23 -07001476 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001477 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001478 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001479 + " on display " + (container == null ? (mFocusedStack == null ?
1480 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1481 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1482 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001483 }
1484
1485 ActivityRecord sourceRecord = null;
1486 ActivityRecord resultRecord = null;
1487 if (resultTo != null) {
1488 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001489 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1490 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001491 if (sourceRecord != null) {
1492 if (requestCode >= 0 && !sourceRecord.finishing) {
1493 resultRecord = sourceRecord;
1494 }
1495 }
1496 }
Craig Mautner6170f732013-04-02 13:05:23 -07001497
Dianne Hackborn91097de2014-04-04 18:02:06 -07001498 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001499
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001500 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001501 // Transfer the result target from the source activity to the new
1502 // one being started, including any failures.
1503 if (requestCode >= 0) {
1504 ActivityOptions.abort(options);
1505 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1506 }
1507 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001508 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1509 resultRecord = null;
1510 }
Craig Mautner6170f732013-04-02 13:05:23 -07001511 resultWho = sourceRecord.resultWho;
1512 requestCode = sourceRecord.requestCode;
1513 sourceRecord.resultTo = null;
1514 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001515 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001516 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001517 if (sourceRecord.launchedFromUid == callingUid) {
1518 // The new activity is being launched from the same uid as the previous
1519 // activity in the flow, and asking to forward its result back to the
1520 // previous. In this case the activity is serving as a trampoline between
1521 // the two, so we also want to update its launchedFromPackage to be the
1522 // same as the previous activity. Note that this is safe, since we know
1523 // these two packages come from the same uid; the caller could just as
1524 // well have supplied that same package name itself. This specifially
1525 // deals with the case of an intent picker/chooser being launched in the app
1526 // flow to redirect to an activity picked by the user, where we want the final
1527 // activity to consider it to have been launched by the previous app activity.
1528 callingPackage = sourceRecord.launchedFromPackage;
1529 }
Craig Mautner6170f732013-04-02 13:05:23 -07001530 }
1531
1532 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1533 // We couldn't find a class that can handle the given Intent.
1534 // That's the end of that!
1535 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1536 }
1537
1538 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1539 // We couldn't find the specific class specified in the Intent.
1540 // Also the end of the line.
1541 err = ActivityManager.START_CLASS_NOT_FOUND;
1542 }
1543
Dianne Hackborn91097de2014-04-04 18:02:06 -07001544 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1545 && sourceRecord.task.voiceSession != null) {
1546 // If this activity is being launched as part of a voice session, we need
1547 // to ensure that it is safe to do so. If the upcoming activity will also
1548 // be part of the voice session, we can only launch it if it has explicitly
1549 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001550 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001551 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1552 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001553 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001554 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1555 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001556 Slog.w(TAG,
1557 "Activity being started in current voice task does not support voice: "
1558 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001559 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1560 }
1561 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001562 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001563 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1564 }
1565 }
1566 }
1567
1568 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1569 // If the caller is starting a new voice session, just make sure the target
1570 // is actually allowing it to run this way.
1571 try {
1572 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1573 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001574 Slog.w(TAG,
1575 "Activity being started in new voice task does not support: "
1576 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001577 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1578 }
1579 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001580 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001581 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1582 }
1583 }
1584
louis_changcd5d1982014-08-01 10:09:08 +08001585 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1586
Craig Mautner6170f732013-04-02 13:05:23 -07001587 if (err != ActivityManager.START_SUCCESS) {
1588 if (resultRecord != null) {
1589 resultStack.sendActivityResultLocked(-1,
1590 resultRecord, resultWho, requestCode,
1591 Activity.RESULT_CANCELED, null);
1592 }
Craig Mautner6170f732013-04-02 13:05:23 -07001593 ActivityOptions.abort(options);
1594 return err;
1595 }
1596
Svetoslav7008b512015-06-24 18:47:07 -07001597 boolean abort = false;
1598
Svet Ganov99b60432015-06-27 13:15:22 -07001599 final int startAnyPerm = mService.checkPermission(
1600 START_ANY_ACTIVITY, callingPid, callingUid);
1601
1602 if (startAnyPerm != PERMISSION_GRANTED) {
1603 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001604 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001605 final int actionRestriction = getActionRestrictionForCallingPackage(
1606 intent.getAction(), callingPackage, callingPid, callingUid);
1607
1608 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1609 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1610 if (resultRecord != null) {
1611 resultStack.sendActivityResultLocked(-1,
1612 resultRecord, resultWho, requestCode,
1613 Activity.RESULT_CANCELED, null);
1614 }
1615 String msg;
1616 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1617 msg = "Permission Denial: starting " + intent.toString()
1618 + " from " + callerApp + " (pid=" + callingPid
1619 + ", uid=" + callingUid + ")" + " with revoked permission "
1620 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1621 } else if (!aInfo.exported) {
1622 msg = "Permission Denial: starting " + intent.toString()
1623 + " from " + callerApp + " (pid=" + callingPid
1624 + ", uid=" + callingUid + ")"
1625 + " not exported from uid " + aInfo.applicationInfo.uid;
1626 } else {
1627 msg = "Permission Denial: starting " + intent.toString()
1628 + " from " + callerApp + " (pid=" + callingPid
1629 + ", uid=" + callingUid + ")"
1630 + " requires " + aInfo.permission;
1631 }
1632 Slog.w(TAG, msg);
1633 throw new SecurityException(msg);
1634 }
1635
1636 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1637 String message = "Appop Denial: starting " + intent.toString()
1638 + " from " + callerApp + " (pid=" + callingPid
1639 + ", uid=" + callingUid + ")"
1640 + " requires " + AppOpsManager.permissionToOp(
1641 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1642 Slog.w(TAG, message);
1643 abort = true;
1644 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1645 String message = "Appop Denial: starting " + intent.toString()
1646 + " from " + callerApp + " (pid=" + callingPid
1647 + ", uid=" + callingUid + ")"
1648 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1649 Slog.w(TAG, message);
1650 abort = true;
1651 }
Svetoslav7008b512015-06-24 18:47:07 -07001652 }
1653
1654 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001655 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001656
Craig Mautner6170f732013-04-02 13:05:23 -07001657 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001658 try {
1659 // The Intent we give to the watcher has the extra data
1660 // stripped off, since it can contain private information.
1661 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001662 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001663 aInfo.applicationInfo.packageName);
1664 } catch (RemoteException e) {
1665 mService.mController = null;
1666 }
Ben Gruver5e207332013-04-03 17:41:37 -07001667 }
Craig Mautner6170f732013-04-02 13:05:23 -07001668
Clara Bayarrif7fea162015-10-22 16:09:52 +01001669 UserInfo user = getUserInfo(userId);
1670 // TODO: Timeout for work challenge
Jeff Sharkeyba512352015-11-12 20:17:45 -08001671 if (user.isManagedProfile() && StorageManager.isFileBasedEncryptionEnabled()) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001672 KeyguardManager km = (KeyguardManager) mService.mContext
1673 .getSystemService(Context.KEYGUARD_SERVICE);
1674
1675 IIntentSender target = mService.getIntentSenderLocked(
1676 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1677 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1678 new String[]{ resolvedType },
1679 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1680 | PendingIntent.FLAG_IMMUTABLE, null);
1681 int flags = intent.getFlags();
1682 intent = km.createConfirmDeviceCredentialIntent(null, null);
1683 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1684 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1685 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1686 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1687 intent.putExtra(Intent.EXTRA_USER_ID, userId);
1688 intent.setFlags(flags);
1689
1690 resolvedType = null;
1691 callingUid = realCallingUid;
1692 callingPid = realCallingPid;
1693
1694 UserInfo parent = UserManager.get(mService.mContext).getProfileParent(userId);
1695 aInfo = resolveActivity(intent, null, PackageManager.MATCH_DEFAULT_ONLY
1696 | ActivityManagerService.STOCK_PM_FLAGS, null, parent.id);
1697 }
1698
Ben Gruver5e207332013-04-03 17:41:37 -07001699 if (abort) {
1700 if (resultRecord != null) {
1701 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001702 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001703 }
Ben Gruver5e207332013-04-03 17:41:37 -07001704 // We pretend to the caller that it was really started, but
1705 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001706 ActivityOptions.abort(options);
1707 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001708 }
1709
1710 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001711 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001712 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001713 if (outActivity != null) {
1714 outActivity[0] = r;
1715 }
1716
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001717 if (r.appTimeTracker == null && sourceRecord != null) {
1718 // If the caller didn't specify an explicit time tracker, we want to continue
1719 // tracking under any it has.
1720 r.appTimeTracker = sourceRecord.appTimeTracker;
1721 }
1722
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001723 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001724 if (voiceSession == null && (stack.mResumedActivity == null
1725 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001726 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1727 realCallingPid, realCallingUid, "Activity start")) {
Chong Zhang280d3322015-11-03 17:27:26 -08001728 PendingActivityLaunch pal = new PendingActivityLaunch(r,
Robert Carr13997f52015-10-23 13:13:39 -07001729 sourceRecord, startFlags, stack, callerApp);
Craig Mautneree36c772014-07-16 14:56:05 -07001730 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001731 ActivityOptions.abort(options);
1732 return ActivityManager.START_SWITCHES_CANCELED;
1733 }
1734 }
1735
1736 if (mService.mDidAppSwitch) {
1737 // This is the second allowed switch since we stopped switches,
1738 // so now just generally allow switches. Use case: user presses
1739 // home (switches disabled, switch to home, mDidAppSwitch now true);
1740 // user taps a home icon (coming from home so allowed, we hit here
1741 // and now allow anyone to switch again).
1742 mService.mAppSwitchesAllowedTime = 0;
1743 } else {
1744 mService.mDidAppSwitch = true;
1745 }
1746
Craig Mautneree36c772014-07-16 14:56:05 -07001747 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001748
Dianne Hackborn91097de2014-04-04 18:02:06 -07001749 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001750 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001751
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001752 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001753 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001754 // activity start, but we are not actually doing an activity
1755 // switch... just dismiss the keyguard now, because we
1756 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001757 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001758 }
1759 return err;
1760 }
1761
Clara Bayarrif7fea162015-10-22 16:09:52 +01001762 private UserInfo getUserInfo(int userId) {
1763 final long identity = Binder.clearCallingIdentity();
1764 try {
1765 return UserManager.get(mService.mContext).getUserInfo(userId);
1766 } finally {
1767 Binder.restoreCallingIdentity(identity);
1768 }
1769 }
1770
Svet Ganov99b60432015-06-27 13:15:22 -07001771 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001772 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001773 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1774 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001775 == PackageManager.PERMISSION_DENIED) {
1776 return ACTIVITY_RESTRICTION_PERMISSION;
1777 }
1778
Christopher Tateff7add02015-08-17 10:23:22 -07001779 if (activityInfo.permission == null) {
1780 return ACTIVITY_RESTRICTION_NONE;
1781 }
1782
Svet Ganov99b60432015-06-27 13:15:22 -07001783 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1784 if (opCode == AppOpsManager.OP_NONE) {
1785 return ACTIVITY_RESTRICTION_NONE;
1786 }
1787
1788 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1789 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001790 if (!ignoreTargetSecurity) {
1791 return ACTIVITY_RESTRICTION_APPOP;
1792 }
Svet Ganov99b60432015-06-27 13:15:22 -07001793 }
1794
1795 return ACTIVITY_RESTRICTION_NONE;
1796 }
1797
Svetoslav7008b512015-06-24 18:47:07 -07001798 private int getActionRestrictionForCallingPackage(String action,
1799 String callingPackage, int callingPid, int callingUid) {
1800 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001801 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001802 }
1803
1804 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1805 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001806 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001807 }
1808
1809 final PackageInfo packageInfo;
1810 try {
1811 packageInfo = mService.mContext.getPackageManager()
1812 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1813 } catch (PackageManager.NameNotFoundException e) {
1814 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001815 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001816 }
1817
1818 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001819 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001820 }
1821
1822 if (mService.checkPermission(permission, callingPid, callingUid) ==
1823 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001824 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001825 }
1826
1827 final int opCode = AppOpsManager.permissionToOpCode(permission);
1828 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001829 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001830 }
1831
1832 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1833 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001834 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001835 }
1836
Svet Ganov99b60432015-06-27 13:15:22 -07001837 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001838 }
1839
Chong Zhang0fa656b2015-08-31 15:17:21 -07001840 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001841 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001842
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001843 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1844 return mHomeStack;
1845 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001846
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001847 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001848
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001849 if (task != null && task.stack != null) {
1850 stack = task.stack;
1851 if (stack.isOnHomeDisplay()) {
1852 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001853 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001854 "computeStackFocus: Setting " + "focused stack to r=" + r
1855 + " task=" + task);
1856 } else {
1857 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1858 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001859 }
1860 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001861 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001862 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001863
1864 final ActivityContainer container = r.mInitialActivityContainer;
1865 if (container != null) {
1866 // The first time put it on the desired stack, after this put on task stack.
1867 r.mInitialActivityContainer = null;
1868 return container.mStack;
1869 }
1870
1871 // The fullscreen stack can contain any task regardless of if the task is resizeable
1872 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1873 // If the freeform stack has focus, and the activity to be launched is resizeable,
1874 // we can also put it in the focused stack.
1875 final boolean canUseFocusedStack =
1876 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1877 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1878 if (canUseFocusedStack
1879 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1880 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1881 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1882 return mFocusedStack;
1883 }
1884
1885 // We first try to put the task in the first dynamic stack.
1886 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1887 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1888 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001889 if (!StackId.isStaticStack(stack.mStackId)) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001890 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1891 "computeStackFocus: Setting focused stack=" + stack);
1892 return stack;
1893 }
1894 }
1895
1896 // If there is no suitable dynamic stack then we figure out which static stack to use.
1897 final int stackId = task != null ? task.getLaunchStackId() :
1898 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1899 FULLSCREEN_WORKSPACE_STACK_ID;
1900 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1901 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1902 + r + " stackId=" + stack.mStackId);
1903 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001904 }
1905
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001906 boolean setFocusedStack(ActivityRecord r, String reason) {
1907 if (r == null) {
1908 // Not sure what you are trying to do, but it is not going to work...
1909 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001910 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001911 final TaskRecord task = r.task;
1912 if (task == null || task.stack == null) {
1913 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1914 return false;
1915 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001916 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001917 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001918 }
1919
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001920 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001921 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Chong Zhang280d3322015-11-03 17:27:26 -08001922 boolean doResume, ActivityOptions options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001923 final Intent intent = r.intent;
1924 final int callingUid = r.launchedFromUid;
1925
Chong Zhang0fa656b2015-08-31 15:17:21 -07001926 boolean overrideBounds = false;
1927 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001928 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001929 if (canUseActivityOptionsLaunchBounds(options)) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001930 overrideBounds = true;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001931 newBounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001932 }
1933 }
1934
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001935 // In some flows in to this function, we retrieve the task record and hold on to it
1936 // without a lock before calling back in to here... so the task at this point may
1937 // not actually be in recents. Check for that, and if it isn't in recents just
1938 // consider it invalid.
1939 if (inTask != null && !inTask.inRecents) {
1940 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1941 inTask = null;
1942 }
1943
Craig Mautnerad400af2014-08-13 16:41:36 -07001944 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001945 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1946 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001947
Craig Mautnera228ae92014-07-09 05:44:55 -07001948 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001949 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001950 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001951 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1952 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1953 "\"singleInstance\" or \"singleTask\"");
1954 launchFlags &=
1955 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1956 } else {
1957 switch (r.info.documentLaunchMode) {
1958 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1959 break;
1960 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1961 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1962 break;
1963 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1964 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1965 break;
1966 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1967 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1968 break;
1969 }
1970 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001971
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001972 final boolean launchTaskBehind = r.mLaunchTaskBehind
1973 && !launchSingleTask && !launchSingleInstance
1974 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001975
Wale Ogunwale7d701172015-03-11 15:36:30 -07001976 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1977 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001978 // For whatever reason this activity is being launched into a new
1979 // task... yet the caller has requested a result back. Well, that
1980 // is pretty messed up, so instead immediately send back a cancel
1981 // and let the new task continue launched as normal without a
1982 // dependency on its originator.
1983 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1984 r.resultTo.task.stack.sendActivityResultLocked(-1,
1985 r.resultTo, r.resultWho, r.requestCode,
1986 Activity.RESULT_CANCELED, null);
1987 r.resultTo = null;
1988 }
1989
1990 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1991 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1992 }
1993
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001994 // If we are actually going to launch in to a new task, there are some cases where
1995 // we further want to do multiple task.
1996 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1997 if (launchTaskBehind
1998 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1999 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
2000 }
2001 }
2002
Craig Mautner8849a5e2013-04-02 16:41:03 -07002003 // We'll invoke onUserLeaving before onPause only if the launching
2004 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07002005 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002006 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2007 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002008
2009 // If the caller has asked not to resume at this point, we make note
2010 // of this in the record so that we can skip it when trying to find
2011 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07002012 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002013 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002014 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002015 }
2016
Craig Mautnera254cd72014-05-25 16:47:39 -07002017 ActivityRecord notTop =
2018 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002019
2020 // If the onlyIfNeeded flag is set, then we can do this if the activity
2021 // being launched is the same as the one making the call... or, as
2022 // a special case, if we do not know the caller then we count the
2023 // current top activity as the caller.
2024 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2025 ActivityRecord checkedCaller = sourceRecord;
2026 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002027 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002028 }
2029 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2030 // Caller is not the same as launcher, so always needed.
2031 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
2032 }
2033 }
2034
Craig Mautner8849a5e2013-04-02 16:41:03 -07002035 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002036 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002037
2038 // If the caller is not coming from another activity, but has given us an
2039 // explicit task into which they would like us to launch the new activity,
2040 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002041 if (sourceRecord == null && inTask != null && inTask.stack != null) {
2042 final Intent baseIntent = inTask.getBaseIntent();
2043 final ActivityRecord root = inTask.getRootActivity();
2044 if (baseIntent == null) {
2045 ActivityOptions.abort(options);
2046 throw new IllegalArgumentException("Launching into task without base intent: "
2047 + inTask);
2048 }
2049
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002050 // If this task is empty, then we are adding the first activity -- it
2051 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002052 if (launchSingleInstance || launchSingleTask) {
2053 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2054 ActivityOptions.abort(options);
2055 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2056 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002057 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002058 if (root != null) {
2059 ActivityOptions.abort(options);
2060 throw new IllegalArgumentException("Caller with inTask " + inTask
2061 + " has root " + root + " but target is singleInstance/Task");
2062 }
2063 }
2064
2065 // If task is empty, then adopt the interesting intent launch flags in to the
2066 // activity being started.
2067 if (root == null) {
2068 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2069 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2070 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2071 launchFlags = (launchFlags&~flagsOfInterest)
2072 | (baseIntent.getFlags()&flagsOfInterest);
2073 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002074 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002075 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002076
Dianne Hackborn962d5352014-08-29 16:27:40 -07002077 // If the task is not empty and the caller is asking to start it as the root
2078 // of a new task, then we don't actually want to start this on the task. We
2079 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002080 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002081 addingToTask = false;
2082
2083 } else {
2084 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002085 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002086
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002087 reuseTask = inTask;
2088 } else {
2089 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002090 // Launch ResolverActivity in the source task, so that it stays in the task
2091 // bounds when in freeform workspace.
2092 // Also put noDisplay activities in the source task. These by itself can
2093 // be placed in any task/stack, however it could launch other activities
2094 // like ResolverActivity, and we want those to stay in the original task.
2095 if (r.isResolverActivity() || r.noDisplay) {
2096 addingToTask = true;
2097 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002098 }
2099
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002100 if (inTask == null) {
2101 if (sourceRecord == null) {
2102 // This activity is not being started from another... in this
2103 // case we -always- start a new task.
2104 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2105 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2106 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2107 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2108 }
2109 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2110 // The original activity who is starting us is running as a single
2111 // instance... this new activity it is starting must go on its
2112 // own task.
2113 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2114 } else if (launchSingleInstance || launchSingleTask) {
2115 // The activity being started is a single instance... it always
2116 // gets launched into its own task.
2117 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2118 }
2119 }
2120
2121 ActivityInfo newTaskInfo = null;
2122 Intent newTaskIntent = null;
2123 ActivityStack sourceStack;
2124 if (sourceRecord != null) {
2125 if (sourceRecord.finishing) {
2126 // If the source is finishing, we can't further count it as our source. This
2127 // is because the task it is associated with may now be empty and on its way out,
2128 // so we don't want to blindly throw it in to that task. Instead we will take
2129 // the NEW_TASK flow and try to find a task for it. But save the task information
2130 // so it can be used when creating the new task.
2131 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2132 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2133 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2134 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2135 newTaskInfo = sourceRecord.info;
2136 newTaskIntent = sourceRecord.task.intent;
2137 }
2138 sourceRecord = null;
2139 sourceStack = null;
2140 } else {
2141 sourceStack = sourceRecord.task.stack;
2142 }
2143 } else {
2144 sourceStack = null;
2145 }
2146
2147 boolean movedHome = false;
2148 ActivityStack targetStack;
2149
2150 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002151 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002152
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002153 // We may want to try to place the new activity in to an existing task. We always
2154 // do this if the target activity is singleTask or singleInstance; we will also do
2155 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2156 // us to still place it in a new task: multi task, always doc mode, or being asked to
2157 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002158 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2159 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002160 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002161 // If bring to front is requested, and no result is requested and we have not
2162 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002163 // we can find a task that was started with this same
2164 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002165 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002166 // See if there is a task to bring to the front. If this is
2167 // a SINGLE_INSTANCE activity, there can be one and only one
2168 // instance of it in the history, and it is always in its own
2169 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002170 ActivityRecord intentActivity = !launchSingleInstance ?
2171 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002172 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002173 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2174 // but still needs to be a lock task mode violation since the task gets
2175 // cleared out and the device would otherwise leave the locked task.
2176 if (isLockTaskModeViolation(intentActivity.task,
2177 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2178 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002179 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002180 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002181 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2182 }
Craig Mautner29219d92013-04-16 20:19:12 -07002183 if (r.task == null) {
2184 r.task = intentActivity.task;
2185 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002186 if (intentActivity.task.intent == null) {
2187 // This task was started because of movement of
2188 // the activity based on affinity... now that we
2189 // are actually launching it, we can assign the
2190 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002191 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002192 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002193 targetStack = intentActivity.task.stack;
2194 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002195 // If the target task is not in the front, then we need
2196 // to bring it to the front... except... well, with
2197 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2198 // to have the same behavior as if a new instance was
2199 // being started, which means not bringing it to the front
2200 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002201 final ActivityStack focusStack = getFocusedStack();
2202 ActivityRecord curTop = (focusStack == null)
2203 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002204 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002205 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002206 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002207 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002208 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2209 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002210 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002211 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002212 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2213 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002214 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002215 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002216 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002217 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002218 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002219 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2220 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002221 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002222 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002223 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002224 options = null;
2225 }
2226 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002227 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002228 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002229 + " from " + intentActivity);
2230 targetStack.moveToFront("intentActivityFound");
2231 }
2232
Craig Mautner8849a5e2013-04-02 16:41:03 -07002233 // If the caller has requested that the target task be
2234 // reset, then do so.
2235 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2236 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2237 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002238 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002239 // We don't need to start a new activity, and
2240 // the client said not to do anything if that
2241 // is the case, so this is it! And for paranoia, make
2242 // sure we have correctly resumed the top activity.
2243 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002244 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002245
2246 // Make sure to notify Keyguard as well if we are not running an app
2247 // transition later.
2248 if (!movedToFront) {
2249 notifyActivityDrawnForKeyguard();
2250 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002251 } else {
2252 ActivityOptions.abort(options);
2253 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002254 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002255 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2256 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002257 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002258 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002259 // The caller has requested to completely replace any
2260 // existing task with its new activity. Well that should
2261 // not be too hard...
2262 reuseTask = intentActivity.task;
2263 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002264 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002265 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002266 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002267 // In this situation we want to remove all activities
2268 // from the task up to the one being started. In most
2269 // cases this means we are resetting the task to its
2270 // initial state.
2271 ActivityRecord top =
2272 intentActivity.task.performClearTaskLocked(r, launchFlags);
2273 if (top != null) {
2274 if (top.frontOfTask) {
2275 // Activity aliases may mean we use different
2276 // intents for the top activity, so make sure
2277 // the task now has the identity of the new
2278 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002279 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002280 }
2281 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2282 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002283 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002284 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002285 // A special case: we need to start the activity because it is not
2286 // currently running, and the caller has asked to clear the current
2287 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002288 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002289 // Now pretend like this activity is being started by the top of its
2290 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002291 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002292 TaskRecord task = sourceRecord.task;
2293 if (task != null && task.stack == null) {
2294 // Target stack got cleared when we all activities were removed
2295 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002296 targetStack = computeStackFocus(
2297 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002298 targetStack.addTask(
2299 task, !launchTaskBehind /* toTop */, false /* moving */);
2300 }
2301
Craig Mautner8849a5e2013-04-02 16:41:03 -07002302 }
2303 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2304 // In this case the top activity on the task is the
2305 // same as the one being launched, so we take that
2306 // as a request to bring the task to the foreground.
2307 // If the top activity in the task is the root
2308 // activity, deliver this new intent to it if it
2309 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002310 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002311 && intentActivity.realActivity.equals(r.realActivity)) {
2312 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2313 intentActivity.task);
2314 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002315 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002316 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002317 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2318 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002319 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2320 // In this case we are launching the root activity
2321 // of the task, but with a different intent. We
2322 // should start a new instance on top.
2323 addingToTask = true;
2324 sourceRecord = intentActivity;
2325 }
2326 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2327 // In this case an activity is being launched in to an
2328 // existing task, without resetting that task. This
2329 // is typically the situation of launching an activity
2330 // from a notification or shortcut. We want to place
2331 // the new activity on top of the current task.
2332 addingToTask = true;
2333 sourceRecord = intentActivity;
2334 } else if (!intentActivity.task.rootWasReset) {
2335 // In this case we are launching in to an existing task
2336 // that has not yet been started from its front door.
2337 // The current task has been brought to the front.
2338 // Ideally, we'd probably like to place this new task
2339 // at the bottom of its stack, but that's a little hard
2340 // to do with the current organization of the code so
2341 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002342 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002343 }
2344 if (!addingToTask && reuseTask == null) {
2345 // We didn't do anything... but it was needed (a.k.a., client
2346 // don't use that intent!) And for paranoia, make
2347 // sure we have correctly resumed the top activity.
2348 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002349 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002350 if (!movedToFront) {
2351 // Make sure to notify Keyguard as well if we are not running an app
2352 // transition later.
2353 notifyActivityDrawnForKeyguard();
2354 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002355 } else {
2356 ActivityOptions.abort(options);
2357 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002358 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002359 return ActivityManager.START_TASK_TO_FRONT;
2360 }
2361 }
2362 }
2363 }
2364
2365 //String uri = r.intent.toURI();
2366 //Intent intent2 = new Intent(uri);
2367 //Slog.i(TAG, "Given intent: " + r.intent);
2368 //Slog.i(TAG, "URI is: " + uri);
2369 //Slog.i(TAG, "To intent: " + intent2);
2370
2371 if (r.packageName != null) {
2372 // If the activity being launched is the same as the one currently
2373 // at the top, then we need to check if it should only be launched
2374 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002375 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002376 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002377 if (top != null && r.resultTo == null) {
2378 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2379 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002380 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002381 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002382 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2383 top.task);
2384 // For paranoia, make sure we have correctly
2385 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002386 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002387 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002388 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002389 }
2390 ActivityOptions.abort(options);
2391 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2392 // We don't need to start a new activity, and
2393 // the client said not to do anything if that
2394 // is the case, so this is it!
2395 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2396 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002397 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002398 return ActivityManager.START_DELIVERED_TO_TOP;
2399 }
2400 }
2401 }
2402 }
2403
2404 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002405 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002406 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2407 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002408 }
2409 ActivityOptions.abort(options);
2410 return ActivityManager.START_CLASS_NOT_FOUND;
2411 }
2412
2413 boolean newTask = false;
2414 boolean keepCurTransition = false;
2415
Craig Mautnerbb742462014-07-07 15:28:55 -07002416 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002417 sourceRecord.task : null;
2418
Craig Mautner8849a5e2013-04-02 16:41:03 -07002419 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002420 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002421 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002422 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002423 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002424 if (doResume) {
2425 targetStack.moveToFront("startingNewTask");
2426 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002427
Craig Mautner8849a5e2013-04-02 16:41:03 -07002428 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002429 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2430 newTaskInfo != null ? newTaskInfo : r.info,
2431 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002432 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2433 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002434 if (overrideBounds) {
2435 r.task.updateOverrideConfiguration(newBounds);
2436 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002437 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2438 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002439 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002440 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002441 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002442 if (isLockTaskModeViolation(r.task)) {
2443 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2444 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2445 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002446 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002447 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002448 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2449 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002450 // Caller wants to appear on home activity, so before starting
2451 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002452 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002453 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002454 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002455 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002456 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002457 if (isLockTaskModeViolation(sourceTask)) {
2458 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2459 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2460 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002461 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002462 if (doResume) {
2463 targetStack.moveToFront("sourceStackToFront");
2464 }
Craig Mautner737fae22014-10-15 12:52:10 -07002465 final TaskRecord topTask = targetStack.topTask();
2466 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002467 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002468 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002469 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002470 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002471 // In this case, we are adding the activity to an existing
2472 // task, but the caller has asked to clear that task if the
2473 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002474 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002475 keepCurTransition = true;
2476 if (top != null) {
2477 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002478 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002479 // For paranoia, make sure we have correctly
2480 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002481 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002482 if (doResume) {
2483 targetStack.resumeTopActivityLocked(null);
2484 }
2485 ActivityOptions.abort(options);
2486 return ActivityManager.START_DELIVERED_TO_TOP;
2487 }
2488 } else if (!addingToTask &&
2489 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2490 // In this case, we are launching an activity in our own task
2491 // that may already be running somewhere in the history, and
2492 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002493 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002494 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002495 final TaskRecord task = top.task;
2496 task.moveActivityToFrontLocked(top);
2497 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002498 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002499 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002500 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002501 if (doResume) {
2502 targetStack.resumeTopActivityLocked(null);
2503 }
2504 return ActivityManager.START_DELIVERED_TO_TOP;
2505 }
2506 }
2507 // An existing activity is starting this new activity, so we want
2508 // to keep the new one in the same task as the one that is starting
2509 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002510 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002511 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002512 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002513
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002514 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002515 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002516 // task it has provided to us.
2517 if (isLockTaskModeViolation(inTask)) {
2518 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2519 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2520 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002521 if (overrideBounds) {
2522 inTask.updateOverrideConfiguration(newBounds);
2523 int stackId = inTask.getLaunchStackId();
2524 if (stackId != inTask.stack.mStackId) {
2525 moveTaskToStackUncheckedLocked(
2526 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2527 }
2528 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002529 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002530 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2531 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002532
2533 // Check whether we should actually launch the new activity in to the task,
2534 // or just reuse the current activity on top.
2535 ActivityRecord top = inTask.getTopActivity();
2536 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2537 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2538 || launchSingleTop || launchSingleTask) {
2539 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2540 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2541 // We don't need to start a new activity, and
2542 // the client said not to do anything if that
2543 // is the case, so this is it!
2544 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2545 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002546 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002547 return ActivityManager.START_DELIVERED_TO_TOP;
2548 }
2549 }
2550
Dianne Hackborn962d5352014-08-29 16:27:40 -07002551 if (!addingToTask) {
2552 // We don't actually want to have this activity added to the task, so just
2553 // stop here but still tell the caller that we consumed the intent.
2554 ActivityOptions.abort(options);
2555 return ActivityManager.START_TASK_TO_FRONT;
2556 }
2557
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002558 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002559 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002560 + " in explicit task " + r.task);
2561
Craig Mautner8849a5e2013-04-02 16:41:03 -07002562 } else {
2563 // This not being started from an existing activity, and not part
2564 // of a new task... just put it in the top task, though these days
2565 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002566 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002567 if (doResume) {
2568 targetStack.moveToFront("addingToTopTask");
2569 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002570 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002571 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002572 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002573 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002574 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002575 + " in new guessed " + r.task);
2576 }
2577
2578 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002579 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002580
Craig Mautner76e2a762014-06-24 09:05:43 -07002581 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2582 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2583 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002584 if (newTask) {
2585 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2586 }
2587 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002588 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002589 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002590 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002591 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002592 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002593 updateUserStackLocked(r.userId, targetStack);
Chong Zhangc806d902015-11-30 09:44:27 -08002594
2595 if (!r.task.mResizeable && isStackDockedInEffect(targetStack.mStackId)) {
2596 showNonResizeableDockToast(r.task.taskId);
2597 }
2598
Craig Mautner8849a5e2013-04-02 16:41:03 -07002599 return ActivityManager.START_SUCCESS;
2600 }
2601
Craig Mautneree36c772014-07-16 14:56:05 -07002602 final void doPendingActivityLaunchesLocked(boolean doResume) {
2603 while (!mPendingActivityLaunches.isEmpty()) {
2604 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002605
2606 try {
2607 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Robert Carr13997f52015-10-23 13:13:39 -07002608 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002609 } catch (Exception e) {
Robert Carr13997f52015-10-23 13:13:39 -07002610 Slog.e(TAG, "Exception during pending activity launch pal=" + pal, e);
2611 pal.sendErrorResult(e.getMessage());
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002612 }
Craig Mautneree36c772014-07-16 14:56:05 -07002613 }
2614 }
2615
Craig Mautner7f13ed32014-07-28 14:00:35 -07002616 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002617 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2618 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002619 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002620 mPendingActivityLaunches.remove(palNdx);
2621 }
2622 }
2623 }
2624
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002625 void setLaunchSource(int uid) {
2626 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2627 }
2628
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002629 void acquireLaunchWakelock() {
2630 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2631 throw new IllegalStateException("Calling must be system uid");
2632 }
2633 mLaunchingActivity.acquire();
2634 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2635 // To be safe, don't allow the wake lock to be held for too long.
2636 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2637 }
2638 }
2639
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002640 /**
2641 * Called when the frontmost task is idle.
2642 * @return the state of mService.mBooting before this was called.
2643 */
2644 private boolean checkFinishBootingLocked() {
2645 final boolean booting = mService.mBooting;
2646 boolean enableScreen = false;
2647 mService.mBooting = false;
2648 if (!mService.mBooted) {
2649 mService.mBooted = true;
2650 enableScreen = true;
2651 }
2652 if (booting || enableScreen) {
2653 mService.postFinishBooting(booting, enableScreen);
2654 }
2655 return booting;
2656 }
2657
Craig Mautnerf3333272013-04-22 10:55:53 -07002658 // Checked.
2659 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2660 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002661 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002662
Craig Mautnerf3333272013-04-22 10:55:53 -07002663 ArrayList<ActivityRecord> stops = null;
2664 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002665 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002666 int NS = 0;
2667 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002668 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002669 boolean activityRemoved = false;
2670
Wale Ogunwale7d701172015-03-11 15:36:30 -07002671 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002672 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002673 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2674 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002675 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2676 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002677 if (fromTimeout) {
2678 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002679 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002680
2681 // This is a hack to semi-deal with a race condition
2682 // in the client where it can be constructed with a
2683 // newer configuration from when we asked it to launch.
2684 // We'll update with whatever configuration it now says
2685 // it used to launch.
2686 if (config != null) {
2687 r.configuration = config;
2688 }
2689
2690 // We are now idle. If someone is waiting for a thumbnail from
2691 // us, we can now deliver.
2692 r.idle = true;
2693
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002694 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002695 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002696 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002697 }
2698 }
2699
2700 if (allResumedActivitiesIdle()) {
2701 if (r != null) {
2702 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002703 }
2704
2705 if (mLaunchingActivity.isHeld()) {
2706 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2707 if (VALIDATE_WAKE_LOCK_CALLER &&
2708 Binder.getCallingUid() != Process.myUid()) {
2709 throw new IllegalStateException("Calling must be system uid");
2710 }
2711 mLaunchingActivity.release();
2712 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002713 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002714 }
2715
2716 // Atomically retrieve all of the other things to do.
2717 stops = processStoppingActivitiesLocked(true);
2718 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002719 if ((NF = mFinishingActivities.size()) > 0) {
2720 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002721 mFinishingActivities.clear();
2722 }
2723
Craig Mautnerf3333272013-04-22 10:55:53 -07002724 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002725 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002726 mStartingUsers.clear();
2727 }
2728
Craig Mautnerf3333272013-04-22 10:55:53 -07002729 // Stop any activities that are scheduled to do so but have been
2730 // waiting for the next one to start.
2731 for (int i = 0; i < NS; i++) {
2732 r = stops.get(i);
2733 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002734 if (stack != null) {
2735 if (r.finishing) {
2736 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2737 } else {
2738 stack.stopActivityLocked(r);
2739 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002740 }
2741 }
2742
2743 // Finish any activities that are scheduled to do so but have been
2744 // waiting for the next one to start.
2745 for (int i = 0; i < NF; i++) {
2746 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002747 final ActivityStack stack = r.task.stack;
2748 if (stack != null) {
2749 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2750 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002751 }
2752
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002753 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002754 // Complete user switch
2755 if (startingUsers != null) {
2756 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002757 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002758 }
2759 }
2760 // Complete starting up of background users
2761 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002762 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002763 mStartingBackgroundUsers.clear();
2764 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002765 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002766 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002767 }
2768 }
2769
2770 mService.trimApplications();
2771 //dump();
2772 //mWindowManager.dump();
2773
Craig Mautnerf3333272013-04-22 10:55:53 -07002774 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002775 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002776 }
2777
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002778 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002779 }
2780
Craig Mautner8e569572013-10-11 17:36:59 -07002781 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002782 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002783 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2784 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002785 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2786 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2787 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002788 }
Craig Mautner19091252013-10-05 00:03:53 -07002789 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002790 }
2791
2792 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002793 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2794 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002795 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2796 stacks.get(stackNdx).closeSystemDialogsLocked();
2797 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002798 }
2799 }
2800
Craig Mautner93529a42013-10-04 15:03:13 -07002801 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002802 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002803 }
2804
Craig Mautner8d341ef2013-03-26 09:03:27 -07002805 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002806 * Update the last used stack id for non-current user (current user's last
2807 * used stack is the focused stack)
2808 */
2809 void updateUserStackLocked(int userId, ActivityStack stack) {
2810 if (userId != mCurrentUser) {
2811 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2812 }
2813 }
2814
2815 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002816 * @return true if some activity was finished (or would have finished if doit were true).
2817 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002818 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2819 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002820 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002821 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2822 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002823 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002824 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002825 if (stack.finishDisabledPackageActivitiesLocked(
2826 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002827 didSomething = true;
2828 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002829 }
2830 }
2831 return didSomething;
2832 }
2833
Dianne Hackborna413dc02013-07-12 12:02:55 -07002834 void updatePreviousProcessLocked(ActivityRecord r) {
2835 // Now that this process has stopped, we may want to consider
2836 // it to be the previous app to try to keep around in case
2837 // the user wants to return to it.
2838
2839 // First, found out what is currently the foreground app, so that
2840 // we don't blow away the previous app if this activity is being
2841 // hosted by the process that is actually still the foreground.
2842 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002843 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2844 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002845 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2846 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002847 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002848 if (stack.mResumedActivity != null) {
2849 fgApp = stack.mResumedActivity.app;
2850 } else if (stack.mPausingActivity != null) {
2851 fgApp = stack.mPausingActivity.app;
2852 }
2853 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002854 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002855 }
2856 }
2857
2858 // Now set this one as the previous process, only if that really
2859 // makes sense to.
2860 if (r.app != null && fgApp != null && r.app != fgApp
2861 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002862 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002863 mService.mPreviousProcess = r.app;
2864 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2865 }
2866 }
2867
Craig Mautner05d29032013-05-03 13:40:13 -07002868 boolean resumeTopActivitiesLocked() {
2869 return resumeTopActivitiesLocked(null, null, null);
2870 }
2871
Chong Zhang280d3322015-11-03 17:27:26 -08002872 boolean resumeTopActivitiesLocked(
2873 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Craig Mautner05d29032013-05-03 13:40:13 -07002874 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002875 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002876 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002877 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002878 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002879 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002880 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2881 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002882
Craig Mautnere0a38842013-12-16 16:14:02 -08002883 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2884 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002885 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2886 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002887 if (stack == targetStack) {
2888 // Already started above.
2889 continue;
2890 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002891 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002892 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002893 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002894 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002895 }
Craig Mautner05d29032013-05-03 13:40:13 -07002896 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002897 }
2898
Todd Kennedy539db512014-12-15 09:57:55 -08002899 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002900 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2901 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002902 final int numStacks = stacks.size();
2903 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2904 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002905 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002906 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 }
2908 }
2909
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002910 void finishVoiceTask(IVoiceInteractionSession session) {
2911 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2912 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2913 final int numStacks = stacks.size();
2914 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2915 final ActivityStack stack = stacks.get(stackNdx);
2916 stack.finishVoiceTask(session);
2917 }
2918 }
2919 }
2920
Chong Zhang280d3322015-11-03 17:27:26 -08002921 void findTaskToMoveToFrontLocked(
2922 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002923 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2924 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002925 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002926 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2927 // Caller wants the home activity moved with it. To accomplish this,
2928 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002929 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002930 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002931 if (task.stack == null) {
2932 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2933 + task + " to front. Stack is null");
2934 return;
2935 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002936
Chong Zhang0fa656b2015-08-31 15:17:21 -07002937 if (task.mResizeable && options != null) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002938 if (canUseActivityOptionsLaunchBounds(options)) {
2939 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002940 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08002941 final int stackId = task.getLaunchStackId();
2942 if (stackId != task.stack.mStackId) {
2943 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
2944 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2945 // still need moveTaskToFrontLocked() below for any transition settings.
2946 }
2947 // WM resizeTask must be done after the task is moved to the correct stack,
2948 // because Task's setBounds() also updates dim layer's bounds, but that has
2949 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07002950 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2951 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002952 }
2953 }
2954
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002955 final ActivityRecord r = task.getTopActivity();
2956 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2957 r == null ? null : r.appTimeTracker, reason);
2958
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002959 if (DEBUG_STACK) Slog.d(TAG_STACK,
2960 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002961 }
2962
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002963 private boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
2964 // We use the launch bounds in the activity options is the device supports freeform
2965 // window management.
2966 return options.hasLaunchBounds() && mService.mSupportsFreeformWindowManagement;
2967 }
2968
Craig Mautner967212c2013-04-13 21:10:58 -07002969 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002970 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002971 }
2972
2973 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002974 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2975 if (activityContainer != null) {
2976 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002977 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002978 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002979 return null;
2980 }
2981 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002982 }
2983
Craig Mautner967212c2013-04-13 21:10:58 -07002984 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002985 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002986 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2987 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002988 }
2989 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002990 }
2991
Craig Mautner4a1cb222013-12-04 16:14:06 -08002992 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002993 ActivityRecord homeActivity = getHomeActivity();
2994 if (homeActivity != null) {
2995 return homeActivity.appToken;
2996 }
2997 return null;
2998 }
2999
3000 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07003001 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003002 }
3003
3004 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003005 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
3006 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
3007 final TaskRecord task = tasks.get(taskNdx);
3008 if (task.isHomeTask()) {
3009 final ArrayList<ActivityRecord> activities = task.mActivities;
3010 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3011 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003012 if (r.isHomeActivity()
3013 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003014 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003015 }
3016 }
3017 }
3018 }
3019 return null;
3020 }
3021
Chong Zhangb15758a2015-11-17 12:12:03 -08003022 /**
3023 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
3024 * the docked stack itself, or if it's side-by-side to the docked stack.
3025 */
3026 boolean isStackDockedInEffect(int stackId) {
3027 return stackId == DOCKED_STACK_ID ||
3028 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
3029 }
3030
Todd Kennedyca4d8422015-01-15 15:19:22 -08003031 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08003032 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003033 ActivityContainer activityContainer =
3034 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003035 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003036 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3037 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003038 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003039 return activityContainer;
3040 }
3041
Craig Mautner34b73df2014-01-12 21:11:08 -08003042 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003043 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
3044 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
3045 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003046 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
3047 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003048 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08003049 }
3050 }
3051
Craig Mautner95da1082014-02-24 17:54:35 -08003052 void deleteActivityContainer(IActivityContainer container) {
3053 ActivityContainer activityContainer = (ActivityContainer)container;
3054 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003055 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3056 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08003057 final int stackId = activityContainer.mStackId;
3058 mActivityContainers.remove(stackId);
3059 mWindowManager.removeStack(stackId);
3060 }
3061 }
3062
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003063 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
3064 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003065 final ActivityStack stack = getStack(stackId);
3066 if (stack == null) {
3067 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
3068 return;
3069 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003070
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003071 if (!allowResizeInDockedMode
3072 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
3073 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
3074 // stack size changing so things don't get out of sync.
3075 return;
3076 }
3077
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003078 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003079 mWindowManager.deferSurfaceLayout();
3080 try {
3081 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003082
Jorim Jaggic4025202015-10-22 16:43:34 +02003083 mTmpBounds.clear();
3084 mTmpConfigs.clear();
3085 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3086 for (int i = tasks.size() - 1; i >= 0; i--) {
3087 TaskRecord task = tasks.get(i);
3088 if (task.mResizeable) {
3089 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3090 // For freeform stack we don't adjust the size of the tasks to match that
3091 // of the stack, but we do try to make sure the tasks are still contained
3092 // with the bounds of the stack.
3093 tempRect2.set(task.mBounds);
3094 fitWithinBounds(tempRect2, bounds);
3095 task.updateOverrideConfiguration(tempRect2);
3096 } else {
3097 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003098 }
3099 }
3100
Jorim Jaggic4025202015-10-22 16:43:34 +02003101 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3102 mTmpBounds.put(task.taskId, task.mBounds);
3103 }
3104 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3105 if (stack.mStackId == DOCKED_STACK_ID) {
3106 // Dock stack funness...Yay!
3107 if (stack.mFullscreen) {
3108 // The dock stack went fullscreen which is kinda like dismissing it.
3109 // In this case we make all other static stacks fullscreen and move all
3110 // docked stack tasks to the fullscreen stack.
3111 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003112 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003113 resizeStackLocked(i, null, preserveWindows, true);
3114 }
3115 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003116
Jorim Jaggic4025202015-10-22 16:43:34 +02003117 final int count = tasks.size();
3118 for (int i = 0; i < count; i++) {
3119 moveTaskToStackLocked(tasks.get(i).taskId,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003120 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
3121 true /* animate */);
Jorim Jaggic4025202015-10-22 16:43:34 +02003122 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003123
Jorim Jaggic4025202015-10-22 16:43:34 +02003124 // stack shouldn't contain anymore activities, so nothing to resume.
3125 r = null;
3126 } else {
3127 // Docked stacks occupy a dedicated region on screen so the size of all other
3128 // static stacks need to be adjusted so they don't overlap with the docked stack.
3129 // We get the bounds to use from window manager which has been adjusted for any
3130 // screen controls and is also the same for all stacks.
3131 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3132
3133 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003134 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003135 ActivityStack otherStack = getStack(i);
3136 if (otherStack != null) {
3137 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3138 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003139 }
3140 }
3141 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003142 // Since we are resizing the stack, all other operations should strive to preserve
3143 // windows.
3144 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003145 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003146 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003147
Jorim Jaggic4025202015-10-22 16:43:34 +02003148 if (r != null) {
3149 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3150 // And we need to make sure at this point that all other activities
3151 // are made visible with the correct configuration.
3152 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3153 if (!updated) {
3154 resumeTopActivitiesLocked(stack, null, null);
3155 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003156 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003157 } finally {
3158 mWindowManager.continueSurfaceLayout();
3159 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003160 }
3161 }
3162
Chong Zhang6de2ae82015-09-30 18:25:21 -07003163 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003164 if (!task.mResizeable) {
3165 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3166 return;
3167 }
3168
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003169 adjustForMinimalTaskDimensions(task, bounds);
3170
Chong Zhang87b21722015-09-21 15:39:51 -07003171 // If this is a forced resize, let it go through even if the bounds is not changing,
3172 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003173 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003174 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003175 // Nothing to do here...
3176 return;
3177 }
3178
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003179 if (!mWindowManager.isValidTaskId(task.taskId)) {
3180 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003181 // All we can do for now is update the bounds so it can be used when the task is
3182 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003183 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003184 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3185 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003186 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003187 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003188 return;
3189 }
3190
Chong Zhang6de2ae82015-09-30 18:25:21 -07003191 // Do not move the task to another stack here.
3192 // This method assumes that the task is already placed in the right stack.
3193 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003194
Chong Zhang6de2ae82015-09-30 18:25:21 -07003195 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003196
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003197 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003198 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003199 // way and the activity was kept the way it was. If it's false, it means the activity had
3200 // to be relaunched due to configuration change.
3201 boolean kept = true;
3202 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003203 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003204 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003205 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003206 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003207 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003208 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003209 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003210 resumeTopActivitiesLocked(stack, null, null);
3211 }
3212 }
3213 }
Chong Zhang87b21722015-09-21 15:39:51 -07003214 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003215
3216 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003217 }
3218
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003219 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3220 if (bounds == null) {
3221 return;
3222 }
3223 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3224 : task.mMinimalSize;
3225 final boolean adjustWidth = minimalSize > bounds.width();
3226 final boolean adjustHeight = minimalSize > bounds.height();
3227 if (!(adjustWidth || adjustHeight)) {
3228 return;
3229 }
3230 Rect taskBounds = task.mBounds;
3231 if (adjustWidth) {
3232 if (taskBounds != null && bounds.right == taskBounds.right) {
3233 bounds.left = bounds.right - minimalSize;
3234 } else {
3235 // Either left bounds match, or neither match, or the previous bounds were
3236 // fullscreen and we default to keeping left.
3237 bounds.right = bounds.left + minimalSize;
3238 }
3239 }
3240 if (adjustHeight) {
3241 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3242 bounds.top = bounds.bottom - minimalSize;
3243 } else {
3244 // Either top bounds match, or neither match, or the previous bounds were
3245 // fullscreen and we default to keeping top.
3246 bounds.bottom = bounds.top + minimalSize;
3247 }
3248 }
3249 }
3250
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003251 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003252 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3253 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003254 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003255 }
3256
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003257 ActivityContainer activityContainer = new ActivityContainer(stackId);
3258 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003259 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003260 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003261 }
3262
3263 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003264 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003265 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3266 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003267 break;
3268 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003269 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003270 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003271 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003272 }
3273
Chong Zhang5dcb2752015-08-18 13:50:26 -07003274 /**
3275 * Restores a recent task to a stack
3276 * @param task The recent task to be restored.
3277 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07003278 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003279 * @return true if the task has been restored successfully.
3280 */
3281 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3282 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003283 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003284 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003285 if (task.stack != null) {
3286 // Task has already been restored once. See if we need to do anything more
3287 if (task.stack.mStackId == stackId) {
3288 // Nothing else to do since it is already restored in the right stack.
3289 return true;
3290 }
3291 // Remove current stack association, so we can re-associate the task with the
3292 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003293 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003294 }
3295
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003296 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003297 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003298
3299 if (stack == null) {
3300 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003301 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3302 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003303 return false;
3304 }
3305
3306 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003307 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3308 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003309 final ArrayList<ActivityRecord> activities = task.mActivities;
3310 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003311 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003312 }
3313 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003314 }
3315
Wale Ogunwale040b4702015-08-06 18:10:50 -07003316 /**
3317 * Moves the specified task record to the input stack id.
3318 * WARNING: This method performs an unchecked/raw move of the task and
3319 * can leave the system in an unstable state if used incorrectly.
3320 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3321 * to a stack.
3322 * @param task Task to move.
3323 * @param stackId Id of stack to move task to.
3324 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003325 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003326 * @param reason Reason the task is been moved.
3327 * @return The stack the task was moved to.
3328 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003329 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003330 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3331 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003332 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003333 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3334
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003335 final boolean resizeable = task.mResizeable;
3336 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3337 // if a docked stack is created below which will lead to the stack we are moving from and
3338 // its resizeable tasks being resized.
3339 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003340 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003341 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003342 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3343 if (task.stack != null) {
3344 task.stack.removeTask(task, reason, MOVING);
3345 }
3346 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003347
3348 // If the task had focus before (or we're requested to move focus),
3349 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003350 stack.setFocusAndResumeStateIfNeeded(
3351 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003352
Wale Ogunwale040b4702015-08-06 18:10:50 -07003353 return stack;
3354 }
3355
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003356 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003357 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003358 final TaskRecord task = anyTaskForIdLocked(taskId);
3359 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003360 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003361 return;
3362 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003363
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003364 if (task.stack != null && task.stack.mStackId == stackId) {
3365 // You are already in the right stack silly...
3366 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
3367 return;
3368 }
3369
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003370 final ActivityRecord topActivity = task.getTopActivity();
3371 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003372 // We are about to relaunch the activity because its configuration changed due to
3373 // being maximized, i.e. size change. The activity will first remove the old window
3374 // and then add a new one. This call will tell window manager about this, so it can
3375 // preserve the old window until the new one is drawn. This prevents having a gap
3376 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003377 // entrance of the new window to be properly animated.
Jorim Jaggi030979c2015-11-20 15:14:43 -08003378 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003379 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08003380 final ActivityStack stack = moveTaskToStackUncheckedLocked(
3381 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003382
3383 // Make sure the task has the appropriate bounds/size for the stack it is in.
3384 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003385 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003386 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3387 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003388 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3389 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003390 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003391 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003392 }
3393
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003394 // The task might have already been running and its visibility needs to be synchronized with
3395 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003396 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003397 resumeTopActivitiesLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08003398
3399 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08003400 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003401 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003402 }
3403
Wale Ogunwale079a0042015-10-24 11:44:07 -07003404 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3405 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3406 if (stack == null) {
3407 throw new IllegalArgumentException(
3408 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3409 }
3410
3411 final ActivityRecord r = stack.topRunningActivityLocked();
3412 if (r == null) {
3413 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3414 + " in stack=" + stack);
3415 return false;
3416 }
3417
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003418 if (!r.info.supportsPip) {
3419 Slog.w(TAG,
3420 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003421 + " r=" + r);
3422 return false;
3423 }
3424
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003425 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003426 if (task.mActivities.size() == 1) {
3427 // There is only one activity in the task. So, we can just move the task over to the
3428 // pinned stack without re-parenting the activity in a different task.
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003429 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003430 "moveTopActivityToPinnedStack", true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003431 } else {
3432 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3433 pinnedStack.moveActivityToStack(r);
3434 }
3435
Filip Gruszczynskia21136c2015-11-24 14:43:31 -08003436 resizeStackLocked(PINNED_STACK_ID, bounds, !PRESERVE_WINDOWS, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003437
3438 // The task might have already been running and its visibility needs to be synchronized with
3439 // the visibility of the stack / windows.
3440 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3441 resumeTopActivitiesLocked();
3442 return true;
3443 }
3444
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003445 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3446 final TaskRecord task = anyTaskForIdLocked(taskId);
3447 if (task == null) {
3448 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3449 return;
3450 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08003451 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
3452
3453 task.updateOverrideConfigurationForStack(stack);
3454
3455 mWindowManager.positionTaskInStack(
3456 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003457 final boolean stackChanged = task.stack != null && task.stack != stack;
3458 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003459 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003460 }
3461 stack.positionTask(task, position, stackChanged);
3462 // The task might have already been running and its visibility needs to be synchronized with
3463 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003464 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003465 resumeTopActivitiesLocked();
3466 }
3467
Craig Mautnerac6f8432013-07-17 13:24:59 -07003468 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003469 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003470 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3471 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003472 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3473 final ActivityStack stack = stacks.get(stackNdx);
3474 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003475 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003476 continue;
3477 }
3478 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003479 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3480 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003481 continue;
3482 }
3483 final ActivityRecord ar = stack.findTaskLocked(r);
3484 if (ar != null) {
3485 return ar;
3486 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003487 }
3488 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003489 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003490 return null;
3491 }
3492
3493 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003494 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3495 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003496 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3497 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3498 if (ar != null) {
3499 return ar;
3500 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003501 }
3502 }
3503 return null;
3504 }
3505
Craig Mautner8d341ef2013-03-26 09:03:27 -07003506 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003507 scheduleSleepTimeout();
3508 if (!mGoingToSleep.isHeld()) {
3509 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003510 if (mLaunchingActivity.isHeld()) {
3511 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3512 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003513 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003514 mLaunchingActivity.release();
3515 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003516 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003517 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003518 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003519 }
3520
3521 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003522 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003523
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003524 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003525 final long endTime = System.currentTimeMillis() + timeout;
3526 while (true) {
3527 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003528 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3529 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3531 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3532 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003533 }
3534 if (cantShutdown) {
3535 long timeRemaining = endTime - System.currentTimeMillis();
3536 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003537 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003538 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003539 } catch (InterruptedException e) {
3540 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003541 } else {
3542 Slog.w(TAG, "Activity manager shutdown timed out");
3543 timedout = true;
3544 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003545 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003546 } else {
3547 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003548 }
3549 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003550
3551 // Force checkReadyForSleep to complete.
3552 mSleepTimeout = true;
3553 checkReadyForSleepLocked();
3554
Craig Mautner8d341ef2013-03-26 09:03:27 -07003555 return timedout;
3556 }
3557
3558 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003559 removeSleepTimeouts();
3560 if (mGoingToSleep.isHeld()) {
3561 mGoingToSleep.release();
3562 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003563 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3564 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003565 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3566 final ActivityStack stack = stacks.get(stackNdx);
3567 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003568 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003569 resumeTopActivitiesLocked();
3570 }
Craig Mautner5314a402013-09-26 12:40:16 -07003571 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003572 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003573 mGoingToSleepActivities.clear();
3574 }
3575
3576 void activitySleptLocked(ActivityRecord r) {
3577 mGoingToSleepActivities.remove(r);
3578 checkReadyForSleepLocked();
3579 }
3580
3581 void checkReadyForSleepLocked() {
3582 if (!mService.isSleepingOrShuttingDown()) {
3583 // Do not care.
3584 return;
3585 }
3586
3587 if (!mSleepTimeout) {
3588 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003589 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3590 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003591 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3592 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3593 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003594 }
3595
3596 if (mStoppingActivities.size() > 0) {
3597 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003598 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003599 + mStoppingActivities.size() + " activities");
3600 scheduleIdleLocked();
3601 dontSleep = true;
3602 }
3603
3604 if (mGoingToSleepActivities.size() > 0) {
3605 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003606 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003607 + mGoingToSleepActivities.size() + " activities");
3608 dontSleep = true;
3609 }
3610
3611 if (dontSleep) {
3612 return;
3613 }
3614 }
3615
Craig Mautnere0a38842013-12-16 16:14:02 -08003616 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3617 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003618 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3619 stacks.get(stackNdx).goToSleep();
3620 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003621 }
3622
3623 removeSleepTimeouts();
3624
3625 if (mGoingToSleep.isHeld()) {
3626 mGoingToSleep.release();
3627 }
3628 if (mService.mShuttingDown) {
3629 mService.notifyAll();
3630 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003631 }
3632
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003633 boolean reportResumedActivityLocked(ActivityRecord r) {
3634 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003635 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003636 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003637 }
3638 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003639 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003640 mWindowManager.executeAppTransition();
3641 return true;
3642 }
3643 return false;
3644 }
3645
Craig Mautner8d341ef2013-03-26 09:03:27 -07003646 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003647 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3648 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003649 int stackNdx = stacks.size() - 1;
3650 while (stackNdx >= 0) {
3651 stacks.get(stackNdx).handleAppCrashLocked(app);
3652 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003653 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003654 }
3655 }
3656
Jose Lima4b6c6692014-08-12 17:41:12 -07003657 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003658 final ActivityStack stack = r.task.stack;
3659 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003660 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3661 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003662 return false;
3663 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003664 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003665 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3666 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003667
3668 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003669 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003670 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003671 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003672 return true;
3673 }
3674
3675 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003676 if (visible && top.fullscreen) {
3677 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003678 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3679 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3680 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3681 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003682 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003683 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3684 // Only the activity set as currently visible behind should actively reset its
3685 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003686 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3687 "requestVisibleBehind: returning visible=" + visible
3688 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3689 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003690 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003691 }
3692
Jose Lima4b6c6692014-08-12 17:41:12 -07003693 stack.setVisibleBehindActivity(visible ? r : null);
3694 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003695 // Make the activity immediately above r opaque.
3696 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3697 if (next != null) {
3698 mService.convertFromTranslucent(next.appToken);
3699 }
3700 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003701 if (top.app != null && top.app.thread != null) {
3702 // Notify the top app of the change.
3703 try {
3704 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3705 } catch (RemoteException e) {
3706 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003707 }
3708 return true;
3709 }
3710
Craig Mautnerbb742462014-07-07 15:28:55 -07003711 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3712 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3713 r.mLaunchTaskBehind = false;
3714 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08003715 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003716 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003717 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003718 mWindowManager.setAppVisibility(r.appToken, false);
3719 }
3720
3721 void scheduleLaunchTaskBehindComplete(IBinder token) {
3722 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3723 }
3724
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003725 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3726 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003727 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003728 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3729 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003730 final int topStackNdx = stacks.size() - 1;
3731 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3732 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003733 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003734 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003735 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003736 }
3737
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003738 void invalidateTaskLayers() {
3739 mTaskLayersChanged = true;
3740 }
3741
3742 void rankTaskLayersIfNeeded() {
3743 if (!mTaskLayersChanged) {
3744 return;
3745 }
3746 mTaskLayersChanged = false;
3747 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3748 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3749 int baseLayer = 0;
3750 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3751 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3752 }
3753 }
3754 }
3755
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003756 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3757 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3758 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3759 final int topStackNdx = stacks.size() - 1;
3760 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3761 final ActivityStack stack = stacks.get(stackNdx);
3762 stack.clearOtherAppTimeTrackers(except);
3763 }
3764 }
3765 }
3766
Craig Mautner8d341ef2013-03-26 09:03:27 -07003767 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003768 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3769 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003770 final int numStacks = stacks.size();
3771 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3772 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003773 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003774 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003775 }
3776 }
3777
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003778 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3779 // Examine all activities currently running in the process.
3780 TaskRecord firstTask = null;
3781 // Tasks is non-null only if two or more tasks are found.
3782 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003783 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3784 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003785 ActivityRecord r = app.activities.get(i);
3786 // First, if we find an activity that is in the process of being destroyed,
3787 // then we just aren't going to do anything for now; we want things to settle
3788 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003789 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003790 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003791 return;
3792 }
3793 // Don't consider any activies that are currently not in a state where they
3794 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003795 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3796 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003797 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003798 continue;
3799 }
3800 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003801 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003802 + " from " + r);
3803 if (firstTask == null) {
3804 firstTask = r.task;
3805 } else if (firstTask != r.task) {
3806 if (tasks == null) {
3807 tasks = new ArraySet<>();
3808 tasks.add(firstTask);
3809 }
3810 tasks.add(r.task);
3811 }
3812 }
3813 }
3814 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003815 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003816 return;
3817 }
3818 // If we have activities in multiple tasks that are in a position to be destroyed,
3819 // let's iterate through the tasks and release the oldest one.
3820 final int numDisplays = mActivityDisplays.size();
3821 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3822 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3823 // Step through all stacks starting from behind, to hit the oldest things first.
3824 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3825 final ActivityStack stack = stacks.get(stackNdx);
3826 // Try to release activities in this stack; if we manage to, we are done.
3827 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3828 return;
3829 }
3830 }
3831 }
3832 }
3833
Amith Yamasani37a40c22015-06-17 13:25:42 -07003834 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003835 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003836 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003837 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003838
Craig Mautner858d8a62013-04-23 17:08:34 -07003839 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003840 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3841 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003842 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003843 final ActivityStack stack = stacks.get(stackNdx);
3844 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003845 TaskRecord task = stack.topTask();
3846 if (task != null) {
3847 mWindowManager.moveTaskToTop(task.taskId);
3848 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003849 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003850 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003851
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003852 ActivityStack stack = getStack(restoreStackId);
3853 if (stack == null) {
3854 stack = mHomeStack;
3855 }
3856 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003857 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003858 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003859 } else {
3860 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003861 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003862 }
Craig Mautner93529a42013-10-04 15:03:13 -07003863 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003864 }
3865
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003866 /**
3867 * Add background users to send boot completed events to.
3868 * @param userId The user being started in the background
3869 * @param uss The state object for the user.
3870 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003871 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003872 mStartingBackgroundUsers.add(uss);
3873 }
3874
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003875 /** Checks whether the userid is a profile of the current user. */
3876 boolean isCurrentProfileLocked(int userId) {
3877 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003878 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003879 }
3880
Chong Zhang45c25ce2015-08-10 22:18:26 -07003881 /** Checks whether the activity should be shown for current user. */
3882 boolean okToShowLocked(ActivityRecord r) {
3883 return r != null && (isCurrentProfileLocked(r.userId)
3884 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3885 }
3886
Craig Mautnerde4ef022013-04-07 19:01:33 -07003887 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003888 ArrayList<ActivityRecord> stops = null;
3889
3890 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003891 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3892 ActivityRecord s = mStoppingActivities.get(activityNdx);
3893 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003894 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003895 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3896 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003897 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003898 if (s.finishing) {
3899 // If this activity is finishing, it is sitting on top of
3900 // everyone else but we now know it is no longer needed...
3901 // so get rid of it. Otherwise, we need to go through the
3902 // normal flow and hide it once we determine that it is
3903 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003904 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003905 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003906 }
3907 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003908 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003909 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003910 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003911 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003912 }
3913 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003914 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003915 }
3916 }
3917
3918 return stops;
3919 }
3920
Craig Mautnercf910b02013-04-23 11:23:27 -07003921 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003922 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3923 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3924 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3925 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003926 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003927 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003928 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003929 if (r == null) Slog.e(TAG,
3930 "validateTop...: null top activity, stack=" + stack);
3931 else {
3932 final ActivityRecord pausing = stack.mPausingActivity;
3933 if (pausing != null && pausing == r) Slog.e(TAG,
3934 "validateTop...: top stack has pausing activity r=" + r
3935 + " state=" + state);
3936 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3937 "validateTop...: activity in front not resumed r=" + r
3938 + " state=" + state);
3939 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003940 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003941 final ActivityRecord resumed = stack.mResumedActivity;
3942 if (resumed != null && resumed == r) Slog.e(TAG,
3943 "validateTop...: back stack has resumed activity r=" + r
3944 + " state=" + state);
3945 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3946 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003947 }
3948 }
3949 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003950 }
3951
Craig Mautnere0570202015-05-13 13:06:11 -07003952 private String lockTaskModeToString() {
3953 switch (mLockTaskModeState) {
3954 case LOCK_TASK_MODE_LOCKED:
3955 return "LOCKED";
3956 case LOCK_TASK_MODE_PINNED:
3957 return "PINNED";
3958 case LOCK_TASK_MODE_NONE:
3959 return "NONE";
3960 default: return "unknown=" + mLockTaskModeState;
3961 }
3962 }
3963
Craig Mautner27084302013-03-25 08:05:25 -07003964 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003965 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003966 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003967 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3968 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3969 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003970 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003971 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3972 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3973 if (packages.size() > 0) {
3974 pw.println(" mLockTaskPackages (userId:packages)=");
3975 for (int i = 0; i < packages.size(); ++i) {
3976 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3977 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3978 }
3979 }
3980 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003981 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003982
Craig Mautner20e72272013-04-01 13:45:53 -07003983 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003984 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003985 }
3986
Dianne Hackborn390517b2013-05-30 15:03:32 -07003987 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3988 boolean needSep, String prefix) {
3989 if (activity != null) {
3990 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3991 if (needSep) {
3992 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003993 }
3994 pw.print(prefix);
3995 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003996 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003997 }
3998 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003999 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004000 }
4001
Craig Mautner8d341ef2013-03-26 09:03:27 -07004002 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4003 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004004 boolean printed = false;
4005 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08004006 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4007 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004008 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07004009 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08004010 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07004011 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012 final ActivityStack stack = stacks.get(stackNdx);
4013 StringBuilder stackHeader = new StringBuilder(128);
4014 stackHeader.append(" Stack #");
4015 stackHeader.append(stack.mStackId);
4016 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004017 stackHeader.append("\n");
4018 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
4019 stackHeader.append("\n");
4020 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004021 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
4022 needSep, stackHeader.toString());
4023 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
4024 !dumpAll, false, dumpPackage, true,
4025 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004026
Craig Mautner4a1cb222013-12-04 16:14:06 -08004027 needSep = printed;
4028 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4029 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004030 if (pr) {
4031 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004032 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004033 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004034 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
4035 " mResumedActivity: ");
4036 if (pr) {
4037 printed = true;
4038 needSep = false;
4039 }
4040 if (dumpAll) {
4041 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4042 " mLastPausedActivity: ");
4043 if (pr) {
4044 printed = true;
4045 needSep = true;
4046 }
4047 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4048 needSep, " mLastNoHistoryActivity: ");
4049 }
4050 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004051 }
4052 }
4053
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004054 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4055 false, dumpPackage, true, " Activities waiting to finish:", null);
4056 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4057 false, dumpPackage, true, " Activities waiting to stop:", null);
4058 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
4059 false, dumpPackage, true, " Activities waiting for another to become visible:",
4060 null);
4061 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4062 false, dumpPackage, true, " Activities waiting to sleep:", null);
4063 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4064 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004065
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004066 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004067 }
4068
Dianne Hackborn390517b2013-05-30 15:03:32 -07004069 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004070 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004071 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004072 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004073 String innerPrefix = null;
4074 String[] args = null;
4075 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004076 for (int i=list.size()-1; i>=0; i--) {
4077 final ActivityRecord r = list.get(i);
4078 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4079 continue;
4080 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004081 if (innerPrefix == null) {
4082 innerPrefix = prefix + " ";
4083 args = new String[0];
4084 }
4085 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004086 final boolean full = !brief && (complete || !r.isInHistory());
4087 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004088 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004089 needNL = false;
4090 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004091 if (header1 != null) {
4092 pw.println(header1);
4093 header1 = null;
4094 }
4095 if (header2 != null) {
4096 pw.println(header2);
4097 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004098 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004099 if (lastTask != r.task) {
4100 lastTask = r.task;
4101 pw.print(prefix);
4102 pw.print(full ? "* " : " ");
4103 pw.println(lastTask);
4104 if (full) {
4105 lastTask.dump(pw, prefix + " ");
4106 } else if (complete) {
4107 // Complete + brief == give a summary. Isn't that obvious?!?
4108 if (lastTask.intent != null) {
4109 pw.print(prefix); pw.print(" ");
4110 pw.println(lastTask.intent.toInsecureStringWithClip());
4111 }
4112 }
4113 }
4114 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4115 pw.print(" #"); pw.print(i); pw.print(": ");
4116 pw.println(r);
4117 if (full) {
4118 r.dump(pw, innerPrefix);
4119 } else if (complete) {
4120 // Complete + brief == give a summary. Isn't that obvious?!?
4121 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4122 if (r.app != null) {
4123 pw.print(innerPrefix); pw.println(r.app);
4124 }
4125 }
4126 if (client && r.app != null && r.app.thread != null) {
4127 // flush anything that is already in the PrintWriter since the thread is going
4128 // to write to the file descriptor directly
4129 pw.flush();
4130 try {
4131 TransferPipe tp = new TransferPipe();
4132 try {
4133 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4134 r.appToken, innerPrefix, args);
4135 // Short timeout, since blocking here can
4136 // deadlock with the application.
4137 tp.go(fd, 2000);
4138 } finally {
4139 tp.kill();
4140 }
4141 } catch (IOException e) {
4142 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4143 } catch (RemoteException e) {
4144 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4145 }
4146 needNL = true;
4147 }
4148 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004149 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004150 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004151
Craig Mautnerf3333272013-04-22 10:55:53 -07004152 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004153 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4154 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004155 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4156 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004157 }
4158
4159 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004160 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004161 }
4162
4163 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004164 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4165 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004166 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4167 }
4168
Craig Mautner05d29032013-05-03 13:40:13 -07004169 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004170 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4171 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4172 }
Craig Mautner05d29032013-05-03 13:40:13 -07004173 }
4174
Craig Mautner0eea92c2013-05-16 13:35:39 -07004175 void removeSleepTimeouts() {
4176 mSleepTimeout = false;
4177 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4178 }
4179
4180 final void scheduleSleepTimeout() {
4181 removeSleepTimeouts();
4182 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4183 }
4184
Craig Mautner4a1cb222013-12-04 16:14:06 -08004185 @Override
4186 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004187 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004188 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4189 }
4190
4191 @Override
4192 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004193 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004194 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4195 }
4196
4197 @Override
4198 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004199 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004200 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4201 }
4202
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004203 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004204 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004205 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004206 newDisplay = mActivityDisplays.get(displayId) == null;
4207 if (newDisplay) {
4208 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004209 if (activityDisplay.mDisplay == null) {
4210 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4211 return;
4212 }
Craig Mautner4504de52013-12-20 09:06:56 -08004213 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004214 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004215 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004216 }
Craig Mautner4504de52013-12-20 09:06:56 -08004217 if (newDisplay) {
4218 mWindowManager.onDisplayAdded(displayId);
4219 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004220 }
4221
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004222 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4223 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4224 return;
4225 }
4226 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4227 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4228 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4229 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4230 }
4231
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004232 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004233 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004234 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4235 if (activityDisplay != null) {
4236 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004237 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004238 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004239 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004240 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004241 }
4242 }
4243 mWindowManager.onDisplayRemoved(displayId);
4244 }
4245
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004246 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004247 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004248 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4249 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004250 // TODO: Update the bounds.
4251 }
4252 }
4253 mWindowManager.onDisplayChanged(displayId);
4254 }
4255
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004256 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004257 StackInfo info = new StackInfo();
4258 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4259 info.displayId = Display.DEFAULT_DISPLAY;
4260 info.stackId = stack.mStackId;
4261
4262 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4263 final int numTasks = tasks.size();
4264 int[] taskIds = new int[numTasks];
4265 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004266 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004267 for (int i = 0; i < numTasks; ++i) {
4268 final TaskRecord task = tasks.get(i);
4269 taskIds[i] = task.taskId;
4270 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4271 : task.realActivity != null ? task.realActivity.flattenToString()
4272 : task.getTopActivity() != null ? task.getTopActivity().packageName
4273 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004274 taskBounds[i] = new Rect();
4275 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004276 }
4277 info.taskIds = taskIds;
4278 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004279 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004280 return info;
4281 }
4282
4283 StackInfo getStackInfoLocked(int stackId) {
4284 ActivityStack stack = getStack(stackId);
4285 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004286 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004287 }
4288 return null;
4289 }
4290
4291 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004292 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004293 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4294 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004295 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004296 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004297 }
4298 }
4299 return list;
4300 }
4301
Craig Mautner15df08a2015-04-01 12:17:18 -07004302 TaskRecord getLockedTaskLocked() {
4303 final int top = mLockTaskModeTasks.size() - 1;
4304 if (top >= 0) {
4305 return mLockTaskModeTasks.get(top);
4306 }
4307 return null;
4308 }
4309
4310 boolean isLockedTask(TaskRecord task) {
4311 return mLockTaskModeTasks.contains(task);
4312 }
4313
4314 boolean isLastLockedTask(TaskRecord task) {
4315 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4316 }
4317
4318 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004319 if (!mLockTaskModeTasks.remove(task)) {
4320 return;
4321 }
4322 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4323 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004324 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004325 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4326 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004327 final Message lockTaskMsg = Message.obtain();
4328 lockTaskMsg.arg1 = task.userId;
4329 lockTaskMsg.what = LOCK_TASK_END_MSG;
4330 mHandler.sendMessage(lockTaskMsg);
4331 }
4332 }
4333
Chong Zhangc806d902015-11-30 09:44:27 -08004334 private void showNonResizeableDockToast(int taskId) {
4335 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08004336 }
4337
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004338 void showLockTaskToast() {
4339 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004340 }
4341
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004342 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4343 if (mLockTaskModeTasks.contains(task)) {
4344 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4345 }
4346 }
4347
Craig Mautner432f64e2015-05-20 14:59:57 -07004348 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4349 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004350 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004351 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004352 final TaskRecord lockedTask = getLockedTaskLocked();
4353 if (lockedTask != null) {
4354 removeLockedTaskLocked(lockedTask);
4355 if (!mLockTaskModeTasks.isEmpty()) {
4356 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004357 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4358 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004359 lockedTask.performClearTaskLocked();
4360 resumeTopActivitiesLocked();
4361 return;
4362 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004363 }
Craig Mautnere0570202015-05-13 13:06:11 -07004364 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4365 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004366 return;
4367 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004368
4369 // Should have already been checked, but do it again.
4370 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004371 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4372 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004373 return;
4374 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004375 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004376 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004377 return;
4378 }
4379
4380 if (mLockTaskModeTasks.isEmpty()) {
4381 // First locktask.
4382 final Message lockTaskMsg = Message.obtain();
4383 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4384 lockTaskMsg.arg1 = task.userId;
4385 lockTaskMsg.what = LOCK_TASK_START_MSG;
4386 lockTaskMsg.arg2 = lockTaskModeState;
4387 mHandler.sendMessage(lockTaskMsg);
4388 }
4389 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004390 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4391 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004392 mLockTaskModeTasks.remove(task);
4393 mLockTaskModeTasks.add(task);
4394
4395 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004396 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004397 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004398
4399 if (andResume) {
4400 findTaskToMoveToFrontLocked(task, 0, null, reason);
4401 resumeTopActivitiesLocked();
4402 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004403 }
4404
4405 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004406 return isLockTaskModeViolation(task, false);
4407 }
4408
4409 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4410 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004411 return false;
4412 }
4413 final int lockTaskAuth = task.mLockTaskAuth;
4414 switch (lockTaskAuth) {
4415 case LOCK_TASK_AUTH_DONT_LOCK:
4416 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004417 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004418 case LOCK_TASK_AUTH_LAUNCHABLE:
4419 case LOCK_TASK_AUTH_WHITELISTED:
4420 return false;
4421 case LOCK_TASK_AUTH_PINNABLE:
4422 // Pinnable tasks can't be launched on top of locktask tasks.
4423 return !mLockTaskModeTasks.isEmpty();
4424 default:
4425 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4426 return true;
4427 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004428 }
4429
Craig Mautner15df08a2015-04-01 12:17:18 -07004430 void onLockTaskPackagesUpdatedLocked() {
4431 boolean didSomething = false;
4432 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4433 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004434 final boolean wasWhitelisted =
4435 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4436 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004437 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004438 final boolean isWhitelisted =
4439 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4440 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4441 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004442 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004443 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4444 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004445 removeLockedTaskLocked(lockedTask);
4446 lockedTask.performClearTaskLocked();
4447 didSomething = true;
4448 }
4449 }
4450 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4451 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4452 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4453 final ActivityStack stack = stacks.get(stackNdx);
4454 stack.onLockTaskPackagesUpdatedLocked();
4455 }
4456 }
Craig Mautnere0570202015-05-13 13:06:11 -07004457 final ActivityRecord r = topRunningActivityLocked();
4458 final TaskRecord task = r != null ? r.task : null;
4459 if (mLockTaskModeTasks.isEmpty() && task != null
4460 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4461 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004462 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4463 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4464 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4465 false);
4466 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004467 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004468 if (didSomething) {
4469 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004470 }
4471 }
4472
Benjamin Franz43261142015-02-11 15:59:44 +00004473 int getLockTaskModeState() {
4474 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004475 }
4476
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004477 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004478
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004479 public ActivityStackSupervisorHandler(Looper looper) {
4480 super(looper);
4481 }
4482
Craig Mautnerf3333272013-04-22 10:55:53 -07004483 void activityIdleInternal(ActivityRecord r) {
4484 synchronized (mService) {
4485 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4486 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004487 }
4488
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004489 @Override
4490 public void handleMessage(Message msg) {
4491 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004492 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004493 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4494 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004495 if (mService.mDidDexOpt) {
4496 mService.mDidDexOpt = false;
4497 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4498 nmsg.obj = msg.obj;
4499 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4500 return;
4501 }
4502 // We don't at this point know if the activity is fullscreen,
4503 // so we need to be conservative and assume it isn't.
4504 activityIdleInternal((ActivityRecord)msg.obj);
4505 } break;
4506 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004507 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004508 activityIdleInternal((ActivityRecord)msg.obj);
4509 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004510 case RESUME_TOP_ACTIVITY_MSG: {
4511 synchronized (mService) {
4512 resumeTopActivitiesLocked();
4513 }
4514 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004515 case SLEEP_TIMEOUT_MSG: {
4516 synchronized (mService) {
4517 if (mService.isSleepingOrShuttingDown()) {
4518 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4519 mSleepTimeout = true;
4520 checkReadyForSleepLocked();
4521 }
4522 }
4523 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004524 case LAUNCH_TIMEOUT_MSG: {
4525 if (mService.mDidDexOpt) {
4526 mService.mDidDexOpt = false;
4527 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4528 return;
4529 }
4530 synchronized (mService) {
4531 if (mLaunchingActivity.isHeld()) {
4532 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4533 if (VALIDATE_WAKE_LOCK_CALLER
4534 && Binder.getCallingUid() != Process.myUid()) {
4535 throw new IllegalStateException("Calling must be system uid");
4536 }
4537 mLaunchingActivity.release();
4538 }
4539 }
4540 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004541 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004542 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004543 } break;
4544 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004545 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004546 } break;
4547 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004548 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004549 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004550 case CONTAINER_CALLBACK_VISIBILITY: {
4551 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004552 final IActivityContainerCallback callback = container.mCallback;
4553 if (callback != null) {
4554 try {
4555 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4556 } catch (RemoteException e) {
4557 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004558 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004559 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004560 case LOCK_TASK_START_MSG: {
4561 // When lock task starts, we disable the status bars.
4562 try {
Jason Monk62515be2014-05-21 16:06:19 -04004563 if (mLockTaskNotify == null) {
4564 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004565 }
Jason Monk62515be2014-05-21 16:06:19 -04004566 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004567 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004568 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004569 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004570 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004571 flags = StatusBarManager.DISABLE_MASK
4572 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004573 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004574 flags = StatusBarManager.DISABLE_MASK
4575 & (~StatusBarManager.DISABLE_BACK)
4576 & (~StatusBarManager.DISABLE_HOME)
4577 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004578 }
4579 getStatusBarService().disable(flags, mToken,
4580 mService.mContext.getPackageName());
4581 }
4582 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004583 if (getDevicePolicyManager() != null) {
4584 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004585 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004586 }
justinzhang5286d3f2014-05-12 17:06:01 -04004587 } catch (RemoteException ex) {
4588 throw new RuntimeException(ex);
4589 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004590 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004591 case LOCK_TASK_END_MSG: {
4592 // When lock task ends, we enable the status bars.
4593 try {
Jason Monk62515be2014-05-21 16:06:19 -04004594 if (getStatusBarService() != null) {
4595 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4596 mService.mContext.getPackageName());
4597 }
4598 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004599 if (getDevicePolicyManager() != null) {
4600 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4601 msg.arg1);
4602 }
Jason Monk62515be2014-05-21 16:06:19 -04004603 if (mLockTaskNotify == null) {
4604 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4605 }
4606 mLockTaskNotify.show(false);
4607 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004608 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004609 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004610 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004611 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004612 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004613 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004614 new LockPatternUtils(mService.mContext)
4615 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004616 }
4617 } catch (SettingNotFoundException e) {
4618 // No setting, don't lock.
4619 }
justinzhang5286d3f2014-05-12 17:06:01 -04004620 } catch (RemoteException ex) {
4621 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004622 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004623 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004624 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004625 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004626 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4627 if (mLockTaskNotify == null) {
4628 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4629 }
4630 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4631 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004632 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4633 final ActivityContainer container = (ActivityContainer) msg.obj;
4634 final IActivityContainerCallback callback = container.mCallback;
4635 if (callback != null) {
4636 try {
4637 callback.onAllActivitiesComplete(container.asBinder());
4638 } catch (RemoteException e) {
4639 }
4640 }
4641 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004642 case LAUNCH_TASK_BEHIND_COMPLETE: {
4643 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004644 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004645 if (r != null) {
4646 handleLaunchTaskBehindCompleteLocked(r);
4647 }
4648 }
4649 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004650
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004651 }
4652 }
4653 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004654
Ying Wangb081a592014-04-22 15:20:16 -07004655 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004656 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004657 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004658 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004659 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004660 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004661 ActivityRecord mParentActivity = null;
4662 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004663
Craig Mautnere3a00d72014-04-16 08:31:19 -07004664 boolean mVisible = true;
4665
Craig Mautner4a1cb222013-12-04 16:14:06 -08004666 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004667 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004668
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004669 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4670 final static int CONTAINER_STATE_NO_SURFACE = 1;
4671 final static int CONTAINER_STATE_FINISHING = 2;
4672 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4673
4674 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004675 synchronized (mService) {
4676 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004677 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004678 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004679 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004680 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004681 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004682
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004683 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004684 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004685 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004686 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004687 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004688 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004689 }
4690
4691 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004692 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004693 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004694 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4695 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004696 return;
4697 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004698 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004699 }
4700 }
4701
4702 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004703 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004704 synchronized (mService) {
4705 if (mActivityDisplay != null) {
4706 return mActivityDisplay.mDisplayId;
4707 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004708 }
4709 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004710 }
4711
Jeff Brownca9bc702014-02-11 14:32:56 -08004712 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004713 public int getStackId() {
4714 synchronized (mService) {
4715 return mStackId;
4716 }
4717 }
4718
4719 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004720 public boolean injectEvent(InputEvent event) {
4721 final long origId = Binder.clearCallingIdentity();
4722 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004723 synchronized (mService) {
4724 if (mActivityDisplay != null) {
4725 return mInputManagerInternal.injectInputEvent(event,
4726 mActivityDisplay.mDisplayId,
4727 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4728 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004729 }
4730 return false;
4731 } finally {
4732 Binder.restoreCallingIdentity(origId);
4733 }
4734 }
4735
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004736 @Override
4737 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004738 synchronized (mService) {
4739 if (mContainerState == CONTAINER_STATE_FINISHING) {
4740 return;
4741 }
4742 mContainerState = CONTAINER_STATE_FINISHING;
4743
Craig Mautnerd163e752014-06-13 17:18:47 -07004744 long origId = Binder.clearCallingIdentity();
4745 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004746 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004747 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004748 } finally {
4749 Binder.restoreCallingIdentity(origId);
4750 }
4751 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004752 }
4753
Craig Mautner60257702014-09-17 15:02:33 -07004754 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004755 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004756 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004757 if (mActivityDisplay != null) {
4758 mActivityDisplay.detachActivitiesLocked(mStack);
4759 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004760 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004761 }
4762 }
4763
4764 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004765 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004766 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004767 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004768 Binder.getCallingUid(), mCurrentUser, false,
4769 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004770
Craig Mautnere0a38842013-12-16 16:14:02 -08004771 // TODO: Switch to user app stacks here.
4772 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004773 final Uri data = intent.getData();
4774 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4775 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004776 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004777 checkEmbeddedAllowedInner(userId, intent, mimeType);
4778
4779 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4780 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004781 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004782 }
4783
4784 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004785 public final int startActivityIntentSender(IIntentSender intentSender)
4786 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004787 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4788
4789 if (!(intentSender instanceof PendingIntentRecord)) {
4790 throw new IllegalArgumentException("Bad PendingIntent object");
4791 }
4792
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004793 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004794 Binder.getCallingUid(), mCurrentUser, false,
4795 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004796
4797 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4798 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4799 pendingIntent.key.requestResolvedType);
4800
4801 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4802 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4803 }
4804
4805 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004806 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004807 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004808 throw new SecurityException(
4809 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4810 }
4811 }
4812
Craig Mautnerdf88d732014-01-27 09:21:32 -08004813 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004814 public IBinder asBinder() {
4815 return this;
4816 }
4817
Craig Mautner4504de52013-12-20 09:06:56 -08004818 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004819 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004820 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004821 }
4822
Craig Mautner4a1cb222013-12-04 16:14:06 -08004823 ActivityStackSupervisor getOuter() {
4824 return ActivityStackSupervisor.this;
4825 }
4826
Craig Mautnerd163e752014-06-13 17:18:47 -07004827 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004828 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004829 }
4830
Craig Mautner6985bad2014-04-21 15:22:06 -07004831 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004832 void setVisible(boolean visible) {
4833 if (mVisible != visible) {
4834 mVisible = visible;
4835 if (mCallback != null) {
4836 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4837 0 /* unused */, this).sendToTarget();
4838 }
4839 }
4840 }
4841
Craig Mautner6985bad2014-04-21 15:22:06 -07004842 void setDrawn() {
4843 }
4844
Craig Mautner1b4bf852014-05-26 15:06:32 -07004845 // You can always start a new task on a regular ActivityStack.
4846 boolean isEligibleForNewTasks() {
4847 return true;
4848 }
4849
Craig Mautnerd163e752014-06-13 17:18:47 -07004850 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004851 detachLocked();
4852 deleteActivityContainer(this);
4853 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004854 }
4855
Craig Mautner34b73df2014-01-12 21:11:08 -08004856 @Override
4857 public String toString() {
4858 return mIdString + (mActivityDisplay == null ? "N" : "A");
4859 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004860 }
4861
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004862 private class VirtualActivityContainer extends ActivityContainer {
4863 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004864 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004865
4866 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4867 super(getNextStackId());
4868 mParentActivity = parent;
4869 mCallback = callback;
4870 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004871 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004872 }
4873
4874 @Override
4875 public void setSurface(Surface surface, int width, int height, int density) {
4876 super.setSurface(surface, width, height, density);
4877
4878 synchronized (mService) {
4879 final long origId = Binder.clearCallingIdentity();
4880 try {
4881 setSurfaceLocked(surface, width, height, density);
4882 } finally {
4883 Binder.restoreCallingIdentity(origId);
4884 }
4885 }
4886 }
4887
4888 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4889 if (mContainerState == CONTAINER_STATE_FINISHING) {
4890 return;
4891 }
4892 VirtualActivityDisplay virtualActivityDisplay =
4893 (VirtualActivityDisplay) mActivityDisplay;
4894 if (virtualActivityDisplay == null) {
4895 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004896 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004897 mActivityDisplay = virtualActivityDisplay;
4898 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004899 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004900 }
4901
4902 if (mSurface != null) {
4903 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004904 }
4905
Craig Mautner6985bad2014-04-21 15:22:06 -07004906 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004907 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004908 mStack.resumeTopActivityLocked(null);
4909 } else {
4910 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004911 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004912 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004913 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004914 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004915 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004916
Craig Mautnerd163e752014-06-13 17:18:47 -07004917 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004918
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004919 if (DEBUG_STACK) Slog.d(TAG_STACK,
4920 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004921 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004922
Craig Mautner6985bad2014-04-21 15:22:06 -07004923 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004924 boolean isAttachedLocked() {
4925 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004926 }
4927
4928 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004929 void setDrawn() {
4930 synchronized (mService) {
4931 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004932 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004933 }
4934 }
4935
Craig Mautner1b4bf852014-05-26 15:06:32 -07004936 // Never start a new task on an ActivityView if it isn't explicitly specified.
4937 @Override
4938 boolean isEligibleForNewTasks() {
4939 return false;
4940 }
4941
Craig Mautnerd163e752014-06-13 17:18:47 -07004942 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004943 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004944 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4945 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4946 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4947 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4948 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004949 }
4950 }
4951
Craig Mautner4a1cb222013-12-04 16:14:06 -08004952 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4953 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004954 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004955 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004956 int mDisplayId;
4957 Display mDisplay;
4958 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004959
Craig Mautner4a1cb222013-12-04 16:14:06 -08004960 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4961 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004962 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004963
Jose Lima4b6c6692014-08-12 17:41:12 -07004964 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004965
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004966 ActivityDisplay() {
4967 }
Craig Mautner4504de52013-12-20 09:06:56 -08004968
Craig Mautner1a70a162014-09-13 12:09:31 -07004969 // After instantiation, check that mDisplay is not null before using this. The alternative
4970 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004971 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004972 final Display display = mDisplayManager.getDisplay(displayId);
4973 if (display == null) {
4974 return;
4975 }
4976 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004977 }
4978
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004979 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004980 mDisplay = display;
4981 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004982 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004983 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004984
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004985 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004986 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004987 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4988 + " onTop=" + onTop);
4989 if (onTop) {
4990 mStacks.add(stack);
4991 } else {
4992 mStacks.add(0, stack);
4993 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004994 }
4995
Craig Mautnere0a38842013-12-16 16:14:02 -08004996 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004997 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004998 + " from displayId=" + mDisplayId);
4999 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08005000 }
5001
Jose Lima4b6c6692014-08-12 17:41:12 -07005002 void setVisibleBehindActivity(ActivityRecord r) {
5003 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005004 }
5005
Jose Lima4b6c6692014-08-12 17:41:12 -07005006 boolean hasVisibleBehindActivity() {
5007 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005008 }
5009
Craig Mautner34b73df2014-01-12 21:11:08 -08005010 @Override
5011 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005012 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
5013 }
5014 }
5015
5016 class VirtualActivityDisplay extends ActivityDisplay {
5017 VirtualDisplay mVirtualDisplay;
5018
Craig Mautner6985bad2014-04-21 15:22:06 -07005019 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005020 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07005021 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
5022 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
5023 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
5024 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005025
5026 init(mVirtualDisplay.getDisplay());
5027
5028 mWindowManager.handleDisplayAdded(mDisplayId);
5029 }
5030
5031 void setSurface(Surface surface) {
5032 if (mVirtualDisplay != null) {
5033 mVirtualDisplay.setSurface(surface);
5034 }
5035 }
5036
5037 @Override
5038 void detachActivitiesLocked(ActivityStack stack) {
5039 super.detachActivitiesLocked(stack);
5040 if (mVirtualDisplay != null) {
5041 mVirtualDisplay.release();
5042 mVirtualDisplay = null;
5043 }
5044 }
5045
5046 @Override
5047 public String toString() {
5048 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005049 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005050 }
Jose Lima58e66d62014-05-27 20:00:27 -07005051
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07005052 /**
5053 * Adjust bounds to stay within stack bounds.
5054 *
5055 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
5056 * that keep them unchanged, but be contained within the stack bounds.
5057 *
5058 * @param bounds Bounds to be adjusted.
5059 * @param stackBounds Bounds within which the other bounds should remain.
5060 */
5061 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
5062 if (stackBounds == null || stackBounds.contains(bounds)) {
5063 return;
5064 }
5065
5066 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
5067 final int maxRight = stackBounds.right
5068 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
5069 int horizontalDiff = stackBounds.left - bounds.left;
5070 if ((horizontalDiff < 0 && bounds.left >= maxRight)
5071 || (bounds.left + horizontalDiff >= maxRight)) {
5072 horizontalDiff = maxRight - bounds.left;
5073 }
5074 bounds.left += horizontalDiff;
5075 bounds.right += horizontalDiff;
5076 }
5077
5078 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
5079 final int maxBottom = stackBounds.bottom
5080 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
5081 int verticalDiff = stackBounds.top - bounds.top;
5082 if ((verticalDiff < 0 && bounds.top >= maxBottom)
5083 || (bounds.top + verticalDiff >= maxBottom)) {
5084 verticalDiff = maxBottom - bounds.top;
5085 }
5086 bounds.top += verticalDiff;
5087 bounds.bottom += verticalDiff;
5088 }
5089 }
5090
Craig Mautner27084302013-03-25 08:05:25 -07005091}