blob: 2d5f76c2c2e263b7fdd146b3ed9814571bce6ef0 [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;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080020import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
23import static android.app.ActivityManager.RESIZE_MODE_FORCED;
24import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
Wale Ogunwale3797c222015-10-27 14:21:58 -070025import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
26import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
27import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
29import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.HOME_STACK_ID;
31import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
32import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
33import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070034import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
35import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080036import static android.content.Intent.FLAG_ACTIVITY_LAUNCH_TO_SIDE;
Filip Gruszczynskidc394902015-12-14 10:20:22 -080037import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
Craig Mautner29219d92013-04-16 20:19:12 -070038import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
39import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070040import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070041import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070042import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080043import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PERMISSIONS_REVIEW;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RESULTS;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_USER_LEAVING;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RESULTS;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
73import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
74import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_USER_LEAVING;
75import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
76import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
77import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
78import static com.android.server.am.ActivityManagerService.ANIMATE;
Craig Mautner05d29032013-05-03 13:40:13 -070079import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080080import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070081import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
82import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080083import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
84import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
85import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
86import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
87import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
88import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
89import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
90import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Craig Mautner15df08a2015-04-01 12:17:18 -070091import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
92import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010093import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070094import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
95import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070096
Svetoslav7008b512015-06-24 18:47:07 -070097import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070098import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800100import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700101import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800102import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103import android.app.ActivityOptions;
104import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -0700105import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800106import android.app.IActivityContainer;
107import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700108import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800109import android.app.IActivityManager.WaitResult;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import android.app.IApplicationThread;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100111import android.app.KeyguardManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700112import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -0700113import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700114import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400115import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -0400116import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700120import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700122import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700124import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700125import android.content.pm.PackageManager;
126import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100127import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800129import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800130import android.hardware.display.DisplayManager;
131import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800132import android.hardware.display.DisplayManagerGlobal;
133import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800134import android.hardware.input.InputManager;
135import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -0800136import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700137import android.os.Binder;
Svet Ganov9c165d72015-12-01 19:52:26 -0800138import android.os.Build;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700139import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700140import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700141import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700143import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700144import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700145import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700146import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700147import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700148import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400149import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700150import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700151import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700152import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700153import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100154import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700155import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700156import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400157import android.provider.Settings;
158import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700159import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700160import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700161import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700162import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700163import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800164import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800165import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800166import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800167import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800168import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800169import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800170
Craig Mautner23ac33b2013-04-01 16:26:35 -0700171import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700172import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800173import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700174import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400175import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700176import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400177import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800178import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700179import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700180import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700181
Craig Mautner8d341ef2013-03-26 09:03:27 -0700182import java.io.FileDescriptor;
183import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700184import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700185import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700186import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700187import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700188import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700189
Craig Mautner4a1cb222013-12-04 16:14:06 -0800190public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800191 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700192 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700194 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700196 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700197 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700198 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
200 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700201 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700203 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
204 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700206 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800207
Craig Mautnerf3333272013-04-22 10:55:53 -0700208 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700209 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700210
Craig Mautner0eea92c2013-05-16 13:35:39 -0700211 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700212 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700213
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700214 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700215 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216
Craig Mautner05d29032013-05-03 13:40:13 -0700217 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
218 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
219 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700220 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700221 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800222 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
223 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
224 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700225 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400226 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
227 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700228 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700229 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700230 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800231
Wale Ogunwale040b4702015-08-06 18:10:50 -0700232 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700233
Jason Monk62515be2014-05-21 16:06:19 -0400234 private static final String LOCK_TASK_TAG = "Lock-to-App";
235
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236 // Used to indicate if an object (e.g. stack) that we are trying to get
237 // should be created if it doesn't exist already.
238 private static final boolean CREATE_IF_NEEDED = true;
239
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700240 // Used to indicate that windows of activities should be preserved during the resize.
241 static final boolean PRESERVE_WINDOWS = true;
242
Wale Ogunwale040b4702015-08-06 18:10:50 -0700243 // Used to indicate if an object (e.g. task) should be moved/created
244 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700245 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700246
247 // Used to indicate that an objects (e.g. task) removal from its container
248 // (e.g. stack) is due to it moving to another container.
249 static final boolean MOVING = true;
250
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700251 // Force the focus to change to the stack we are moving a task to..
252 static final boolean FORCE_FOCUS = true;
253
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700254 // Restore task from the saved recents if it can't be found in any live stack.
255 static final boolean RESTORE_FROM_RECENTS = true;
256
Svetoslav7008b512015-06-24 18:47:07 -0700257 // Activity actions an app cannot start if it uses a permission which is not granted.
258 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
259 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700260
Svetoslav7008b512015-06-24 18:47:07 -0700261 static {
262 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
263 Manifest.permission.CAMERA);
264 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
265 Manifest.permission.CAMERA);
266 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
267 Manifest.permission.CALL_PHONE);
268 }
269
Svet Ganov99b60432015-06-27 13:15:22 -0700270 /** Action restriction: launching the activity is not restricted. */
271 private static final int ACTIVITY_RESTRICTION_NONE = 0;
272 /** Action restriction: launching the activity is restricted by a permission. */
273 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
274 /** Action restriction: launching the activity is restricted by an app op. */
275 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700276
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700277 // The height/width divide used when fitting a task within a bounds with method
278 // {@link #fitWithinBounds}.
279 // We always want the task to to be visible in the bounds without affecting its size when
280 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
281 // the input bounds right or bottom side minus the width or height divided by this value.
282 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
283
justinzhang5286d3f2014-05-12 17:06:01 -0400284 /** Status Bar Service **/
285 private IBinder mToken = new Binder();
286 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400287 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400288
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700289 // For debugging to make sure the caller when acquiring/releasing our
290 // wake lock is the system process.
291 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700292
Craig Mautner27084302013-03-25 08:05:25 -0700293 final ActivityManagerService mService;
294
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800295 private final RecentTasks mRecentTasks;
296
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700297 final ActivityStackSupervisorHandler mHandler;
298
299 /** Short cut */
300 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800301 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700302
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700303 /** Counter for next free stack ID to use for dynamic activity stacks. */
304 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700305
306 /** Task identifier that activities are currently being started in. Incremented each time a
307 * new task is created. */
308 private int mCurTaskId = 0;
309
Craig Mautner2420ead2013-04-01 17:13:20 -0700310 /** The current user */
311 private int mCurrentUser;
312
Craig Mautnere0a38842013-12-16 16:14:02 -0800313 /** The stack containing the launcher app. Assumed to always be attached to
314 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700315 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700316
Craig Mautnere0a38842013-12-16 16:14:02 -0800317 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700318 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700319
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
321 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800322 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800323 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700324
325 /** List of activities that are waiting for a new activity to become visible before completing
326 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800327 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700328
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800330 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700331
332 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800333 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700334
Craig Mautnerde4ef022013-04-07 19:01:33 -0700335 /** List of activities that are ready to be stopped, but waiting for the next activity to
336 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800337 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700338
Craig Mautnerf3333272013-04-22 10:55:53 -0700339 /** List of activities that are ready to be finished, but waiting for the previous activity to
340 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800341 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700342
Craig Mautner0eea92c2013-05-16 13:35:39 -0700343 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800344 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700345
Craig Mautnerf3333272013-04-22 10:55:53 -0700346 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700347 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700348
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700349 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700350 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700351
Craig Mautnerde4ef022013-04-07 19:01:33 -0700352 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
353 * is being brought in front of us. */
354 boolean mUserLeaving = false;
355
Craig Mautner0eea92c2013-05-16 13:35:39 -0700356 /** Set when we have taken too long waiting to go to sleep. */
357 boolean mSleepTimeout = false;
358
359 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700360 * We don't want to allow the device to go to sleep while in the process
361 * of launching an activity. This is primarily to allow alarm intent
362 * receivers to launch an activity and get that to run before the device
363 * goes back to sleep.
364 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700365 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700366
367 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368 * Set when the system is going to sleep, until we have
369 * successfully paused the current activity and released our wake lock.
370 * At that point the system is allowed to actually sleep.
371 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700372 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700373
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700374 /** Stack id of the front stack when user switched, indexed by userId. */
375 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700376
Craig Mautner4504de52013-12-20 09:06:56 -0800377 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700379 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380
381 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700382 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383
Jeff Brownca9bc702014-02-11 14:32:56 -0800384 InputManagerInternal mInputManagerInternal;
385
Craig Mautner15df08a2015-04-01 12:17:18 -0700386 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
387 * may be finished until there is only one entry left. If this is empty the system is not
388 * in lockTask mode. */
389 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000390 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700391 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
392 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000393 */
394 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400395 /**
396 * Notifies the user when entering/exiting lock-task.
397 */
398 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800399
Craig Mautner15df08a2015-04-01 12:17:18 -0700400 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700401
Craig Mautner42d04db2014-11-06 12:13:23 -0800402 /** Used to keep resumeTopActivityLocked() from being entered recursively */
403 boolean inResumeTopActivity;
404
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700405 // 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 -0700406 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700407 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700408
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700409 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
410 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
411
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700412 // The default minimal size that will be used if the activity doesn't specify its minimal size.
413 // It will be calculated when the default display gets added.
414 private int mDefaultMinimalSizeOfResizeableTask = -1;
415
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700416 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
417 private boolean mTaskLayersChanged = true;
418
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800419 private final ActivityMetricsLogger mActivityMetricsLogger;
420
Wale Ogunwale39381972015-12-17 17:15:29 -0800421 static class FindTaskResult {
422 ActivityRecord r;
423 boolean matchedByRootAffinity;
424 }
425 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
426
Craig Mautneree36c772014-07-16 14:56:05 -0700427 /**
428 * Description of a request to start a new activity, which has been held
429 * due to app switches being disabled.
430 */
431 static class PendingActivityLaunch {
432 final ActivityRecord r;
433 final ActivityRecord sourceRecord;
434 final int startFlags;
435 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700436 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700437
438 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700439 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700440 r = _r;
441 sourceRecord = _sourceRecord;
442 startFlags = _startFlags;
443 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700444 callerApp = _callerApp;
445 }
446
447 void sendErrorResult(String message) {
448 try {
449 if (callerApp.thread != null) {
450 callerApp.thread.scheduleCrash(message);
451 }
452 } catch (RemoteException e) {
453 Slog.e(TAG, "Exception scheduling crash of failed "
454 + "activity launcher sourceRecord=" + sourceRecord, e);
455 }
Craig Mautneree36c772014-07-16 14:56:05 -0700456 }
457 }
458
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800459 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700460 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800461 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700462 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800463 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jeff Brown2c43c332014-06-12 22:38:59 -0700464 }
465
466 /**
467 * At the time when the constructor runs, the power manager has not yet been
468 * initialized. So we initialize our wakelocks afterwards.
469 */
470 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800471 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700472 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700473 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700474 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700475 }
476
justinzhang5286d3f2014-05-12 17:06:01 -0400477 // This function returns a IStatusBarService. The value is from ServiceManager.
478 // getService and is cached.
479 private IStatusBarService getStatusBarService() {
480 synchronized (mService) {
481 if (mStatusBarService == null) {
482 mStatusBarService = IStatusBarService.Stub.asInterface(
483 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
484 if (mStatusBarService == null) {
485 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
486 }
487 }
488 return mStatusBarService;
489 }
490 }
491
Jason Monk35c62a42014-06-17 10:24:47 -0400492 private IDevicePolicyManager getDevicePolicyManager() {
493 synchronized (mService) {
494 if (mDevicePolicyManager == null) {
495 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
496 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
497 if (mDevicePolicyManager == null) {
498 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
499 }
500 }
501 return mDevicePolicyManager;
502 }
503 }
504
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700505 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800506 synchronized (mService) {
507 mWindowManager = wm;
508
509 mDisplayManager =
510 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
511 mDisplayManager.registerDisplayListener(this, null);
512
513 Display[] displays = mDisplayManager.getDisplays();
514 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
515 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800516 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700517 if (activityDisplay.mDisplay == null) {
518 throw new IllegalStateException("Default Display does not exist");
519 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800520 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700521 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 }
523
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700524 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800525 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800526
527 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800528 }
Craig Mautner27084302013-03-25 08:05:25 -0700529 }
530
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200531 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700532 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200533 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700534 }
535
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700536 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800537 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700538 }
539
Craig Mautnerde4ef022013-04-07 19:01:33 -0700540 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800541 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700542 }
543
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700544 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700545 if (stack == null) {
546 return false;
547 }
548
Craig Mautnerdf88d732014-01-27 09:21:32 -0800549 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
550 if (parent != null) {
551 stack = parent.task.stack;
552 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800553 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700554 }
555
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700556 /** The top most stack. */
557 boolean isFrontStack(ActivityStack stack) {
558 if (stack == null) {
559 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800560 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700561
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700562 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
563 if (parent != null) {
564 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800565 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700566 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
567 }
568
569 void setFocusStack(String reason, ActivityStack focusedStack) {
570 mLastFocusedStack = mFocusedStack;
571 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800572
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700573 EventLogTags.writeAmFocusedStack(
574 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
575 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800576
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800577 if (mService.mBooting || !mService.mBooted) {
578 final ActivityRecord r = topRunningActivityLocked();
579 if (r != null && r.idle) {
580 checkFinishBootingLocked();
581 }
582 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700583 }
584
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700585 void moveHomeStackToFront(String reason) {
586 mHomeStack.moveToFront(reason);
587 }
588
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700589 /** Returns true if the focus activity was adjusted to the home stack top activity. */
590 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700591 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
592 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700593 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700594 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700595
Craig Mautner84984fa2014-06-19 11:19:20 -0700596 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700597
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700598 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700599 if (top == null) {
600 return false;
601 }
602 mService.setFocusedActivityLocked(top, reason);
603 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700604 }
605
Craig Mautner299f9602015-01-26 09:47:33 -0800606 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700607 if (!mService.mBooting && !mService.mBooted) {
608 // Not ready yet!
609 return false;
610 }
611
Craig Mautner84984fa2014-06-19 11:19:20 -0700612 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
613 mWindowManager.showRecentApps();
614 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700615 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700616
Craig Mautner84984fa2014-06-19 11:19:20 -0700617 if (prev != null) {
618 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
619 }
620
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700621 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
622 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800623 // Only resume home activity if isn't finishing.
624 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800625 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700626 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700627 }
Craig Mautner299f9602015-01-26 09:47:33 -0800628 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700629 }
630
Craig Mautner8d341ef2013-03-26 09:03:27 -0700631 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700632 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700633 }
634
635 /**
636 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
637 * @param id Id of the task we would like returned.
638 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
639 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700640 * @param stackId The stack to restore the task to (default launch stack will be used if
641 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700642 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700643 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800644 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800645 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800646 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800647 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
648 ActivityStack stack = stacks.get(stackNdx);
649 TaskRecord task = stack.taskForIdLocked(id);
650 if (task != null) {
651 return task;
652 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700653 }
654 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800655
656 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700657 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800658 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800659 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700660 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800661 return null;
662 }
663
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700664 if (!restoreFromRecents) {
665 return task;
666 }
667
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700668 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700669 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
670 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800671 return null;
672 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700673 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800674 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700675 }
676
Craig Mautner6170f732013-04-02 13:05:23 -0700677 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800678 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800680 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800681 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
682 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
683 if (r != null) {
684 return r;
685 }
Craig Mautner6170f732013-04-02 13:05:23 -0700686 }
687 }
688 return null;
689 }
690
Craig Mautneref73ee12014-04-23 11:45:37 -0700691 void setNextTaskId(int taskId) {
692 if (taskId > mCurTaskId) {
693 mCurTaskId = taskId;
694 }
695 }
696
Craig Mautner8d341ef2013-03-26 09:03:27 -0700697 int getNextTaskId() {
698 do {
699 mCurTaskId++;
700 if (mCurTaskId <= 0) {
701 mCurTaskId = 1;
702 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700703 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700704 return mCurTaskId;
705 }
706
Craig Mautnerde4ef022013-04-07 19:01:33 -0700707 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800708 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700709 if (stack == null) {
710 return null;
711 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700712 ActivityRecord resumedActivity = stack.mResumedActivity;
713 if (resumedActivity == null || resumedActivity.app == null) {
714 resumedActivity = stack.mPausingActivity;
715 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700716 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700717 }
718 }
719 return resumedActivity;
720 }
721
Dianne Hackbornff072722014-09-24 10:56:28 -0700722 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700723 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
726 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
728 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700729 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800730 continue;
731 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700732 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800733 if (hr != null) {
734 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
735 && processName.equals(hr.processName)) {
736 try {
George Mount2c92c972014-03-20 09:38:23 -0700737 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800738 didSomething = true;
739 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700740 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800741 Slog.w(TAG, "Exception in new application when starting activity "
742 + hr.intent.getComponent().flattenToShortString(), e);
743 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700744 }
Craig Mautner20e72272013-04-01 13:45:53 -0700745 }
Craig Mautner20e72272013-04-01 13:45:53 -0700746 }
747 }
748 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700749 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700750 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700751 }
Craig Mautner20e72272013-04-01 13:45:53 -0700752 return didSomething;
753 }
754
755 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800756 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
757 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800758 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
759 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700760 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800761 continue;
762 }
763 final ActivityRecord resumedActivity = stack.mResumedActivity;
764 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700765 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800766 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 return false;
768 }
Craig Mautner20e72272013-04-01 13:45:53 -0700769 }
770 }
771 return true;
772 }
773
Craig Mautnerde4ef022013-04-07 19:01:33 -0700774 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800775 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
776 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800777 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
778 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700779 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800780 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700781 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800782 return false;
783 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700784 }
785 }
786 }
787 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700788 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800789 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
790 mLastFocusedStack + " to=" + mFocusedStack);
791 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700792 return true;
793 }
794
795 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800796 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800797 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
798 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800799 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
800 final ActivityStack stack = stacks.get(stackNdx);
801 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800802 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700803 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800804 return false;
805 }
806 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800807 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700808 }
809 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800810 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700811 }
812
Craig Mautner2acc3892013-09-23 10:28:14 -0700813 /**
814 * Pause all activities in either all of the stacks or just the back stacks.
815 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700816 * @return true if any activity was paused as a result of this call.
817 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700818 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700819 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800820 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
821 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800822 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
823 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700824 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700825 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700827 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
828 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800829 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700830 }
831 }
832 return someActivityPaused;
833 }
834
Craig Mautnerde4ef022013-04-07 19:01:33 -0700835 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700836 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800837 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
838 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800839 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
840 final ActivityStack stack = stacks.get(stackNdx);
841 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700842 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800843 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700844 Slog.d(TAG_STATES,
845 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800846 pausing = false;
847 } else {
848 return false;
849 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700850 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700851 }
852 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700853 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700854 }
855
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700856 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
857 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500858 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800859 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
860 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
861 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
862 final ActivityStack stack = stacks.get(stackNdx);
863 if (stack.mResumedActivity != null &&
864 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700865 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800866 }
867 }
868 }
869 }
870
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700871 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700872 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700873 }
874
875 void sendWaitingVisibleReportLocked(ActivityRecord r) {
876 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700877 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700878 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700879 if (w.who == null) {
880 changed = true;
881 w.timeout = false;
882 if (r != null) {
883 w.who = new ComponentName(r.info.packageName, r.info.name);
884 }
885 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
886 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700887 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700888 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700889 if (changed) {
890 mService.notifyAll();
891 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700892 }
893
894 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
895 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700896 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700897 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700898 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700899 if (w.who == null) {
900 changed = true;
901 w.timeout = timeout;
902 if (r != null) {
903 w.who = new ComponentName(r.info.packageName, r.info.name);
904 }
905 w.thisTime = thisTime;
906 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700907 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700908 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700909 if (changed) {
910 mService.notifyAll();
911 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700912 }
913
Craig Mautner29219d92013-04-16 20:19:12 -0700914 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800915 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700916 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700917 if (r != null) {
918 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700919 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700920
Craig Mautner4a1cb222013-12-04 16:14:06 -0800921 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800922 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800923 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
924 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700925 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700926 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700927 if (r != null) {
928 return r;
929 }
930 }
931 }
932 return null;
933 }
934
Dianne Hackborn09233282014-04-30 11:33:59 -0700935 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700936 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800937 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
938 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800939 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800940 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800941 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
943 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700944 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800945 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700946 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700947 }
948 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700949
950 // The lists are already sorted from most recent to oldest. Just pull the most recent off
951 // each list and add it to list. Stop when all lists are empty or maxNum reached.
952 while (maxNum > 0) {
953 long mostRecentActiveTime = Long.MIN_VALUE;
954 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800955 final int numTaskLists = runningTaskLists.size();
956 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
957 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700958 if (!stackTaskList.isEmpty()) {
959 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
960 if (lastActiveTime > mostRecentActiveTime) {
961 mostRecentActiveTime = lastActiveTime;
962 selectedStackList = stackTaskList;
963 }
964 }
965 }
966 if (selectedStackList != null) {
967 list.add(selectedStackList.remove(0));
968 --maxNum;
969 } else {
970 break;
971 }
972 }
Craig Mautner20e72272013-04-01 13:45:53 -0700973 }
974
Todd Kennedy7440f172015-12-09 14:31:22 -0800975 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
976 ProfilerInfo profilerInfo) {
977 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700978 if (aInfo != null) {
979 // Store the found target back into the intent, because now that
980 // we have it we never want to do this again. For example, if the
981 // user navigates back to this point in the history, we should
982 // always restart the exact same activity.
983 intent.setComponent(new ComponentName(
984 aInfo.applicationInfo.packageName, aInfo.name));
985
986 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700987 if (!aInfo.processName.equals("system")) {
988 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700989 mService.setDebugApp(aInfo.processName, true, false);
990 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700991
Man Caocfa78b22015-06-11 20:14:34 -0700992 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
993 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
994 }
995
996 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700997 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700998 }
999 }
1000 }
1001 return aInfo;
1002 }
1003
Todd Kennedy7440f172015-12-09 14:31:22 -08001004 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
1005 try {
1006 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
1007 PackageManager.MATCH_DEFAULT_ONLY
1008 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1009 } catch (RemoteException e) {
1010 }
1011 return null;
1012 }
1013
1014 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1015 ProfilerInfo profilerInfo, int userId) {
1016 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1017 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1018 }
1019
Craig Mautner299f9602015-01-26 09:47:33 -08001020 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
1021 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Todd Kennedy7440f172015-12-09 14:31:22 -08001022 startActivityLocked(null /*caller*/, intent, null /*ephemeralIntent*/,
1023 null /*resolvedType*/, aInfo, null /*rInfo*/, null /*voiceSession*/,
1024 null /*voiceInteractor*/, null /*resultTo*/, null /*resultWho*/,
1025 0 /*requestCode*/, 0 /*callingPid*/, 0 /*callingUid*/, null /*callingPackage*/,
1026 0 /*realCallingPid*/, 0 /*realCallingUid*/, 0 /*startFlags*/, null /*options*/,
1027 false /*ignoreTargetSecurity*/, false /*componentSpecified*/, null /*outActivity*/,
1028 null /*container*/, null /*inTask*/);
Filip Gruszczynskidd913622015-06-19 15:14:53 -07001029 if (inResumeTopActivity) {
1030 // If we are in resume section already, home activity will be initialized, but not
1031 // resumed (to avoid recursive resume) and will stay that way until something pokes it
1032 // again. We need to schedule another resume.
1033 scheduleResumeTopActivities();
1034 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001035 }
1036
Craig Mautner23ac33b2013-04-01 16:26:35 -07001037 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001038 String callingPackage, Intent intent, String resolvedType,
1039 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -07001040 IBinder resultTo, String resultWho, int requestCode, int startFlags,
1041 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Chong Zhang280d3322015-11-03 17:27:26 -08001042 Bundle bOptions, boolean ignoreTargetSecurity, int userId,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001043 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001044 // Refuse possible leaked file descriptors
1045 if (intent != null && intent.hasFileDescriptors()) {
1046 throw new IllegalArgumentException("File descriptors passed in Intent");
1047 }
1048 boolean componentSpecified = intent.getComponent() != null;
1049
Todd Kennedy7440f172015-12-09 14:31:22 -08001050 // Save a copy in case ephemeral needs it
1051 final Intent ephemeralIntent = new Intent(intent);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001052 // Don't modify the client's object!
1053 intent = new Intent(intent);
1054
Todd Kennedy7440f172015-12-09 14:31:22 -08001055 ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001056 // Collect information about the target of the Intent.
Todd Kennedy7440f172015-12-09 14:31:22 -08001057 ActivityInfo aInfo = resolveActivity(intent, rInfo, startFlags, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001058
Chong Zhang280d3322015-11-03 17:27:26 -08001059 ActivityOptions options = ActivityOptions.fromBundle(bOptions);
Craig Mautnere0a38842013-12-16 16:14:02 -08001060 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001061 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001062 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001063 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001064 // Cannot start a child activity if the parent is not resumed.
1065 return ActivityManager.START_CANCELED;
1066 }
Dianne Hackborn95465202014-09-15 16:21:55 -07001067 final int realCallingPid = Binder.getCallingPid();
1068 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -07001069 int callingPid;
1070 if (callingUid >= 0) {
1071 callingPid = -1;
1072 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001073 callingPid = realCallingPid;
1074 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001075 } else {
1076 callingPid = callingUid = -1;
1077 }
1078
Craig Mautnere0a38842013-12-16 16:14:02 -08001079 final ActivityStack stack;
1080 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001081 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001082 } else {
1083 stack = container.mStack;
1084 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001085 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001086 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001087 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001088
1089 final long origId = Binder.clearCallingIdentity();
1090
1091 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001092 (aInfo.applicationInfo.privateFlags
1093 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001094 // This may be a heavy-weight process! Check to see if we already
1095 // have another, different heavy-weight process running.
1096 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1097 if (mService.mHeavyWeightProcess != null &&
1098 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1099 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001100 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001101 if (caller != null) {
1102 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1103 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001104 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001105 } else {
1106 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001107 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001108 + intent.toString());
1109 ActivityOptions.abort(options);
1110 return ActivityManager.START_PERMISSION_DENIED;
1111 }
1112 }
1113
1114 IIntentSender target = mService.getIntentSenderLocked(
1115 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001116 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001117 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1118 | PendingIntent.FLAG_ONE_SHOT, null);
1119
1120 Intent newIntent = new Intent();
1121 if (requestCode >= 0) {
1122 // Caller is requesting a result.
1123 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1124 }
1125 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1126 new IntentSender(target));
1127 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1128 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1129 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1130 hist.packageName);
1131 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1132 hist.task.taskId);
1133 }
1134 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1135 aInfo.packageName);
1136 newIntent.setFlags(intent.getFlags());
1137 newIntent.setClassName("android",
1138 HeavyWeightSwitcherActivity.class.getName());
1139 intent = newIntent;
1140 resolvedType = null;
1141 caller = null;
1142 callingUid = Binder.getCallingUid();
1143 callingPid = Binder.getCallingPid();
1144 componentSpecified = true;
Todd Kennedy7440f172015-12-09 14:31:22 -08001145 rInfo = resolveIntent(intent, null /*resolvedType*/, userId);
1146 aInfo = rInfo != null ? rInfo.activityInfo : null;
1147 if (aInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001148 aInfo = mService.getActivityInfoForUser(aInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001149 }
1150 }
1151 }
1152 }
1153
Todd Kennedy7440f172015-12-09 14:31:22 -08001154 int res = startActivityLocked(caller, intent, ephemeralIntent, resolvedType,
1155 aInfo, rInfo, voiceSession, voiceInteractor,
1156 resultTo, resultWho, requestCode, callingPid,
1157 callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
1158 options, ignoreTargetSecurity, componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001159
Craig Mautner85c11a82014-07-17 12:38:18 -07001160 Binder.restoreCallingIdentity(origId);
1161
Craig Mautnerde4ef022013-04-07 19:01:33 -07001162 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001163 // If the caller also wants to switch to a new configuration,
1164 // do so now. This allows a clean switch, as we are waiting
1165 // for the current activity to pause (so we will not destroy
1166 // it), and have not yet started the next activity.
1167 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1168 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001169 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001170 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001171 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001172 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001173 }
1174
Craig Mautner23ac33b2013-04-01 16:26:35 -07001175 if (outResult != null) {
1176 outResult.result = res;
1177 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001178 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001179 do {
1180 try {
1181 mService.wait();
1182 } catch (InterruptedException e) {
1183 }
1184 } while (!outResult.timeout && outResult.who == null);
1185 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001186 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001187 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001188 outResult.timeout = false;
1189 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1190 outResult.totalTime = 0;
1191 outResult.thisTime = 0;
1192 } else {
1193 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001194 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001195 do {
1196 try {
1197 mService.wait();
1198 } catch (InterruptedException e) {
1199 }
1200 } while (!outResult.timeout && outResult.who == null);
1201 }
1202 }
1203 }
1204
1205 return res;
1206 }
1207 }
1208
1209 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1210 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
Chong Zhang280d3322015-11-03 17:27:26 -08001211 Bundle bOptions, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001212 if (intents == null) {
1213 throw new NullPointerException("intents is null");
1214 }
1215 if (resolvedTypes == null) {
1216 throw new NullPointerException("resolvedTypes is null");
1217 }
1218 if (intents.length != resolvedTypes.length) {
1219 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1220 }
1221
Craig Mautner23ac33b2013-04-01 16:26:35 -07001222
1223 int callingPid;
1224 if (callingUid >= 0) {
1225 callingPid = -1;
1226 } else if (caller == null) {
1227 callingPid = Binder.getCallingPid();
1228 callingUid = Binder.getCallingUid();
1229 } else {
1230 callingPid = callingUid = -1;
1231 }
1232 final long origId = Binder.clearCallingIdentity();
1233 try {
1234 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001235 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001236 for (int i=0; i<intents.length; i++) {
1237 Intent intent = intents[i];
1238 if (intent == null) {
1239 continue;
1240 }
1241
1242 // Refuse possible leaked file descriptors
1243 if (intent != null && intent.hasFileDescriptors()) {
1244 throw new IllegalArgumentException("File descriptors passed in Intent");
1245 }
1246
1247 boolean componentSpecified = intent.getComponent() != null;
1248
1249 // Don't modify the client's object!
1250 intent = new Intent(intent);
1251
1252 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001253 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001254 // TODO: New, check if this is correct
1255 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1256
1257 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001258 (aInfo.applicationInfo.privateFlags
1259 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001260 throw new IllegalArgumentException(
1261 "FLAG_CANT_SAVE_STATE not supported here");
1262 }
1263
Chong Zhang280d3322015-11-03 17:27:26 -08001264 ActivityOptions options = ActivityOptions.fromBundle(
1265 i == intents.length - 1 ? bOptions : null);
Todd Kennedy7440f172015-12-09 14:31:22 -08001266 int res = startActivityLocked(caller, intent, null /*ephemeralIntent*/,
1267 resolvedTypes[i], aInfo, null /*rInfo*/, null, null, resultTo, null, -1,
1268 callingPid, callingUid, callingPackage, callingPid, callingUid, 0,
1269 options, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001270 if (res < 0) {
1271 return res;
1272 }
1273
1274 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1275 }
1276 }
1277 } finally {
1278 Binder.restoreCallingIdentity(origId);
1279 }
1280
1281 return ActivityManager.START_SUCCESS;
1282 }
1283
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001285 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001286 throws RemoteException {
1287
Craig Mautner2568c3a2015-03-26 14:22:34 -07001288 if (andResume) {
1289 r.startFreezingScreenLocked(app, 0);
1290 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001291
Craig Mautner2568c3a2015-03-26 14:22:34 -07001292 // schedule launch ticks to collect information about slow apps.
1293 r.startLaunchTickingLocked();
1294 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001295
1296 // Have the window manager re-evaluate the orientation of
1297 // the screen based on the new activity order. Note that
1298 // as a result of this, it can call back into the activity
1299 // manager with a new orientation. We don't care about that,
1300 // because the activity is not currently running so we are
1301 // just restarting it anyway.
1302 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001303 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001304 mService.mConfiguration,
1305 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001306 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001307 }
1308
1309 r.app = app;
1310 app.waitingToKill = null;
1311 r.launchCount++;
1312 r.lastLaunchTime = SystemClock.uptimeMillis();
1313
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001314 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001315
1316 int idx = app.activities.indexOf(r);
1317 if (idx < 0) {
1318 app.activities.add(r);
1319 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001320 mService.updateLruProcessLocked(app, true, null);
1321 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001322
Craig Mautner15df08a2015-04-01 12:17:18 -07001323 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001324 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1325 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001326 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001327 }
1328
1329 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001330 try {
1331 if (app.thread == null) {
1332 throw new RemoteException();
1333 }
1334 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001335 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001336 if (andResume) {
1337 results = r.results;
1338 newIntents = r.newIntents;
1339 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001340 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1341 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1342 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001343 if (andResume) {
1344 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1345 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001346 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001347 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001348 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001349 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001350 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001351 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001352 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001353 r.sleeping = false;
1354 r.forceNewConfig = false;
1355 mService.showAskCompatModeDialogLocked(r);
1356 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001357 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001358 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1359 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1360 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001361 final String profileFile = mService.mProfileFile;
1362 if (profileFile != null) {
1363 ParcelFileDescriptor profileFd = mService.mProfileFd;
1364 if (profileFd != null) {
1365 try {
1366 profileFd = profileFd.dup();
1367 } catch (IOException e) {
1368 if (profileFd != null) {
1369 try {
1370 profileFd.close();
1371 } catch (IOException o) {
1372 }
1373 profileFd = null;
1374 }
1375 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001376 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001377
1378 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1379 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001380 }
1381 }
1382 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001383
Craig Mautner2568c3a2015-03-26 14:22:34 -07001384 if (andResume) {
1385 app.hasShownUi = true;
1386 app.pendingUiClean = true;
1387 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001388 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001389 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001390 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001391 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001392 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001393 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001394
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001395 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001396 // This may be a heavy-weight process! Note that the package
1397 // manager will ensure that only activity can run in the main
1398 // process of the .apk, which is the only thing that will be
1399 // considered heavy-weight.
1400 if (app.processName.equals(app.info.packageName)) {
1401 if (mService.mHeavyWeightProcess != null
1402 && mService.mHeavyWeightProcess != app) {
1403 Slog.w(TAG, "Starting new heavy weight process " + app
1404 + " when already running "
1405 + mService.mHeavyWeightProcess);
1406 }
1407 mService.mHeavyWeightProcess = app;
1408 Message msg = mService.mHandler.obtainMessage(
1409 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1410 msg.obj = r;
1411 mService.mHandler.sendMessage(msg);
1412 }
1413 }
1414
1415 } catch (RemoteException e) {
1416 if (r.launchFailed) {
1417 // This is the second time we failed -- finish activity
1418 // and give up.
1419 Slog.e(TAG, "Second failure launching "
1420 + r.intent.getComponent().flattenToShortString()
1421 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001422 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001423 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1424 "2nd-crash", false);
1425 return false;
1426 }
1427
1428 // This is the first time we failed -- restart process and
1429 // retry.
1430 app.activities.remove(r);
1431 throw e;
1432 }
1433
1434 r.launchFailed = false;
1435 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001436 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001437 }
1438
1439 if (andResume) {
1440 // As part of the process of launching, ActivityThread also performs
1441 // a resume.
1442 stack.minimalResumeActivityLocked(r);
1443 } else {
1444 // This activity is not starting in the resumed state... which
1445 // should look like we asked it to pause+stop (but remain visible),
1446 // and it has done so and reported back the current icicle and
1447 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001448 if (DEBUG_STATES) Slog.v(TAG_STATES,
1449 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001450 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001451 r.stopped = true;
1452 }
1453
1454 // Launch the new version setup screen if needed. We do this -after-
1455 // launching the initial activity (that is, home), so that it can have
1456 // a chance to initialize itself while in the background, making the
1457 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001458 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001459 mService.startSetupActivityLocked();
1460 }
1461
Dianne Hackborn465fa392014-09-14 14:21:18 -07001462 // Update any services we are bound to that might care about whether
1463 // their client may have activities.
1464 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1465
Craig Mautner2420ead2013-04-01 17:13:20 -07001466 return true;
1467 }
1468
Craig Mautnere79d42682013-04-01 19:01:53 -07001469 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001470 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001471 // Is this activity's application already running?
1472 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001473 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001474
1475 r.task.stack.setLaunchTime(r);
1476
1477 if (app != null && app.thread != null) {
1478 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001479 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1480 || !"android".equals(r.info.packageName)) {
1481 // Don't add this if it is a platform component that is marked
1482 // to run in multiple processes, because this is actually
1483 // part of the framework so doesn't make sense to track as a
1484 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001485 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1486 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001487 }
George Mount2c92c972014-03-20 09:38:23 -07001488 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001489 return;
1490 } catch (RemoteException e) {
1491 Slog.w(TAG, "Exception when starting activity "
1492 + r.intent.getComponent().flattenToShortString(), e);
1493 }
1494
1495 // If a dead object exception was thrown -- fall through to
1496 // restart the application.
1497 }
1498
1499 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001500 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001501 }
1502
Todd Kennedy7440f172015-12-09 14:31:22 -08001503 final int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
1504 String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001505 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Todd Kennedy7440f172015-12-09 14:31:22 -08001506 IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
1507 String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
1508 ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
1509 ActivityRecord[] outActivity, ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001510 int err = ActivityManager.START_SUCCESS;
1511
1512 ProcessRecord callerApp = null;
1513 if (caller != null) {
1514 callerApp = mService.getRecordForAppLocked(caller);
1515 if (callerApp != null) {
1516 callingPid = callerApp.pid;
1517 callingUid = callerApp.info.uid;
1518 } else {
1519 Slog.w(TAG, "Unable to find app for caller " + caller
1520 + " (pid=" + callingPid + ") when starting: "
1521 + intent.toString());
1522 err = ActivityManager.START_PERMISSION_DENIED;
1523 }
1524 }
1525
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001526 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1527
Craig Mautner6170f732013-04-02 13:05:23 -07001528 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001529 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001530 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001531 + " on display " + (container == null ? (mFocusedStack == null ?
1532 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1533 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1534 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001535 }
1536
1537 ActivityRecord sourceRecord = null;
1538 ActivityRecord resultRecord = null;
1539 if (resultTo != null) {
1540 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001541 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1542 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001543 if (sourceRecord != null) {
1544 if (requestCode >= 0 && !sourceRecord.finishing) {
1545 resultRecord = sourceRecord;
1546 }
1547 }
1548 }
Craig Mautner6170f732013-04-02 13:05:23 -07001549
Dianne Hackborn91097de2014-04-04 18:02:06 -07001550 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001551
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001552 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001553 // Transfer the result target from the source activity to the new
1554 // one being started, including any failures.
1555 if (requestCode >= 0) {
1556 ActivityOptions.abort(options);
1557 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1558 }
1559 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001560 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1561 resultRecord = null;
1562 }
Craig Mautner6170f732013-04-02 13:05:23 -07001563 resultWho = sourceRecord.resultWho;
1564 requestCode = sourceRecord.requestCode;
1565 sourceRecord.resultTo = null;
1566 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001567 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001568 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001569 if (sourceRecord.launchedFromUid == callingUid) {
1570 // The new activity is being launched from the same uid as the previous
1571 // activity in the flow, and asking to forward its result back to the
1572 // previous. In this case the activity is serving as a trampoline between
1573 // the two, so we also want to update its launchedFromPackage to be the
1574 // same as the previous activity. Note that this is safe, since we know
1575 // these two packages come from the same uid; the caller could just as
1576 // well have supplied that same package name itself. This specifially
1577 // deals with the case of an intent picker/chooser being launched in the app
1578 // flow to redirect to an activity picked by the user, where we want the final
1579 // activity to consider it to have been launched by the previous app activity.
1580 callingPackage = sourceRecord.launchedFromPackage;
1581 }
Craig Mautner6170f732013-04-02 13:05:23 -07001582 }
1583
1584 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1585 // We couldn't find a class that can handle the given Intent.
1586 // That's the end of that!
1587 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1588 }
1589
1590 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1591 // We couldn't find the specific class specified in the Intent.
1592 // Also the end of the line.
1593 err = ActivityManager.START_CLASS_NOT_FOUND;
1594 }
1595
Dianne Hackborn91097de2014-04-04 18:02:06 -07001596 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1597 && sourceRecord.task.voiceSession != null) {
1598 // If this activity is being launched as part of a voice session, we need
1599 // to ensure that it is safe to do so. If the upcoming activity will also
1600 // be part of the voice session, we can only launch it if it has explicitly
1601 // said it supports the VOICE category, or it is a part of the calling app.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001602 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001603 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1604 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001605 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001606 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1607 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001608 Slog.w(TAG,
1609 "Activity being started in current voice task does not support voice: "
1610 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001611 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1612 }
1613 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001614 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001615 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1616 }
1617 }
1618 }
1619
1620 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1621 // If the caller is starting a new voice session, just make sure the target
1622 // is actually allowing it to run this way.
1623 try {
1624 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1625 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001626 Slog.w(TAG,
1627 "Activity being started in new voice task does not support: "
1628 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001629 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1630 }
1631 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001632 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001633 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1634 }
1635 }
1636
louis_changcd5d1982014-08-01 10:09:08 +08001637 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1638
Craig Mautner6170f732013-04-02 13:05:23 -07001639 if (err != ActivityManager.START_SUCCESS) {
1640 if (resultRecord != null) {
1641 resultStack.sendActivityResultLocked(-1,
1642 resultRecord, resultWho, requestCode,
1643 Activity.RESULT_CANCELED, null);
1644 }
Craig Mautner6170f732013-04-02 13:05:23 -07001645 ActivityOptions.abort(options);
1646 return err;
1647 }
1648
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001649 boolean abort = !checkStartAnyActivityPermission(intent, aInfo, resultWho, requestCode,
1650 callingPid, callingUid, callingPackage, ignoreTargetSecurity, callerApp,
1651 resultRecord, resultStack);
Svetoslav7008b512015-06-24 18:47:07 -07001652 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001653 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001654
Craig Mautner6170f732013-04-02 13:05:23 -07001655 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001656 try {
1657 // The Intent we give to the watcher has the extra data
1658 // stripped off, since it can contain private information.
1659 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001660 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001661 aInfo.applicationInfo.packageName);
1662 } catch (RemoteException e) {
1663 mService.mController = null;
1664 }
Ben Gruver5e207332013-04-03 17:41:37 -07001665 }
Craig Mautner6170f732013-04-02 13:05:23 -07001666
Clara Bayarrif7fea162015-10-22 16:09:52 +01001667 UserInfo user = getUserInfo(userId);
Clara Bayarri56878a92015-10-29 15:43:55 +00001668 KeyguardManager km = (KeyguardManager) mService.mContext
1669 .getSystemService(Context.KEYGUARD_SERVICE);
1670 if (user.isManagedProfile()
Clara Bayarri10ad84a2015-12-01 17:38:05 +00001671 && LockPatternUtils.isSeparateWorkChallengeEnabled()
Clara Bayarri56878a92015-10-29 15:43:55 +00001672 && km.isDeviceLocked(userId)) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001673 IIntentSender target = mService.getIntentSenderLocked(
1674 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1675 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1676 new String[]{ resolvedType },
1677 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1678 | PendingIntent.FLAG_IMMUTABLE, null);
Todd Kennedy7440f172015-12-09 14:31:22 -08001679 final int flags = intent.getFlags();
1680 final Intent newIntent = km.createConfirmDeviceCredentialIntent(null, null, user.id);
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001681 if (newIntent != null) {
1682 intent = newIntent;
Todd Kennedy7440f172015-12-09 14:31:22 -08001683 intent.setFlags(flags
1684 | Intent.FLAG_ACTIVITY_NEW_TASK
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001685 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1686 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1687 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
Clara Bayarrif7fea162015-10-22 16:09:52 +01001688
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001689 resolvedType = null;
1690 callingUid = realCallingUid;
1691 callingPid = realCallingPid;
Clara Bayarrif7fea162015-10-22 16:09:52 +01001692
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001693 UserInfo parent = UserManager.get(mService.mContext).getProfileParent(userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001694 rInfo = resolveIntent(intent, resolvedType, parent.id);
1695 aInfo = resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001696 }
Clara Bayarrif7fea162015-10-22 16:09:52 +01001697 }
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
Svet Ganov9c165d72015-12-01 19:52:26 -08001710 // If permissions need a review before any of the app components can run, we
1711 // launch the review activity and pass a pending intent to start the activity
1712 // we are to launching now after the review is completed.
1713 if (Build.PERMISSIONS_REVIEW_REQUIRED && aInfo != null) {
1714 if (mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
1715 aInfo.packageName, userId)) {
1716 IIntentSender target = mService.getIntentSenderLocked(
1717 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1718 callingUid, userId, null, null, 0, new Intent[]{intent},
1719 new String[]{resolvedType}, PendingIntent.FLAG_CANCEL_CURRENT
1720 | PendingIntent.FLAG_ONE_SHOT, null);
1721
Todd Kennedy7440f172015-12-09 14:31:22 -08001722 final int flags = intent.getFlags();
Svet Ganov9c165d72015-12-01 19:52:26 -08001723 Intent newIntent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
Todd Kennedy7440f172015-12-09 14:31:22 -08001724 newIntent.setFlags(flags
1725 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Svet Ganov9c165d72015-12-01 19:52:26 -08001726 newIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1727 newIntent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1728 if (resultRecord != null) {
1729 newIntent.putExtra(Intent.EXTRA_RESULT_NEEDED, true);
1730 }
Svet Ganov9c165d72015-12-01 19:52:26 -08001731 intent = newIntent;
1732
1733 resolvedType = null;
1734 callingUid = realCallingUid;
1735 callingPid = realCallingPid;
1736
Todd Kennedy7440f172015-12-09 14:31:22 -08001737 rInfo = resolveIntent(intent, resolvedType, userId);
1738 aInfo = resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
Svet Ganov9c165d72015-12-01 19:52:26 -08001739
1740 if (DEBUG_PERMISSIONS_REVIEW) {
1741 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true,
1742 true, false) + "} from uid " + callingUid + " on display "
1743 + (container == null ? (mFocusedStack == null ?
1744 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1745 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1746 container.mActivityDisplay.mDisplayId)));
1747 }
1748 }
1749 }
1750
Todd Kennedy7440f172015-12-09 14:31:22 -08001751 // If we have an ephemeral app, abort the process of launching the resolved intent.
1752 // Instead, launch the ephemeral installer. Once the installer is finished, it
1753 // starts either the intent we resolved here [on install error] or the ephemeral
1754 // app [on install success].
1755 if (rInfo != null && rInfo.ephemeralResolveInfo != null) {
1756 // Create a pending intent to start the intent resolved here.
1757 final IIntentSender failureTarget = mService.getIntentSenderLocked(
1758 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1759 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1760 new String[]{ resolvedType },
1761 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1762 | PendingIntent.FLAG_IMMUTABLE, null);
1763
1764 // Create a pending intent to start the ephemeral application; force it to be
1765 // directed to the ephemeral package.
1766 ephemeralIntent.setPackage(rInfo.ephemeralResolveInfo.getPackageName());
1767 final IIntentSender ephemeralTarget = mService.getIntentSenderLocked(
1768 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1769 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ ephemeralIntent },
1770 new String[]{ resolvedType },
1771 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1772 | PendingIntent.FLAG_IMMUTABLE, null);
1773
1774 int flags = intent.getFlags();
1775 intent = new Intent();
1776 intent.setFlags(flags
1777 | Intent.FLAG_ACTIVITY_NEW_TASK
1778 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1779 intent.putExtra(Intent.EXTRA_PACKAGE_NAME,
1780 rInfo.ephemeralResolveInfo.getPackageName());
1781 intent.putExtra(Intent.EXTRA_EPHEMERAL_FAILURE, new IntentSender(failureTarget));
1782 intent.putExtra(Intent.EXTRA_EPHEMERAL_SUCCESS, new IntentSender(ephemeralTarget));
1783
1784 resolvedType = null;
1785 callingUid = realCallingUid;
1786 callingPid = realCallingPid;
1787
1788 rInfo = rInfo.ephemeralInstaller;
1789 aInfo = resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
1790 }
1791
Craig Mautner6170f732013-04-02 13:05:23 -07001792 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001793 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001794 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001795 if (outActivity != null) {
1796 outActivity[0] = r;
1797 }
1798
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001799 if (r.appTimeTracker == null && sourceRecord != null) {
1800 // If the caller didn't specify an explicit time tracker, we want to continue
1801 // tracking under any it has.
1802 r.appTimeTracker = sourceRecord.appTimeTracker;
1803 }
1804
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001805 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001806 if (voiceSession == null && (stack.mResumedActivity == null
1807 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001808 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1809 realCallingPid, realCallingUid, "Activity start")) {
Chong Zhang280d3322015-11-03 17:27:26 -08001810 PendingActivityLaunch pal = new PendingActivityLaunch(r,
Robert Carr13997f52015-10-23 13:13:39 -07001811 sourceRecord, startFlags, stack, callerApp);
Craig Mautneree36c772014-07-16 14:56:05 -07001812 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001813 ActivityOptions.abort(options);
1814 return ActivityManager.START_SWITCHES_CANCELED;
1815 }
1816 }
1817
1818 if (mService.mDidAppSwitch) {
1819 // This is the second allowed switch since we stopped switches,
1820 // so now just generally allow switches. Use case: user presses
1821 // home (switches disabled, switch to home, mDidAppSwitch now true);
1822 // user taps a home icon (coming from home so allowed, we hit here
1823 // and now allow anyone to switch again).
1824 mService.mAppSwitchesAllowedTime = 0;
1825 } else {
1826 mService.mDidAppSwitch = true;
1827 }
1828
Craig Mautneree36c772014-07-16 14:56:05 -07001829 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001830
Dianne Hackborn91097de2014-04-04 18:02:06 -07001831 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001832 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001833
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001834 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001835 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001836 // activity start, but we are not actually doing an activity
1837 // switch... just dismiss the keyguard now, because we
1838 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001839 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001840 }
1841 return err;
1842 }
1843
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001844 private boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
1845 String resultWho, int requestCode, int callingPid, int callingUid,
1846 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1847 ActivityRecord resultRecord, ActivityStack resultStack) {
1848 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1849 callingUid);
1850 if (startAnyPerm == PERMISSION_GRANTED) {
1851 return true;
1852 }
1853 final int componentRestriction = getComponentRestrictionForCallingPackage(
1854 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1855 final int actionRestriction = getActionRestrictionForCallingPackage(
1856 intent.getAction(), callingPackage, callingPid, callingUid);
1857 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1858 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1859 if (resultRecord != null) {
1860 resultStack.sendActivityResultLocked(-1,
1861 resultRecord, resultWho, requestCode,
1862 Activity.RESULT_CANCELED, null);
1863 }
1864 final String msg;
1865 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1866 msg = "Permission Denial: starting " + intent.toString()
1867 + " from " + callerApp + " (pid=" + callingPid
1868 + ", uid=" + callingUid + ")" + " with revoked permission "
1869 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1870 } else if (!aInfo.exported) {
1871 msg = "Permission Denial: starting " + intent.toString()
1872 + " from " + callerApp + " (pid=" + callingPid
1873 + ", uid=" + callingUid + ")"
1874 + " not exported from uid " + aInfo.applicationInfo.uid;
1875 } else {
1876 msg = "Permission Denial: starting " + intent.toString()
1877 + " from " + callerApp + " (pid=" + callingPid
1878 + ", uid=" + callingUid + ")"
1879 + " requires " + aInfo.permission;
1880 }
1881 Slog.w(TAG, msg);
1882 throw new SecurityException(msg);
1883 }
1884
1885 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1886 final String message = "Appop Denial: starting " + intent.toString()
1887 + " from " + callerApp + " (pid=" + callingPid
1888 + ", uid=" + callingUid + ")"
1889 + " requires " + AppOpsManager.permissionToOp(
1890 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1891 Slog.w(TAG, message);
1892 return false;
1893 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1894 final String message = "Appop Denial: starting " + intent.toString()
1895 + " from " + callerApp + " (pid=" + callingPid
1896 + ", uid=" + callingUid + ")"
1897 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1898 Slog.w(TAG, message);
1899 return false;
1900 }
1901 return true;
1902 }
1903
Clara Bayarrif7fea162015-10-22 16:09:52 +01001904 private UserInfo getUserInfo(int userId) {
1905 final long identity = Binder.clearCallingIdentity();
1906 try {
1907 return UserManager.get(mService.mContext).getUserInfo(userId);
1908 } finally {
1909 Binder.restoreCallingIdentity(identity);
1910 }
1911 }
1912
Svet Ganov99b60432015-06-27 13:15:22 -07001913 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001914 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001915 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1916 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001917 == PackageManager.PERMISSION_DENIED) {
1918 return ACTIVITY_RESTRICTION_PERMISSION;
1919 }
1920
Christopher Tateff7add02015-08-17 10:23:22 -07001921 if (activityInfo.permission == null) {
1922 return ACTIVITY_RESTRICTION_NONE;
1923 }
1924
Svet Ganov99b60432015-06-27 13:15:22 -07001925 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1926 if (opCode == AppOpsManager.OP_NONE) {
1927 return ACTIVITY_RESTRICTION_NONE;
1928 }
1929
1930 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1931 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001932 if (!ignoreTargetSecurity) {
1933 return ACTIVITY_RESTRICTION_APPOP;
1934 }
Svet Ganov99b60432015-06-27 13:15:22 -07001935 }
1936
1937 return ACTIVITY_RESTRICTION_NONE;
1938 }
1939
Svetoslav7008b512015-06-24 18:47:07 -07001940 private int getActionRestrictionForCallingPackage(String action,
1941 String callingPackage, int callingPid, int callingUid) {
1942 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001943 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001944 }
1945
1946 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1947 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001948 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001949 }
1950
1951 final PackageInfo packageInfo;
1952 try {
1953 packageInfo = mService.mContext.getPackageManager()
1954 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1955 } catch (PackageManager.NameNotFoundException e) {
1956 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001957 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001958 }
1959
1960 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001961 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001962 }
1963
1964 if (mService.checkPermission(permission, callingPid, callingUid) ==
1965 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001966 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001967 }
1968
1969 final int opCode = AppOpsManager.permissionToOpCode(permission);
1970 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001971 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001972 }
1973
1974 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1975 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001976 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001977 }
1978
Svet Ganov99b60432015-06-27 13:15:22 -07001979 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001980 }
1981
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08001982 private ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds,
1983 int launchFlags) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001984 final TaskRecord task = r.task;
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001985 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1986 return mHomeStack;
1987 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001988
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08001989 ActivityStack stack = getLaunchToSideStack(r, launchFlags, task);
1990 if (stack != null) {
1991 return stack;
1992 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001993
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001994 if (task != null && task.stack != null) {
1995 stack = task.stack;
1996 if (stack.isOnHomeDisplay()) {
1997 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001998 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001999 "computeStackFocus: Setting " + "focused stack to r=" + r
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002000 + " task=" + task);
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002001 } else {
2002 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002003 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07002004 }
2005 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002006 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002007 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002008
2009 final ActivityContainer container = r.mInitialActivityContainer;
2010 if (container != null) {
2011 // The first time put it on the desired stack, after this put on task stack.
2012 r.mInitialActivityContainer = null;
2013 return container.mStack;
2014 }
2015
2016 // The fullscreen stack can contain any task regardless of if the task is resizeable
2017 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002018 // If the freeform or docked stack has focus, and the activity to be launched is resizeable,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002019 // we can also put it in the focused stack.
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002020 final int focusedStackId = mFocusedStack.mStackId;
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002021 final boolean canUseFocusedStack =
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002022 focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
2023 || focusedStackId == DOCKED_STACK_ID
2024 || (focusedStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable);
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002025 if (canUseFocusedStack
2026 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
2027 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
2028 "computeStackFocus: Have a focused stack=" + mFocusedStack);
2029 return mFocusedStack;
2030 }
2031
2032 // We first try to put the task in the first dynamic stack.
2033 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2034 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2035 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07002036 if (!StackId.isStaticStack(stack.mStackId)) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002037 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
2038 "computeStackFocus: Setting focused stack=" + stack);
2039 return stack;
2040 }
2041 }
2042
2043 // If there is no suitable dynamic stack then we figure out which static stack to use.
2044 final int stackId = task != null ? task.getLaunchStackId() :
2045 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
2046 FULLSCREEN_WORKSPACE_STACK_ID;
2047 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2048 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
2049 + r + " stackId=" + stack.mStackId);
2050 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002051 }
2052
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002053 private ActivityStack getLaunchToSideStack(ActivityRecord r, int launchFlags, TaskRecord task) {
2054 if ((launchFlags & FLAG_ACTIVITY_LAUNCH_TO_SIDE) == 0) {
2055 return null;
2056 }
2057 // The parent activity doesn't want to launch the activity on top of itself, but
2058 // instead tries to put it onto other side in side-by-side mode.
2059 final ActivityStack parentStack = task != null ? task.stack
2060 : r.mInitialActivityContainer != null ? r.mInitialActivityContainer.mStack
2061 : mFocusedStack;
2062 if (parentStack != null && parentStack.mStackId == DOCKED_STACK_ID) {
2063 // If parent was in docked stack, the natural place to launch another activity
2064 // will be fullscreen, so it can appear alongside the docked window.
2065 return getStack(FULLSCREEN_WORKSPACE_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2066 } else {
2067 // If the parent is not in the docked stack, we check if there is docked window
2068 // and if yes, we will launch into that stack. If not, we just put the new
2069 // activity into parent's stack, because we can't find a better place.
2070 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2071 if (stack != null && !stack.isStackVisibleLocked()) {
2072 // There is a docked stack, but it isn't visible, so we can't launch into that.
2073 return null;
2074 } else {
2075 return stack;
2076 }
2077 }
2078 }
2079
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002080 boolean setFocusedStack(ActivityRecord r, String reason) {
2081 if (r == null) {
2082 // Not sure what you are trying to do, but it is not going to work...
2083 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07002084 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002085 final TaskRecord task = r.task;
2086 if (task == null || task.stack == null) {
2087 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
2088 return false;
2089 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07002090 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002091 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07002092 }
2093
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002094 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002095 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Chong Zhang280d3322015-11-03 17:27:26 -08002096 boolean doResume, ActivityOptions options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002097 final Intent intent = r.intent;
2098 final int callingUid = r.launchedFromUid;
2099
Chong Zhang0fa656b2015-08-31 15:17:21 -07002100 boolean overrideBounds = false;
2101 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07002102 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002103 if (canUseActivityOptionsLaunchBounds(options)) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07002104 overrideBounds = true;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002105 newBounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002106 }
2107 }
2108
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002109 // In some flows in to this function, we retrieve the task record and hold on to it
2110 // without a lock before calling back in to here... so the task at this point may
2111 // not actually be in recents. Check for that, and if it isn't in recents just
2112 // consider it invalid.
2113 if (inTask != null && !inTask.inRecents) {
2114 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
2115 inTask = null;
2116 }
2117
Craig Mautnerad400af2014-08-13 16:41:36 -07002118 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07002119 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
2120 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121
Craig Mautnera228ae92014-07-09 05:44:55 -07002122 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002123 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07002124 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002125 // We have a conflict between the Intent and the Activity manifest, manifest wins.
2126 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
2127 "\"singleInstance\" or \"singleTask\"");
2128 launchFlags &=
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002129 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_MULTIPLE_TASK);
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002130 } else {
2131 switch (r.info.documentLaunchMode) {
2132 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
2133 break;
2134 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
2135 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2136 break;
2137 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
2138 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2139 break;
2140 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002141 launchFlags &= ~FLAG_ACTIVITY_MULTIPLE_TASK;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002142 break;
2143 }
2144 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002145
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002146 final boolean launchTaskBehind = r.mLaunchTaskBehind
2147 && !launchSingleTask && !launchSingleInstance
2148 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002149
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002150 if (r.resultTo != null && (launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0
Wale Ogunwale7d701172015-03-11 15:36:30 -07002151 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002152 // For whatever reason this activity is being launched into a new
2153 // task... yet the caller has requested a result back. Well, that
2154 // is pretty messed up, so instead immediately send back a cancel
2155 // and let the new task continue launched as normal without a
2156 // dependency on its originator.
2157 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
2158 r.resultTo.task.stack.sendActivityResultLocked(-1,
2159 r.resultTo, r.resultWho, r.requestCode,
2160 Activity.RESULT_CANCELED, null);
2161 r.resultTo = null;
2162 }
2163
2164 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002165 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002166 }
2167
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002168 // If we are actually going to launch in to a new task, there are some cases where
2169 // we further want to do multiple task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002170 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002171 if (launchTaskBehind
2172 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002173 launchFlags |= FLAG_ACTIVITY_MULTIPLE_TASK;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002174 }
2175 }
2176
Craig Mautner8849a5e2013-04-02 16:41:03 -07002177 // We'll invoke onUserLeaving before onPause only if the launching
2178 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07002179 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002180 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2181 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002182
2183 // If the caller has asked not to resume at this point, we make note
2184 // of this in the record so that we can skip it when trying to find
2185 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07002186 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002187 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002188 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002189 }
2190
Craig Mautnera254cd72014-05-25 16:47:39 -07002191 ActivityRecord notTop =
2192 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002193
2194 // If the onlyIfNeeded flag is set, then we can do this if the activity
2195 // being launched is the same as the one making the call... or, as
2196 // a special case, if we do not know the caller then we count the
2197 // current top activity as the caller.
2198 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2199 ActivityRecord checkedCaller = sourceRecord;
2200 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002201 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002202 }
2203 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2204 // Caller is not the same as launcher, so always needed.
2205 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
2206 }
2207 }
2208
Craig Mautner8849a5e2013-04-02 16:41:03 -07002209 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002210 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002211
2212 // If the caller is not coming from another activity, but has given us an
2213 // explicit task into which they would like us to launch the new activity,
2214 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002215 if (sourceRecord == null && inTask != null && inTask.stack != null) {
2216 final Intent baseIntent = inTask.getBaseIntent();
2217 final ActivityRecord root = inTask.getRootActivity();
2218 if (baseIntent == null) {
2219 ActivityOptions.abort(options);
2220 throw new IllegalArgumentException("Launching into task without base intent: "
2221 + inTask);
2222 }
2223
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002224 // If this task is empty, then we are adding the first activity -- it
2225 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002226 if (launchSingleInstance || launchSingleTask) {
2227 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2228 ActivityOptions.abort(options);
2229 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2230 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002231 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002232 if (root != null) {
2233 ActivityOptions.abort(options);
2234 throw new IllegalArgumentException("Caller with inTask " + inTask
2235 + " has root " + root + " but target is singleInstance/Task");
2236 }
2237 }
2238
2239 // If task is empty, then adopt the interesting intent launch flags in to the
2240 // activity being started.
2241 if (root == null) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002242 final int flagsOfInterest = FLAG_ACTIVITY_NEW_TASK
2243 | FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002244 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2245 launchFlags = (launchFlags&~flagsOfInterest)
2246 | (baseIntent.getFlags()&flagsOfInterest);
2247 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002248 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002249 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002250
Dianne Hackborn962d5352014-08-29 16:27:40 -07002251 // If the task is not empty and the caller is asking to start it as the root
2252 // of a new task, then we don't actually want to start this on the task. We
2253 // will bring the task to the front, and possibly give it a new intent.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002254 } else if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002255 addingToTask = false;
2256
2257 } else {
2258 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002259 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002260
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002261 reuseTask = inTask;
2262 } else {
2263 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002264 // Launch ResolverActivity in the source task, so that it stays in the task
2265 // bounds when in freeform workspace.
2266 // Also put noDisplay activities in the source task. These by itself can
2267 // be placed in any task/stack, however it could launch other activities
2268 // like ResolverActivity, and we want those to stay in the original task.
2269 if (r.isResolverActivity() || r.noDisplay) {
2270 addingToTask = true;
2271 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002272 }
2273
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002274 if (inTask == null) {
2275 if (sourceRecord == null) {
2276 // This activity is not being started from another... in this
2277 // case we -always- start a new task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002278 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002279 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2280 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002281 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002282 }
2283 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2284 // The original activity who is starting us is running as a single
2285 // instance... this new activity it is starting must go on its
2286 // own task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002287 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002288 } else if (launchSingleInstance || launchSingleTask) {
2289 // The activity being started is a single instance... it always
2290 // gets launched into its own task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002291 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002292 }
2293 }
2294
2295 ActivityInfo newTaskInfo = null;
2296 Intent newTaskIntent = null;
Filip Gruszczynskia8e98b92015-12-15 16:35:38 -08002297 final ActivityStack sourceStack;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002298 if (sourceRecord != null) {
2299 if (sourceRecord.finishing) {
2300 // If the source is finishing, we can't further count it as our source. This
2301 // is because the task it is associated with may now be empty and on its way out,
2302 // so we don't want to blindly throw it in to that task. Instead we will take
2303 // the NEW_TASK flow and try to find a task for it. But save the task information
2304 // so it can be used when creating the new task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002305 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002306 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2307 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002308 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002309 newTaskInfo = sourceRecord.info;
2310 newTaskIntent = sourceRecord.task.intent;
2311 }
2312 sourceRecord = null;
2313 sourceStack = null;
2314 } else {
2315 sourceStack = sourceRecord.task.stack;
2316 }
2317 } else {
2318 sourceStack = null;
2319 }
2320
2321 boolean movedHome = false;
2322 ActivityStack targetStack;
2323
2324 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002325 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002326
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002327 ActivityRecord intentActivity = getReusableIntentActivity(r, inTask, intent,
2328 launchSingleInstance, launchSingleTask, launchFlags);
2329 if (intentActivity != null) {
2330 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2331 // but still needs to be a lock task mode violation since the task gets
2332 // cleared out and the device would otherwise leave the locked task.
2333 if (isLockTaskModeViolation(intentActivity.task,
2334 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2335 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
2336 showLockTaskToast();
2337 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
2338 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2339 }
2340 if (r.task == null) {
2341 r.task = intentActivity.task;
2342 }
2343 if (intentActivity.task.intent == null) {
2344 // This task was started because of movement of the activity based on affinity...
2345 // Now that we are actually launching it, we can assign the base intent.
2346 intentActivity.task.setIntent(r);
2347 }
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002348
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002349 targetStack = intentActivity.task.stack;
2350 targetStack.mLastPausedActivity = null;
2351 // If the target task is not in the front, then we need
2352 // to bring it to the front... except... well, with
2353 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2354 // to have the same behavior as if a new instance was
2355 // being started, which means not bringing it to the front
2356 // if the caller is not itself in the front.
2357 final ActivityStack focusStack = getFocusedStack();
2358 ActivityRecord curTop = (focusStack == null)
2359 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
2360 boolean movedToFront = false;
2361 if (curTop != null && (curTop.task != intentActivity.task ||
2362 curTop.task != focusStack.topTask())) {
2363 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
2364 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2365 sourceStack.topActivity().task == sourceRecord.task)) {
2366 // We really do want to push this one into the user's face, right now.
2367 if (launchTaskBehind && sourceRecord != null) {
2368 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
Craig Mautneraea74a52014-03-08 14:23:10 -08002369 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002370 movedHome = true;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002371 final ActivityStack sideStack = getLaunchToSideStack(r, launchFlags, r.task);
2372 if (sideStack == null || sideStack == targetStack) {
2373 // We only want to move to the front, if we aren't going to launch on a
2374 // different stack. If we launch on a different stack, we will put the
2375 // task on top there.
2376 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
2377 options, r.appTimeTracker, "bringingFoundTaskToFront");
2378 movedToFront = true;
2379 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002380 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2381 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
2382 // Caller wants to appear on home activity.
2383 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautner29219d92013-04-16 20:19:12 -07002384 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002385 options = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002386 }
2387 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002388 if (!movedToFront && doResume) {
2389 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
2390 + " from " + intentActivity);
2391 targetStack.moveToFront("intentActivityFound");
2392 }
2393
2394 // If the caller has requested that the target task be
2395 // reset, then do so.
2396 if ((launchFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2397 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2398 }
2399 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2400 // We don't need to start a new activity, and
2401 // the client said not to do anything if that
2402 // is the case, so this is it! And for paranoia, make
2403 // sure we have correctly resumed the top activity.
2404 if (doResume) {
2405 resumeTopActivitiesLocked(targetStack, null, options);
2406
2407 // Make sure to notify Keyguard as well if we are not running an app
2408 // transition later.
2409 if (!movedToFront) {
2410 notifyActivityDrawnForKeyguard();
2411 }
2412 } else {
2413 ActivityOptions.abort(options);
2414 }
2415 updateUserStackLocked(r.userId, targetStack);
2416 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2417 }
2418 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2419 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
2420 // The caller has requested to completely replace any
2421 // existing task with its new activity. Well that should
2422 // not be too hard...
2423 reuseTask = intentActivity.task;
2424 reuseTask.performClearTaskLocked();
2425 reuseTask.setIntent(r);
2426 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
2427 || launchSingleInstance || launchSingleTask) {
2428 // In this situation we want to remove all activities
2429 // from the task up to the one being started. In most
2430 // cases this means we are resetting the task to its
2431 // initial state.
2432 ActivityRecord top = intentActivity.task.performClearTaskLocked(r, launchFlags);
2433 if (top != null) {
2434 if (top.frontOfTask) {
2435 // Activity aliases may mean we use different
2436 // intents for the top activity, so make sure
2437 // the task now has the identity of the new
2438 // intent.
2439 top.task.setIntent(r);
2440 }
2441 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
2442 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
2443 } else {
2444 // A special case: we need to start the activity because it is not
2445 // currently running, and the caller has asked to clear the current
2446 // task to have this activity at the top.
2447 addingToTask = true;
2448 // Now pretend like this activity is being started by the top of its
2449 // task, so it is put in the right place.
2450 sourceRecord = intentActivity;
2451 TaskRecord task = sourceRecord.task;
2452 if (task != null && task.stack == null) {
2453 // Target stack got cleared when we all activities were removed
2454 // above. Go ahead and reset it.
2455 targetStack = computeStackFocus(
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002456 sourceRecord, false /* newTask */, null /* bounds */, launchFlags);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002457 targetStack.addTask(task,
2458 !launchTaskBehind /* toTop */, "startActivityUnchecked");
2459 }
2460
2461 }
2462 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2463 // In this case the top activity on the task is the
2464 // same as the one being launched, so we take that
2465 // as a request to bring the task to the foreground.
2466 // If the top activity in the task is the root
2467 // activity, deliver this new intent to it if it
2468 // desires.
2469 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
2470 && intentActivity.realActivity.equals(r.realActivity)) {
2471 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2472 intentActivity.task);
2473 if (intentActivity.frontOfTask) {
2474 intentActivity.task.setIntent(r);
2475 }
2476 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2477 r.launchedFromPackage);
2478 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2479 // In this case we are launching the root activity
2480 // of the task, but with a different intent. We
2481 // should start a new instance on top.
2482 addingToTask = true;
2483 sourceRecord = intentActivity;
2484 }
2485 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2486 // In this case an activity is being launched in to an
2487 // existing task, without resetting that task. This
2488 // is typically the situation of launching an activity
2489 // from a notification or shortcut. We want to place
2490 // the new activity on top of the current task.
2491 addingToTask = true;
2492 sourceRecord = intentActivity;
2493 } else if (!intentActivity.task.rootWasReset) {
2494 // In this case we are launching in to an existing task
2495 // that has not yet been started from its front door.
2496 // The current task has been brought to the front.
2497 // Ideally, we'd probably like to place this new task
2498 // at the bottom of its stack, but that's a little hard
2499 // to do with the current organization of the code so
2500 // for now we'll just drop it.
2501 intentActivity.task.setIntent(r);
2502 }
2503 if (!addingToTask && reuseTask == null) {
2504 // We didn't do anything... but it was needed (a.k.a., client
2505 // don't use that intent!) And for paranoia, make
2506 // sure we have correctly resumed the top activity.
2507 if (doResume) {
2508 targetStack.resumeTopActivityLocked(null, options);
2509 if (!movedToFront) {
2510 // Make sure to notify Keyguard as well if we are not running an app
2511 // transition later.
2512 notifyActivityDrawnForKeyguard();
2513 }
2514 } else {
2515 ActivityOptions.abort(options);
2516 }
2517 updateUserStackLocked(r.userId, targetStack);
2518 return ActivityManager.START_TASK_TO_FRONT;
2519 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002520 }
2521
2522 //String uri = r.intent.toURI();
2523 //Intent intent2 = new Intent(uri);
2524 //Slog.i(TAG, "Given intent: " + r.intent);
2525 //Slog.i(TAG, "URI is: " + uri);
2526 //Slog.i(TAG, "To intent: " + intent2);
2527
2528 if (r.packageName != null) {
2529 // If the activity being launched is the same as the one currently
2530 // at the top, then we need to check if it should only be launched
2531 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002532 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002533 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Filip Gruszczynskia8e98b92015-12-15 16:35:38 -08002534 final boolean dontStart = top != null && r.resultTo == null
2535 && top.realActivity.equals(r.realActivity) && top.userId == r.userId
2536 && top.app != null && top.app.thread != null
2537 && ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2538 || launchSingleTop || launchSingleTask);
2539 if (dontStart) {
2540 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2541 // For paranoia, make sure we have correctly resumed the top activity.
2542 topStack.mLastPausedActivity = null;
2543 if (doResume) {
2544 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002545 }
Filip Gruszczynskia8e98b92015-12-15 16:35:38 -08002546 ActivityOptions.abort(options);
2547 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2548 // We don't need to start a new activity, and the client said not to do
2549 // anything if that is the case, so this is it!
2550 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2551 }
2552 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
2553 return ActivityManager.START_DELIVERED_TO_TOP;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002554 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002555 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002556 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002557 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2558 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002559 }
2560 ActivityOptions.abort(options);
2561 return ActivityManager.START_CLASS_NOT_FOUND;
2562 }
2563
2564 boolean newTask = false;
2565 boolean keepCurTransition = false;
2566
Craig Mautnerbb742462014-07-07 15:28:55 -07002567 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002568 sourceRecord.task : null;
2569
Craig Mautner8849a5e2013-04-02 16:41:03 -07002570 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002571 if (r.resultTo == null && inTask == null && !addingToTask
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002572 && (launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002573 newTask = true;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002574 targetStack = computeStackFocus(r, newTask, newBounds, launchFlags);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002575 if (doResume) {
2576 targetStack.moveToFront("startingNewTask");
2577 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002578
Craig Mautner8849a5e2013-04-02 16:41:03 -07002579 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002580 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2581 newTaskInfo != null ? newTaskInfo : r.info,
2582 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002583 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2584 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002585 if (overrideBounds) {
2586 r.task.updateOverrideConfiguration(newBounds);
2587 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002588 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2589 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002590 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002591 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002592 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002593 if (isLockTaskModeViolation(r.task)) {
2594 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2595 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2596 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002597 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002598 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002599 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2600 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002601 // Caller wants to appear on home activity, so before starting
2602 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002603 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002604 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002605 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002606 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002607 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002608 if (isLockTaskModeViolation(sourceTask)) {
2609 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2610 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2611 }
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002612 targetStack = null;
2613 if (sourceTask.stack.topTask() != sourceTask) {
2614 // We only want to allow changing stack if the target task is not the top one,
2615 // otherwise we would move the launching task to the other side, rather than show
2616 // two side by side.
2617 targetStack = getLaunchToSideStack(r, launchFlags, r.task);
2618 }
2619 if (targetStack == null) {
2620 targetStack = sourceTask.stack;
2621 } else if (targetStack != sourceTask.stack) {
2622 moveTaskToStackLocked(sourceTask.taskId, targetStack.mStackId, ON_TOP,
2623 FORCE_FOCUS, "launchToSide", !ANIMATE);
2624 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002625 if (doResume) {
2626 targetStack.moveToFront("sourceStackToFront");
2627 }
Craig Mautner737fae22014-10-15 12:52:10 -07002628 final TaskRecord topTask = targetStack.topTask();
2629 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002630 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002631 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002632 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002633 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002634 // In this case, we are adding the activity to an existing
2635 // task, but the caller has asked to clear that task if the
2636 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002637 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002638 keepCurTransition = true;
2639 if (top != null) {
2640 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002641 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002642 // For paranoia, make sure we have correctly
2643 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002644 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002645 if (doResume) {
2646 targetStack.resumeTopActivityLocked(null);
2647 }
2648 ActivityOptions.abort(options);
2649 return ActivityManager.START_DELIVERED_TO_TOP;
2650 }
2651 } else if (!addingToTask &&
2652 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2653 // In this case, we are launching an activity in our own task
2654 // that may already be running somewhere in the history, and
2655 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002656 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002657 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002658 final TaskRecord task = top.task;
2659 task.moveActivityToFrontLocked(top);
2660 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002661 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002662 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002663 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002664 if (doResume) {
2665 targetStack.resumeTopActivityLocked(null);
2666 }
2667 return ActivityManager.START_DELIVERED_TO_TOP;
2668 }
2669 }
2670 // An existing activity is starting this new activity, so we want
2671 // to keep the new one in the same task as the one that is starting
2672 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002673 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002674 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002675 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002676
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002677 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002678 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002679 // task it has provided to us.
2680 if (isLockTaskModeViolation(inTask)) {
2681 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2682 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2683 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002684 if (overrideBounds) {
2685 inTask.updateOverrideConfiguration(newBounds);
2686 int stackId = inTask.getLaunchStackId();
2687 if (stackId != inTask.stack.mStackId) {
2688 moveTaskToStackUncheckedLocked(
2689 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2690 }
2691 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002692 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002693 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2694 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002695
2696 // Check whether we should actually launch the new activity in to the task,
2697 // or just reuse the current activity on top.
2698 ActivityRecord top = inTask.getTopActivity();
2699 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2700 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2701 || launchSingleTop || launchSingleTask) {
2702 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2703 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2704 // We don't need to start a new activity, and
2705 // the client said not to do anything if that
2706 // is the case, so this is it!
2707 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2708 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002709 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002710 return ActivityManager.START_DELIVERED_TO_TOP;
2711 }
2712 }
2713
Dianne Hackborn962d5352014-08-29 16:27:40 -07002714 if (!addingToTask) {
2715 // We don't actually want to have this activity added to the task, so just
2716 // stop here but still tell the caller that we consumed the intent.
2717 ActivityOptions.abort(options);
2718 return ActivityManager.START_TASK_TO_FRONT;
2719 }
2720
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002721 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002722 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002723 + " in explicit task " + r.task);
2724
Craig Mautner8849a5e2013-04-02 16:41:03 -07002725 } else {
2726 // This not being started from an existing activity, and not part
2727 // of a new task... just put it in the top task, though these days
2728 // this case should never happen.
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002729 targetStack = computeStackFocus(r, newTask, null /* bounds */, launchFlags);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002730 if (doResume) {
2731 targetStack.moveToFront("addingToTopTask");
2732 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002733 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002734 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002735 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002736 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002737 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002738 + " in new guessed " + r.task);
2739 }
2740
2741 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002742 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002743
Craig Mautner76e2a762014-06-24 09:05:43 -07002744 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2745 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2746 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002747 if (newTask) {
2748 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2749 }
2750 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002751 targetStack.mLastPausedActivity = null;
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002752 targetStack.startActivityLocked(r, newTask, keepCurTransition, options);
2753 if (doResume) {
2754 if (!launchTaskBehind) {
2755 mService.setFocusedActivityLocked(r, "startedActivity");
2756 }
2757 resumeTopActivitiesLocked(targetStack, r, options);
2758 } else {
2759 targetStack.addRecentActivityLocked(r);
Craig Mautnerbb742462014-07-07 15:28:55 -07002760 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002761 updateUserStackLocked(r.userId, targetStack);
Chong Zhangc806d902015-11-30 09:44:27 -08002762
2763 if (!r.task.mResizeable && isStackDockedInEffect(targetStack.mStackId)) {
2764 showNonResizeableDockToast(r.task.taskId);
2765 }
2766
Craig Mautner8849a5e2013-04-02 16:41:03 -07002767 return ActivityManager.START_SUCCESS;
2768 }
2769
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002770 /**
2771 * Decide whether the new activity should be inserted into an existing task. Returns null if not
2772 * or an ActivityRecord with the task into which the new activity should be added.
2773 */
2774 private ActivityRecord getReusableIntentActivity(ActivityRecord r, TaskRecord inTask, Intent intent,
2775 boolean launchSingleInstance, boolean launchSingleTask, int launchFlags) {
2776 // We may want to try to place the new activity in to an existing task. We always
2777 // do this if the target activity is singleTask or singleInstance; we will also do
2778 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2779 // us to still place it in a new task: multi task, always doc mode, or being asked to
2780 // launch this as a new task behind the current one.
2781 boolean putIntoExistingTask = ((launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0 &&
2782 (launchFlags & FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
2783 || launchSingleInstance || launchSingleTask;
2784 // If bring to front is requested, and no result is requested and we have not
2785 // been given an explicit task to launch in to, and
2786 // we can find a task that was started with this same
2787 // component, then instead of launching bring that one to the front.
2788 putIntoExistingTask &= inTask == null && r.resultTo == null;
2789 ActivityRecord intentActivity = null;
2790 if (putIntoExistingTask) {
2791 // See if there is a task to bring to the front. If this is
2792 // a SINGLE_INSTANCE activity, there can be one and only one
2793 // instance of it in the history, and it is always in its own
2794 // unique task, so we do a special search.
2795 intentActivity = launchSingleInstance ?
2796 findActivityLocked(intent, r.info) : findTaskLocked(r);
2797 }
2798 return intentActivity;
2799 }
2800
Craig Mautneree36c772014-07-16 14:56:05 -07002801 final void doPendingActivityLaunchesLocked(boolean doResume) {
2802 while (!mPendingActivityLaunches.isEmpty()) {
2803 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002804
2805 try {
2806 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Robert Carr13997f52015-10-23 13:13:39 -07002807 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002808 } catch (Exception e) {
Robert Carr13997f52015-10-23 13:13:39 -07002809 Slog.e(TAG, "Exception during pending activity launch pal=" + pal, e);
2810 pal.sendErrorResult(e.getMessage());
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002811 }
Craig Mautneree36c772014-07-16 14:56:05 -07002812 }
2813 }
2814
Craig Mautner7f13ed32014-07-28 14:00:35 -07002815 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002816 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2817 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002818 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002819 mPendingActivityLaunches.remove(palNdx);
2820 }
2821 }
2822 }
2823
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002824 void setLaunchSource(int uid) {
2825 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2826 }
2827
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002828 void acquireLaunchWakelock() {
2829 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2830 throw new IllegalStateException("Calling must be system uid");
2831 }
2832 mLaunchingActivity.acquire();
2833 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2834 // To be safe, don't allow the wake lock to be held for too long.
2835 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2836 }
2837 }
2838
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002839 /**
2840 * Called when the frontmost task is idle.
2841 * @return the state of mService.mBooting before this was called.
2842 */
2843 private boolean checkFinishBootingLocked() {
2844 final boolean booting = mService.mBooting;
2845 boolean enableScreen = false;
2846 mService.mBooting = false;
2847 if (!mService.mBooted) {
2848 mService.mBooted = true;
2849 enableScreen = true;
2850 }
2851 if (booting || enableScreen) {
2852 mService.postFinishBooting(booting, enableScreen);
2853 }
2854 return booting;
2855 }
2856
Craig Mautnerf3333272013-04-22 10:55:53 -07002857 // Checked.
2858 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2859 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002860 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002861
Craig Mautnerf3333272013-04-22 10:55:53 -07002862 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002863 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002864 int NS = 0;
2865 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002866 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002867 boolean activityRemoved = false;
2868
Wale Ogunwale7d701172015-03-11 15:36:30 -07002869 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002870 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002871 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2872 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002873 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2874 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002875 if (fromTimeout) {
2876 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002877 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002878
2879 // This is a hack to semi-deal with a race condition
2880 // in the client where it can be constructed with a
2881 // newer configuration from when we asked it to launch.
2882 // We'll update with whatever configuration it now says
2883 // it used to launch.
2884 if (config != null) {
2885 r.configuration = config;
2886 }
2887
2888 // We are now idle. If someone is waiting for a thumbnail from
2889 // us, we can now deliver.
2890 r.idle = true;
2891
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002892 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002893 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002894 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002895 }
2896 }
2897
2898 if (allResumedActivitiesIdle()) {
2899 if (r != null) {
2900 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002901 }
2902
2903 if (mLaunchingActivity.isHeld()) {
2904 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2905 if (VALIDATE_WAKE_LOCK_CALLER &&
2906 Binder.getCallingUid() != Process.myUid()) {
2907 throw new IllegalStateException("Calling must be system uid");
2908 }
2909 mLaunchingActivity.release();
2910 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002911 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002912 }
2913
2914 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002915 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07002916 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002917 if ((NF = mFinishingActivities.size()) > 0) {
2918 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002919 mFinishingActivities.clear();
2920 }
2921
Craig Mautnerf3333272013-04-22 10:55:53 -07002922 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002923 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002924 mStartingUsers.clear();
2925 }
2926
Craig Mautnerf3333272013-04-22 10:55:53 -07002927 // Stop any activities that are scheduled to do so but have been
2928 // waiting for the next one to start.
2929 for (int i = 0; i < NS; i++) {
2930 r = stops.get(i);
2931 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002932 if (stack != null) {
2933 if (r.finishing) {
2934 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2935 } else {
2936 stack.stopActivityLocked(r);
2937 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002938 }
2939 }
2940
2941 // Finish any activities that are scheduled to do so but have been
2942 // waiting for the next one to start.
2943 for (int i = 0; i < NF; i++) {
2944 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002945 final ActivityStack stack = r.task.stack;
2946 if (stack != null) {
2947 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2948 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002949 }
2950
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002951 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002952 // Complete user switch
2953 if (startingUsers != null) {
2954 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002955 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002956 }
2957 }
2958 // Complete starting up of background users
2959 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002960 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002961 mStartingBackgroundUsers.clear();
2962 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002963 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002964 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002965 }
2966 }
2967
2968 mService.trimApplications();
2969 //dump();
2970 //mWindowManager.dump();
2971
Craig Mautnerf3333272013-04-22 10:55:53 -07002972 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002973 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002974 }
2975
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002976 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002977 }
2978
Craig Mautner8e569572013-10-11 17:36:59 -07002979 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002980 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002981 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2982 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002983 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2984 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2985 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002986 }
Craig Mautner19091252013-10-05 00:03:53 -07002987 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002988 }
2989
2990 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002991 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2992 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002993 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2994 stacks.get(stackNdx).closeSystemDialogsLocked();
2995 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002996 }
2997 }
2998
Craig Mautner93529a42013-10-04 15:03:13 -07002999 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003000 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07003001 }
3002
Craig Mautner8d341ef2013-03-26 09:03:27 -07003003 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07003004 * Update the last used stack id for non-current user (current user's last
3005 * used stack is the focused stack)
3006 */
3007 void updateUserStackLocked(int userId, ActivityStack stack) {
3008 if (userId != mCurrentUser) {
3009 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
3010 }
3011 }
3012
3013 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07003014 * @return true if some activity was finished (or would have finished if doit were true).
3015 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07003016 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
3017 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003018 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003019 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3020 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08003021 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003022 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07003023 if (stack.finishDisabledPackageActivitiesLocked(
3024 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003025 didSomething = true;
3026 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003027 }
3028 }
3029 return didSomething;
3030 }
3031
Dianne Hackborna413dc02013-07-12 12:02:55 -07003032 void updatePreviousProcessLocked(ActivityRecord r) {
3033 // Now that this process has stopped, we may want to consider
3034 // it to be the previous app to try to keep around in case
3035 // the user wants to return to it.
3036
3037 // First, found out what is currently the foreground app, so that
3038 // we don't blow away the previous app if this activity is being
3039 // hosted by the process that is actually still the foreground.
3040 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08003041 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3042 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003043 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3044 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003045 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003046 if (stack.mResumedActivity != null) {
3047 fgApp = stack.mResumedActivity.app;
3048 } else if (stack.mPausingActivity != null) {
3049 fgApp = stack.mPausingActivity.app;
3050 }
3051 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07003052 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07003053 }
3054 }
3055
3056 // Now set this one as the previous process, only if that really
3057 // makes sense to.
3058 if (r.app != null && fgApp != null && r.app != fgApp
3059 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07003060 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07003061 mService.mPreviousProcess = r.app;
3062 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
3063 }
3064 }
3065
Craig Mautner05d29032013-05-03 13:40:13 -07003066 boolean resumeTopActivitiesLocked() {
3067 return resumeTopActivitiesLocked(null, null, null);
3068 }
3069
Chong Zhang280d3322015-11-03 17:27:26 -08003070 boolean resumeTopActivitiesLocked(
3071 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Craig Mautner05d29032013-05-03 13:40:13 -07003072 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003073 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07003074 }
Craig Mautner12ff7392014-02-21 21:08:00 -08003075 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07003076 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003077 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08003078 result = targetStack.resumeTopActivityLocked(target, targetOptions);
3079 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003080
Craig Mautnere0a38842013-12-16 16:14:02 -08003081 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3082 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003083 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3084 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08003085 if (stack == targetStack) {
3086 // Already started above.
3087 continue;
3088 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003089 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08003090 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003091 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003092 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003093 }
Craig Mautner05d29032013-05-03 13:40:13 -07003094 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003095 }
3096
Todd Kennedy539db512014-12-15 09:57:55 -08003097 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003098 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3099 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003100 final int numStacks = stacks.size();
3101 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3102 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08003103 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003104 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003105 }
3106 }
3107
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003108 void finishVoiceTask(IVoiceInteractionSession session) {
3109 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3110 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3111 final int numStacks = stacks.size();
3112 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3113 final ActivityStack stack = stacks.get(stackNdx);
3114 stack.finishVoiceTask(session);
3115 }
3116 }
3117 }
3118
Chong Zhang280d3322015-11-03 17:27:26 -08003119 void findTaskToMoveToFrontLocked(
3120 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003121 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
3122 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003123 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003124 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3125 // Caller wants the home activity moved with it. To accomplish this,
3126 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07003127 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08003128 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003129 if (task.stack == null) {
3130 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
3131 + task + " to front. Stack is null");
3132 return;
3133 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07003134
Chong Zhang0fa656b2015-08-31 15:17:21 -07003135 if (task.mResizeable && options != null) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08003136 if (canUseActivityOptionsLaunchBounds(options)) {
3137 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07003138 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08003139 final int stackId = task.getLaunchStackId();
3140 if (stackId != task.stack.mStackId) {
3141 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
3142 // moveTaskToStackUncheckedLocked() should already placed the task on top,
3143 // still need moveTaskToFrontLocked() below for any transition settings.
3144 }
3145 // WM resizeTask must be done after the task is moved to the correct stack,
3146 // because Task's setBounds() also updates dim layer's bounds, but that has
3147 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07003148 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
3149 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07003150 }
3151 }
3152
Chong Zhangdb20b5f2015-10-23 14:01:43 -07003153 final ActivityRecord r = task.getTopActivity();
3154 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
3155 r == null ? null : r.appTimeTracker, reason);
3156
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003157 if (DEBUG_STACK) Slog.d(TAG_STACK,
3158 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003159 }
3160
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08003161 private boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
3162 // We use the launch bounds in the activity options is the device supports freeform
3163 // window management.
3164 return options.hasLaunchBounds() && mService.mSupportsFreeformWindowManagement;
3165 }
3166
Craig Mautner967212c2013-04-13 21:10:58 -07003167 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003168 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003169 }
3170
3171 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003172 ActivityContainer activityContainer = mActivityContainers.get(stackId);
3173 if (activityContainer != null) {
3174 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003175 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07003176 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003177 return null;
3178 }
3179 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003180 }
3181
Craig Mautner967212c2013-04-13 21:10:58 -07003182 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003183 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003184 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3185 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003186 }
3187 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07003188 }
3189
Craig Mautner4a1cb222013-12-04 16:14:06 -08003190 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003191 ActivityRecord homeActivity = getHomeActivity();
3192 if (homeActivity != null) {
3193 return homeActivity.appToken;
3194 }
3195 return null;
3196 }
3197
3198 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07003199 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003200 }
3201
3202 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003203 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
3204 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
3205 final TaskRecord task = tasks.get(taskNdx);
3206 if (task.isHomeTask()) {
3207 final ArrayList<ActivityRecord> activities = task.mActivities;
3208 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3209 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003210 if (r.isHomeActivity()
3211 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003212 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003213 }
3214 }
3215 }
3216 }
3217 return null;
3218 }
3219
Chong Zhangb15758a2015-11-17 12:12:03 -08003220 /**
3221 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
3222 * the docked stack itself, or if it's side-by-side to the docked stack.
3223 */
3224 boolean isStackDockedInEffect(int stackId) {
3225 return stackId == DOCKED_STACK_ID ||
3226 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
3227 }
3228
Todd Kennedyca4d8422015-01-15 15:19:22 -08003229 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08003230 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003231 ActivityContainer activityContainer =
3232 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003233 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003234 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3235 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003236 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003237 return activityContainer;
3238 }
3239
Craig Mautner34b73df2014-01-12 21:11:08 -08003240 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003241 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
3242 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
3243 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003244 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
3245 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003246 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08003247 }
3248 }
3249
Craig Mautner95da1082014-02-24 17:54:35 -08003250 void deleteActivityContainer(IActivityContainer container) {
3251 ActivityContainer activityContainer = (ActivityContainer)container;
3252 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003253 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3254 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08003255 final int stackId = activityContainer.mStackId;
3256 mActivityContainers.remove(stackId);
3257 mWindowManager.removeStack(stackId);
3258 }
3259 }
3260
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003261 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
3262 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003263 final ActivityStack stack = getStack(stackId);
3264 if (stack == null) {
3265 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
3266 return;
3267 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003268
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003269 if (!allowResizeInDockedMode
3270 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
3271 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
3272 // stack size changing so things don't get out of sync.
3273 return;
3274 }
3275
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003276 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003277 mWindowManager.deferSurfaceLayout();
3278 try {
Wale Ogunwaleb4ec0a32015-12-14 10:31:43 -08003279
3280 if (bounds != null && mWindowManager.isFullscreenBounds(stackId, bounds)) {
3281 // The bounds passed in corresponds to the fullscreen bounds which we normally
3282 // represent with null. Go ahead and set it to null so that all tasks configuration
3283 // can have the right fullscreen state.
3284 bounds = null;
3285 }
3286
Jorim Jaggic4025202015-10-22 16:43:34 +02003287 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003288
Jorim Jaggic4025202015-10-22 16:43:34 +02003289 mTmpBounds.clear();
3290 mTmpConfigs.clear();
3291 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3292 for (int i = tasks.size() - 1; i >= 0; i--) {
3293 TaskRecord task = tasks.get(i);
3294 if (task.mResizeable) {
3295 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3296 // For freeform stack we don't adjust the size of the tasks to match that
3297 // of the stack, but we do try to make sure the tasks are still contained
3298 // with the bounds of the stack.
3299 tempRect2.set(task.mBounds);
3300 fitWithinBounds(tempRect2, bounds);
3301 task.updateOverrideConfiguration(tempRect2);
3302 } else {
3303 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003304 }
3305 }
3306
Jorim Jaggic4025202015-10-22 16:43:34 +02003307 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3308 mTmpBounds.put(task.taskId, task.mBounds);
3309 }
3310 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3311 if (stack.mStackId == DOCKED_STACK_ID) {
3312 // Dock stack funness...Yay!
3313 if (stack.mFullscreen) {
3314 // The dock stack went fullscreen which is kinda like dismissing it.
3315 // In this case we make all other static stacks fullscreen and move all
3316 // docked stack tasks to the fullscreen stack.
3317 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003318 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003319 resizeStackLocked(i, null, preserveWindows, true);
3320 }
3321 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003322
Jorim Jaggic4025202015-10-22 16:43:34 +02003323 final int count = tasks.size();
3324 for (int i = 0; i < count; i++) {
3325 moveTaskToStackLocked(tasks.get(i).taskId,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003326 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
Jorim Jaggi55387522015-11-24 18:21:10 -08003327 false /* animate */);
Jorim Jaggic4025202015-10-22 16:43:34 +02003328 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003329
Jorim Jaggic4025202015-10-22 16:43:34 +02003330 // stack shouldn't contain anymore activities, so nothing to resume.
3331 r = null;
3332 } else {
3333 // Docked stacks occupy a dedicated region on screen so the size of all other
3334 // static stacks need to be adjusted so they don't overlap with the docked stack.
3335 // We get the bounds to use from window manager which has been adjusted for any
3336 // screen controls and is also the same for all stacks.
3337 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3338
3339 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003340 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003341 ActivityStack otherStack = getStack(i);
3342 if (otherStack != null) {
3343 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3344 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003345 }
3346 }
3347 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003348 // Since we are resizing the stack, all other operations should strive to preserve
3349 // windows.
3350 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003351 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003352 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003353
Jorim Jaggic4025202015-10-22 16:43:34 +02003354 if (r != null) {
3355 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3356 // And we need to make sure at this point that all other activities
3357 // are made visible with the correct configuration.
3358 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3359 if (!updated) {
3360 resumeTopActivitiesLocked(stack, null, null);
3361 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003362 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003363 } finally {
3364 mWindowManager.continueSurfaceLayout();
3365 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003366 }
3367 }
3368
Chong Zhang6de2ae82015-09-30 18:25:21 -07003369 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003370 if (!task.mResizeable) {
3371 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3372 return;
3373 }
3374
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003375 adjustForMinimalTaskDimensions(task, bounds);
3376
Chong Zhang87b21722015-09-21 15:39:51 -07003377 // If this is a forced resize, let it go through even if the bounds is not changing,
3378 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003379 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003380 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003381 // Nothing to do here...
3382 return;
3383 }
3384
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003385 if (!mWindowManager.isValidTaskId(task.taskId)) {
3386 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003387 // All we can do for now is update the bounds so it can be used when the task is
3388 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003389 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003390 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3391 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003392 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003393 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003394 return;
3395 }
3396
Chong Zhang6de2ae82015-09-30 18:25:21 -07003397 // Do not move the task to another stack here.
3398 // This method assumes that the task is already placed in the right stack.
3399 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003400
Chong Zhang6de2ae82015-09-30 18:25:21 -07003401 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003402
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003403 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003404 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003405 // way and the activity was kept the way it was. If it's false, it means the activity had
3406 // to be relaunched due to configuration change.
3407 boolean kept = true;
3408 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003409 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003410 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003411 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003412 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003413 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003414 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003415 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003416 resumeTopActivitiesLocked(stack, null, null);
3417 }
3418 }
3419 }
Chong Zhang87b21722015-09-21 15:39:51 -07003420 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003421
3422 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003423 }
3424
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003425 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3426 if (bounds == null) {
3427 return;
3428 }
3429 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3430 : task.mMinimalSize;
3431 final boolean adjustWidth = minimalSize > bounds.width();
3432 final boolean adjustHeight = minimalSize > bounds.height();
3433 if (!(adjustWidth || adjustHeight)) {
3434 return;
3435 }
3436 Rect taskBounds = task.mBounds;
3437 if (adjustWidth) {
3438 if (taskBounds != null && bounds.right == taskBounds.right) {
3439 bounds.left = bounds.right - minimalSize;
3440 } else {
3441 // Either left bounds match, or neither match, or the previous bounds were
3442 // fullscreen and we default to keeping left.
3443 bounds.right = bounds.left + minimalSize;
3444 }
3445 }
3446 if (adjustHeight) {
3447 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3448 bounds.top = bounds.bottom - minimalSize;
3449 } else {
3450 // Either top bounds match, or neither match, or the previous bounds were
3451 // fullscreen and we default to keeping top.
3452 bounds.bottom = bounds.top + minimalSize;
3453 }
3454 }
3455 }
3456
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003457 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003458 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3459 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003460 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003461 }
3462
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003463 ActivityContainer activityContainer = new ActivityContainer(stackId);
3464 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003465 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003466 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003467 }
3468
3469 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003470 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003471 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3472 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003473 break;
3474 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003475 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003476 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003477 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003478 }
3479
Chong Zhang5dcb2752015-08-18 13:50:26 -07003480 /**
3481 * Restores a recent task to a stack
3482 * @param task The recent task to be restored.
3483 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07003484 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003485 * @return true if the task has been restored successfully.
3486 */
3487 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3488 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003489 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003490 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003491 if (task.stack != null) {
3492 // Task has already been restored once. See if we need to do anything more
3493 if (task.stack.mStackId == stackId) {
3494 // Nothing else to do since it is already restored in the right stack.
3495 return true;
3496 }
3497 // Remove current stack association, so we can re-associate the task with the
3498 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003499 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003500 }
3501
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003502 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003503 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003504
3505 if (stack == null) {
3506 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003507 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3508 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003509 return false;
3510 }
3511
Wale Ogunwale5f986092015-12-04 15:35:38 -08003512 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003513 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3514 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003515 final ArrayList<ActivityRecord> activities = task.mActivities;
3516 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003517 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003518 }
3519 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003520 }
3521
Wale Ogunwale040b4702015-08-06 18:10:50 -07003522 /**
3523 * Moves the specified task record to the input stack id.
3524 * WARNING: This method performs an unchecked/raw move of the task and
3525 * can leave the system in an unstable state if used incorrectly.
3526 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3527 * to a stack.
3528 * @param task Task to move.
3529 * @param stackId Id of stack to move task to.
3530 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003531 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003532 * @param reason Reason the task is been moved.
3533 * @return The stack the task was moved to.
3534 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003535 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003536 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3537 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003538 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003539 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3540
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003541 final boolean resizeable = task.mResizeable;
3542 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3543 // if a docked stack is created below which will lead to the stack we are moving from and
3544 // its resizeable tasks being resized.
3545 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003546 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003547 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003548 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003549 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003550
3551 // If the task had focus before (or we're requested to move focus),
3552 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003553 stack.setFocusAndResumeStateIfNeeded(
3554 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003555
Wale Ogunwale040b4702015-08-06 18:10:50 -07003556 return stack;
3557 }
3558
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003559 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003560 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003561 final TaskRecord task = anyTaskForIdLocked(taskId);
3562 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003563 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003564 return;
3565 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003566
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003567 if (task.stack != null && task.stack.mStackId == stackId) {
3568 // You are already in the right stack silly...
3569 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
3570 return;
3571 }
3572
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003573 final ActivityRecord topActivity = task.getTopActivity();
3574 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003575 // We are about to relaunch the activity because its configuration changed due to
3576 // being maximized, i.e. size change. The activity will first remove the old window
3577 // and then add a new one. This call will tell window manager about this, so it can
3578 // preserve the old window until the new one is drawn. This prevents having a gap
3579 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003580 // entrance of the new window to be properly animated.
Jorim Jaggi030979c2015-11-20 15:14:43 -08003581 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003582 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08003583 final ActivityStack stack = moveTaskToStackUncheckedLocked(
3584 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003585
Jorim Jaggi55387522015-11-24 18:21:10 -08003586 if (!animate) {
3587 stack.mNoAnimActivities.add(topActivity);
3588 }
3589
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003590 // Make sure the task has the appropriate bounds/size for the stack it is in.
3591 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003592 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003593 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3594 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003595 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3596 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003597 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003598 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003599 }
3600
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003601 // The task might have already been running and its visibility needs to be synchronized with
3602 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003603 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003604 resumeTopActivitiesLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08003605
3606 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08003607 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003608 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003609 }
3610
Wale Ogunwale079a0042015-10-24 11:44:07 -07003611 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3612 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3613 if (stack == null) {
3614 throw new IllegalArgumentException(
3615 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3616 }
3617
3618 final ActivityRecord r = stack.topRunningActivityLocked();
3619 if (r == null) {
3620 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3621 + " in stack=" + stack);
3622 return false;
3623 }
3624
Youngsang Choba80ba52015-12-11 14:32:35 -08003625 if (!mService.mForceResizableActivities && !r.info.supportsPip) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003626 Slog.w(TAG,
3627 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003628 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003629 return false;
3630 }
3631
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003632 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", bounds);
3633 return true;
3634 }
3635
3636 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003637 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003638 if (task.mActivities.size() == 1) {
3639 // There is only one activity in the task. So, we can just move the task over to the
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003640 // stack without re-parenting the activity in a different task.
3641 moveTaskToStackLocked(
3642 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003643 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08003644 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
3645 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003646 }
3647
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003648 if (bounds != null) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003649 resizeStackLocked(stackId, bounds, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003650 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07003651
3652 // The task might have already been running and its visibility needs to be synchronized with
3653 // the visibility of the stack / windows.
3654 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3655 resumeTopActivitiesLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003656 }
3657
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003658 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3659 final TaskRecord task = anyTaskForIdLocked(taskId);
3660 if (task == null) {
3661 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3662 return;
3663 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08003664 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
3665
3666 task.updateOverrideConfigurationForStack(stack);
3667
3668 mWindowManager.positionTaskInStack(
3669 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003670 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003671 // The task might have already been running and its visibility needs to be synchronized with
3672 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003673 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003674 resumeTopActivitiesLocked();
3675 }
3676
Craig Mautnerac6f8432013-07-17 13:24:59 -07003677 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003678 mTmpFindTaskResult.r = null;
3679 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003680 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003681 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3682 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003683 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3684 final ActivityStack stack = stacks.get(stackNdx);
3685 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003686 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003687 continue;
3688 }
3689 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003690 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3691 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003692 continue;
3693 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003694 stack.findTaskLocked(r, mTmpFindTaskResult);
3695 // It is possible to have task in multiple stacks with the same root affinity.
3696 // If the match we found was based on root affinity we keep on looking to see if
3697 // there is a better match in another stack. We eventually return the match based
3698 // on root affinity if we don't find a better match.
3699 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
3700 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003701 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003702 }
3703 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003704 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
3705 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003706 }
3707
3708 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003709 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3710 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003711 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3712 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3713 if (ar != null) {
3714 return ar;
3715 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003716 }
3717 }
3718 return null;
3719 }
3720
Craig Mautner8d341ef2013-03-26 09:03:27 -07003721 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003722 scheduleSleepTimeout();
3723 if (!mGoingToSleep.isHeld()) {
3724 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003725 if (mLaunchingActivity.isHeld()) {
3726 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3727 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003728 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003729 mLaunchingActivity.release();
3730 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003731 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003732 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003733 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003734 }
3735
3736 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003737 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003738
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003739 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003740 final long endTime = System.currentTimeMillis() + timeout;
3741 while (true) {
3742 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003743 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3744 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003745 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3746 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3747 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003748 }
3749 if (cantShutdown) {
3750 long timeRemaining = endTime - System.currentTimeMillis();
3751 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003752 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003753 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003754 } catch (InterruptedException e) {
3755 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003756 } else {
3757 Slog.w(TAG, "Activity manager shutdown timed out");
3758 timedout = true;
3759 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003760 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003761 } else {
3762 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003763 }
3764 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003765
3766 // Force checkReadyForSleep to complete.
3767 mSleepTimeout = true;
3768 checkReadyForSleepLocked();
3769
Craig Mautner8d341ef2013-03-26 09:03:27 -07003770 return timedout;
3771 }
3772
3773 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003774 removeSleepTimeouts();
3775 if (mGoingToSleep.isHeld()) {
3776 mGoingToSleep.release();
3777 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003778 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3779 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3781 final ActivityStack stack = stacks.get(stackNdx);
3782 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003783 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003784 resumeTopActivitiesLocked();
3785 }
Craig Mautner5314a402013-09-26 12:40:16 -07003786 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003787 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003788 mGoingToSleepActivities.clear();
3789 }
3790
3791 void activitySleptLocked(ActivityRecord r) {
3792 mGoingToSleepActivities.remove(r);
3793 checkReadyForSleepLocked();
3794 }
3795
3796 void checkReadyForSleepLocked() {
3797 if (!mService.isSleepingOrShuttingDown()) {
3798 // Do not care.
3799 return;
3800 }
3801
3802 if (!mSleepTimeout) {
3803 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003804 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3805 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3807 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3808 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003809 }
3810
3811 if (mStoppingActivities.size() > 0) {
3812 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003813 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003814 + mStoppingActivities.size() + " activities");
3815 scheduleIdleLocked();
3816 dontSleep = true;
3817 }
3818
3819 if (mGoingToSleepActivities.size() > 0) {
3820 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003821 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003822 + mGoingToSleepActivities.size() + " activities");
3823 dontSleep = true;
3824 }
3825
3826 if (dontSleep) {
3827 return;
3828 }
3829 }
3830
Craig Mautnere0a38842013-12-16 16:14:02 -08003831 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3832 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003833 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3834 stacks.get(stackNdx).goToSleep();
3835 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003836 }
3837
3838 removeSleepTimeouts();
3839
3840 if (mGoingToSleep.isHeld()) {
3841 mGoingToSleep.release();
3842 }
3843 if (mService.mShuttingDown) {
3844 mService.notifyAll();
3845 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003846 }
3847
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003848 boolean reportResumedActivityLocked(ActivityRecord r) {
3849 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003850 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003851 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003852 }
3853 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003854 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003855 mWindowManager.executeAppTransition();
3856 return true;
3857 }
3858 return false;
3859 }
3860
Craig Mautner8d341ef2013-03-26 09:03:27 -07003861 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003862 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3863 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003864 int stackNdx = stacks.size() - 1;
3865 while (stackNdx >= 0) {
3866 stacks.get(stackNdx).handleAppCrashLocked(app);
3867 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003868 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003869 }
3870 }
3871
Jose Lima4b6c6692014-08-12 17:41:12 -07003872 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003873 final ActivityStack stack = r.task.stack;
3874 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003875 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3876 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003877 return false;
3878 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003879 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003880 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3881 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003882
3883 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003884 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003885 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003886 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003887 return true;
3888 }
3889
3890 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003891 if (visible && top.fullscreen) {
3892 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003893 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3894 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3895 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3896 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003897 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003898 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3899 // Only the activity set as currently visible behind should actively reset its
3900 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003901 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3902 "requestVisibleBehind: returning visible=" + visible
3903 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3904 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003905 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003906 }
3907
Jose Lima4b6c6692014-08-12 17:41:12 -07003908 stack.setVisibleBehindActivity(visible ? r : null);
3909 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003910 // Make the activity immediately above r opaque.
3911 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3912 if (next != null) {
3913 mService.convertFromTranslucent(next.appToken);
3914 }
3915 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003916 if (top.app != null && top.app.thread != null) {
3917 // Notify the top app of the change.
3918 try {
3919 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3920 } catch (RemoteException e) {
3921 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003922 }
3923 return true;
3924 }
3925
Craig Mautnerbb742462014-07-07 15:28:55 -07003926 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3927 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3928 r.mLaunchTaskBehind = false;
3929 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08003930 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003931 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003932 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003933 mWindowManager.setAppVisibility(r.appToken, false);
3934 }
3935
3936 void scheduleLaunchTaskBehindComplete(IBinder token) {
3937 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3938 }
3939
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003940 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3941 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003942 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003943 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3944 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003945 final int topStackNdx = stacks.size() - 1;
3946 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3947 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003948 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003949 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003950 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003951 }
3952
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003953 void invalidateTaskLayers() {
3954 mTaskLayersChanged = true;
3955 }
3956
3957 void rankTaskLayersIfNeeded() {
3958 if (!mTaskLayersChanged) {
3959 return;
3960 }
3961 mTaskLayersChanged = false;
3962 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3963 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3964 int baseLayer = 0;
3965 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3966 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3967 }
3968 }
3969 }
3970
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003971 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3972 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3973 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3974 final int topStackNdx = stacks.size() - 1;
3975 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3976 final ActivityStack stack = stacks.get(stackNdx);
3977 stack.clearOtherAppTimeTrackers(except);
3978 }
3979 }
3980 }
3981
Craig Mautner8d341ef2013-03-26 09:03:27 -07003982 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003983 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3984 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003985 final int numStacks = stacks.size();
3986 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3987 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003988 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003989 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003990 }
3991 }
3992
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003993 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3994 // Examine all activities currently running in the process.
3995 TaskRecord firstTask = null;
3996 // Tasks is non-null only if two or more tasks are found.
3997 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003998 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3999 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004000 ActivityRecord r = app.activities.get(i);
4001 // First, if we find an activity that is in the process of being destroyed,
4002 // then we just aren't going to do anything for now; we want things to settle
4003 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004004 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004005 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004006 return;
4007 }
4008 // Don't consider any activies that are currently not in a state where they
4009 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004010 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
4011 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004012 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004013 continue;
4014 }
4015 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004016 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004017 + " from " + r);
4018 if (firstTask == null) {
4019 firstTask = r.task;
4020 } else if (firstTask != r.task) {
4021 if (tasks == null) {
4022 tasks = new ArraySet<>();
4023 tasks.add(firstTask);
4024 }
4025 tasks.add(r.task);
4026 }
4027 }
4028 }
4029 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004030 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004031 return;
4032 }
4033 // If we have activities in multiple tasks that are in a position to be destroyed,
4034 // let's iterate through the tasks and release the oldest one.
4035 final int numDisplays = mActivityDisplays.size();
4036 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
4037 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4038 // Step through all stacks starting from behind, to hit the oldest things first.
4039 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
4040 final ActivityStack stack = stacks.get(stackNdx);
4041 // Try to release activities in this stack; if we manage to, we are done.
4042 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
4043 return;
4044 }
4045 }
4046 }
4047 }
4048
Amith Yamasani37a40c22015-06-17 13:25:42 -07004049 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004050 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07004051 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07004052 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07004053
Craig Mautner858d8a62013-04-23 17:08:34 -07004054 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08004055 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4056 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004057 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004058 final ActivityStack stack = stacks.get(stackNdx);
4059 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00004060 TaskRecord task = stack.topTask();
4061 if (task != null) {
4062 mWindowManager.moveTaskToTop(task.taskId);
4063 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004064 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07004065 }
Craig Mautner858d8a62013-04-23 17:08:34 -07004066
Craig Mautner4f1df4f2013-10-15 15:44:14 -07004067 ActivityStack stack = getStack(restoreStackId);
4068 if (stack == null) {
4069 stack = mHomeStack;
4070 }
4071 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08004072 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004073 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08004074 } else {
4075 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08004076 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08004077 }
Craig Mautner93529a42013-10-04 15:03:13 -07004078 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07004079 }
4080
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07004081 /**
4082 * Add background users to send boot completed events to.
4083 * @param userId The user being started in the background
4084 * @param uss The state object for the user.
4085 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07004086 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07004087 mStartingBackgroundUsers.add(uss);
4088 }
4089
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07004090 /** Checks whether the userid is a profile of the current user. */
4091 boolean isCurrentProfileLocked(int userId) {
4092 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07004093 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07004094 }
4095
Chong Zhang45c25ce2015-08-10 22:18:26 -07004096 /** Checks whether the activity should be shown for current user. */
4097 boolean okToShowLocked(ActivityRecord r) {
4098 return r != null && (isCurrentProfileLocked(r.userId)
4099 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
4100 }
4101
Craig Mautnerde4ef022013-04-07 19:01:33 -07004102 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07004103 ArrayList<ActivityRecord> stops = null;
4104
4105 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08004106 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
4107 ActivityRecord s = mStoppingActivities.get(activityNdx);
4108 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004109 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08004110 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
4111 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07004112 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004113 if (s.finishing) {
4114 // If this activity is finishing, it is sitting on top of
4115 // everyone else but we now know it is no longer needed...
4116 // so get rid of it. Otherwise, we need to go through the
4117 // normal flow and hide it once we determine that it is
4118 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004119 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004120 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004121 }
4122 }
Craig Mautner8c14c152015-01-15 17:32:07 -08004123 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004124 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004125 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08004126 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07004127 }
4128 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08004129 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004130 }
4131 }
4132
4133 return stops;
4134 }
4135
Craig Mautnercf910b02013-04-23 11:23:27 -07004136 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004137 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4138 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4139 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4140 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004141 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004142 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004143 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004144 if (r == null) Slog.e(TAG,
4145 "validateTop...: null top activity, stack=" + stack);
4146 else {
4147 final ActivityRecord pausing = stack.mPausingActivity;
4148 if (pausing != null && pausing == r) Slog.e(TAG,
4149 "validateTop...: top stack has pausing activity r=" + r
4150 + " state=" + state);
4151 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
4152 "validateTop...: activity in front not resumed r=" + r
4153 + " state=" + state);
4154 }
Craig Mautnercf910b02013-04-23 11:23:27 -07004155 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004156 final ActivityRecord resumed = stack.mResumedActivity;
4157 if (resumed != null && resumed == r) Slog.e(TAG,
4158 "validateTop...: back stack has resumed activity r=" + r
4159 + " state=" + state);
4160 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
4161 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07004162 }
4163 }
4164 }
Craig Mautner76ea2242013-05-15 11:40:05 -07004165 }
4166
Craig Mautnere0570202015-05-13 13:06:11 -07004167 private String lockTaskModeToString() {
4168 switch (mLockTaskModeState) {
4169 case LOCK_TASK_MODE_LOCKED:
4170 return "LOCKED";
4171 case LOCK_TASK_MODE_PINNED:
4172 return "PINNED";
4173 case LOCK_TASK_MODE_NONE:
4174 return "NONE";
4175 default: return "unknown=" + mLockTaskModeState;
4176 }
4177 }
4178
Craig Mautner27084302013-03-25 08:05:25 -07004179 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07004180 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004181 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07004182 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
4183 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
4184 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08004185 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07004186 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
4187 final SparseArray<String[]> packages = mService.mLockTaskPackages;
4188 if (packages.size() > 0) {
4189 pw.println(" mLockTaskPackages (userId:packages)=");
4190 for (int i = 0; i < packages.size(); ++i) {
4191 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
4192 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
4193 }
4194 }
4195 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07004196 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004197
Craig Mautner20e72272013-04-01 13:45:53 -07004198 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004199 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07004200 }
4201
Dianne Hackborn390517b2013-05-30 15:03:32 -07004202 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
4203 boolean needSep, String prefix) {
4204 if (activity != null) {
4205 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
4206 if (needSep) {
4207 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07004208 }
4209 pw.print(prefix);
4210 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004211 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004212 }
4213 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004214 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004215 }
4216
Craig Mautner8d341ef2013-03-26 09:03:27 -07004217 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4218 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004219 boolean printed = false;
4220 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08004221 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4222 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004223 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07004224 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08004225 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07004226 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004227 final ActivityStack stack = stacks.get(stackNdx);
4228 StringBuilder stackHeader = new StringBuilder(128);
4229 stackHeader.append(" Stack #");
4230 stackHeader.append(stack.mStackId);
4231 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004232 stackHeader.append("\n");
4233 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
4234 stackHeader.append("\n");
4235 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004236 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
4237 needSep, stackHeader.toString());
4238 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
4239 !dumpAll, false, dumpPackage, true,
4240 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004241
Craig Mautner4a1cb222013-12-04 16:14:06 -08004242 needSep = printed;
4243 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4244 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004245 if (pr) {
4246 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004247 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004248 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004249 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
4250 " mResumedActivity: ");
4251 if (pr) {
4252 printed = true;
4253 needSep = false;
4254 }
4255 if (dumpAll) {
4256 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4257 " mLastPausedActivity: ");
4258 if (pr) {
4259 printed = true;
4260 needSep = true;
4261 }
4262 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4263 needSep, " mLastNoHistoryActivity: ");
4264 }
4265 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004266 }
4267 }
4268
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004269 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4270 false, dumpPackage, true, " Activities waiting to finish:", null);
4271 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4272 false, dumpPackage, true, " Activities waiting to stop:", null);
4273 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
4274 false, dumpPackage, true, " Activities waiting for another to become visible:",
4275 null);
4276 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4277 false, dumpPackage, true, " Activities waiting to sleep:", null);
4278 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4279 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004280
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004281 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004282 }
4283
Dianne Hackborn390517b2013-05-30 15:03:32 -07004284 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004285 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004286 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004287 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004288 String innerPrefix = null;
4289 String[] args = null;
4290 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004291 for (int i=list.size()-1; i>=0; i--) {
4292 final ActivityRecord r = list.get(i);
4293 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4294 continue;
4295 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004296 if (innerPrefix == null) {
4297 innerPrefix = prefix + " ";
4298 args = new String[0];
4299 }
4300 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004301 final boolean full = !brief && (complete || !r.isInHistory());
4302 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004303 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004304 needNL = false;
4305 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004306 if (header1 != null) {
4307 pw.println(header1);
4308 header1 = null;
4309 }
4310 if (header2 != null) {
4311 pw.println(header2);
4312 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004313 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004314 if (lastTask != r.task) {
4315 lastTask = r.task;
4316 pw.print(prefix);
4317 pw.print(full ? "* " : " ");
4318 pw.println(lastTask);
4319 if (full) {
4320 lastTask.dump(pw, prefix + " ");
4321 } else if (complete) {
4322 // Complete + brief == give a summary. Isn't that obvious?!?
4323 if (lastTask.intent != null) {
4324 pw.print(prefix); pw.print(" ");
4325 pw.println(lastTask.intent.toInsecureStringWithClip());
4326 }
4327 }
4328 }
4329 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4330 pw.print(" #"); pw.print(i); pw.print(": ");
4331 pw.println(r);
4332 if (full) {
4333 r.dump(pw, innerPrefix);
4334 } else if (complete) {
4335 // Complete + brief == give a summary. Isn't that obvious?!?
4336 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4337 if (r.app != null) {
4338 pw.print(innerPrefix); pw.println(r.app);
4339 }
4340 }
4341 if (client && r.app != null && r.app.thread != null) {
4342 // flush anything that is already in the PrintWriter since the thread is going
4343 // to write to the file descriptor directly
4344 pw.flush();
4345 try {
4346 TransferPipe tp = new TransferPipe();
4347 try {
4348 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4349 r.appToken, innerPrefix, args);
4350 // Short timeout, since blocking here can
4351 // deadlock with the application.
4352 tp.go(fd, 2000);
4353 } finally {
4354 tp.kill();
4355 }
4356 } catch (IOException e) {
4357 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4358 } catch (RemoteException e) {
4359 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4360 }
4361 needNL = true;
4362 }
4363 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004364 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004365 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004366
Craig Mautnerf3333272013-04-22 10:55:53 -07004367 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004368 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4369 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004370 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4371 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004372 }
4373
4374 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004375 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004376 }
4377
4378 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004379 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4380 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004381 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4382 }
4383
Craig Mautner05d29032013-05-03 13:40:13 -07004384 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004385 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4386 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4387 }
Craig Mautner05d29032013-05-03 13:40:13 -07004388 }
4389
Craig Mautner0eea92c2013-05-16 13:35:39 -07004390 void removeSleepTimeouts() {
4391 mSleepTimeout = false;
4392 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4393 }
4394
4395 final void scheduleSleepTimeout() {
4396 removeSleepTimeouts();
4397 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4398 }
4399
Craig Mautner4a1cb222013-12-04 16:14:06 -08004400 @Override
4401 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004402 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004403 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4404 }
4405
4406 @Override
4407 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004408 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004409 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4410 }
4411
4412 @Override
4413 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004414 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004415 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4416 }
4417
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004418 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004419 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004420 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004421 newDisplay = mActivityDisplays.get(displayId) == null;
4422 if (newDisplay) {
4423 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004424 if (activityDisplay.mDisplay == null) {
4425 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4426 return;
4427 }
Craig Mautner4504de52013-12-20 09:06:56 -08004428 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004429 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004430 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004431 }
Craig Mautner4504de52013-12-20 09:06:56 -08004432 if (newDisplay) {
4433 mWindowManager.onDisplayAdded(displayId);
4434 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004435 }
4436
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004437 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4438 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4439 return;
4440 }
4441 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4442 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4443 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4444 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4445 }
4446
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004447 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004448 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004449 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4450 if (activityDisplay != null) {
4451 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004452 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004453 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004454 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004455 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004456 }
4457 }
4458 mWindowManager.onDisplayRemoved(displayId);
4459 }
4460
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004461 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004462 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004463 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4464 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004465 // TODO: Update the bounds.
4466 }
4467 }
4468 mWindowManager.onDisplayChanged(displayId);
4469 }
4470
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004471 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004472 StackInfo info = new StackInfo();
4473 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4474 info.displayId = Display.DEFAULT_DISPLAY;
4475 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004476 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004477
4478 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4479 final int numTasks = tasks.size();
4480 int[] taskIds = new int[numTasks];
4481 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004482 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004483 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004484 for (int i = 0; i < numTasks; ++i) {
4485 final TaskRecord task = tasks.get(i);
4486 taskIds[i] = task.taskId;
4487 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4488 : task.realActivity != null ? task.realActivity.flattenToString()
4489 : task.getTopActivity() != null ? task.getTopActivity().packageName
4490 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004491 taskBounds[i] = new Rect();
4492 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004493 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004494 }
4495 info.taskIds = taskIds;
4496 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004497 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004498 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004499 return info;
4500 }
4501
4502 StackInfo getStackInfoLocked(int stackId) {
4503 ActivityStack stack = getStack(stackId);
4504 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004505 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004506 }
4507 return null;
4508 }
4509
4510 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004511 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004512 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4513 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004514 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004515 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004516 }
4517 }
4518 return list;
4519 }
4520
Craig Mautner15df08a2015-04-01 12:17:18 -07004521 TaskRecord getLockedTaskLocked() {
4522 final int top = mLockTaskModeTasks.size() - 1;
4523 if (top >= 0) {
4524 return mLockTaskModeTasks.get(top);
4525 }
4526 return null;
4527 }
4528
4529 boolean isLockedTask(TaskRecord task) {
4530 return mLockTaskModeTasks.contains(task);
4531 }
4532
4533 boolean isLastLockedTask(TaskRecord task) {
4534 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4535 }
4536
4537 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004538 if (!mLockTaskModeTasks.remove(task)) {
4539 return;
4540 }
4541 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4542 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004543 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004544 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4545 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004546 final Message lockTaskMsg = Message.obtain();
4547 lockTaskMsg.arg1 = task.userId;
4548 lockTaskMsg.what = LOCK_TASK_END_MSG;
4549 mHandler.sendMessage(lockTaskMsg);
4550 }
4551 }
4552
Chong Zhangc806d902015-11-30 09:44:27 -08004553 private void showNonResizeableDockToast(int taskId) {
4554 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08004555 }
4556
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004557 void showLockTaskToast() {
4558 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004559 }
4560
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004561 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4562 if (mLockTaskModeTasks.contains(task)) {
4563 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4564 }
4565 }
4566
Craig Mautner432f64e2015-05-20 14:59:57 -07004567 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4568 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004569 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004570 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004571 final TaskRecord lockedTask = getLockedTaskLocked();
4572 if (lockedTask != null) {
4573 removeLockedTaskLocked(lockedTask);
4574 if (!mLockTaskModeTasks.isEmpty()) {
4575 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004576 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4577 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004578 lockedTask.performClearTaskLocked();
4579 resumeTopActivitiesLocked();
4580 return;
4581 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004582 }
Craig Mautnere0570202015-05-13 13:06:11 -07004583 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4584 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004585 return;
4586 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004587
4588 // Should have already been checked, but do it again.
4589 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004590 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4591 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004592 return;
4593 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004594 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004595 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004596 return;
4597 }
4598
4599 if (mLockTaskModeTasks.isEmpty()) {
4600 // First locktask.
4601 final Message lockTaskMsg = Message.obtain();
4602 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4603 lockTaskMsg.arg1 = task.userId;
4604 lockTaskMsg.what = LOCK_TASK_START_MSG;
4605 lockTaskMsg.arg2 = lockTaskModeState;
4606 mHandler.sendMessage(lockTaskMsg);
4607 }
4608 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004609 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4610 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004611 mLockTaskModeTasks.remove(task);
4612 mLockTaskModeTasks.add(task);
4613
4614 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004615 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004616 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004617
4618 if (andResume) {
4619 findTaskToMoveToFrontLocked(task, 0, null, reason);
4620 resumeTopActivitiesLocked();
4621 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004622 }
4623
4624 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004625 return isLockTaskModeViolation(task, false);
4626 }
4627
4628 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4629 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004630 return false;
4631 }
4632 final int lockTaskAuth = task.mLockTaskAuth;
4633 switch (lockTaskAuth) {
4634 case LOCK_TASK_AUTH_DONT_LOCK:
4635 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004636 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004637 case LOCK_TASK_AUTH_LAUNCHABLE:
4638 case LOCK_TASK_AUTH_WHITELISTED:
4639 return false;
4640 case LOCK_TASK_AUTH_PINNABLE:
4641 // Pinnable tasks can't be launched on top of locktask tasks.
4642 return !mLockTaskModeTasks.isEmpty();
4643 default:
4644 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4645 return true;
4646 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004647 }
4648
Craig Mautner15df08a2015-04-01 12:17:18 -07004649 void onLockTaskPackagesUpdatedLocked() {
4650 boolean didSomething = false;
4651 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4652 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004653 final boolean wasWhitelisted =
4654 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4655 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004656 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004657 final boolean isWhitelisted =
4658 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4659 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4660 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004661 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004662 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4663 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004664 removeLockedTaskLocked(lockedTask);
4665 lockedTask.performClearTaskLocked();
4666 didSomething = true;
4667 }
4668 }
4669 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4670 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4671 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4672 final ActivityStack stack = stacks.get(stackNdx);
4673 stack.onLockTaskPackagesUpdatedLocked();
4674 }
4675 }
Craig Mautnere0570202015-05-13 13:06:11 -07004676 final ActivityRecord r = topRunningActivityLocked();
4677 final TaskRecord task = r != null ? r.task : null;
4678 if (mLockTaskModeTasks.isEmpty() && task != null
4679 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4680 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004681 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4682 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4683 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4684 false);
4685 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004686 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004687 if (didSomething) {
4688 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004689 }
4690 }
4691
Benjamin Franz43261142015-02-11 15:59:44 +00004692 int getLockTaskModeState() {
4693 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004694 }
4695
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004696 void logStackState() {
4697 mActivityMetricsLogger.logWindowState();
4698 }
4699
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004700 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004701
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004702 public ActivityStackSupervisorHandler(Looper looper) {
4703 super(looper);
4704 }
4705
Craig Mautnerf3333272013-04-22 10:55:53 -07004706 void activityIdleInternal(ActivityRecord r) {
4707 synchronized (mService) {
4708 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4709 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004710 }
4711
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004712 @Override
4713 public void handleMessage(Message msg) {
4714 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004715 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004716 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4717 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004718 if (mService.mDidDexOpt) {
4719 mService.mDidDexOpt = false;
4720 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4721 nmsg.obj = msg.obj;
4722 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4723 return;
4724 }
4725 // We don't at this point know if the activity is fullscreen,
4726 // so we need to be conservative and assume it isn't.
4727 activityIdleInternal((ActivityRecord)msg.obj);
4728 } break;
4729 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004730 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004731 activityIdleInternal((ActivityRecord)msg.obj);
4732 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004733 case RESUME_TOP_ACTIVITY_MSG: {
4734 synchronized (mService) {
4735 resumeTopActivitiesLocked();
4736 }
4737 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004738 case SLEEP_TIMEOUT_MSG: {
4739 synchronized (mService) {
4740 if (mService.isSleepingOrShuttingDown()) {
4741 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4742 mSleepTimeout = true;
4743 checkReadyForSleepLocked();
4744 }
4745 }
4746 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004747 case LAUNCH_TIMEOUT_MSG: {
4748 if (mService.mDidDexOpt) {
4749 mService.mDidDexOpt = false;
4750 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4751 return;
4752 }
4753 synchronized (mService) {
4754 if (mLaunchingActivity.isHeld()) {
4755 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4756 if (VALIDATE_WAKE_LOCK_CALLER
4757 && Binder.getCallingUid() != Process.myUid()) {
4758 throw new IllegalStateException("Calling must be system uid");
4759 }
4760 mLaunchingActivity.release();
4761 }
4762 }
4763 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004764 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004765 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004766 } break;
4767 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004768 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004769 } break;
4770 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004771 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004772 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004773 case CONTAINER_CALLBACK_VISIBILITY: {
4774 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004775 final IActivityContainerCallback callback = container.mCallback;
4776 if (callback != null) {
4777 try {
4778 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4779 } catch (RemoteException e) {
4780 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004781 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004782 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004783 case LOCK_TASK_START_MSG: {
4784 // When lock task starts, we disable the status bars.
4785 try {
Jason Monk62515be2014-05-21 16:06:19 -04004786 if (mLockTaskNotify == null) {
4787 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004788 }
Jason Monk62515be2014-05-21 16:06:19 -04004789 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004790 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004791 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004792 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004793 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004794 flags = StatusBarManager.DISABLE_MASK
4795 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004796 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004797 flags = StatusBarManager.DISABLE_MASK
4798 & (~StatusBarManager.DISABLE_BACK)
4799 & (~StatusBarManager.DISABLE_HOME)
4800 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004801 }
4802 getStatusBarService().disable(flags, mToken,
4803 mService.mContext.getPackageName());
4804 }
4805 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004806 if (getDevicePolicyManager() != null) {
4807 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004808 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004809 }
justinzhang5286d3f2014-05-12 17:06:01 -04004810 } catch (RemoteException ex) {
4811 throw new RuntimeException(ex);
4812 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004813 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004814 case LOCK_TASK_END_MSG: {
4815 // When lock task ends, we enable the status bars.
4816 try {
Jason Monk62515be2014-05-21 16:06:19 -04004817 if (getStatusBarService() != null) {
4818 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4819 mService.mContext.getPackageName());
4820 }
4821 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004822 if (getDevicePolicyManager() != null) {
4823 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4824 msg.arg1);
4825 }
Jason Monk62515be2014-05-21 16:06:19 -04004826 if (mLockTaskNotify == null) {
4827 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4828 }
4829 mLockTaskNotify.show(false);
4830 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004831 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004832 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004833 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004834 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004835 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004836 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004837 new LockPatternUtils(mService.mContext)
4838 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004839 }
4840 } catch (SettingNotFoundException e) {
4841 // No setting, don't lock.
4842 }
justinzhang5286d3f2014-05-12 17:06:01 -04004843 } catch (RemoteException ex) {
4844 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004845 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004846 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004847 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004848 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004849 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4850 if (mLockTaskNotify == null) {
4851 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4852 }
4853 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4854 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004855 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4856 final ActivityContainer container = (ActivityContainer) msg.obj;
4857 final IActivityContainerCallback callback = container.mCallback;
4858 if (callback != null) {
4859 try {
4860 callback.onAllActivitiesComplete(container.asBinder());
4861 } catch (RemoteException e) {
4862 }
4863 }
4864 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004865 case LAUNCH_TASK_BEHIND_COMPLETE: {
4866 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004867 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004868 if (r != null) {
4869 handleLaunchTaskBehindCompleteLocked(r);
4870 }
4871 }
4872 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004873
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004874 }
4875 }
4876 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004877
Ying Wangb081a592014-04-22 15:20:16 -07004878 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004879 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4880 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004881 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004882 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004883 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004884 ActivityRecord mParentActivity = null;
4885 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004886
Craig Mautnere3a00d72014-04-16 08:31:19 -07004887 boolean mVisible = true;
4888
Craig Mautner4a1cb222013-12-04 16:14:06 -08004889 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004890 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004891
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004892 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4893 final static int CONTAINER_STATE_NO_SURFACE = 1;
4894 final static int CONTAINER_STATE_FINISHING = 2;
4895 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4896
4897 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004898 synchronized (mService) {
4899 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004900 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004901 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004902 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004903 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004904 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004905
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004906 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004907 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004908 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004909 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004910 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004911 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004912 }
4913
4914 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004915 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004916 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004917 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4918 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004919 return;
4920 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004921 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004922 }
4923 }
4924
4925 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004926 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004927 synchronized (mService) {
4928 if (mActivityDisplay != null) {
4929 return mActivityDisplay.mDisplayId;
4930 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004931 }
4932 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004933 }
4934
Jeff Brownca9bc702014-02-11 14:32:56 -08004935 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004936 public int getStackId() {
4937 synchronized (mService) {
4938 return mStackId;
4939 }
4940 }
4941
4942 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004943 public boolean injectEvent(InputEvent event) {
4944 final long origId = Binder.clearCallingIdentity();
4945 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004946 synchronized (mService) {
4947 if (mActivityDisplay != null) {
4948 return mInputManagerInternal.injectInputEvent(event,
4949 mActivityDisplay.mDisplayId,
4950 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4951 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004952 }
4953 return false;
4954 } finally {
4955 Binder.restoreCallingIdentity(origId);
4956 }
4957 }
4958
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004959 @Override
4960 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004961 synchronized (mService) {
4962 if (mContainerState == CONTAINER_STATE_FINISHING) {
4963 return;
4964 }
4965 mContainerState = CONTAINER_STATE_FINISHING;
4966
Craig Mautnerd163e752014-06-13 17:18:47 -07004967 long origId = Binder.clearCallingIdentity();
4968 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004969 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004970 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004971 } finally {
4972 Binder.restoreCallingIdentity(origId);
4973 }
4974 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004975 }
4976
Craig Mautner60257702014-09-17 15:02:33 -07004977 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004978 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004979 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004980 if (mActivityDisplay != null) {
4981 mActivityDisplay.detachActivitiesLocked(mStack);
4982 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004983 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004984 }
4985 }
4986
4987 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004988 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004989 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004990 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004991 Binder.getCallingUid(), mCurrentUser, false,
4992 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004993
Craig Mautnere0a38842013-12-16 16:14:02 -08004994 // TODO: Switch to user app stacks here.
4995 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004996 final Uri data = intent.getData();
4997 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4998 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004999 }
Craig Mautnerb9168362015-02-26 20:40:19 -08005000 checkEmbeddedAllowedInner(userId, intent, mimeType);
5001
5002 intent.addFlags(FORCE_NEW_TASK_FLAGS);
5003 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07005004 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08005005 }
5006
5007 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07005008 public final int startActivityIntentSender(IIntentSender intentSender)
5009 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08005010 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
5011
5012 if (!(intentSender instanceof PendingIntentRecord)) {
5013 throw new IllegalArgumentException("Bad PendingIntent object");
5014 }
5015
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07005016 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07005017 Binder.getCallingUid(), mCurrentUser, false,
5018 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08005019
5020 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
5021 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
5022 pendingIntent.key.requestResolvedType);
5023
5024 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
5025 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
5026 }
5027
5028 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07005029 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07005030 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07005031 throw new SecurityException(
5032 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
5033 }
5034 }
5035
Craig Mautnerdf88d732014-01-27 09:21:32 -08005036 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08005037 public IBinder asBinder() {
5038 return this;
5039 }
5040
Craig Mautner4504de52013-12-20 09:06:56 -08005041 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005042 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08005043 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08005044 }
5045
Craig Mautner4a1cb222013-12-04 16:14:06 -08005046 ActivityStackSupervisor getOuter() {
5047 return ActivityStackSupervisor.this;
5048 }
5049
Craig Mautnerd163e752014-06-13 17:18:47 -07005050 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08005051 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08005052 }
5053
Craig Mautner6985bad2014-04-21 15:22:06 -07005054 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07005055 void setVisible(boolean visible) {
5056 if (mVisible != visible) {
5057 mVisible = visible;
5058 if (mCallback != null) {
5059 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
5060 0 /* unused */, this).sendToTarget();
5061 }
5062 }
5063 }
5064
Craig Mautner6985bad2014-04-21 15:22:06 -07005065 void setDrawn() {
5066 }
5067
Craig Mautner1b4bf852014-05-26 15:06:32 -07005068 // You can always start a new task on a regular ActivityStack.
5069 boolean isEligibleForNewTasks() {
5070 return true;
5071 }
5072
Craig Mautnerd163e752014-06-13 17:18:47 -07005073 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005074 detachLocked();
5075 deleteActivityContainer(this);
5076 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07005077 }
5078
Craig Mautner34b73df2014-01-12 21:11:08 -08005079 @Override
5080 public String toString() {
5081 return mIdString + (mActivityDisplay == null ? "N" : "A");
5082 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005083 }
5084
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005085 private class VirtualActivityContainer extends ActivityContainer {
5086 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07005087 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005088
5089 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
5090 super(getNextStackId());
5091 mParentActivity = parent;
5092 mCallback = callback;
5093 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07005094 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005095 }
5096
5097 @Override
5098 public void setSurface(Surface surface, int width, int height, int density) {
5099 super.setSurface(surface, width, height, density);
5100
5101 synchronized (mService) {
5102 final long origId = Binder.clearCallingIdentity();
5103 try {
5104 setSurfaceLocked(surface, width, height, density);
5105 } finally {
5106 Binder.restoreCallingIdentity(origId);
5107 }
5108 }
5109 }
5110
5111 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
5112 if (mContainerState == CONTAINER_STATE_FINISHING) {
5113 return;
5114 }
5115 VirtualActivityDisplay virtualActivityDisplay =
5116 (VirtualActivityDisplay) mActivityDisplay;
5117 if (virtualActivityDisplay == null) {
5118 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07005119 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005120 mActivityDisplay = virtualActivityDisplay;
5121 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005122 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005123 }
5124
5125 if (mSurface != null) {
5126 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005127 }
5128
Craig Mautner6985bad2014-04-21 15:22:06 -07005129 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005130 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005131 mStack.resumeTopActivityLocked(null);
5132 } else {
5133 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07005134 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07005135 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07005136 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07005137 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005138 }
Craig Mautner6985bad2014-04-21 15:22:06 -07005139
Craig Mautnerd163e752014-06-13 17:18:47 -07005140 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07005141
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005142 if (DEBUG_STACK) Slog.d(TAG_STACK,
5143 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07005144 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005145
Craig Mautner6985bad2014-04-21 15:22:06 -07005146 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07005147 boolean isAttachedLocked() {
5148 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07005149 }
5150
5151 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07005152 void setDrawn() {
5153 synchronized (mService) {
5154 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07005155 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07005156 }
5157 }
5158
Craig Mautner1b4bf852014-05-26 15:06:32 -07005159 // Never start a new task on an ActivityView if it isn't explicitly specified.
5160 @Override
5161 boolean isEligibleForNewTasks() {
5162 return false;
5163 }
5164
Craig Mautnerd163e752014-06-13 17:18:47 -07005165 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005166 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07005167 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
5168 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
5169 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
5170 mContainerState = CONTAINER_STATE_HAS_SURFACE;
5171 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005172 }
5173 }
5174
Craig Mautner4a1cb222013-12-04 16:14:06 -08005175 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
5176 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005177 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08005178 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08005179 int mDisplayId;
5180 Display mDisplay;
5181 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08005182
Craig Mautner4a1cb222013-12-04 16:14:06 -08005183 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
5184 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08005185 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08005186
Jose Lima4b6c6692014-08-12 17:41:12 -07005187 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005188
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005189 ActivityDisplay() {
5190 }
Craig Mautner4504de52013-12-20 09:06:56 -08005191
Craig Mautner1a70a162014-09-13 12:09:31 -07005192 // After instantiation, check that mDisplay is not null before using this. The alternative
5193 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08005194 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07005195 final Display display = mDisplayManager.getDisplay(displayId);
5196 if (display == null) {
5197 return;
5198 }
5199 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08005200 }
5201
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005202 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08005203 mDisplay = display;
5204 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08005205 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08005206 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08005207
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005208 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005209 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005210 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
5211 + " onTop=" + onTop);
5212 if (onTop) {
5213 mStacks.add(stack);
5214 } else {
5215 mStacks.add(0, stack);
5216 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08005217 }
5218
Craig Mautnere0a38842013-12-16 16:14:02 -08005219 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005220 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08005221 + " from displayId=" + mDisplayId);
5222 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08005223 }
5224
Jose Lima4b6c6692014-08-12 17:41:12 -07005225 void setVisibleBehindActivity(ActivityRecord r) {
5226 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005227 }
5228
Jose Lima4b6c6692014-08-12 17:41:12 -07005229 boolean hasVisibleBehindActivity() {
5230 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005231 }
5232
Craig Mautner34b73df2014-01-12 21:11:08 -08005233 @Override
5234 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005235 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
5236 }
5237 }
5238
5239 class VirtualActivityDisplay extends ActivityDisplay {
5240 VirtualDisplay mVirtualDisplay;
5241
Craig Mautner6985bad2014-04-21 15:22:06 -07005242 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005243 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07005244 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
5245 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
5246 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
5247 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005248
5249 init(mVirtualDisplay.getDisplay());
5250
5251 mWindowManager.handleDisplayAdded(mDisplayId);
5252 }
5253
5254 void setSurface(Surface surface) {
5255 if (mVirtualDisplay != null) {
5256 mVirtualDisplay.setSurface(surface);
5257 }
5258 }
5259
5260 @Override
5261 void detachActivitiesLocked(ActivityStack stack) {
5262 super.detachActivitiesLocked(stack);
5263 if (mVirtualDisplay != null) {
5264 mVirtualDisplay.release();
5265 mVirtualDisplay = null;
5266 }
5267 }
5268
5269 @Override
5270 public String toString() {
5271 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005272 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005273 }
Jose Lima58e66d62014-05-27 20:00:27 -07005274
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07005275 /**
5276 * Adjust bounds to stay within stack bounds.
5277 *
5278 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
5279 * that keep them unchanged, but be contained within the stack bounds.
5280 *
5281 * @param bounds Bounds to be adjusted.
5282 * @param stackBounds Bounds within which the other bounds should remain.
5283 */
5284 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
5285 if (stackBounds == null || stackBounds.contains(bounds)) {
5286 return;
5287 }
5288
5289 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
5290 final int maxRight = stackBounds.right
5291 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
5292 int horizontalDiff = stackBounds.left - bounds.left;
5293 if ((horizontalDiff < 0 && bounds.left >= maxRight)
5294 || (bounds.left + horizontalDiff >= maxRight)) {
5295 horizontalDiff = maxRight - bounds.left;
5296 }
5297 bounds.left += horizontalDiff;
5298 bounds.right += horizontalDiff;
5299 }
5300
5301 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
5302 final int maxBottom = stackBounds.bottom
5303 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
5304 int verticalDiff = stackBounds.top - bounds.top;
5305 if ((verticalDiff < 0 && bounds.top >= maxBottom)
5306 || (bounds.top + verticalDiff >= maxBottom)) {
5307 verticalDiff = maxBottom - bounds.top;
5308 }
5309 bounds.top += verticalDiff;
5310 bounds.bottom += verticalDiff;
5311 }
5312 }
5313
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005314 ActivityStack findStackBehind(ActivityStack stack) {
5315 // TODO(multi-display): We are only looking for stacks on the default display.
5316 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
5317 if (display == null) {
5318 return null;
5319 }
5320 final ArrayList<ActivityStack> stacks = display.mStacks;
5321 for (int i = stacks.size() - 1; i >= 0; i--) {
5322 if (stacks.get(i) == stack && i > 0) {
5323 return stacks.get(i - 1);
5324 }
5325 }
5326 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
5327 + " in=" + stacks);
5328 }
Craig Mautner27084302013-03-25 08:05:25 -07005329}