blob: 271a9437b554622d1a8909bb102aa5e8f64143b4 [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
Craig Mautneree36c772014-07-16 14:56:05 -0700421 /**
422 * Description of a request to start a new activity, which has been held
423 * due to app switches being disabled.
424 */
425 static class PendingActivityLaunch {
426 final ActivityRecord r;
427 final ActivityRecord sourceRecord;
428 final int startFlags;
429 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700430 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700431
432 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700433 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700434 r = _r;
435 sourceRecord = _sourceRecord;
436 startFlags = _startFlags;
437 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700438 callerApp = _callerApp;
439 }
440
441 void sendErrorResult(String message) {
442 try {
443 if (callerApp.thread != null) {
444 callerApp.thread.scheduleCrash(message);
445 }
446 } catch (RemoteException e) {
447 Slog.e(TAG, "Exception scheduling crash of failed "
448 + "activity launcher sourceRecord=" + sourceRecord, e);
449 }
Craig Mautneree36c772014-07-16 14:56:05 -0700450 }
451 }
452
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800453 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700454 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800455 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700456 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800457 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jeff Brown2c43c332014-06-12 22:38:59 -0700458 }
459
460 /**
461 * At the time when the constructor runs, the power manager has not yet been
462 * initialized. So we initialize our wakelocks afterwards.
463 */
464 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800465 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700466 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700467 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700468 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700469 }
470
justinzhang5286d3f2014-05-12 17:06:01 -0400471 // This function returns a IStatusBarService. The value is from ServiceManager.
472 // getService and is cached.
473 private IStatusBarService getStatusBarService() {
474 synchronized (mService) {
475 if (mStatusBarService == null) {
476 mStatusBarService = IStatusBarService.Stub.asInterface(
477 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
478 if (mStatusBarService == null) {
479 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
480 }
481 }
482 return mStatusBarService;
483 }
484 }
485
Jason Monk35c62a42014-06-17 10:24:47 -0400486 private IDevicePolicyManager getDevicePolicyManager() {
487 synchronized (mService) {
488 if (mDevicePolicyManager == null) {
489 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
490 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
491 if (mDevicePolicyManager == null) {
492 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
493 }
494 }
495 return mDevicePolicyManager;
496 }
497 }
498
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700499 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800500 synchronized (mService) {
501 mWindowManager = wm;
502
503 mDisplayManager =
504 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
505 mDisplayManager.registerDisplayListener(this, null);
506
507 Display[] displays = mDisplayManager.getDisplays();
508 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
509 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800510 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700511 if (activityDisplay.mDisplay == null) {
512 throw new IllegalStateException("Default Display does not exist");
513 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800514 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700515 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800516 }
517
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700518 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800519 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800520
521 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 }
Craig Mautner27084302013-03-25 08:05:25 -0700523 }
524
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200525 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700526 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200527 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700528 }
529
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700530 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800531 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700532 }
533
Craig Mautnerde4ef022013-04-07 19:01:33 -0700534 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800535 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700536 }
537
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700538 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700539 if (stack == null) {
540 return false;
541 }
542
Craig Mautnerdf88d732014-01-27 09:21:32 -0800543 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
544 if (parent != null) {
545 stack = parent.task.stack;
546 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800547 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700548 }
549
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700550 /** The top most stack. */
551 boolean isFrontStack(ActivityStack stack) {
552 if (stack == null) {
553 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800554 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700555
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700556 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
557 if (parent != null) {
558 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800559 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700560 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
561 }
562
563 void setFocusStack(String reason, ActivityStack focusedStack) {
564 mLastFocusedStack = mFocusedStack;
565 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800566
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700567 EventLogTags.writeAmFocusedStack(
568 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
569 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800570
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800571 if (mService.mBooting || !mService.mBooted) {
572 final ActivityRecord r = topRunningActivityLocked();
573 if (r != null && r.idle) {
574 checkFinishBootingLocked();
575 }
576 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700577 }
578
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700579 void moveHomeStackToFront(String reason) {
580 mHomeStack.moveToFront(reason);
581 }
582
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700583 /** Returns true if the focus activity was adjusted to the home stack top activity. */
584 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700585 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
586 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700587 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700588 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700589
Craig Mautner84984fa2014-06-19 11:19:20 -0700590 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700591
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700592 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700593 if (top == null) {
594 return false;
595 }
596 mService.setFocusedActivityLocked(top, reason);
597 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700598 }
599
Craig Mautner299f9602015-01-26 09:47:33 -0800600 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700601 if (!mService.mBooting && !mService.mBooted) {
602 // Not ready yet!
603 return false;
604 }
605
Craig Mautner84984fa2014-06-19 11:19:20 -0700606 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
607 mWindowManager.showRecentApps();
608 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700609 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700610
Craig Mautner84984fa2014-06-19 11:19:20 -0700611 if (prev != null) {
612 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
613 }
614
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700615 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
616 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800617 // Only resume home activity if isn't finishing.
618 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800619 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700620 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700621 }
Craig Mautner299f9602015-01-26 09:47:33 -0800622 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700623 }
624
Craig Mautner8d341ef2013-03-26 09:03:27 -0700625 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700626 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700627 }
628
629 /**
630 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
631 * @param id Id of the task we would like returned.
632 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
633 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700634 * @param stackId The stack to restore the task to (default launch stack will be used if
635 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700636 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700637 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800638 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800639 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800640 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
642 ActivityStack stack = stacks.get(stackNdx);
643 TaskRecord task = stack.taskForIdLocked(id);
644 if (task != null) {
645 return task;
646 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700647 }
648 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800649
650 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700651 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800652 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800653 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700654 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800655 return null;
656 }
657
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700658 if (!restoreFromRecents) {
659 return task;
660 }
661
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700662 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700663 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
664 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800665 return null;
666 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700667 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800668 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700669 }
670
Craig Mautner6170f732013-04-02 13:05:23 -0700671 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800672 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800673 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800674 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
676 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
677 if (r != null) {
678 return r;
679 }
Craig Mautner6170f732013-04-02 13:05:23 -0700680 }
681 }
682 return null;
683 }
684
Craig Mautneref73ee12014-04-23 11:45:37 -0700685 void setNextTaskId(int taskId) {
686 if (taskId > mCurTaskId) {
687 mCurTaskId = taskId;
688 }
689 }
690
Craig Mautner8d341ef2013-03-26 09:03:27 -0700691 int getNextTaskId() {
692 do {
693 mCurTaskId++;
694 if (mCurTaskId <= 0) {
695 mCurTaskId = 1;
696 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700697 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700698 return mCurTaskId;
699 }
700
Craig Mautnerde4ef022013-04-07 19:01:33 -0700701 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800702 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700703 if (stack == null) {
704 return null;
705 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700706 ActivityRecord resumedActivity = stack.mResumedActivity;
707 if (resumedActivity == null || resumedActivity.app == null) {
708 resumedActivity = stack.mPausingActivity;
709 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700710 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700711 }
712 }
713 return resumedActivity;
714 }
715
Dianne Hackbornff072722014-09-24 10:56:28 -0700716 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700717 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800718 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800719 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
720 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800721 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
722 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700723 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 continue;
725 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700726 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 if (hr != null) {
728 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
729 && processName.equals(hr.processName)) {
730 try {
George Mount2c92c972014-03-20 09:38:23 -0700731 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800732 didSomething = true;
733 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700734 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800735 Slog.w(TAG, "Exception in new application when starting activity "
736 + hr.intent.getComponent().flattenToShortString(), e);
737 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700738 }
Craig Mautner20e72272013-04-01 13:45:53 -0700739 }
Craig Mautner20e72272013-04-01 13:45:53 -0700740 }
741 }
742 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700743 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700744 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700745 }
Craig Mautner20e72272013-04-01 13:45:53 -0700746 return didSomething;
747 }
748
749 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800750 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
751 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800752 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
753 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700754 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800755 continue;
756 }
757 final ActivityRecord resumedActivity = stack.mResumedActivity;
758 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700759 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800760 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800761 return false;
762 }
Craig Mautner20e72272013-04-01 13:45:53 -0700763 }
764 }
765 return true;
766 }
767
Craig Mautnerde4ef022013-04-07 19:01:33 -0700768 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800769 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
770 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
772 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700773 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700775 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 return false;
777 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700778 }
779 }
780 }
781 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700782 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800783 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
784 mLastFocusedStack + " to=" + mFocusedStack);
785 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700786 return true;
787 }
788
789 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800790 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800791 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
792 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800793 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
794 final ActivityStack stack = stacks.get(stackNdx);
795 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800796 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700797 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800798 return false;
799 }
800 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800801 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700802 }
803 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800804 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700805 }
806
Craig Mautner2acc3892013-09-23 10:28:14 -0700807 /**
808 * Pause all activities in either all of the stacks or just the back stacks.
809 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700810 * @return true if any activity was paused as a result of this call.
811 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700812 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700813 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800814 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
815 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800816 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
817 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700818 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700819 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800820 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700821 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
822 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800823 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700824 }
825 }
826 return someActivityPaused;
827 }
828
Craig Mautnerde4ef022013-04-07 19:01:33 -0700829 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700830 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800831 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
832 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
834 final ActivityStack stack = stacks.get(stackNdx);
835 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700836 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700838 Slog.d(TAG_STATES,
839 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 pausing = false;
841 } else {
842 return false;
843 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700844 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700845 }
846 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700847 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700848 }
849
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700850 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
851 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500852 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800853 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
854 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
855 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
856 final ActivityStack stack = stacks.get(stackNdx);
857 if (stack.mResumedActivity != null &&
858 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700859 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800860 }
861 }
862 }
863 }
864
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700865 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700866 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700867 }
868
869 void sendWaitingVisibleReportLocked(ActivityRecord r) {
870 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700871 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700872 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700873 if (w.who == null) {
874 changed = true;
875 w.timeout = false;
876 if (r != null) {
877 w.who = new ComponentName(r.info.packageName, r.info.name);
878 }
879 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
880 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700881 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700882 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700883 if (changed) {
884 mService.notifyAll();
885 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700886 }
887
888 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
889 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700890 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700891 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700892 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700893 if (w.who == null) {
894 changed = true;
895 w.timeout = timeout;
896 if (r != null) {
897 w.who = new ComponentName(r.info.packageName, r.info.name);
898 }
899 w.thisTime = thisTime;
900 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700901 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700902 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700903 if (changed) {
904 mService.notifyAll();
905 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700906 }
907
Craig Mautner29219d92013-04-16 20:19:12 -0700908 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800909 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700910 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700911 if (r != null) {
912 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700913 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700914
Craig Mautner4a1cb222013-12-04 16:14:06 -0800915 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800916 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800917 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
918 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700919 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700920 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700921 if (r != null) {
922 return r;
923 }
924 }
925 }
926 return null;
927 }
928
Dianne Hackborn09233282014-04-30 11:33:59 -0700929 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700930 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
932 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800933 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800934 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800935 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800936 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
937 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700938 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800939 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700940 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700941 }
942 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700943
944 // The lists are already sorted from most recent to oldest. Just pull the most recent off
945 // each list and add it to list. Stop when all lists are empty or maxNum reached.
946 while (maxNum > 0) {
947 long mostRecentActiveTime = Long.MIN_VALUE;
948 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800949 final int numTaskLists = runningTaskLists.size();
950 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
951 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700952 if (!stackTaskList.isEmpty()) {
953 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
954 if (lastActiveTime > mostRecentActiveTime) {
955 mostRecentActiveTime = lastActiveTime;
956 selectedStackList = stackTaskList;
957 }
958 }
959 }
960 if (selectedStackList != null) {
961 list.add(selectedStackList.remove(0));
962 --maxNum;
963 } else {
964 break;
965 }
966 }
Craig Mautner20e72272013-04-01 13:45:53 -0700967 }
968
Todd Kennedy7440f172015-12-09 14:31:22 -0800969 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
970 ProfilerInfo profilerInfo) {
971 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700972 if (aInfo != null) {
973 // Store the found target back into the intent, because now that
974 // we have it we never want to do this again. For example, if the
975 // user navigates back to this point in the history, we should
976 // always restart the exact same activity.
977 intent.setComponent(new ComponentName(
978 aInfo.applicationInfo.packageName, aInfo.name));
979
980 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700981 if (!aInfo.processName.equals("system")) {
982 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700983 mService.setDebugApp(aInfo.processName, true, false);
984 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985
Man Caocfa78b22015-06-11 20:14:34 -0700986 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
987 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
988 }
989
990 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700991 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700992 }
993 }
994 }
995 return aInfo;
996 }
997
Todd Kennedy7440f172015-12-09 14:31:22 -0800998 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
999 try {
1000 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
1001 PackageManager.MATCH_DEFAULT_ONLY
1002 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1003 } catch (RemoteException e) {
1004 }
1005 return null;
1006 }
1007
1008 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1009 ProfilerInfo profilerInfo, int userId) {
1010 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1011 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1012 }
1013
Craig Mautner299f9602015-01-26 09:47:33 -08001014 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
1015 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Todd Kennedy7440f172015-12-09 14:31:22 -08001016 startActivityLocked(null /*caller*/, intent, null /*ephemeralIntent*/,
1017 null /*resolvedType*/, aInfo, null /*rInfo*/, null /*voiceSession*/,
1018 null /*voiceInteractor*/, null /*resultTo*/, null /*resultWho*/,
1019 0 /*requestCode*/, 0 /*callingPid*/, 0 /*callingUid*/, null /*callingPackage*/,
1020 0 /*realCallingPid*/, 0 /*realCallingUid*/, 0 /*startFlags*/, null /*options*/,
1021 false /*ignoreTargetSecurity*/, false /*componentSpecified*/, null /*outActivity*/,
1022 null /*container*/, null /*inTask*/);
Filip Gruszczynskidd913622015-06-19 15:14:53 -07001023 if (inResumeTopActivity) {
1024 // If we are in resume section already, home activity will be initialized, but not
1025 // resumed (to avoid recursive resume) and will stay that way until something pokes it
1026 // again. We need to schedule another resume.
1027 scheduleResumeTopActivities();
1028 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001029 }
1030
Craig Mautner23ac33b2013-04-01 16:26:35 -07001031 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001032 String callingPackage, Intent intent, String resolvedType,
1033 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -07001034 IBinder resultTo, String resultWho, int requestCode, int startFlags,
1035 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Chong Zhang280d3322015-11-03 17:27:26 -08001036 Bundle bOptions, boolean ignoreTargetSecurity, int userId,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001037 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001038 // Refuse possible leaked file descriptors
1039 if (intent != null && intent.hasFileDescriptors()) {
1040 throw new IllegalArgumentException("File descriptors passed in Intent");
1041 }
1042 boolean componentSpecified = intent.getComponent() != null;
1043
Todd Kennedy7440f172015-12-09 14:31:22 -08001044 // Save a copy in case ephemeral needs it
1045 final Intent ephemeralIntent = new Intent(intent);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001046 // Don't modify the client's object!
1047 intent = new Intent(intent);
1048
Todd Kennedy7440f172015-12-09 14:31:22 -08001049 ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001050 // Collect information about the target of the Intent.
Todd Kennedy7440f172015-12-09 14:31:22 -08001051 ActivityInfo aInfo = resolveActivity(intent, rInfo, startFlags, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001052
Chong Zhang280d3322015-11-03 17:27:26 -08001053 ActivityOptions options = ActivityOptions.fromBundle(bOptions);
Craig Mautnere0a38842013-12-16 16:14:02 -08001054 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001055 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001056 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001057 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001058 // Cannot start a child activity if the parent is not resumed.
1059 return ActivityManager.START_CANCELED;
1060 }
Dianne Hackborn95465202014-09-15 16:21:55 -07001061 final int realCallingPid = Binder.getCallingPid();
1062 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -07001063 int callingPid;
1064 if (callingUid >= 0) {
1065 callingPid = -1;
1066 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001067 callingPid = realCallingPid;
1068 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001069 } else {
1070 callingPid = callingUid = -1;
1071 }
1072
Craig Mautnere0a38842013-12-16 16:14:02 -08001073 final ActivityStack stack;
1074 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001075 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001076 } else {
1077 stack = container.mStack;
1078 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001079 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001080 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001081 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001082
1083 final long origId = Binder.clearCallingIdentity();
1084
1085 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001086 (aInfo.applicationInfo.privateFlags
1087 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001088 // This may be a heavy-weight process! Check to see if we already
1089 // have another, different heavy-weight process running.
1090 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1091 if (mService.mHeavyWeightProcess != null &&
1092 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1093 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001094 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095 if (caller != null) {
1096 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1097 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001098 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001099 } else {
1100 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001101 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001102 + intent.toString());
1103 ActivityOptions.abort(options);
1104 return ActivityManager.START_PERMISSION_DENIED;
1105 }
1106 }
1107
1108 IIntentSender target = mService.getIntentSenderLocked(
1109 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001110 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001111 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1112 | PendingIntent.FLAG_ONE_SHOT, null);
1113
1114 Intent newIntent = new Intent();
1115 if (requestCode >= 0) {
1116 // Caller is requesting a result.
1117 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1118 }
1119 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1120 new IntentSender(target));
1121 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1122 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1123 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1124 hist.packageName);
1125 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1126 hist.task.taskId);
1127 }
1128 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1129 aInfo.packageName);
1130 newIntent.setFlags(intent.getFlags());
1131 newIntent.setClassName("android",
1132 HeavyWeightSwitcherActivity.class.getName());
1133 intent = newIntent;
1134 resolvedType = null;
1135 caller = null;
1136 callingUid = Binder.getCallingUid();
1137 callingPid = Binder.getCallingPid();
1138 componentSpecified = true;
Todd Kennedy7440f172015-12-09 14:31:22 -08001139 rInfo = resolveIntent(intent, null /*resolvedType*/, userId);
1140 aInfo = rInfo != null ? rInfo.activityInfo : null;
1141 if (aInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001142 aInfo = mService.getActivityInfoForUser(aInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001143 }
1144 }
1145 }
1146 }
1147
Todd Kennedy7440f172015-12-09 14:31:22 -08001148 int res = startActivityLocked(caller, intent, ephemeralIntent, resolvedType,
1149 aInfo, rInfo, voiceSession, voiceInteractor,
1150 resultTo, resultWho, requestCode, callingPid,
1151 callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
1152 options, ignoreTargetSecurity, componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001153
Craig Mautner85c11a82014-07-17 12:38:18 -07001154 Binder.restoreCallingIdentity(origId);
1155
Craig Mautnerde4ef022013-04-07 19:01:33 -07001156 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001157 // If the caller also wants to switch to a new configuration,
1158 // do so now. This allows a clean switch, as we are waiting
1159 // for the current activity to pause (so we will not destroy
1160 // it), and have not yet started the next activity.
1161 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1162 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001163 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001164 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001165 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001166 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001167 }
1168
Craig Mautner23ac33b2013-04-01 16:26:35 -07001169 if (outResult != null) {
1170 outResult.result = res;
1171 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001172 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001173 do {
1174 try {
1175 mService.wait();
1176 } catch (InterruptedException e) {
1177 }
1178 } while (!outResult.timeout && outResult.who == null);
1179 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001180 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001181 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001182 outResult.timeout = false;
1183 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1184 outResult.totalTime = 0;
1185 outResult.thisTime = 0;
1186 } else {
1187 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001188 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001189 do {
1190 try {
1191 mService.wait();
1192 } catch (InterruptedException e) {
1193 }
1194 } while (!outResult.timeout && outResult.who == null);
1195 }
1196 }
1197 }
1198
1199 return res;
1200 }
1201 }
1202
1203 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1204 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
Chong Zhang280d3322015-11-03 17:27:26 -08001205 Bundle bOptions, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001206 if (intents == null) {
1207 throw new NullPointerException("intents is null");
1208 }
1209 if (resolvedTypes == null) {
1210 throw new NullPointerException("resolvedTypes is null");
1211 }
1212 if (intents.length != resolvedTypes.length) {
1213 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1214 }
1215
Craig Mautner23ac33b2013-04-01 16:26:35 -07001216
1217 int callingPid;
1218 if (callingUid >= 0) {
1219 callingPid = -1;
1220 } else if (caller == null) {
1221 callingPid = Binder.getCallingPid();
1222 callingUid = Binder.getCallingUid();
1223 } else {
1224 callingPid = callingUid = -1;
1225 }
1226 final long origId = Binder.clearCallingIdentity();
1227 try {
1228 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001229 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001230 for (int i=0; i<intents.length; i++) {
1231 Intent intent = intents[i];
1232 if (intent == null) {
1233 continue;
1234 }
1235
1236 // Refuse possible leaked file descriptors
1237 if (intent != null && intent.hasFileDescriptors()) {
1238 throw new IllegalArgumentException("File descriptors passed in Intent");
1239 }
1240
1241 boolean componentSpecified = intent.getComponent() != null;
1242
1243 // Don't modify the client's object!
1244 intent = new Intent(intent);
1245
1246 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001247 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001248 // TODO: New, check if this is correct
1249 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1250
1251 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001252 (aInfo.applicationInfo.privateFlags
1253 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001254 throw new IllegalArgumentException(
1255 "FLAG_CANT_SAVE_STATE not supported here");
1256 }
1257
Chong Zhang280d3322015-11-03 17:27:26 -08001258 ActivityOptions options = ActivityOptions.fromBundle(
1259 i == intents.length - 1 ? bOptions : null);
Todd Kennedy7440f172015-12-09 14:31:22 -08001260 int res = startActivityLocked(caller, intent, null /*ephemeralIntent*/,
1261 resolvedTypes[i], aInfo, null /*rInfo*/, null, null, resultTo, null, -1,
1262 callingPid, callingUid, callingPackage, callingPid, callingUid, 0,
1263 options, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001264 if (res < 0) {
1265 return res;
1266 }
1267
1268 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1269 }
1270 }
1271 } finally {
1272 Binder.restoreCallingIdentity(origId);
1273 }
1274
1275 return ActivityManager.START_SUCCESS;
1276 }
1277
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001279 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 throws RemoteException {
1281
Craig Mautner2568c3a2015-03-26 14:22:34 -07001282 if (andResume) {
1283 r.startFreezingScreenLocked(app, 0);
1284 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001285
Craig Mautner2568c3a2015-03-26 14:22:34 -07001286 // schedule launch ticks to collect information about slow apps.
1287 r.startLaunchTickingLocked();
1288 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001289
1290 // Have the window manager re-evaluate the orientation of
1291 // the screen based on the new activity order. Note that
1292 // as a result of this, it can call back into the activity
1293 // manager with a new orientation. We don't care about that,
1294 // because the activity is not currently running so we are
1295 // just restarting it anyway.
1296 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001297 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001298 mService.mConfiguration,
1299 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001300 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001301 }
1302
1303 r.app = app;
1304 app.waitingToKill = null;
1305 r.launchCount++;
1306 r.lastLaunchTime = SystemClock.uptimeMillis();
1307
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001308 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001309
1310 int idx = app.activities.indexOf(r);
1311 if (idx < 0) {
1312 app.activities.add(r);
1313 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001314 mService.updateLruProcessLocked(app, true, null);
1315 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001316
Craig Mautner15df08a2015-04-01 12:17:18 -07001317 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001318 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1319 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001320 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001321 }
1322
1323 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 try {
1325 if (app.thread == null) {
1326 throw new RemoteException();
1327 }
1328 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001329 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001330 if (andResume) {
1331 results = r.results;
1332 newIntents = r.newIntents;
1333 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001334 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1335 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1336 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001337 if (andResume) {
1338 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1339 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001340 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001341 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001342 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001343 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001344 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001345 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001346 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001347 r.sleeping = false;
1348 r.forceNewConfig = false;
1349 mService.showAskCompatModeDialogLocked(r);
1350 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001351 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001352 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1353 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1354 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001355 final String profileFile = mService.mProfileFile;
1356 if (profileFile != null) {
1357 ParcelFileDescriptor profileFd = mService.mProfileFd;
1358 if (profileFd != null) {
1359 try {
1360 profileFd = profileFd.dup();
1361 } catch (IOException e) {
1362 if (profileFd != null) {
1363 try {
1364 profileFd.close();
1365 } catch (IOException o) {
1366 }
1367 profileFd = null;
1368 }
1369 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001370 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001371
1372 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1373 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001374 }
1375 }
1376 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001377
Craig Mautner2568c3a2015-03-26 14:22:34 -07001378 if (andResume) {
1379 app.hasShownUi = true;
1380 app.pendingUiClean = true;
1381 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001382 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001383 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001384 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001385 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001386 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001387 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001388
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001389 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001390 // This may be a heavy-weight process! Note that the package
1391 // manager will ensure that only activity can run in the main
1392 // process of the .apk, which is the only thing that will be
1393 // considered heavy-weight.
1394 if (app.processName.equals(app.info.packageName)) {
1395 if (mService.mHeavyWeightProcess != null
1396 && mService.mHeavyWeightProcess != app) {
1397 Slog.w(TAG, "Starting new heavy weight process " + app
1398 + " when already running "
1399 + mService.mHeavyWeightProcess);
1400 }
1401 mService.mHeavyWeightProcess = app;
1402 Message msg = mService.mHandler.obtainMessage(
1403 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1404 msg.obj = r;
1405 mService.mHandler.sendMessage(msg);
1406 }
1407 }
1408
1409 } catch (RemoteException e) {
1410 if (r.launchFailed) {
1411 // This is the second time we failed -- finish activity
1412 // and give up.
1413 Slog.e(TAG, "Second failure launching "
1414 + r.intent.getComponent().flattenToShortString()
1415 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001416 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001417 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1418 "2nd-crash", false);
1419 return false;
1420 }
1421
1422 // This is the first time we failed -- restart process and
1423 // retry.
1424 app.activities.remove(r);
1425 throw e;
1426 }
1427
1428 r.launchFailed = false;
1429 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001430 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001431 }
1432
1433 if (andResume) {
1434 // As part of the process of launching, ActivityThread also performs
1435 // a resume.
1436 stack.minimalResumeActivityLocked(r);
1437 } else {
1438 // This activity is not starting in the resumed state... which
1439 // should look like we asked it to pause+stop (but remain visible),
1440 // and it has done so and reported back the current icicle and
1441 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001442 if (DEBUG_STATES) Slog.v(TAG_STATES,
1443 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001444 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001445 r.stopped = true;
1446 }
1447
1448 // Launch the new version setup screen if needed. We do this -after-
1449 // launching the initial activity (that is, home), so that it can have
1450 // a chance to initialize itself while in the background, making the
1451 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001452 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001453 mService.startSetupActivityLocked();
1454 }
1455
Dianne Hackborn465fa392014-09-14 14:21:18 -07001456 // Update any services we are bound to that might care about whether
1457 // their client may have activities.
1458 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1459
Craig Mautner2420ead2013-04-01 17:13:20 -07001460 return true;
1461 }
1462
Craig Mautnere79d42682013-04-01 19:01:53 -07001463 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001464 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001465 // Is this activity's application already running?
1466 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001467 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001468
1469 r.task.stack.setLaunchTime(r);
1470
1471 if (app != null && app.thread != null) {
1472 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001473 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1474 || !"android".equals(r.info.packageName)) {
1475 // Don't add this if it is a platform component that is marked
1476 // to run in multiple processes, because this is actually
1477 // part of the framework so doesn't make sense to track as a
1478 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001479 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1480 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001481 }
George Mount2c92c972014-03-20 09:38:23 -07001482 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001483 return;
1484 } catch (RemoteException e) {
1485 Slog.w(TAG, "Exception when starting activity "
1486 + r.intent.getComponent().flattenToShortString(), e);
1487 }
1488
1489 // If a dead object exception was thrown -- fall through to
1490 // restart the application.
1491 }
1492
1493 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001494 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001495 }
1496
Todd Kennedy7440f172015-12-09 14:31:22 -08001497 final int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
1498 String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001499 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Todd Kennedy7440f172015-12-09 14:31:22 -08001500 IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
1501 String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
1502 ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
1503 ActivityRecord[] outActivity, ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001504 int err = ActivityManager.START_SUCCESS;
1505
1506 ProcessRecord callerApp = null;
1507 if (caller != null) {
1508 callerApp = mService.getRecordForAppLocked(caller);
1509 if (callerApp != null) {
1510 callingPid = callerApp.pid;
1511 callingUid = callerApp.info.uid;
1512 } else {
1513 Slog.w(TAG, "Unable to find app for caller " + caller
1514 + " (pid=" + callingPid + ") when starting: "
1515 + intent.toString());
1516 err = ActivityManager.START_PERMISSION_DENIED;
1517 }
1518 }
1519
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001520 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1521
Craig Mautner6170f732013-04-02 13:05:23 -07001522 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001523 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001524 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001525 + " on display " + (container == null ? (mFocusedStack == null ?
1526 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1527 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1528 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001529 }
1530
1531 ActivityRecord sourceRecord = null;
1532 ActivityRecord resultRecord = null;
1533 if (resultTo != null) {
1534 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001535 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1536 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001537 if (sourceRecord != null) {
1538 if (requestCode >= 0 && !sourceRecord.finishing) {
1539 resultRecord = sourceRecord;
1540 }
1541 }
1542 }
Craig Mautner6170f732013-04-02 13:05:23 -07001543
Dianne Hackborn91097de2014-04-04 18:02:06 -07001544 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001545
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001546 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001547 // Transfer the result target from the source activity to the new
1548 // one being started, including any failures.
1549 if (requestCode >= 0) {
1550 ActivityOptions.abort(options);
1551 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1552 }
1553 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001554 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1555 resultRecord = null;
1556 }
Craig Mautner6170f732013-04-02 13:05:23 -07001557 resultWho = sourceRecord.resultWho;
1558 requestCode = sourceRecord.requestCode;
1559 sourceRecord.resultTo = null;
1560 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001561 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001562 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001563 if (sourceRecord.launchedFromUid == callingUid) {
1564 // The new activity is being launched from the same uid as the previous
1565 // activity in the flow, and asking to forward its result back to the
1566 // previous. In this case the activity is serving as a trampoline between
1567 // the two, so we also want to update its launchedFromPackage to be the
1568 // same as the previous activity. Note that this is safe, since we know
1569 // these two packages come from the same uid; the caller could just as
1570 // well have supplied that same package name itself. This specifially
1571 // deals with the case of an intent picker/chooser being launched in the app
1572 // flow to redirect to an activity picked by the user, where we want the final
1573 // activity to consider it to have been launched by the previous app activity.
1574 callingPackage = sourceRecord.launchedFromPackage;
1575 }
Craig Mautner6170f732013-04-02 13:05:23 -07001576 }
1577
1578 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1579 // We couldn't find a class that can handle the given Intent.
1580 // That's the end of that!
1581 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1582 }
1583
1584 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1585 // We couldn't find the specific class specified in the Intent.
1586 // Also the end of the line.
1587 err = ActivityManager.START_CLASS_NOT_FOUND;
1588 }
1589
Dianne Hackborn91097de2014-04-04 18:02:06 -07001590 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1591 && sourceRecord.task.voiceSession != null) {
1592 // If this activity is being launched as part of a voice session, we need
1593 // to ensure that it is safe to do so. If the upcoming activity will also
1594 // be part of the voice session, we can only launch it if it has explicitly
1595 // said it supports the VOICE category, or it is a part of the calling app.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001596 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001597 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1598 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001599 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001600 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1601 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001602 Slog.w(TAG,
1603 "Activity being started in current voice task does not support voice: "
1604 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001605 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1606 }
1607 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001608 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001609 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1610 }
1611 }
1612 }
1613
1614 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1615 // If the caller is starting a new voice session, just make sure the target
1616 // is actually allowing it to run this way.
1617 try {
1618 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1619 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001620 Slog.w(TAG,
1621 "Activity being started in new voice task does not support: "
1622 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001623 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1624 }
1625 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001626 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001627 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1628 }
1629 }
1630
louis_changcd5d1982014-08-01 10:09:08 +08001631 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1632
Craig Mautner6170f732013-04-02 13:05:23 -07001633 if (err != ActivityManager.START_SUCCESS) {
1634 if (resultRecord != null) {
1635 resultStack.sendActivityResultLocked(-1,
1636 resultRecord, resultWho, requestCode,
1637 Activity.RESULT_CANCELED, null);
1638 }
Craig Mautner6170f732013-04-02 13:05:23 -07001639 ActivityOptions.abort(options);
1640 return err;
1641 }
1642
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001643 boolean abort = !checkStartAnyActivityPermission(intent, aInfo, resultWho, requestCode,
1644 callingPid, callingUid, callingPackage, ignoreTargetSecurity, callerApp,
1645 resultRecord, resultStack);
Svetoslav7008b512015-06-24 18:47:07 -07001646 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001647 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001648
Craig Mautner6170f732013-04-02 13:05:23 -07001649 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001650 try {
1651 // The Intent we give to the watcher has the extra data
1652 // stripped off, since it can contain private information.
1653 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001654 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001655 aInfo.applicationInfo.packageName);
1656 } catch (RemoteException e) {
1657 mService.mController = null;
1658 }
Ben Gruver5e207332013-04-03 17:41:37 -07001659 }
Craig Mautner6170f732013-04-02 13:05:23 -07001660
Clara Bayarrif7fea162015-10-22 16:09:52 +01001661 UserInfo user = getUserInfo(userId);
Clara Bayarri56878a92015-10-29 15:43:55 +00001662 KeyguardManager km = (KeyguardManager) mService.mContext
1663 .getSystemService(Context.KEYGUARD_SERVICE);
1664 if (user.isManagedProfile()
Clara Bayarri10ad84a2015-12-01 17:38:05 +00001665 && LockPatternUtils.isSeparateWorkChallengeEnabled()
Clara Bayarri56878a92015-10-29 15:43:55 +00001666 && km.isDeviceLocked(userId)) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001667 IIntentSender target = mService.getIntentSenderLocked(
1668 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1669 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1670 new String[]{ resolvedType },
1671 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1672 | PendingIntent.FLAG_IMMUTABLE, null);
Todd Kennedy7440f172015-12-09 14:31:22 -08001673 final int flags = intent.getFlags();
1674 final Intent newIntent = km.createConfirmDeviceCredentialIntent(null, null, user.id);
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001675 if (newIntent != null) {
1676 intent = newIntent;
Todd Kennedy7440f172015-12-09 14:31:22 -08001677 intent.setFlags(flags
1678 | Intent.FLAG_ACTIVITY_NEW_TASK
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001679 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1680 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1681 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
Clara Bayarrif7fea162015-10-22 16:09:52 +01001682
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001683 resolvedType = null;
1684 callingUid = realCallingUid;
1685 callingPid = realCallingPid;
Clara Bayarrif7fea162015-10-22 16:09:52 +01001686
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001687 UserInfo parent = UserManager.get(mService.mContext).getProfileParent(userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001688 rInfo = resolveIntent(intent, resolvedType, parent.id);
1689 aInfo = resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001690 }
Clara Bayarrif7fea162015-10-22 16:09:52 +01001691 }
1692
Ben Gruver5e207332013-04-03 17:41:37 -07001693 if (abort) {
1694 if (resultRecord != null) {
1695 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001696 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001697 }
Ben Gruver5e207332013-04-03 17:41:37 -07001698 // We pretend to the caller that it was really started, but
1699 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001700 ActivityOptions.abort(options);
1701 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001702 }
1703
Svet Ganov9c165d72015-12-01 19:52:26 -08001704 // If permissions need a review before any of the app components can run, we
1705 // launch the review activity and pass a pending intent to start the activity
1706 // we are to launching now after the review is completed.
1707 if (Build.PERMISSIONS_REVIEW_REQUIRED && aInfo != null) {
1708 if (mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
1709 aInfo.packageName, userId)) {
1710 IIntentSender target = mService.getIntentSenderLocked(
1711 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1712 callingUid, userId, null, null, 0, new Intent[]{intent},
1713 new String[]{resolvedType}, PendingIntent.FLAG_CANCEL_CURRENT
1714 | PendingIntent.FLAG_ONE_SHOT, null);
1715
Todd Kennedy7440f172015-12-09 14:31:22 -08001716 final int flags = intent.getFlags();
Svet Ganov9c165d72015-12-01 19:52:26 -08001717 Intent newIntent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
Todd Kennedy7440f172015-12-09 14:31:22 -08001718 newIntent.setFlags(flags
1719 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Svet Ganov9c165d72015-12-01 19:52:26 -08001720 newIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1721 newIntent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1722 if (resultRecord != null) {
1723 newIntent.putExtra(Intent.EXTRA_RESULT_NEEDED, true);
1724 }
Svet Ganov9c165d72015-12-01 19:52:26 -08001725 intent = newIntent;
1726
1727 resolvedType = null;
1728 callingUid = realCallingUid;
1729 callingPid = realCallingPid;
1730
Todd Kennedy7440f172015-12-09 14:31:22 -08001731 rInfo = resolveIntent(intent, resolvedType, userId);
1732 aInfo = resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
Svet Ganov9c165d72015-12-01 19:52:26 -08001733
1734 if (DEBUG_PERMISSIONS_REVIEW) {
1735 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true,
1736 true, false) + "} from uid " + callingUid + " on display "
1737 + (container == null ? (mFocusedStack == null ?
1738 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1739 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1740 container.mActivityDisplay.mDisplayId)));
1741 }
1742 }
1743 }
1744
Todd Kennedy7440f172015-12-09 14:31:22 -08001745 // If we have an ephemeral app, abort the process of launching the resolved intent.
1746 // Instead, launch the ephemeral installer. Once the installer is finished, it
1747 // starts either the intent we resolved here [on install error] or the ephemeral
1748 // app [on install success].
1749 if (rInfo != null && rInfo.ephemeralResolveInfo != null) {
1750 // Create a pending intent to start the intent resolved here.
1751 final IIntentSender failureTarget = mService.getIntentSenderLocked(
1752 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1753 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1754 new String[]{ resolvedType },
1755 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1756 | PendingIntent.FLAG_IMMUTABLE, null);
1757
1758 // Create a pending intent to start the ephemeral application; force it to be
1759 // directed to the ephemeral package.
1760 ephemeralIntent.setPackage(rInfo.ephemeralResolveInfo.getPackageName());
1761 final IIntentSender ephemeralTarget = mService.getIntentSenderLocked(
1762 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1763 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ ephemeralIntent },
1764 new String[]{ resolvedType },
1765 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1766 | PendingIntent.FLAG_IMMUTABLE, null);
1767
1768 int flags = intent.getFlags();
1769 intent = new Intent();
1770 intent.setFlags(flags
1771 | Intent.FLAG_ACTIVITY_NEW_TASK
1772 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1773 intent.putExtra(Intent.EXTRA_PACKAGE_NAME,
1774 rInfo.ephemeralResolveInfo.getPackageName());
1775 intent.putExtra(Intent.EXTRA_EPHEMERAL_FAILURE, new IntentSender(failureTarget));
1776 intent.putExtra(Intent.EXTRA_EPHEMERAL_SUCCESS, new IntentSender(ephemeralTarget));
1777
1778 resolvedType = null;
1779 callingUid = realCallingUid;
1780 callingPid = realCallingPid;
1781
1782 rInfo = rInfo.ephemeralInstaller;
1783 aInfo = resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
1784 }
1785
Craig Mautner6170f732013-04-02 13:05:23 -07001786 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001787 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001788 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001789 if (outActivity != null) {
1790 outActivity[0] = r;
1791 }
1792
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001793 if (r.appTimeTracker == null && sourceRecord != null) {
1794 // If the caller didn't specify an explicit time tracker, we want to continue
1795 // tracking under any it has.
1796 r.appTimeTracker = sourceRecord.appTimeTracker;
1797 }
1798
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001799 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001800 if (voiceSession == null && (stack.mResumedActivity == null
1801 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001802 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1803 realCallingPid, realCallingUid, "Activity start")) {
Chong Zhang280d3322015-11-03 17:27:26 -08001804 PendingActivityLaunch pal = new PendingActivityLaunch(r,
Robert Carr13997f52015-10-23 13:13:39 -07001805 sourceRecord, startFlags, stack, callerApp);
Craig Mautneree36c772014-07-16 14:56:05 -07001806 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001807 ActivityOptions.abort(options);
1808 return ActivityManager.START_SWITCHES_CANCELED;
1809 }
1810 }
1811
1812 if (mService.mDidAppSwitch) {
1813 // This is the second allowed switch since we stopped switches,
1814 // so now just generally allow switches. Use case: user presses
1815 // home (switches disabled, switch to home, mDidAppSwitch now true);
1816 // user taps a home icon (coming from home so allowed, we hit here
1817 // and now allow anyone to switch again).
1818 mService.mAppSwitchesAllowedTime = 0;
1819 } else {
1820 mService.mDidAppSwitch = true;
1821 }
1822
Craig Mautneree36c772014-07-16 14:56:05 -07001823 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001824
Dianne Hackborn91097de2014-04-04 18:02:06 -07001825 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001826 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001827
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001828 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001829 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001830 // activity start, but we are not actually doing an activity
1831 // switch... just dismiss the keyguard now, because we
1832 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001833 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001834 }
1835 return err;
1836 }
1837
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001838 private boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
1839 String resultWho, int requestCode, int callingPid, int callingUid,
1840 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1841 ActivityRecord resultRecord, ActivityStack resultStack) {
1842 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1843 callingUid);
1844 if (startAnyPerm == PERMISSION_GRANTED) {
1845 return true;
1846 }
1847 final int componentRestriction = getComponentRestrictionForCallingPackage(
1848 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1849 final int actionRestriction = getActionRestrictionForCallingPackage(
1850 intent.getAction(), callingPackage, callingPid, callingUid);
1851 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1852 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1853 if (resultRecord != null) {
1854 resultStack.sendActivityResultLocked(-1,
1855 resultRecord, resultWho, requestCode,
1856 Activity.RESULT_CANCELED, null);
1857 }
1858 final String msg;
1859 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1860 msg = "Permission Denial: starting " + intent.toString()
1861 + " from " + callerApp + " (pid=" + callingPid
1862 + ", uid=" + callingUid + ")" + " with revoked permission "
1863 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1864 } else if (!aInfo.exported) {
1865 msg = "Permission Denial: starting " + intent.toString()
1866 + " from " + callerApp + " (pid=" + callingPid
1867 + ", uid=" + callingUid + ")"
1868 + " not exported from uid " + aInfo.applicationInfo.uid;
1869 } else {
1870 msg = "Permission Denial: starting " + intent.toString()
1871 + " from " + callerApp + " (pid=" + callingPid
1872 + ", uid=" + callingUid + ")"
1873 + " requires " + aInfo.permission;
1874 }
1875 Slog.w(TAG, msg);
1876 throw new SecurityException(msg);
1877 }
1878
1879 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1880 final String message = "Appop Denial: starting " + intent.toString()
1881 + " from " + callerApp + " (pid=" + callingPid
1882 + ", uid=" + callingUid + ")"
1883 + " requires " + AppOpsManager.permissionToOp(
1884 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1885 Slog.w(TAG, message);
1886 return false;
1887 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1888 final String message = "Appop Denial: starting " + intent.toString()
1889 + " from " + callerApp + " (pid=" + callingPid
1890 + ", uid=" + callingUid + ")"
1891 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1892 Slog.w(TAG, message);
1893 return false;
1894 }
1895 return true;
1896 }
1897
Clara Bayarrif7fea162015-10-22 16:09:52 +01001898 private UserInfo getUserInfo(int userId) {
1899 final long identity = Binder.clearCallingIdentity();
1900 try {
1901 return UserManager.get(mService.mContext).getUserInfo(userId);
1902 } finally {
1903 Binder.restoreCallingIdentity(identity);
1904 }
1905 }
1906
Svet Ganov99b60432015-06-27 13:15:22 -07001907 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001908 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001909 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1910 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001911 == PackageManager.PERMISSION_DENIED) {
1912 return ACTIVITY_RESTRICTION_PERMISSION;
1913 }
1914
Christopher Tateff7add02015-08-17 10:23:22 -07001915 if (activityInfo.permission == null) {
1916 return ACTIVITY_RESTRICTION_NONE;
1917 }
1918
Svet Ganov99b60432015-06-27 13:15:22 -07001919 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1920 if (opCode == AppOpsManager.OP_NONE) {
1921 return ACTIVITY_RESTRICTION_NONE;
1922 }
1923
1924 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1925 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001926 if (!ignoreTargetSecurity) {
1927 return ACTIVITY_RESTRICTION_APPOP;
1928 }
Svet Ganov99b60432015-06-27 13:15:22 -07001929 }
1930
1931 return ACTIVITY_RESTRICTION_NONE;
1932 }
1933
Svetoslav7008b512015-06-24 18:47:07 -07001934 private int getActionRestrictionForCallingPackage(String action,
1935 String callingPackage, int callingPid, int callingUid) {
1936 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001937 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001938 }
1939
1940 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1941 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001942 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001943 }
1944
1945 final PackageInfo packageInfo;
1946 try {
1947 packageInfo = mService.mContext.getPackageManager()
1948 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1949 } catch (PackageManager.NameNotFoundException e) {
1950 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001951 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001952 }
1953
1954 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001955 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001956 }
1957
1958 if (mService.checkPermission(permission, callingPid, callingUid) ==
1959 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001960 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001961 }
1962
1963 final int opCode = AppOpsManager.permissionToOpCode(permission);
1964 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001965 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001966 }
1967
1968 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1969 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001970 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001971 }
1972
Svet Ganov99b60432015-06-27 13:15:22 -07001973 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001974 }
1975
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08001976 private ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds,
1977 int launchFlags) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001978 final TaskRecord task = r.task;
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001979 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1980 return mHomeStack;
1981 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001982
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08001983 ActivityStack stack = getLaunchToSideStack(r, launchFlags, task);
1984 if (stack != null) {
1985 return stack;
1986 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001987
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001988 if (task != null && task.stack != null) {
1989 stack = task.stack;
1990 if (stack.isOnHomeDisplay()) {
1991 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001992 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001993 "computeStackFocus: Setting " + "focused stack to r=" + r
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08001994 + " task=" + task);
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001995 } else {
1996 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08001997 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001998 }
1999 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002000 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002001 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002002
2003 final ActivityContainer container = r.mInitialActivityContainer;
2004 if (container != null) {
2005 // The first time put it on the desired stack, after this put on task stack.
2006 r.mInitialActivityContainer = null;
2007 return container.mStack;
2008 }
2009
2010 // The fullscreen stack can contain any task regardless of if the task is resizeable
2011 // 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 -08002012 // If the freeform or docked stack has focus, and the activity to be launched is resizeable,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002013 // we can also put it in the focused stack.
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002014 final int focusedStackId = mFocusedStack.mStackId;
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002015 final boolean canUseFocusedStack =
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002016 focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
2017 || focusedStackId == DOCKED_STACK_ID
2018 || (focusedStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable);
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002019 if (canUseFocusedStack
2020 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
2021 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
2022 "computeStackFocus: Have a focused stack=" + mFocusedStack);
2023 return mFocusedStack;
2024 }
2025
2026 // We first try to put the task in the first dynamic stack.
2027 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
2028 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
2029 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07002030 if (!StackId.isStaticStack(stack.mStackId)) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002031 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
2032 "computeStackFocus: Setting focused stack=" + stack);
2033 return stack;
2034 }
2035 }
2036
2037 // If there is no suitable dynamic stack then we figure out which static stack to use.
2038 final int stackId = task != null ? task.getLaunchStackId() :
2039 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
2040 FULLSCREEN_WORKSPACE_STACK_ID;
2041 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2042 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
2043 + r + " stackId=" + stack.mStackId);
2044 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002045 }
2046
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002047 private ActivityStack getLaunchToSideStack(ActivityRecord r, int launchFlags, TaskRecord task) {
2048 if ((launchFlags & FLAG_ACTIVITY_LAUNCH_TO_SIDE) == 0) {
2049 return null;
2050 }
2051 // The parent activity doesn't want to launch the activity on top of itself, but
2052 // instead tries to put it onto other side in side-by-side mode.
2053 final ActivityStack parentStack = task != null ? task.stack
2054 : r.mInitialActivityContainer != null ? r.mInitialActivityContainer.mStack
2055 : mFocusedStack;
2056 if (parentStack != null && parentStack.mStackId == DOCKED_STACK_ID) {
2057 // If parent was in docked stack, the natural place to launch another activity
2058 // will be fullscreen, so it can appear alongside the docked window.
2059 return getStack(FULLSCREEN_WORKSPACE_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2060 } else {
2061 // If the parent is not in the docked stack, we check if there is docked window
2062 // and if yes, we will launch into that stack. If not, we just put the new
2063 // activity into parent's stack, because we can't find a better place.
2064 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2065 if (stack != null && !stack.isStackVisibleLocked()) {
2066 // There is a docked stack, but it isn't visible, so we can't launch into that.
2067 return null;
2068 } else {
2069 return stack;
2070 }
2071 }
2072 }
2073
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002074 boolean setFocusedStack(ActivityRecord r, String reason) {
2075 if (r == null) {
2076 // Not sure what you are trying to do, but it is not going to work...
2077 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07002078 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002079 final TaskRecord task = r.task;
2080 if (task == null || task.stack == null) {
2081 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
2082 return false;
2083 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07002084 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002085 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07002086 }
2087
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002088 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002089 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Chong Zhang280d3322015-11-03 17:27:26 -08002090 boolean doResume, ActivityOptions options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002091 final Intent intent = r.intent;
2092 final int callingUid = r.launchedFromUid;
2093
Chong Zhang0fa656b2015-08-31 15:17:21 -07002094 boolean overrideBounds = false;
2095 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07002096 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002097 if (canUseActivityOptionsLaunchBounds(options)) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07002098 overrideBounds = true;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002099 newBounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002100 }
2101 }
2102
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002103 // In some flows in to this function, we retrieve the task record and hold on to it
2104 // without a lock before calling back in to here... so the task at this point may
2105 // not actually be in recents. Check for that, and if it isn't in recents just
2106 // consider it invalid.
2107 if (inTask != null && !inTask.inRecents) {
2108 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
2109 inTask = null;
2110 }
2111
Craig Mautnerad400af2014-08-13 16:41:36 -07002112 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07002113 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
2114 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002115
Craig Mautnera228ae92014-07-09 05:44:55 -07002116 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002117 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07002118 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002119 // We have a conflict between the Intent and the Activity manifest, manifest wins.
2120 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
2121 "\"singleInstance\" or \"singleTask\"");
2122 launchFlags &=
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002123 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_MULTIPLE_TASK);
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002124 } else {
2125 switch (r.info.documentLaunchMode) {
2126 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
2127 break;
2128 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
2129 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2130 break;
2131 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
2132 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2133 break;
2134 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002135 launchFlags &= ~FLAG_ACTIVITY_MULTIPLE_TASK;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002136 break;
2137 }
2138 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002139
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002140 final boolean launchTaskBehind = r.mLaunchTaskBehind
2141 && !launchSingleTask && !launchSingleInstance
2142 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002143
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002144 if (r.resultTo != null && (launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0
Wale Ogunwale7d701172015-03-11 15:36:30 -07002145 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002146 // For whatever reason this activity is being launched into a new
2147 // task... yet the caller has requested a result back. Well, that
2148 // is pretty messed up, so instead immediately send back a cancel
2149 // and let the new task continue launched as normal without a
2150 // dependency on its originator.
2151 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
2152 r.resultTo.task.stack.sendActivityResultLocked(-1,
2153 r.resultTo, r.resultWho, r.requestCode,
2154 Activity.RESULT_CANCELED, null);
2155 r.resultTo = null;
2156 }
2157
2158 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002159 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002160 }
2161
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002162 // If we are actually going to launch in to a new task, there are some cases where
2163 // we further want to do multiple task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002164 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002165 if (launchTaskBehind
2166 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002167 launchFlags |= FLAG_ACTIVITY_MULTIPLE_TASK;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002168 }
2169 }
2170
Craig Mautner8849a5e2013-04-02 16:41:03 -07002171 // We'll invoke onUserLeaving before onPause only if the launching
2172 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07002173 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002174 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2175 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002176
2177 // If the caller has asked not to resume at this point, we make note
2178 // of this in the record so that we can skip it when trying to find
2179 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07002180 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002181 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002182 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002183 }
2184
Craig Mautnera254cd72014-05-25 16:47:39 -07002185 ActivityRecord notTop =
2186 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002187
2188 // If the onlyIfNeeded flag is set, then we can do this if the activity
2189 // being launched is the same as the one making the call... or, as
2190 // a special case, if we do not know the caller then we count the
2191 // current top activity as the caller.
2192 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2193 ActivityRecord checkedCaller = sourceRecord;
2194 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002195 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196 }
2197 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2198 // Caller is not the same as launcher, so always needed.
2199 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
2200 }
2201 }
2202
Craig Mautner8849a5e2013-04-02 16:41:03 -07002203 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002204 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002205
2206 // If the caller is not coming from another activity, but has given us an
2207 // explicit task into which they would like us to launch the new activity,
2208 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002209 if (sourceRecord == null && inTask != null && inTask.stack != null) {
2210 final Intent baseIntent = inTask.getBaseIntent();
2211 final ActivityRecord root = inTask.getRootActivity();
2212 if (baseIntent == null) {
2213 ActivityOptions.abort(options);
2214 throw new IllegalArgumentException("Launching into task without base intent: "
2215 + inTask);
2216 }
2217
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002218 // If this task is empty, then we are adding the first activity -- it
2219 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002220 if (launchSingleInstance || launchSingleTask) {
2221 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2222 ActivityOptions.abort(options);
2223 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2224 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002225 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002226 if (root != null) {
2227 ActivityOptions.abort(options);
2228 throw new IllegalArgumentException("Caller with inTask " + inTask
2229 + " has root " + root + " but target is singleInstance/Task");
2230 }
2231 }
2232
2233 // If task is empty, then adopt the interesting intent launch flags in to the
2234 // activity being started.
2235 if (root == null) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002236 final int flagsOfInterest = FLAG_ACTIVITY_NEW_TASK
2237 | FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002238 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2239 launchFlags = (launchFlags&~flagsOfInterest)
2240 | (baseIntent.getFlags()&flagsOfInterest);
2241 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002242 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002243 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002244
Dianne Hackborn962d5352014-08-29 16:27:40 -07002245 // If the task is not empty and the caller is asking to start it as the root
2246 // of a new task, then we don't actually want to start this on the task. We
2247 // will bring the task to the front, and possibly give it a new intent.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002248 } else if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002249 addingToTask = false;
2250
2251 } else {
2252 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002253 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002254
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002255 reuseTask = inTask;
2256 } else {
2257 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002258 // Launch ResolverActivity in the source task, so that it stays in the task
2259 // bounds when in freeform workspace.
2260 // Also put noDisplay activities in the source task. These by itself can
2261 // be placed in any task/stack, however it could launch other activities
2262 // like ResolverActivity, and we want those to stay in the original task.
2263 if (r.isResolverActivity() || r.noDisplay) {
2264 addingToTask = true;
2265 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002266 }
2267
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002268 if (inTask == null) {
2269 if (sourceRecord == null) {
2270 // This activity is not being started from another... in this
2271 // case we -always- start a new task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002272 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002273 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2274 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002275 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002276 }
2277 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2278 // The original activity who is starting us is running as a single
2279 // instance... this new activity it is starting must go on its
2280 // own task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002281 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002282 } else if (launchSingleInstance || launchSingleTask) {
2283 // The activity being started is a single instance... it always
2284 // gets launched into its own task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002285 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002286 }
2287 }
2288
2289 ActivityInfo newTaskInfo = null;
2290 Intent newTaskIntent = null;
Filip Gruszczynskia8e98b92015-12-15 16:35:38 -08002291 final ActivityStack sourceStack;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002292 if (sourceRecord != null) {
2293 if (sourceRecord.finishing) {
2294 // If the source is finishing, we can't further count it as our source. This
2295 // is because the task it is associated with may now be empty and on its way out,
2296 // so we don't want to blindly throw it in to that task. Instead we will take
2297 // the NEW_TASK flow and try to find a task for it. But save the task information
2298 // so it can be used when creating the new task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002299 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002300 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2301 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002302 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002303 newTaskInfo = sourceRecord.info;
2304 newTaskIntent = sourceRecord.task.intent;
2305 }
2306 sourceRecord = null;
2307 sourceStack = null;
2308 } else {
2309 sourceStack = sourceRecord.task.stack;
2310 }
2311 } else {
2312 sourceStack = null;
2313 }
2314
2315 boolean movedHome = false;
2316 ActivityStack targetStack;
2317
2318 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002319 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002320
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002321 ActivityRecord intentActivity = getReusableIntentActivity(r, inTask, intent,
2322 launchSingleInstance, launchSingleTask, launchFlags);
2323 if (intentActivity != null) {
2324 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2325 // but still needs to be a lock task mode violation since the task gets
2326 // cleared out and the device would otherwise leave the locked task.
2327 if (isLockTaskModeViolation(intentActivity.task,
2328 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2329 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
2330 showLockTaskToast();
2331 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
2332 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2333 }
2334 if (r.task == null) {
2335 r.task = intentActivity.task;
2336 }
2337 if (intentActivity.task.intent == null) {
2338 // This task was started because of movement of the activity based on affinity...
2339 // Now that we are actually launching it, we can assign the base intent.
2340 intentActivity.task.setIntent(r);
2341 }
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002342
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002343 targetStack = intentActivity.task.stack;
2344 targetStack.mLastPausedActivity = null;
2345 // If the target task is not in the front, then we need
2346 // to bring it to the front... except... well, with
2347 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2348 // to have the same behavior as if a new instance was
2349 // being started, which means not bringing it to the front
2350 // if the caller is not itself in the front.
2351 final ActivityStack focusStack = getFocusedStack();
2352 ActivityRecord curTop = (focusStack == null)
2353 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
2354 boolean movedToFront = false;
2355 if (curTop != null && (curTop.task != intentActivity.task ||
2356 curTop.task != focusStack.topTask())) {
2357 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
2358 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2359 sourceStack.topActivity().task == sourceRecord.task)) {
2360 // We really do want to push this one into the user's face, right now.
2361 if (launchTaskBehind && sourceRecord != null) {
2362 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
Craig Mautneraea74a52014-03-08 14:23:10 -08002363 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002364 movedHome = true;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002365 final ActivityStack sideStack = getLaunchToSideStack(r, launchFlags, r.task);
2366 if (sideStack == null || sideStack == targetStack) {
2367 // We only want to move to the front, if we aren't going to launch on a
2368 // different stack. If we launch on a different stack, we will put the
2369 // task on top there.
2370 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
2371 options, r.appTimeTracker, "bringingFoundTaskToFront");
2372 movedToFront = true;
2373 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002374 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2375 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
2376 // Caller wants to appear on home activity.
2377 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautner29219d92013-04-16 20:19:12 -07002378 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002379 options = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002380 }
2381 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002382 if (!movedToFront && doResume) {
2383 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
2384 + " from " + intentActivity);
2385 targetStack.moveToFront("intentActivityFound");
2386 }
2387
2388 // If the caller has requested that the target task be
2389 // reset, then do so.
2390 if ((launchFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2391 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2392 }
2393 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2394 // We don't need to start a new activity, and
2395 // the client said not to do anything if that
2396 // is the case, so this is it! And for paranoia, make
2397 // sure we have correctly resumed the top activity.
2398 if (doResume) {
2399 resumeTopActivitiesLocked(targetStack, null, options);
2400
2401 // Make sure to notify Keyguard as well if we are not running an app
2402 // transition later.
2403 if (!movedToFront) {
2404 notifyActivityDrawnForKeyguard();
2405 }
2406 } else {
2407 ActivityOptions.abort(options);
2408 }
2409 updateUserStackLocked(r.userId, targetStack);
2410 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2411 }
2412 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2413 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
2414 // The caller has requested to completely replace any
2415 // existing task with its new activity. Well that should
2416 // not be too hard...
2417 reuseTask = intentActivity.task;
2418 reuseTask.performClearTaskLocked();
2419 reuseTask.setIntent(r);
2420 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
2421 || launchSingleInstance || launchSingleTask) {
2422 // In this situation we want to remove all activities
2423 // from the task up to the one being started. In most
2424 // cases this means we are resetting the task to its
2425 // initial state.
2426 ActivityRecord top = intentActivity.task.performClearTaskLocked(r, launchFlags);
2427 if (top != null) {
2428 if (top.frontOfTask) {
2429 // Activity aliases may mean we use different
2430 // intents for the top activity, so make sure
2431 // the task now has the identity of the new
2432 // intent.
2433 top.task.setIntent(r);
2434 }
2435 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
2436 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
2437 } else {
2438 // A special case: we need to start the activity because it is not
2439 // currently running, and the caller has asked to clear the current
2440 // task to have this activity at the top.
2441 addingToTask = true;
2442 // Now pretend like this activity is being started by the top of its
2443 // task, so it is put in the right place.
2444 sourceRecord = intentActivity;
2445 TaskRecord task = sourceRecord.task;
2446 if (task != null && task.stack == null) {
2447 // Target stack got cleared when we all activities were removed
2448 // above. Go ahead and reset it.
2449 targetStack = computeStackFocus(
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002450 sourceRecord, false /* newTask */, null /* bounds */, launchFlags);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002451 targetStack.addTask(task,
2452 !launchTaskBehind /* toTop */, "startActivityUnchecked");
2453 }
2454
2455 }
2456 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2457 // In this case the top activity on the task is the
2458 // same as the one being launched, so we take that
2459 // as a request to bring the task to the foreground.
2460 // If the top activity in the task is the root
2461 // activity, deliver this new intent to it if it
2462 // desires.
2463 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
2464 && intentActivity.realActivity.equals(r.realActivity)) {
2465 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2466 intentActivity.task);
2467 if (intentActivity.frontOfTask) {
2468 intentActivity.task.setIntent(r);
2469 }
2470 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2471 r.launchedFromPackage);
2472 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2473 // In this case we are launching the root activity
2474 // of the task, but with a different intent. We
2475 // should start a new instance on top.
2476 addingToTask = true;
2477 sourceRecord = intentActivity;
2478 }
2479 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2480 // In this case an activity is being launched in to an
2481 // existing task, without resetting that task. This
2482 // is typically the situation of launching an activity
2483 // from a notification or shortcut. We want to place
2484 // the new activity on top of the current task.
2485 addingToTask = true;
2486 sourceRecord = intentActivity;
2487 } else if (!intentActivity.task.rootWasReset) {
2488 // In this case we are launching in to an existing task
2489 // that has not yet been started from its front door.
2490 // The current task has been brought to the front.
2491 // Ideally, we'd probably like to place this new task
2492 // at the bottom of its stack, but that's a little hard
2493 // to do with the current organization of the code so
2494 // for now we'll just drop it.
2495 intentActivity.task.setIntent(r);
2496 }
2497 if (!addingToTask && reuseTask == null) {
2498 // We didn't do anything... but it was needed (a.k.a., client
2499 // don't use that intent!) And for paranoia, make
2500 // sure we have correctly resumed the top activity.
2501 if (doResume) {
2502 targetStack.resumeTopActivityLocked(null, options);
2503 if (!movedToFront) {
2504 // Make sure to notify Keyguard as well if we are not running an app
2505 // transition later.
2506 notifyActivityDrawnForKeyguard();
2507 }
2508 } else {
2509 ActivityOptions.abort(options);
2510 }
2511 updateUserStackLocked(r.userId, targetStack);
2512 return ActivityManager.START_TASK_TO_FRONT;
2513 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002514 }
2515
2516 //String uri = r.intent.toURI();
2517 //Intent intent2 = new Intent(uri);
2518 //Slog.i(TAG, "Given intent: " + r.intent);
2519 //Slog.i(TAG, "URI is: " + uri);
2520 //Slog.i(TAG, "To intent: " + intent2);
2521
2522 if (r.packageName != null) {
2523 // If the activity being launched is the same as the one currently
2524 // at the top, then we need to check if it should only be launched
2525 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002526 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002527 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Filip Gruszczynskia8e98b92015-12-15 16:35:38 -08002528 final boolean dontStart = top != null && r.resultTo == null
2529 && top.realActivity.equals(r.realActivity) && top.userId == r.userId
2530 && top.app != null && top.app.thread != null
2531 && ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2532 || launchSingleTop || launchSingleTask);
2533 if (dontStart) {
2534 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2535 // For paranoia, make sure we have correctly resumed the top activity.
2536 topStack.mLastPausedActivity = null;
2537 if (doResume) {
2538 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002539 }
Filip Gruszczynskia8e98b92015-12-15 16:35:38 -08002540 ActivityOptions.abort(options);
2541 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2542 // We don't need to start a new activity, and the client said not to do
2543 // anything if that is the case, so this is it!
2544 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2545 }
2546 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
2547 return ActivityManager.START_DELIVERED_TO_TOP;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002548 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002549 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002550 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002551 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2552 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002553 }
2554 ActivityOptions.abort(options);
2555 return ActivityManager.START_CLASS_NOT_FOUND;
2556 }
2557
2558 boolean newTask = false;
2559 boolean keepCurTransition = false;
2560
Craig Mautnerbb742462014-07-07 15:28:55 -07002561 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002562 sourceRecord.task : null;
2563
Craig Mautner8849a5e2013-04-02 16:41:03 -07002564 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002565 if (r.resultTo == null && inTask == null && !addingToTask
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002566 && (launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002567 newTask = true;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002568 targetStack = computeStackFocus(r, newTask, newBounds, launchFlags);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002569 if (doResume) {
2570 targetStack.moveToFront("startingNewTask");
2571 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002572
Craig Mautner8849a5e2013-04-02 16:41:03 -07002573 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002574 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2575 newTaskInfo != null ? newTaskInfo : r.info,
2576 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002577 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2578 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002579 if (overrideBounds) {
2580 r.task.updateOverrideConfiguration(newBounds);
2581 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002582 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2583 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002584 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002585 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002586 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002587 if (isLockTaskModeViolation(r.task)) {
2588 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2589 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2590 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002591 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002592 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002593 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2594 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002595 // Caller wants to appear on home activity, so before starting
2596 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002597 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002598 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002599 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002600 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002601 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002602 if (isLockTaskModeViolation(sourceTask)) {
2603 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2604 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2605 }
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002606 targetStack = null;
2607 if (sourceTask.stack.topTask() != sourceTask) {
2608 // We only want to allow changing stack if the target task is not the top one,
2609 // otherwise we would move the launching task to the other side, rather than show
2610 // two side by side.
2611 targetStack = getLaunchToSideStack(r, launchFlags, r.task);
2612 }
2613 if (targetStack == null) {
2614 targetStack = sourceTask.stack;
2615 } else if (targetStack != sourceTask.stack) {
2616 moveTaskToStackLocked(sourceTask.taskId, targetStack.mStackId, ON_TOP,
2617 FORCE_FOCUS, "launchToSide", !ANIMATE);
2618 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002619 if (doResume) {
2620 targetStack.moveToFront("sourceStackToFront");
2621 }
Craig Mautner737fae22014-10-15 12:52:10 -07002622 final TaskRecord topTask = targetStack.topTask();
2623 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002624 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002625 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002626 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002627 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002628 // In this case, we are adding the activity to an existing
2629 // task, but the caller has asked to clear that task if the
2630 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002631 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002632 keepCurTransition = true;
2633 if (top != null) {
2634 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002635 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002636 // For paranoia, make sure we have correctly
2637 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002638 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002639 if (doResume) {
2640 targetStack.resumeTopActivityLocked(null);
2641 }
2642 ActivityOptions.abort(options);
2643 return ActivityManager.START_DELIVERED_TO_TOP;
2644 }
2645 } else if (!addingToTask &&
2646 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2647 // In this case, we are launching an activity in our own task
2648 // that may already be running somewhere in the history, and
2649 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002650 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002651 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002652 final TaskRecord task = top.task;
2653 task.moveActivityToFrontLocked(top);
2654 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002655 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002656 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002657 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002658 if (doResume) {
2659 targetStack.resumeTopActivityLocked(null);
2660 }
2661 return ActivityManager.START_DELIVERED_TO_TOP;
2662 }
2663 }
2664 // An existing activity is starting this new activity, so we want
2665 // to keep the new one in the same task as the one that is starting
2666 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002667 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002668 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002669 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002670
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002671 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002672 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002673 // task it has provided to us.
2674 if (isLockTaskModeViolation(inTask)) {
2675 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2676 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2677 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002678 if (overrideBounds) {
2679 inTask.updateOverrideConfiguration(newBounds);
2680 int stackId = inTask.getLaunchStackId();
2681 if (stackId != inTask.stack.mStackId) {
2682 moveTaskToStackUncheckedLocked(
2683 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2684 }
2685 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002686 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002687 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2688 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002689
2690 // Check whether we should actually launch the new activity in to the task,
2691 // or just reuse the current activity on top.
2692 ActivityRecord top = inTask.getTopActivity();
2693 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2694 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2695 || launchSingleTop || launchSingleTask) {
2696 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2697 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2698 // We don't need to start a new activity, and
2699 // the client said not to do anything if that
2700 // is the case, so this is it!
2701 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2702 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002703 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002704 return ActivityManager.START_DELIVERED_TO_TOP;
2705 }
2706 }
2707
Dianne Hackborn962d5352014-08-29 16:27:40 -07002708 if (!addingToTask) {
2709 // We don't actually want to have this activity added to the task, so just
2710 // stop here but still tell the caller that we consumed the intent.
2711 ActivityOptions.abort(options);
2712 return ActivityManager.START_TASK_TO_FRONT;
2713 }
2714
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002715 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002716 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002717 + " in explicit task " + r.task);
2718
Craig Mautner8849a5e2013-04-02 16:41:03 -07002719 } else {
2720 // This not being started from an existing activity, and not part
2721 // of a new task... just put it in the top task, though these days
2722 // this case should never happen.
Filip Gruszczynski80e29f12015-12-14 14:58:30 -08002723 targetStack = computeStackFocus(r, newTask, null /* bounds */, launchFlags);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002724 if (doResume) {
2725 targetStack.moveToFront("addingToTopTask");
2726 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002727 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002728 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002729 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002730 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002731 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002732 + " in new guessed " + r.task);
2733 }
2734
2735 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002736 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002737
Craig Mautner76e2a762014-06-24 09:05:43 -07002738 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2739 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2740 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002741 if (newTask) {
2742 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2743 }
2744 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002745 targetStack.mLastPausedActivity = null;
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002746 targetStack.startActivityLocked(r, newTask, keepCurTransition, options);
2747 if (doResume) {
2748 if (!launchTaskBehind) {
2749 mService.setFocusedActivityLocked(r, "startedActivity");
2750 }
2751 resumeTopActivitiesLocked(targetStack, r, options);
2752 } else {
2753 targetStack.addRecentActivityLocked(r);
Craig Mautnerbb742462014-07-07 15:28:55 -07002754 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002755 updateUserStackLocked(r.userId, targetStack);
Chong Zhangc806d902015-11-30 09:44:27 -08002756
2757 if (!r.task.mResizeable && isStackDockedInEffect(targetStack.mStackId)) {
2758 showNonResizeableDockToast(r.task.taskId);
2759 }
2760
Craig Mautner8849a5e2013-04-02 16:41:03 -07002761 return ActivityManager.START_SUCCESS;
2762 }
2763
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002764 /**
2765 * Decide whether the new activity should be inserted into an existing task. Returns null if not
2766 * or an ActivityRecord with the task into which the new activity should be added.
2767 */
2768 private ActivityRecord getReusableIntentActivity(ActivityRecord r, TaskRecord inTask, Intent intent,
2769 boolean launchSingleInstance, boolean launchSingleTask, int launchFlags) {
2770 // We may want to try to place the new activity in to an existing task. We always
2771 // do this if the target activity is singleTask or singleInstance; we will also do
2772 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2773 // us to still place it in a new task: multi task, always doc mode, or being asked to
2774 // launch this as a new task behind the current one.
2775 boolean putIntoExistingTask = ((launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0 &&
2776 (launchFlags & FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
2777 || launchSingleInstance || launchSingleTask;
2778 // If bring to front is requested, and no result is requested and we have not
2779 // been given an explicit task to launch in to, and
2780 // we can find a task that was started with this same
2781 // component, then instead of launching bring that one to the front.
2782 putIntoExistingTask &= inTask == null && r.resultTo == null;
2783 ActivityRecord intentActivity = null;
2784 if (putIntoExistingTask) {
2785 // See if there is a task to bring to the front. If this is
2786 // a SINGLE_INSTANCE activity, there can be one and only one
2787 // instance of it in the history, and it is always in its own
2788 // unique task, so we do a special search.
2789 intentActivity = launchSingleInstance ?
2790 findActivityLocked(intent, r.info) : findTaskLocked(r);
2791 }
2792 return intentActivity;
2793 }
2794
Craig Mautneree36c772014-07-16 14:56:05 -07002795 final void doPendingActivityLaunchesLocked(boolean doResume) {
2796 while (!mPendingActivityLaunches.isEmpty()) {
2797 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002798
2799 try {
2800 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Robert Carr13997f52015-10-23 13:13:39 -07002801 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002802 } catch (Exception e) {
Robert Carr13997f52015-10-23 13:13:39 -07002803 Slog.e(TAG, "Exception during pending activity launch pal=" + pal, e);
2804 pal.sendErrorResult(e.getMessage());
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002805 }
Craig Mautneree36c772014-07-16 14:56:05 -07002806 }
2807 }
2808
Craig Mautner7f13ed32014-07-28 14:00:35 -07002809 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002810 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2811 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002812 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002813 mPendingActivityLaunches.remove(palNdx);
2814 }
2815 }
2816 }
2817
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002818 void setLaunchSource(int uid) {
2819 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2820 }
2821
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002822 void acquireLaunchWakelock() {
2823 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2824 throw new IllegalStateException("Calling must be system uid");
2825 }
2826 mLaunchingActivity.acquire();
2827 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2828 // To be safe, don't allow the wake lock to be held for too long.
2829 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2830 }
2831 }
2832
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002833 /**
2834 * Called when the frontmost task is idle.
2835 * @return the state of mService.mBooting before this was called.
2836 */
2837 private boolean checkFinishBootingLocked() {
2838 final boolean booting = mService.mBooting;
2839 boolean enableScreen = false;
2840 mService.mBooting = false;
2841 if (!mService.mBooted) {
2842 mService.mBooted = true;
2843 enableScreen = true;
2844 }
2845 if (booting || enableScreen) {
2846 mService.postFinishBooting(booting, enableScreen);
2847 }
2848 return booting;
2849 }
2850
Craig Mautnerf3333272013-04-22 10:55:53 -07002851 // Checked.
2852 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2853 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002854 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002855
Craig Mautnerf3333272013-04-22 10:55:53 -07002856 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002857 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002858 int NS = 0;
2859 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002860 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002861 boolean activityRemoved = false;
2862
Wale Ogunwale7d701172015-03-11 15:36:30 -07002863 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002864 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002865 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2866 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002867 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2868 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002869 if (fromTimeout) {
2870 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002871 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002872
2873 // This is a hack to semi-deal with a race condition
2874 // in the client where it can be constructed with a
2875 // newer configuration from when we asked it to launch.
2876 // We'll update with whatever configuration it now says
2877 // it used to launch.
2878 if (config != null) {
2879 r.configuration = config;
2880 }
2881
2882 // We are now idle. If someone is waiting for a thumbnail from
2883 // us, we can now deliver.
2884 r.idle = true;
2885
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002886 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002887 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002888 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002889 }
2890 }
2891
2892 if (allResumedActivitiesIdle()) {
2893 if (r != null) {
2894 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002895 }
2896
2897 if (mLaunchingActivity.isHeld()) {
2898 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2899 if (VALIDATE_WAKE_LOCK_CALLER &&
2900 Binder.getCallingUid() != Process.myUid()) {
2901 throw new IllegalStateException("Calling must be system uid");
2902 }
2903 mLaunchingActivity.release();
2904 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002905 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002906 }
2907
2908 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002909 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07002910 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002911 if ((NF = mFinishingActivities.size()) > 0) {
2912 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002913 mFinishingActivities.clear();
2914 }
2915
Craig Mautnerf3333272013-04-22 10:55:53 -07002916 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002917 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002918 mStartingUsers.clear();
2919 }
2920
Craig Mautnerf3333272013-04-22 10:55:53 -07002921 // Stop any activities that are scheduled to do so but have been
2922 // waiting for the next one to start.
2923 for (int i = 0; i < NS; i++) {
2924 r = stops.get(i);
2925 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002926 if (stack != null) {
2927 if (r.finishing) {
2928 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2929 } else {
2930 stack.stopActivityLocked(r);
2931 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002932 }
2933 }
2934
2935 // Finish any activities that are scheduled to do so but have been
2936 // waiting for the next one to start.
2937 for (int i = 0; i < NF; i++) {
2938 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002939 final ActivityStack stack = r.task.stack;
2940 if (stack != null) {
2941 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2942 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002943 }
2944
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002945 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002946 // Complete user switch
2947 if (startingUsers != null) {
2948 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002949 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002950 }
2951 }
2952 // Complete starting up of background users
2953 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002954 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002955 mStartingBackgroundUsers.clear();
2956 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002957 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002958 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002959 }
2960 }
2961
2962 mService.trimApplications();
2963 //dump();
2964 //mWindowManager.dump();
2965
Craig Mautnerf3333272013-04-22 10:55:53 -07002966 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002967 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002968 }
2969
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002970 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002971 }
2972
Craig Mautner8e569572013-10-11 17:36:59 -07002973 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002974 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2976 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002977 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2978 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2979 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002980 }
Craig Mautner19091252013-10-05 00:03:53 -07002981 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002982 }
2983
2984 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002985 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2986 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002987 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2988 stacks.get(stackNdx).closeSystemDialogsLocked();
2989 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002990 }
2991 }
2992
Craig Mautner93529a42013-10-04 15:03:13 -07002993 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002994 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002995 }
2996
Craig Mautner8d341ef2013-03-26 09:03:27 -07002997 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002998 * Update the last used stack id for non-current user (current user's last
2999 * used stack is the focused stack)
3000 */
3001 void updateUserStackLocked(int userId, ActivityStack stack) {
3002 if (userId != mCurrentUser) {
3003 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
3004 }
3005 }
3006
3007 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07003008 * @return true if some activity was finished (or would have finished if doit were true).
3009 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07003010 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
3011 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003012 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003013 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3014 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08003015 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003016 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07003017 if (stack.finishDisabledPackageActivitiesLocked(
3018 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003019 didSomething = true;
3020 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003021 }
3022 }
3023 return didSomething;
3024 }
3025
Dianne Hackborna413dc02013-07-12 12:02:55 -07003026 void updatePreviousProcessLocked(ActivityRecord r) {
3027 // Now that this process has stopped, we may want to consider
3028 // it to be the previous app to try to keep around in case
3029 // the user wants to return to it.
3030
3031 // First, found out what is currently the foreground app, so that
3032 // we don't blow away the previous app if this activity is being
3033 // hosted by the process that is actually still the foreground.
3034 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08003035 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3036 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003037 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3038 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003039 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003040 if (stack.mResumedActivity != null) {
3041 fgApp = stack.mResumedActivity.app;
3042 } else if (stack.mPausingActivity != null) {
3043 fgApp = stack.mPausingActivity.app;
3044 }
3045 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07003046 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07003047 }
3048 }
3049
3050 // Now set this one as the previous process, only if that really
3051 // makes sense to.
3052 if (r.app != null && fgApp != null && r.app != fgApp
3053 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07003054 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07003055 mService.mPreviousProcess = r.app;
3056 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
3057 }
3058 }
3059
Craig Mautner05d29032013-05-03 13:40:13 -07003060 boolean resumeTopActivitiesLocked() {
3061 return resumeTopActivitiesLocked(null, null, null);
3062 }
3063
Chong Zhang280d3322015-11-03 17:27:26 -08003064 boolean resumeTopActivitiesLocked(
3065 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Craig Mautner05d29032013-05-03 13:40:13 -07003066 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003067 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07003068 }
Craig Mautner12ff7392014-02-21 21:08:00 -08003069 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07003070 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003071 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08003072 result = targetStack.resumeTopActivityLocked(target, targetOptions);
3073 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003074
Craig Mautnere0a38842013-12-16 16:14:02 -08003075 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3076 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003077 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3078 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08003079 if (stack == targetStack) {
3080 // Already started above.
3081 continue;
3082 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003083 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08003084 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003085 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003086 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003087 }
Craig Mautner05d29032013-05-03 13:40:13 -07003088 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003089 }
3090
Todd Kennedy539db512014-12-15 09:57:55 -08003091 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003092 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3093 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003094 final int numStacks = stacks.size();
3095 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3096 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08003097 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003098 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003099 }
3100 }
3101
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003102 void finishVoiceTask(IVoiceInteractionSession session) {
3103 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3104 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3105 final int numStacks = stacks.size();
3106 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3107 final ActivityStack stack = stacks.get(stackNdx);
3108 stack.finishVoiceTask(session);
3109 }
3110 }
3111 }
3112
Chong Zhang280d3322015-11-03 17:27:26 -08003113 void findTaskToMoveToFrontLocked(
3114 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003115 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
3116 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003117 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003118 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3119 // Caller wants the home activity moved with it. To accomplish this,
3120 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07003121 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08003122 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003123 if (task.stack == null) {
3124 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
3125 + task + " to front. Stack is null");
3126 return;
3127 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07003128
Chong Zhang0fa656b2015-08-31 15:17:21 -07003129 if (task.mResizeable && options != null) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08003130 if (canUseActivityOptionsLaunchBounds(options)) {
3131 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07003132 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08003133 final int stackId = task.getLaunchStackId();
3134 if (stackId != task.stack.mStackId) {
3135 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
3136 // moveTaskToStackUncheckedLocked() should already placed the task on top,
3137 // still need moveTaskToFrontLocked() below for any transition settings.
3138 }
3139 // WM resizeTask must be done after the task is moved to the correct stack,
3140 // because Task's setBounds() also updates dim layer's bounds, but that has
3141 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07003142 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
3143 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07003144 }
3145 }
3146
Chong Zhangdb20b5f2015-10-23 14:01:43 -07003147 final ActivityRecord r = task.getTopActivity();
3148 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
3149 r == null ? null : r.appTimeTracker, reason);
3150
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003151 if (DEBUG_STACK) Slog.d(TAG_STACK,
3152 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003153 }
3154
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08003155 private boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
3156 // We use the launch bounds in the activity options is the device supports freeform
3157 // window management.
3158 return options.hasLaunchBounds() && mService.mSupportsFreeformWindowManagement;
3159 }
3160
Craig Mautner967212c2013-04-13 21:10:58 -07003161 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003162 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003163 }
3164
3165 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003166 ActivityContainer activityContainer = mActivityContainers.get(stackId);
3167 if (activityContainer != null) {
3168 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003169 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07003170 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003171 return null;
3172 }
3173 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003174 }
3175
Craig Mautner967212c2013-04-13 21:10:58 -07003176 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003177 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003178 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3179 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003180 }
3181 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07003182 }
3183
Craig Mautner4a1cb222013-12-04 16:14:06 -08003184 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003185 ActivityRecord homeActivity = getHomeActivity();
3186 if (homeActivity != null) {
3187 return homeActivity.appToken;
3188 }
3189 return null;
3190 }
3191
3192 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07003193 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003194 }
3195
3196 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003197 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
3198 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
3199 final TaskRecord task = tasks.get(taskNdx);
3200 if (task.isHomeTask()) {
3201 final ArrayList<ActivityRecord> activities = task.mActivities;
3202 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3203 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003204 if (r.isHomeActivity()
3205 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003206 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003207 }
3208 }
3209 }
3210 }
3211 return null;
3212 }
3213
Chong Zhangb15758a2015-11-17 12:12:03 -08003214 /**
3215 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
3216 * the docked stack itself, or if it's side-by-side to the docked stack.
3217 */
3218 boolean isStackDockedInEffect(int stackId) {
3219 return stackId == DOCKED_STACK_ID ||
3220 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
3221 }
3222
Todd Kennedyca4d8422015-01-15 15:19:22 -08003223 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08003224 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003225 ActivityContainer activityContainer =
3226 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003227 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003228 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3229 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003230 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003231 return activityContainer;
3232 }
3233
Craig Mautner34b73df2014-01-12 21:11:08 -08003234 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003235 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
3236 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
3237 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003238 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
3239 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003240 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08003241 }
3242 }
3243
Craig Mautner95da1082014-02-24 17:54:35 -08003244 void deleteActivityContainer(IActivityContainer container) {
3245 ActivityContainer activityContainer = (ActivityContainer)container;
3246 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003247 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3248 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08003249 final int stackId = activityContainer.mStackId;
3250 mActivityContainers.remove(stackId);
3251 mWindowManager.removeStack(stackId);
3252 }
3253 }
3254
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003255 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
3256 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003257 final ActivityStack stack = getStack(stackId);
3258 if (stack == null) {
3259 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
3260 return;
3261 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003262
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003263 if (!allowResizeInDockedMode
3264 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
3265 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
3266 // stack size changing so things don't get out of sync.
3267 return;
3268 }
3269
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003270 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003271 mWindowManager.deferSurfaceLayout();
3272 try {
Wale Ogunwaleb4ec0a32015-12-14 10:31:43 -08003273
3274 if (bounds != null && mWindowManager.isFullscreenBounds(stackId, bounds)) {
3275 // The bounds passed in corresponds to the fullscreen bounds which we normally
3276 // represent with null. Go ahead and set it to null so that all tasks configuration
3277 // can have the right fullscreen state.
3278 bounds = null;
3279 }
3280
Jorim Jaggic4025202015-10-22 16:43:34 +02003281 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003282
Jorim Jaggic4025202015-10-22 16:43:34 +02003283 mTmpBounds.clear();
3284 mTmpConfigs.clear();
3285 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3286 for (int i = tasks.size() - 1; i >= 0; i--) {
3287 TaskRecord task = tasks.get(i);
3288 if (task.mResizeable) {
3289 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3290 // For freeform stack we don't adjust the size of the tasks to match that
3291 // of the stack, but we do try to make sure the tasks are still contained
3292 // with the bounds of the stack.
3293 tempRect2.set(task.mBounds);
3294 fitWithinBounds(tempRect2, bounds);
3295 task.updateOverrideConfiguration(tempRect2);
3296 } else {
3297 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003298 }
3299 }
3300
Jorim Jaggic4025202015-10-22 16:43:34 +02003301 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3302 mTmpBounds.put(task.taskId, task.mBounds);
3303 }
3304 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3305 if (stack.mStackId == DOCKED_STACK_ID) {
3306 // Dock stack funness...Yay!
3307 if (stack.mFullscreen) {
3308 // The dock stack went fullscreen which is kinda like dismissing it.
3309 // In this case we make all other static stacks fullscreen and move all
3310 // docked stack tasks to the fullscreen stack.
3311 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003312 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003313 resizeStackLocked(i, null, preserveWindows, true);
3314 }
3315 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003316
Jorim Jaggic4025202015-10-22 16:43:34 +02003317 final int count = tasks.size();
3318 for (int i = 0; i < count; i++) {
3319 moveTaskToStackLocked(tasks.get(i).taskId,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003320 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
Jorim Jaggi55387522015-11-24 18:21:10 -08003321 false /* animate */);
Jorim Jaggic4025202015-10-22 16:43:34 +02003322 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003323
Jorim Jaggic4025202015-10-22 16:43:34 +02003324 // stack shouldn't contain anymore activities, so nothing to resume.
3325 r = null;
3326 } else {
3327 // Docked stacks occupy a dedicated region on screen so the size of all other
3328 // static stacks need to be adjusted so they don't overlap with the docked stack.
3329 // We get the bounds to use from window manager which has been adjusted for any
3330 // screen controls and is also the same for all stacks.
3331 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3332
3333 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003334 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003335 ActivityStack otherStack = getStack(i);
3336 if (otherStack != null) {
3337 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3338 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003339 }
3340 }
3341 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003342 // Since we are resizing the stack, all other operations should strive to preserve
3343 // windows.
3344 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003345 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003346 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003347
Jorim Jaggic4025202015-10-22 16:43:34 +02003348 if (r != null) {
3349 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3350 // And we need to make sure at this point that all other activities
3351 // are made visible with the correct configuration.
3352 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3353 if (!updated) {
3354 resumeTopActivitiesLocked(stack, null, null);
3355 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003356 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003357 } finally {
3358 mWindowManager.continueSurfaceLayout();
3359 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003360 }
3361 }
3362
Chong Zhang6de2ae82015-09-30 18:25:21 -07003363 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003364 if (!task.mResizeable) {
3365 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3366 return;
3367 }
3368
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003369 adjustForMinimalTaskDimensions(task, bounds);
3370
Chong Zhang87b21722015-09-21 15:39:51 -07003371 // If this is a forced resize, let it go through even if the bounds is not changing,
3372 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003373 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003374 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003375 // Nothing to do here...
3376 return;
3377 }
3378
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003379 if (!mWindowManager.isValidTaskId(task.taskId)) {
3380 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003381 // All we can do for now is update the bounds so it can be used when the task is
3382 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003383 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003384 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3385 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003386 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003387 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003388 return;
3389 }
3390
Chong Zhang6de2ae82015-09-30 18:25:21 -07003391 // Do not move the task to another stack here.
3392 // This method assumes that the task is already placed in the right stack.
3393 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003394
Chong Zhang6de2ae82015-09-30 18:25:21 -07003395 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003396
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003397 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003398 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003399 // way and the activity was kept the way it was. If it's false, it means the activity had
3400 // to be relaunched due to configuration change.
3401 boolean kept = true;
3402 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003403 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003404 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003405 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003406 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003407 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003408 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003409 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003410 resumeTopActivitiesLocked(stack, null, null);
3411 }
3412 }
3413 }
Chong Zhang87b21722015-09-21 15:39:51 -07003414 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003415
3416 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003417 }
3418
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003419 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3420 if (bounds == null) {
3421 return;
3422 }
3423 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3424 : task.mMinimalSize;
3425 final boolean adjustWidth = minimalSize > bounds.width();
3426 final boolean adjustHeight = minimalSize > bounds.height();
3427 if (!(adjustWidth || adjustHeight)) {
3428 return;
3429 }
3430 Rect taskBounds = task.mBounds;
3431 if (adjustWidth) {
3432 if (taskBounds != null && bounds.right == taskBounds.right) {
3433 bounds.left = bounds.right - minimalSize;
3434 } else {
3435 // Either left bounds match, or neither match, or the previous bounds were
3436 // fullscreen and we default to keeping left.
3437 bounds.right = bounds.left + minimalSize;
3438 }
3439 }
3440 if (adjustHeight) {
3441 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3442 bounds.top = bounds.bottom - minimalSize;
3443 } else {
3444 // Either top bounds match, or neither match, or the previous bounds were
3445 // fullscreen and we default to keeping top.
3446 bounds.bottom = bounds.top + minimalSize;
3447 }
3448 }
3449 }
3450
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003451 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003452 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3453 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003454 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003455 }
3456
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003457 ActivityContainer activityContainer = new ActivityContainer(stackId);
3458 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003459 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003460 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003461 }
3462
3463 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003464 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003465 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3466 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003467 break;
3468 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003469 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003470 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003471 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003472 }
3473
Chong Zhang5dcb2752015-08-18 13:50:26 -07003474 /**
3475 * Restores a recent task to a stack
3476 * @param task The recent task to be restored.
3477 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07003478 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003479 * @return true if the task has been restored successfully.
3480 */
3481 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3482 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003483 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003484 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003485 if (task.stack != null) {
3486 // Task has already been restored once. See if we need to do anything more
3487 if (task.stack.mStackId == stackId) {
3488 // Nothing else to do since it is already restored in the right stack.
3489 return true;
3490 }
3491 // Remove current stack association, so we can re-associate the task with the
3492 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003493 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003494 }
3495
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003496 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003497 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003498
3499 if (stack == null) {
3500 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003501 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3502 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003503 return false;
3504 }
3505
Wale Ogunwale5f986092015-12-04 15:35:38 -08003506 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003507 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3508 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003509 final ArrayList<ActivityRecord> activities = task.mActivities;
3510 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003511 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003512 }
3513 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003514 }
3515
Wale Ogunwale040b4702015-08-06 18:10:50 -07003516 /**
3517 * Moves the specified task record to the input stack id.
3518 * WARNING: This method performs an unchecked/raw move of the task and
3519 * can leave the system in an unstable state if used incorrectly.
3520 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3521 * to a stack.
3522 * @param task Task to move.
3523 * @param stackId Id of stack to move task to.
3524 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003525 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003526 * @param reason Reason the task is been moved.
3527 * @return The stack the task was moved to.
3528 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003529 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003530 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3531 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003532 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003533 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3534
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003535 final boolean resizeable = task.mResizeable;
3536 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3537 // if a docked stack is created below which will lead to the stack we are moving from and
3538 // its resizeable tasks being resized.
3539 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003540 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003541 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003542 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003543 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003544
3545 // If the task had focus before (or we're requested to move focus),
3546 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003547 stack.setFocusAndResumeStateIfNeeded(
3548 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003549
Wale Ogunwale040b4702015-08-06 18:10:50 -07003550 return stack;
3551 }
3552
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003553 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003554 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003555 final TaskRecord task = anyTaskForIdLocked(taskId);
3556 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003557 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003558 return;
3559 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003560
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003561 if (task.stack != null && task.stack.mStackId == stackId) {
3562 // You are already in the right stack silly...
3563 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
3564 return;
3565 }
3566
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003567 final ActivityRecord topActivity = task.getTopActivity();
3568 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003569 // We are about to relaunch the activity because its configuration changed due to
3570 // being maximized, i.e. size change. The activity will first remove the old window
3571 // and then add a new one. This call will tell window manager about this, so it can
3572 // preserve the old window until the new one is drawn. This prevents having a gap
3573 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003574 // entrance of the new window to be properly animated.
Jorim Jaggi030979c2015-11-20 15:14:43 -08003575 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003576 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08003577 final ActivityStack stack = moveTaskToStackUncheckedLocked(
3578 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003579
Jorim Jaggi55387522015-11-24 18:21:10 -08003580 if (!animate) {
3581 stack.mNoAnimActivities.add(topActivity);
3582 }
3583
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003584 // Make sure the task has the appropriate bounds/size for the stack it is in.
3585 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003586 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003587 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3588 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003589 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3590 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003591 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003592 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003593 }
3594
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003595 // The task might have already been running and its visibility needs to be synchronized with
3596 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003597 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003598 resumeTopActivitiesLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08003599
3600 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08003601 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003602 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003603 }
3604
Wale Ogunwale079a0042015-10-24 11:44:07 -07003605 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3606 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3607 if (stack == null) {
3608 throw new IllegalArgumentException(
3609 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3610 }
3611
3612 final ActivityRecord r = stack.topRunningActivityLocked();
3613 if (r == null) {
3614 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3615 + " in stack=" + stack);
3616 return false;
3617 }
3618
Youngsang Choba80ba52015-12-11 14:32:35 -08003619 if (!mService.mForceResizableActivities && !r.info.supportsPip) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003620 Slog.w(TAG,
3621 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003622 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003623 return false;
3624 }
3625
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003626 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", bounds);
3627 return true;
3628 }
3629
3630 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003631 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003632 if (task.mActivities.size() == 1) {
3633 // 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 -08003634 // stack without re-parenting the activity in a different task.
3635 moveTaskToStackLocked(
3636 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003637 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08003638 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
3639 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003640 }
3641
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003642 if (bounds != null) {
3643 resizeStackLocked(PINNED_STACK_ID, bounds, !PRESERVE_WINDOWS, true);
3644 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07003645
3646 // The task might have already been running and its visibility needs to be synchronized with
3647 // the visibility of the stack / windows.
3648 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3649 resumeTopActivitiesLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003650 }
3651
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003652 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3653 final TaskRecord task = anyTaskForIdLocked(taskId);
3654 if (task == null) {
3655 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3656 return;
3657 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08003658 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
3659
3660 task.updateOverrideConfigurationForStack(stack);
3661
3662 mWindowManager.positionTaskInStack(
3663 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003664 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003665 // The task might have already been running and its visibility needs to be synchronized with
3666 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003667 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003668 resumeTopActivitiesLocked();
3669 }
3670
Craig Mautnerac6f8432013-07-17 13:24:59 -07003671 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003672 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3674 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003675 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3676 final ActivityStack stack = stacks.get(stackNdx);
3677 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003678 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003679 continue;
3680 }
3681 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003682 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3683 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003684 continue;
3685 }
3686 final ActivityRecord ar = stack.findTaskLocked(r);
3687 if (ar != null) {
3688 return ar;
3689 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003690 }
3691 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003692 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003693 return null;
3694 }
3695
3696 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003697 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3698 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003699 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3700 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3701 if (ar != null) {
3702 return ar;
3703 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003704 }
3705 }
3706 return null;
3707 }
3708
Craig Mautner8d341ef2013-03-26 09:03:27 -07003709 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003710 scheduleSleepTimeout();
3711 if (!mGoingToSleep.isHeld()) {
3712 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003713 if (mLaunchingActivity.isHeld()) {
3714 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3715 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003716 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003717 mLaunchingActivity.release();
3718 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003719 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003720 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003721 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003722 }
3723
3724 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003725 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003726
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003727 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003728 final long endTime = System.currentTimeMillis() + timeout;
3729 while (true) {
3730 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003731 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3732 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003733 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3734 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3735 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003736 }
3737 if (cantShutdown) {
3738 long timeRemaining = endTime - System.currentTimeMillis();
3739 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003740 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003741 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003742 } catch (InterruptedException e) {
3743 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003744 } else {
3745 Slog.w(TAG, "Activity manager shutdown timed out");
3746 timedout = true;
3747 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003748 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003749 } else {
3750 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003751 }
3752 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003753
3754 // Force checkReadyForSleep to complete.
3755 mSleepTimeout = true;
3756 checkReadyForSleepLocked();
3757
Craig Mautner8d341ef2013-03-26 09:03:27 -07003758 return timedout;
3759 }
3760
3761 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003762 removeSleepTimeouts();
3763 if (mGoingToSleep.isHeld()) {
3764 mGoingToSleep.release();
3765 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003766 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3767 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003768 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3769 final ActivityStack stack = stacks.get(stackNdx);
3770 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003771 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003772 resumeTopActivitiesLocked();
3773 }
Craig Mautner5314a402013-09-26 12:40:16 -07003774 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003775 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003776 mGoingToSleepActivities.clear();
3777 }
3778
3779 void activitySleptLocked(ActivityRecord r) {
3780 mGoingToSleepActivities.remove(r);
3781 checkReadyForSleepLocked();
3782 }
3783
3784 void checkReadyForSleepLocked() {
3785 if (!mService.isSleepingOrShuttingDown()) {
3786 // Do not care.
3787 return;
3788 }
3789
3790 if (!mSleepTimeout) {
3791 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003792 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3793 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3795 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3796 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003797 }
3798
3799 if (mStoppingActivities.size() > 0) {
3800 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003801 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003802 + mStoppingActivities.size() + " activities");
3803 scheduleIdleLocked();
3804 dontSleep = true;
3805 }
3806
3807 if (mGoingToSleepActivities.size() > 0) {
3808 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003809 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003810 + mGoingToSleepActivities.size() + " activities");
3811 dontSleep = true;
3812 }
3813
3814 if (dontSleep) {
3815 return;
3816 }
3817 }
3818
Craig Mautnere0a38842013-12-16 16:14:02 -08003819 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3820 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003821 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3822 stacks.get(stackNdx).goToSleep();
3823 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003824 }
3825
3826 removeSleepTimeouts();
3827
3828 if (mGoingToSleep.isHeld()) {
3829 mGoingToSleep.release();
3830 }
3831 if (mService.mShuttingDown) {
3832 mService.notifyAll();
3833 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003834 }
3835
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003836 boolean reportResumedActivityLocked(ActivityRecord r) {
3837 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003838 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003839 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003840 }
3841 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003842 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003843 mWindowManager.executeAppTransition();
3844 return true;
3845 }
3846 return false;
3847 }
3848
Craig Mautner8d341ef2013-03-26 09:03:27 -07003849 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003850 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3851 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003852 int stackNdx = stacks.size() - 1;
3853 while (stackNdx >= 0) {
3854 stacks.get(stackNdx).handleAppCrashLocked(app);
3855 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003856 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003857 }
3858 }
3859
Jose Lima4b6c6692014-08-12 17:41:12 -07003860 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003861 final ActivityStack stack = r.task.stack;
3862 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003863 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3864 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003865 return false;
3866 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003867 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003868 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3869 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003870
3871 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003872 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003873 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003874 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003875 return true;
3876 }
3877
3878 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003879 if (visible && top.fullscreen) {
3880 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003881 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3882 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3883 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3884 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003885 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003886 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3887 // Only the activity set as currently visible behind should actively reset its
3888 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003889 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3890 "requestVisibleBehind: returning visible=" + visible
3891 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3892 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003893 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003894 }
3895
Jose Lima4b6c6692014-08-12 17:41:12 -07003896 stack.setVisibleBehindActivity(visible ? r : null);
3897 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003898 // Make the activity immediately above r opaque.
3899 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3900 if (next != null) {
3901 mService.convertFromTranslucent(next.appToken);
3902 }
3903 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003904 if (top.app != null && top.app.thread != null) {
3905 // Notify the top app of the change.
3906 try {
3907 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3908 } catch (RemoteException e) {
3909 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003910 }
3911 return true;
3912 }
3913
Craig Mautnerbb742462014-07-07 15:28:55 -07003914 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3915 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3916 r.mLaunchTaskBehind = false;
3917 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08003918 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003919 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003920 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003921 mWindowManager.setAppVisibility(r.appToken, false);
3922 }
3923
3924 void scheduleLaunchTaskBehindComplete(IBinder token) {
3925 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3926 }
3927
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003928 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3929 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003930 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003931 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3932 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003933 final int topStackNdx = stacks.size() - 1;
3934 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3935 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003936 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003937 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003938 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003939 }
3940
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003941 void invalidateTaskLayers() {
3942 mTaskLayersChanged = true;
3943 }
3944
3945 void rankTaskLayersIfNeeded() {
3946 if (!mTaskLayersChanged) {
3947 return;
3948 }
3949 mTaskLayersChanged = false;
3950 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3951 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3952 int baseLayer = 0;
3953 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3954 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3955 }
3956 }
3957 }
3958
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003959 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3960 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3961 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3962 final int topStackNdx = stacks.size() - 1;
3963 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3964 final ActivityStack stack = stacks.get(stackNdx);
3965 stack.clearOtherAppTimeTrackers(except);
3966 }
3967 }
3968 }
3969
Craig Mautner8d341ef2013-03-26 09:03:27 -07003970 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003971 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3972 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003973 final int numStacks = stacks.size();
3974 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3975 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003976 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003977 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003978 }
3979 }
3980
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003981 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3982 // Examine all activities currently running in the process.
3983 TaskRecord firstTask = null;
3984 // Tasks is non-null only if two or more tasks are found.
3985 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003986 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3987 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003988 ActivityRecord r = app.activities.get(i);
3989 // First, if we find an activity that is in the process of being destroyed,
3990 // then we just aren't going to do anything for now; we want things to settle
3991 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003992 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003993 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003994 return;
3995 }
3996 // Don't consider any activies that are currently not in a state where they
3997 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003998 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3999 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004000 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004001 continue;
4002 }
4003 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004004 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004005 + " from " + r);
4006 if (firstTask == null) {
4007 firstTask = r.task;
4008 } else if (firstTask != r.task) {
4009 if (tasks == null) {
4010 tasks = new ArraySet<>();
4011 tasks.add(firstTask);
4012 }
4013 tasks.add(r.task);
4014 }
4015 }
4016 }
4017 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004018 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004019 return;
4020 }
4021 // If we have activities in multiple tasks that are in a position to be destroyed,
4022 // let's iterate through the tasks and release the oldest one.
4023 final int numDisplays = mActivityDisplays.size();
4024 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
4025 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4026 // Step through all stacks starting from behind, to hit the oldest things first.
4027 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
4028 final ActivityStack stack = stacks.get(stackNdx);
4029 // Try to release activities in this stack; if we manage to, we are done.
4030 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
4031 return;
4032 }
4033 }
4034 }
4035 }
4036
Amith Yamasani37a40c22015-06-17 13:25:42 -07004037 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004038 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07004039 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07004040 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07004041
Craig Mautner858d8a62013-04-23 17:08:34 -07004042 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08004043 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4044 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004045 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004046 final ActivityStack stack = stacks.get(stackNdx);
4047 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00004048 TaskRecord task = stack.topTask();
4049 if (task != null) {
4050 mWindowManager.moveTaskToTop(task.taskId);
4051 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004052 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07004053 }
Craig Mautner858d8a62013-04-23 17:08:34 -07004054
Craig Mautner4f1df4f2013-10-15 15:44:14 -07004055 ActivityStack stack = getStack(restoreStackId);
4056 if (stack == null) {
4057 stack = mHomeStack;
4058 }
4059 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08004060 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004061 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08004062 } else {
4063 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08004064 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08004065 }
Craig Mautner93529a42013-10-04 15:03:13 -07004066 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07004067 }
4068
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07004069 /**
4070 * Add background users to send boot completed events to.
4071 * @param userId The user being started in the background
4072 * @param uss The state object for the user.
4073 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07004074 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07004075 mStartingBackgroundUsers.add(uss);
4076 }
4077
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07004078 /** Checks whether the userid is a profile of the current user. */
4079 boolean isCurrentProfileLocked(int userId) {
4080 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07004081 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07004082 }
4083
Chong Zhang45c25ce2015-08-10 22:18:26 -07004084 /** Checks whether the activity should be shown for current user. */
4085 boolean okToShowLocked(ActivityRecord r) {
4086 return r != null && (isCurrentProfileLocked(r.userId)
4087 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
4088 }
4089
Craig Mautnerde4ef022013-04-07 19:01:33 -07004090 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07004091 ArrayList<ActivityRecord> stops = null;
4092
4093 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08004094 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
4095 ActivityRecord s = mStoppingActivities.get(activityNdx);
4096 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004097 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08004098 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
4099 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07004100 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004101 if (s.finishing) {
4102 // If this activity is finishing, it is sitting on top of
4103 // everyone else but we now know it is no longer needed...
4104 // so get rid of it. Otherwise, we need to go through the
4105 // normal flow and hide it once we determine that it is
4106 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004107 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004108 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004109 }
4110 }
Craig Mautner8c14c152015-01-15 17:32:07 -08004111 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004112 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004113 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08004114 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07004115 }
4116 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08004117 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004118 }
4119 }
4120
4121 return stops;
4122 }
4123
Craig Mautnercf910b02013-04-23 11:23:27 -07004124 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004125 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4126 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4127 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4128 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004129 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004130 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004131 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004132 if (r == null) Slog.e(TAG,
4133 "validateTop...: null top activity, stack=" + stack);
4134 else {
4135 final ActivityRecord pausing = stack.mPausingActivity;
4136 if (pausing != null && pausing == r) Slog.e(TAG,
4137 "validateTop...: top stack has pausing activity r=" + r
4138 + " state=" + state);
4139 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
4140 "validateTop...: activity in front not resumed r=" + r
4141 + " state=" + state);
4142 }
Craig Mautnercf910b02013-04-23 11:23:27 -07004143 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004144 final ActivityRecord resumed = stack.mResumedActivity;
4145 if (resumed != null && resumed == r) Slog.e(TAG,
4146 "validateTop...: back stack has resumed activity r=" + r
4147 + " state=" + state);
4148 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
4149 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07004150 }
4151 }
4152 }
Craig Mautner76ea2242013-05-15 11:40:05 -07004153 }
4154
Craig Mautnere0570202015-05-13 13:06:11 -07004155 private String lockTaskModeToString() {
4156 switch (mLockTaskModeState) {
4157 case LOCK_TASK_MODE_LOCKED:
4158 return "LOCKED";
4159 case LOCK_TASK_MODE_PINNED:
4160 return "PINNED";
4161 case LOCK_TASK_MODE_NONE:
4162 return "NONE";
4163 default: return "unknown=" + mLockTaskModeState;
4164 }
4165 }
4166
Craig Mautner27084302013-03-25 08:05:25 -07004167 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07004168 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004169 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07004170 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
4171 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
4172 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08004173 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07004174 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
4175 final SparseArray<String[]> packages = mService.mLockTaskPackages;
4176 if (packages.size() > 0) {
4177 pw.println(" mLockTaskPackages (userId:packages)=");
4178 for (int i = 0; i < packages.size(); ++i) {
4179 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
4180 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
4181 }
4182 }
4183 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07004184 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004185
Craig Mautner20e72272013-04-01 13:45:53 -07004186 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004187 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07004188 }
4189
Dianne Hackborn390517b2013-05-30 15:03:32 -07004190 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
4191 boolean needSep, String prefix) {
4192 if (activity != null) {
4193 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
4194 if (needSep) {
4195 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07004196 }
4197 pw.print(prefix);
4198 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004199 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004200 }
4201 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004202 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004203 }
4204
Craig Mautner8d341ef2013-03-26 09:03:27 -07004205 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4206 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004207 boolean printed = false;
4208 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08004209 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4210 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004211 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07004212 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08004213 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07004214 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004215 final ActivityStack stack = stacks.get(stackNdx);
4216 StringBuilder stackHeader = new StringBuilder(128);
4217 stackHeader.append(" Stack #");
4218 stackHeader.append(stack.mStackId);
4219 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004220 stackHeader.append("\n");
4221 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
4222 stackHeader.append("\n");
4223 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004224 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
4225 needSep, stackHeader.toString());
4226 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
4227 !dumpAll, false, dumpPackage, true,
4228 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004229
Craig Mautner4a1cb222013-12-04 16:14:06 -08004230 needSep = printed;
4231 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4232 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004233 if (pr) {
4234 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004235 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004236 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004237 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
4238 " mResumedActivity: ");
4239 if (pr) {
4240 printed = true;
4241 needSep = false;
4242 }
4243 if (dumpAll) {
4244 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4245 " mLastPausedActivity: ");
4246 if (pr) {
4247 printed = true;
4248 needSep = true;
4249 }
4250 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4251 needSep, " mLastNoHistoryActivity: ");
4252 }
4253 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004254 }
4255 }
4256
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004257 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4258 false, dumpPackage, true, " Activities waiting to finish:", null);
4259 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4260 false, dumpPackage, true, " Activities waiting to stop:", null);
4261 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
4262 false, dumpPackage, true, " Activities waiting for another to become visible:",
4263 null);
4264 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4265 false, dumpPackage, true, " Activities waiting to sleep:", null);
4266 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4267 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004268
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004269 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004270 }
4271
Dianne Hackborn390517b2013-05-30 15:03:32 -07004272 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004273 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004274 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004275 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004276 String innerPrefix = null;
4277 String[] args = null;
4278 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004279 for (int i=list.size()-1; i>=0; i--) {
4280 final ActivityRecord r = list.get(i);
4281 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4282 continue;
4283 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004284 if (innerPrefix == null) {
4285 innerPrefix = prefix + " ";
4286 args = new String[0];
4287 }
4288 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004289 final boolean full = !brief && (complete || !r.isInHistory());
4290 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004291 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004292 needNL = false;
4293 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004294 if (header1 != null) {
4295 pw.println(header1);
4296 header1 = null;
4297 }
4298 if (header2 != null) {
4299 pw.println(header2);
4300 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004301 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004302 if (lastTask != r.task) {
4303 lastTask = r.task;
4304 pw.print(prefix);
4305 pw.print(full ? "* " : " ");
4306 pw.println(lastTask);
4307 if (full) {
4308 lastTask.dump(pw, prefix + " ");
4309 } else if (complete) {
4310 // Complete + brief == give a summary. Isn't that obvious?!?
4311 if (lastTask.intent != null) {
4312 pw.print(prefix); pw.print(" ");
4313 pw.println(lastTask.intent.toInsecureStringWithClip());
4314 }
4315 }
4316 }
4317 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4318 pw.print(" #"); pw.print(i); pw.print(": ");
4319 pw.println(r);
4320 if (full) {
4321 r.dump(pw, innerPrefix);
4322 } else if (complete) {
4323 // Complete + brief == give a summary. Isn't that obvious?!?
4324 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4325 if (r.app != null) {
4326 pw.print(innerPrefix); pw.println(r.app);
4327 }
4328 }
4329 if (client && r.app != null && r.app.thread != null) {
4330 // flush anything that is already in the PrintWriter since the thread is going
4331 // to write to the file descriptor directly
4332 pw.flush();
4333 try {
4334 TransferPipe tp = new TransferPipe();
4335 try {
4336 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4337 r.appToken, innerPrefix, args);
4338 // Short timeout, since blocking here can
4339 // deadlock with the application.
4340 tp.go(fd, 2000);
4341 } finally {
4342 tp.kill();
4343 }
4344 } catch (IOException e) {
4345 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4346 } catch (RemoteException e) {
4347 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4348 }
4349 needNL = true;
4350 }
4351 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004352 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004353 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004354
Craig Mautnerf3333272013-04-22 10:55:53 -07004355 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004356 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4357 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004358 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4359 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004360 }
4361
4362 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004363 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004364 }
4365
4366 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004367 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4368 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004369 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4370 }
4371
Craig Mautner05d29032013-05-03 13:40:13 -07004372 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004373 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4374 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4375 }
Craig Mautner05d29032013-05-03 13:40:13 -07004376 }
4377
Craig Mautner0eea92c2013-05-16 13:35:39 -07004378 void removeSleepTimeouts() {
4379 mSleepTimeout = false;
4380 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4381 }
4382
4383 final void scheduleSleepTimeout() {
4384 removeSleepTimeouts();
4385 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4386 }
4387
Craig Mautner4a1cb222013-12-04 16:14:06 -08004388 @Override
4389 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004390 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004391 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4392 }
4393
4394 @Override
4395 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004396 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004397 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4398 }
4399
4400 @Override
4401 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004402 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004403 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4404 }
4405
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004406 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004407 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004408 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004409 newDisplay = mActivityDisplays.get(displayId) == null;
4410 if (newDisplay) {
4411 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004412 if (activityDisplay.mDisplay == null) {
4413 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4414 return;
4415 }
Craig Mautner4504de52013-12-20 09:06:56 -08004416 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004417 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004418 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004419 }
Craig Mautner4504de52013-12-20 09:06:56 -08004420 if (newDisplay) {
4421 mWindowManager.onDisplayAdded(displayId);
4422 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004423 }
4424
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004425 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4426 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4427 return;
4428 }
4429 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4430 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4431 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4432 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4433 }
4434
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004435 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004436 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004437 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4438 if (activityDisplay != null) {
4439 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004440 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004441 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004442 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004443 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004444 }
4445 }
4446 mWindowManager.onDisplayRemoved(displayId);
4447 }
4448
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004449 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004450 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004451 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4452 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004453 // TODO: Update the bounds.
4454 }
4455 }
4456 mWindowManager.onDisplayChanged(displayId);
4457 }
4458
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004459 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004460 StackInfo info = new StackInfo();
4461 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4462 info.displayId = Display.DEFAULT_DISPLAY;
4463 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004464 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004465
4466 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4467 final int numTasks = tasks.size();
4468 int[] taskIds = new int[numTasks];
4469 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004470 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004471 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004472 for (int i = 0; i < numTasks; ++i) {
4473 final TaskRecord task = tasks.get(i);
4474 taskIds[i] = task.taskId;
4475 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4476 : task.realActivity != null ? task.realActivity.flattenToString()
4477 : task.getTopActivity() != null ? task.getTopActivity().packageName
4478 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004479 taskBounds[i] = new Rect();
4480 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004481 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004482 }
4483 info.taskIds = taskIds;
4484 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004485 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004486 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004487 return info;
4488 }
4489
4490 StackInfo getStackInfoLocked(int stackId) {
4491 ActivityStack stack = getStack(stackId);
4492 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004493 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004494 }
4495 return null;
4496 }
4497
4498 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004499 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004500 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4501 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004502 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004503 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004504 }
4505 }
4506 return list;
4507 }
4508
Craig Mautner15df08a2015-04-01 12:17:18 -07004509 TaskRecord getLockedTaskLocked() {
4510 final int top = mLockTaskModeTasks.size() - 1;
4511 if (top >= 0) {
4512 return mLockTaskModeTasks.get(top);
4513 }
4514 return null;
4515 }
4516
4517 boolean isLockedTask(TaskRecord task) {
4518 return mLockTaskModeTasks.contains(task);
4519 }
4520
4521 boolean isLastLockedTask(TaskRecord task) {
4522 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4523 }
4524
4525 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004526 if (!mLockTaskModeTasks.remove(task)) {
4527 return;
4528 }
4529 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4530 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004531 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004532 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4533 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004534 final Message lockTaskMsg = Message.obtain();
4535 lockTaskMsg.arg1 = task.userId;
4536 lockTaskMsg.what = LOCK_TASK_END_MSG;
4537 mHandler.sendMessage(lockTaskMsg);
4538 }
4539 }
4540
Chong Zhangc806d902015-11-30 09:44:27 -08004541 private void showNonResizeableDockToast(int taskId) {
4542 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08004543 }
4544
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004545 void showLockTaskToast() {
4546 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004547 }
4548
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004549 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4550 if (mLockTaskModeTasks.contains(task)) {
4551 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4552 }
4553 }
4554
Craig Mautner432f64e2015-05-20 14:59:57 -07004555 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4556 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004557 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004558 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004559 final TaskRecord lockedTask = getLockedTaskLocked();
4560 if (lockedTask != null) {
4561 removeLockedTaskLocked(lockedTask);
4562 if (!mLockTaskModeTasks.isEmpty()) {
4563 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004564 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4565 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004566 lockedTask.performClearTaskLocked();
4567 resumeTopActivitiesLocked();
4568 return;
4569 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004570 }
Craig Mautnere0570202015-05-13 13:06:11 -07004571 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4572 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004573 return;
4574 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004575
4576 // Should have already been checked, but do it again.
4577 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004578 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4579 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004580 return;
4581 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004582 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004583 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004584 return;
4585 }
4586
4587 if (mLockTaskModeTasks.isEmpty()) {
4588 // First locktask.
4589 final Message lockTaskMsg = Message.obtain();
4590 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4591 lockTaskMsg.arg1 = task.userId;
4592 lockTaskMsg.what = LOCK_TASK_START_MSG;
4593 lockTaskMsg.arg2 = lockTaskModeState;
4594 mHandler.sendMessage(lockTaskMsg);
4595 }
4596 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004597 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4598 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004599 mLockTaskModeTasks.remove(task);
4600 mLockTaskModeTasks.add(task);
4601
4602 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004603 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004604 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004605
4606 if (andResume) {
4607 findTaskToMoveToFrontLocked(task, 0, null, reason);
4608 resumeTopActivitiesLocked();
4609 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004610 }
4611
4612 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004613 return isLockTaskModeViolation(task, false);
4614 }
4615
4616 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4617 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004618 return false;
4619 }
4620 final int lockTaskAuth = task.mLockTaskAuth;
4621 switch (lockTaskAuth) {
4622 case LOCK_TASK_AUTH_DONT_LOCK:
4623 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004624 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004625 case LOCK_TASK_AUTH_LAUNCHABLE:
4626 case LOCK_TASK_AUTH_WHITELISTED:
4627 return false;
4628 case LOCK_TASK_AUTH_PINNABLE:
4629 // Pinnable tasks can't be launched on top of locktask tasks.
4630 return !mLockTaskModeTasks.isEmpty();
4631 default:
4632 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4633 return true;
4634 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004635 }
4636
Craig Mautner15df08a2015-04-01 12:17:18 -07004637 void onLockTaskPackagesUpdatedLocked() {
4638 boolean didSomething = false;
4639 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4640 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004641 final boolean wasWhitelisted =
4642 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4643 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004644 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004645 final boolean isWhitelisted =
4646 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4647 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4648 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004649 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004650 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4651 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004652 removeLockedTaskLocked(lockedTask);
4653 lockedTask.performClearTaskLocked();
4654 didSomething = true;
4655 }
4656 }
4657 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4658 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4659 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4660 final ActivityStack stack = stacks.get(stackNdx);
4661 stack.onLockTaskPackagesUpdatedLocked();
4662 }
4663 }
Craig Mautnere0570202015-05-13 13:06:11 -07004664 final ActivityRecord r = topRunningActivityLocked();
4665 final TaskRecord task = r != null ? r.task : null;
4666 if (mLockTaskModeTasks.isEmpty() && task != null
4667 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4668 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004669 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4670 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4671 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4672 false);
4673 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004674 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004675 if (didSomething) {
4676 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004677 }
4678 }
4679
Benjamin Franz43261142015-02-11 15:59:44 +00004680 int getLockTaskModeState() {
4681 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004682 }
4683
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004684 void logStackState() {
4685 mActivityMetricsLogger.logWindowState();
4686 }
4687
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004688 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004689
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004690 public ActivityStackSupervisorHandler(Looper looper) {
4691 super(looper);
4692 }
4693
Craig Mautnerf3333272013-04-22 10:55:53 -07004694 void activityIdleInternal(ActivityRecord r) {
4695 synchronized (mService) {
4696 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4697 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004698 }
4699
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004700 @Override
4701 public void handleMessage(Message msg) {
4702 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004703 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004704 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4705 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004706 if (mService.mDidDexOpt) {
4707 mService.mDidDexOpt = false;
4708 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4709 nmsg.obj = msg.obj;
4710 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4711 return;
4712 }
4713 // We don't at this point know if the activity is fullscreen,
4714 // so we need to be conservative and assume it isn't.
4715 activityIdleInternal((ActivityRecord)msg.obj);
4716 } break;
4717 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004718 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004719 activityIdleInternal((ActivityRecord)msg.obj);
4720 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004721 case RESUME_TOP_ACTIVITY_MSG: {
4722 synchronized (mService) {
4723 resumeTopActivitiesLocked();
4724 }
4725 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004726 case SLEEP_TIMEOUT_MSG: {
4727 synchronized (mService) {
4728 if (mService.isSleepingOrShuttingDown()) {
4729 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4730 mSleepTimeout = true;
4731 checkReadyForSleepLocked();
4732 }
4733 }
4734 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004735 case LAUNCH_TIMEOUT_MSG: {
4736 if (mService.mDidDexOpt) {
4737 mService.mDidDexOpt = false;
4738 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4739 return;
4740 }
4741 synchronized (mService) {
4742 if (mLaunchingActivity.isHeld()) {
4743 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4744 if (VALIDATE_WAKE_LOCK_CALLER
4745 && Binder.getCallingUid() != Process.myUid()) {
4746 throw new IllegalStateException("Calling must be system uid");
4747 }
4748 mLaunchingActivity.release();
4749 }
4750 }
4751 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004752 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004753 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004754 } break;
4755 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004756 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004757 } break;
4758 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004759 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004760 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004761 case CONTAINER_CALLBACK_VISIBILITY: {
4762 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004763 final IActivityContainerCallback callback = container.mCallback;
4764 if (callback != null) {
4765 try {
4766 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4767 } catch (RemoteException e) {
4768 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004769 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004770 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004771 case LOCK_TASK_START_MSG: {
4772 // When lock task starts, we disable the status bars.
4773 try {
Jason Monk62515be2014-05-21 16:06:19 -04004774 if (mLockTaskNotify == null) {
4775 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004776 }
Jason Monk62515be2014-05-21 16:06:19 -04004777 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004778 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004779 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004780 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004781 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004782 flags = StatusBarManager.DISABLE_MASK
4783 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004784 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004785 flags = StatusBarManager.DISABLE_MASK
4786 & (~StatusBarManager.DISABLE_BACK)
4787 & (~StatusBarManager.DISABLE_HOME)
4788 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004789 }
4790 getStatusBarService().disable(flags, mToken,
4791 mService.mContext.getPackageName());
4792 }
4793 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004794 if (getDevicePolicyManager() != null) {
4795 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004796 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004797 }
justinzhang5286d3f2014-05-12 17:06:01 -04004798 } catch (RemoteException ex) {
4799 throw new RuntimeException(ex);
4800 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004801 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004802 case LOCK_TASK_END_MSG: {
4803 // When lock task ends, we enable the status bars.
4804 try {
Jason Monk62515be2014-05-21 16:06:19 -04004805 if (getStatusBarService() != null) {
4806 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4807 mService.mContext.getPackageName());
4808 }
4809 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004810 if (getDevicePolicyManager() != null) {
4811 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4812 msg.arg1);
4813 }
Jason Monk62515be2014-05-21 16:06:19 -04004814 if (mLockTaskNotify == null) {
4815 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4816 }
4817 mLockTaskNotify.show(false);
4818 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004819 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004820 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004821 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004822 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004823 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004824 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004825 new LockPatternUtils(mService.mContext)
4826 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004827 }
4828 } catch (SettingNotFoundException e) {
4829 // No setting, don't lock.
4830 }
justinzhang5286d3f2014-05-12 17:06:01 -04004831 } catch (RemoteException ex) {
4832 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004833 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004834 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004835 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004836 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004837 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4838 if (mLockTaskNotify == null) {
4839 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4840 }
4841 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4842 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004843 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4844 final ActivityContainer container = (ActivityContainer) msg.obj;
4845 final IActivityContainerCallback callback = container.mCallback;
4846 if (callback != null) {
4847 try {
4848 callback.onAllActivitiesComplete(container.asBinder());
4849 } catch (RemoteException e) {
4850 }
4851 }
4852 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004853 case LAUNCH_TASK_BEHIND_COMPLETE: {
4854 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004855 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004856 if (r != null) {
4857 handleLaunchTaskBehindCompleteLocked(r);
4858 }
4859 }
4860 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004861
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004862 }
4863 }
4864 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004865
Ying Wangb081a592014-04-22 15:20:16 -07004866 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004867 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4868 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004869 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004870 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004871 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004872 ActivityRecord mParentActivity = null;
4873 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004874
Craig Mautnere3a00d72014-04-16 08:31:19 -07004875 boolean mVisible = true;
4876
Craig Mautner4a1cb222013-12-04 16:14:06 -08004877 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004878 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004879
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004880 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4881 final static int CONTAINER_STATE_NO_SURFACE = 1;
4882 final static int CONTAINER_STATE_FINISHING = 2;
4883 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4884
4885 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004886 synchronized (mService) {
4887 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004888 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004889 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004890 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004891 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004892 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004893
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004894 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004895 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004896 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004897 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004898 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004899 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004900 }
4901
4902 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004903 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004904 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004905 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4906 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004907 return;
4908 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004909 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004910 }
4911 }
4912
4913 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004914 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004915 synchronized (mService) {
4916 if (mActivityDisplay != null) {
4917 return mActivityDisplay.mDisplayId;
4918 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004919 }
4920 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004921 }
4922
Jeff Brownca9bc702014-02-11 14:32:56 -08004923 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004924 public int getStackId() {
4925 synchronized (mService) {
4926 return mStackId;
4927 }
4928 }
4929
4930 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004931 public boolean injectEvent(InputEvent event) {
4932 final long origId = Binder.clearCallingIdentity();
4933 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004934 synchronized (mService) {
4935 if (mActivityDisplay != null) {
4936 return mInputManagerInternal.injectInputEvent(event,
4937 mActivityDisplay.mDisplayId,
4938 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4939 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004940 }
4941 return false;
4942 } finally {
4943 Binder.restoreCallingIdentity(origId);
4944 }
4945 }
4946
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004947 @Override
4948 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004949 synchronized (mService) {
4950 if (mContainerState == CONTAINER_STATE_FINISHING) {
4951 return;
4952 }
4953 mContainerState = CONTAINER_STATE_FINISHING;
4954
Craig Mautnerd163e752014-06-13 17:18:47 -07004955 long origId = Binder.clearCallingIdentity();
4956 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004957 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004958 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004959 } finally {
4960 Binder.restoreCallingIdentity(origId);
4961 }
4962 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004963 }
4964
Craig Mautner60257702014-09-17 15:02:33 -07004965 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004966 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004967 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004968 if (mActivityDisplay != null) {
4969 mActivityDisplay.detachActivitiesLocked(mStack);
4970 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004971 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004972 }
4973 }
4974
4975 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004976 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004977 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004978 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004979 Binder.getCallingUid(), mCurrentUser, false,
4980 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004981
Craig Mautnere0a38842013-12-16 16:14:02 -08004982 // TODO: Switch to user app stacks here.
4983 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004984 final Uri data = intent.getData();
4985 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4986 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004987 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004988 checkEmbeddedAllowedInner(userId, intent, mimeType);
4989
4990 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4991 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004992 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004993 }
4994
4995 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004996 public final int startActivityIntentSender(IIntentSender intentSender)
4997 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004998 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4999
5000 if (!(intentSender instanceof PendingIntentRecord)) {
5001 throw new IllegalArgumentException("Bad PendingIntent object");
5002 }
5003
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07005004 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07005005 Binder.getCallingUid(), mCurrentUser, false,
5006 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08005007
5008 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
5009 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
5010 pendingIntent.key.requestResolvedType);
5011
5012 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
5013 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
5014 }
5015
5016 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07005017 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07005018 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07005019 throw new SecurityException(
5020 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
5021 }
5022 }
5023
Craig Mautnerdf88d732014-01-27 09:21:32 -08005024 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08005025 public IBinder asBinder() {
5026 return this;
5027 }
5028
Craig Mautner4504de52013-12-20 09:06:56 -08005029 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005030 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08005031 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08005032 }
5033
Craig Mautner4a1cb222013-12-04 16:14:06 -08005034 ActivityStackSupervisor getOuter() {
5035 return ActivityStackSupervisor.this;
5036 }
5037
Craig Mautnerd163e752014-06-13 17:18:47 -07005038 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08005039 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08005040 }
5041
Craig Mautner6985bad2014-04-21 15:22:06 -07005042 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07005043 void setVisible(boolean visible) {
5044 if (mVisible != visible) {
5045 mVisible = visible;
5046 if (mCallback != null) {
5047 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
5048 0 /* unused */, this).sendToTarget();
5049 }
5050 }
5051 }
5052
Craig Mautner6985bad2014-04-21 15:22:06 -07005053 void setDrawn() {
5054 }
5055
Craig Mautner1b4bf852014-05-26 15:06:32 -07005056 // You can always start a new task on a regular ActivityStack.
5057 boolean isEligibleForNewTasks() {
5058 return true;
5059 }
5060
Craig Mautnerd163e752014-06-13 17:18:47 -07005061 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005062 detachLocked();
5063 deleteActivityContainer(this);
5064 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07005065 }
5066
Craig Mautner34b73df2014-01-12 21:11:08 -08005067 @Override
5068 public String toString() {
5069 return mIdString + (mActivityDisplay == null ? "N" : "A");
5070 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005071 }
5072
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005073 private class VirtualActivityContainer extends ActivityContainer {
5074 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07005075 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005076
5077 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
5078 super(getNextStackId());
5079 mParentActivity = parent;
5080 mCallback = callback;
5081 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07005082 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005083 }
5084
5085 @Override
5086 public void setSurface(Surface surface, int width, int height, int density) {
5087 super.setSurface(surface, width, height, density);
5088
5089 synchronized (mService) {
5090 final long origId = Binder.clearCallingIdentity();
5091 try {
5092 setSurfaceLocked(surface, width, height, density);
5093 } finally {
5094 Binder.restoreCallingIdentity(origId);
5095 }
5096 }
5097 }
5098
5099 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
5100 if (mContainerState == CONTAINER_STATE_FINISHING) {
5101 return;
5102 }
5103 VirtualActivityDisplay virtualActivityDisplay =
5104 (VirtualActivityDisplay) mActivityDisplay;
5105 if (virtualActivityDisplay == null) {
5106 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07005107 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005108 mActivityDisplay = virtualActivityDisplay;
5109 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005110 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005111 }
5112
5113 if (mSurface != null) {
5114 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005115 }
5116
Craig Mautner6985bad2014-04-21 15:22:06 -07005117 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005118 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005119 mStack.resumeTopActivityLocked(null);
5120 } else {
5121 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07005122 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07005123 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07005124 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07005125 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005126 }
Craig Mautner6985bad2014-04-21 15:22:06 -07005127
Craig Mautnerd163e752014-06-13 17:18:47 -07005128 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07005129
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005130 if (DEBUG_STACK) Slog.d(TAG_STACK,
5131 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07005132 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005133
Craig Mautner6985bad2014-04-21 15:22:06 -07005134 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07005135 boolean isAttachedLocked() {
5136 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07005137 }
5138
5139 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07005140 void setDrawn() {
5141 synchronized (mService) {
5142 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07005143 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07005144 }
5145 }
5146
Craig Mautner1b4bf852014-05-26 15:06:32 -07005147 // Never start a new task on an ActivityView if it isn't explicitly specified.
5148 @Override
5149 boolean isEligibleForNewTasks() {
5150 return false;
5151 }
5152
Craig Mautnerd163e752014-06-13 17:18:47 -07005153 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005154 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07005155 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
5156 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
5157 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
5158 mContainerState = CONTAINER_STATE_HAS_SURFACE;
5159 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005160 }
5161 }
5162
Craig Mautner4a1cb222013-12-04 16:14:06 -08005163 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
5164 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005165 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08005166 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08005167 int mDisplayId;
5168 Display mDisplay;
5169 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08005170
Craig Mautner4a1cb222013-12-04 16:14:06 -08005171 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
5172 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08005173 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08005174
Jose Lima4b6c6692014-08-12 17:41:12 -07005175 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005176
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005177 ActivityDisplay() {
5178 }
Craig Mautner4504de52013-12-20 09:06:56 -08005179
Craig Mautner1a70a162014-09-13 12:09:31 -07005180 // After instantiation, check that mDisplay is not null before using this. The alternative
5181 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08005182 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07005183 final Display display = mDisplayManager.getDisplay(displayId);
5184 if (display == null) {
5185 return;
5186 }
5187 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08005188 }
5189
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005190 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08005191 mDisplay = display;
5192 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08005193 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08005194 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08005195
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005196 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005197 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005198 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
5199 + " onTop=" + onTop);
5200 if (onTop) {
5201 mStacks.add(stack);
5202 } else {
5203 mStacks.add(0, stack);
5204 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08005205 }
5206
Craig Mautnere0a38842013-12-16 16:14:02 -08005207 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005208 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08005209 + " from displayId=" + mDisplayId);
5210 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08005211 }
5212
Jose Lima4b6c6692014-08-12 17:41:12 -07005213 void setVisibleBehindActivity(ActivityRecord r) {
5214 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005215 }
5216
Jose Lima4b6c6692014-08-12 17:41:12 -07005217 boolean hasVisibleBehindActivity() {
5218 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005219 }
5220
Craig Mautner34b73df2014-01-12 21:11:08 -08005221 @Override
5222 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005223 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
5224 }
5225 }
5226
5227 class VirtualActivityDisplay extends ActivityDisplay {
5228 VirtualDisplay mVirtualDisplay;
5229
Craig Mautner6985bad2014-04-21 15:22:06 -07005230 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005231 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07005232 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
5233 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
5234 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
5235 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005236
5237 init(mVirtualDisplay.getDisplay());
5238
5239 mWindowManager.handleDisplayAdded(mDisplayId);
5240 }
5241
5242 void setSurface(Surface surface) {
5243 if (mVirtualDisplay != null) {
5244 mVirtualDisplay.setSurface(surface);
5245 }
5246 }
5247
5248 @Override
5249 void detachActivitiesLocked(ActivityStack stack) {
5250 super.detachActivitiesLocked(stack);
5251 if (mVirtualDisplay != null) {
5252 mVirtualDisplay.release();
5253 mVirtualDisplay = null;
5254 }
5255 }
5256
5257 @Override
5258 public String toString() {
5259 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005260 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005261 }
Jose Lima58e66d62014-05-27 20:00:27 -07005262
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07005263 /**
5264 * Adjust bounds to stay within stack bounds.
5265 *
5266 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
5267 * that keep them unchanged, but be contained within the stack bounds.
5268 *
5269 * @param bounds Bounds to be adjusted.
5270 * @param stackBounds Bounds within which the other bounds should remain.
5271 */
5272 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
5273 if (stackBounds == null || stackBounds.contains(bounds)) {
5274 return;
5275 }
5276
5277 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
5278 final int maxRight = stackBounds.right
5279 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
5280 int horizontalDiff = stackBounds.left - bounds.left;
5281 if ((horizontalDiff < 0 && bounds.left >= maxRight)
5282 || (bounds.left + horizontalDiff >= maxRight)) {
5283 horizontalDiff = maxRight - bounds.left;
5284 }
5285 bounds.left += horizontalDiff;
5286 bounds.right += horizontalDiff;
5287 }
5288
5289 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
5290 final int maxBottom = stackBounds.bottom
5291 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
5292 int verticalDiff = stackBounds.top - bounds.top;
5293 if ((verticalDiff < 0 && bounds.top >= maxBottom)
5294 || (bounds.top + verticalDiff >= maxBottom)) {
5295 verticalDiff = maxBottom - bounds.top;
5296 }
5297 bounds.top += verticalDiff;
5298 bounds.bottom += verticalDiff;
5299 }
5300 }
5301
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005302 ActivityStack findStackBehind(ActivityStack stack) {
5303 // TODO(multi-display): We are only looking for stacks on the default display.
5304 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
5305 if (display == null) {
5306 return null;
5307 }
5308 final ArrayList<ActivityStack> stacks = display.mStacks;
5309 for (int i = stacks.size() - 1; i >= 0; i--) {
5310 if (stacks.get(i) == stack && i > 0) {
5311 return stacks.get(i - 1);
5312 }
5313 }
5314 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
5315 + " in=" + stacks);
5316 }
Craig Mautner27084302013-03-25 08:05:25 -07005317}