blob: 8f7f396418df434405a944eee9edb91e599e836c [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
Svetoslav7008b512015-06-24 18:47:07 -070019import android.Manifest;
Tony Mak853304c2016-04-18 15:17:41 +010020import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -070021import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070022import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080023import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070024import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080025import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070026import android.app.ActivityOptions;
27import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070028import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080029import android.app.IActivityContainer;
30import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070031import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080032import android.app.IActivityManager.WaitResult;
Tony Mak853304c2016-04-18 15:17:41 +010033import android.app.KeyguardManager;
34import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070035import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070036import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040037import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040038import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070040import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070042import android.content.Intent;
Tony Mak853304c2016-04-18 15:17:41 +010043import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070044import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070045import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070046import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.content.pm.PackageManager;
48import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010049import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080051import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080052import android.hardware.display.DisplayManager;
53import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080054import android.hardware.display.DisplayManagerGlobal;
55import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080056import android.hardware.input.InputManager;
57import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010059import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070060import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070061import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070063import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070064import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070066import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070067import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070068import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040069import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070070import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070071import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070072import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070073import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +010074import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070075import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070076import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040077import android.provider.Settings;
78import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070079import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070080import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070081import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070082import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070083import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080084import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -080085import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080086import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080087import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080088import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080089import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -080090
Dianne Hackborn85d558c2014-11-04 10:31:54 -080091import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070092import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040093import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -070094import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -040095import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -080096import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -070097import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070098import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import java.io.FileDescriptor;
101import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700102import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700103import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700104import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700105import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700106import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800107import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700108import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700109
Jorim Jaggife89d122015-12-22 16:28:44 +0100110import static android.Manifest.permission.START_ANY_ACTIVITY;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100111import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100112import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
113import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
114import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
115import static android.app.ActivityManager.RESIZE_MODE_FORCED;
116import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
117import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
118import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
119import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
120import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
121import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
122import static android.app.ActivityManager.StackId.HOME_STACK_ID;
123import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
124import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
125import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
126import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
127import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
128import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggi51605272016-02-24 18:06:49 -0500129import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100130import static android.content.pm.PackageManager.PERMISSION_GRANTED;
131import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
140import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
141import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
142import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
143import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
144import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
151import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
152import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
153import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
154import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
155import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
156import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
157import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800158import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100159import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggid53f0922016-04-06 22:16:23 -0700160import static com.android.server.am.ActivityManagerService.NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100161import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100162import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
163import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
164import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
165import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
166import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
167import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
168import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
169import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
170import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
171import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
172import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Tony Mak853304c2016-04-18 15:17:41 +0100173import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100174import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
175import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
176import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
177import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
178import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100179import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100180
Craig Mautner4a1cb222013-12-04 16:14:06 -0800181public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800182 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700185 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700186 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700191 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800192 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800194
Craig Mautnerf3333272013-04-22 10:55:53 -0700195 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700197
Craig Mautner0eea92c2013-05-16 13:35:39 -0700198 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700200
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700201 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700203
Craig Mautner05d29032013-05-03 13:40:13 -0700204 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
205 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
206 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700208 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800209 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
210 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
211 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700212 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400213 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
214 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700215 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700216 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700217 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800218 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
219 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800220
Wale Ogunwale040b4702015-08-06 18:10:50 -0700221 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700222
Jason Monk62515be2014-05-21 16:06:19 -0400223 private static final String LOCK_TASK_TAG = "Lock-to-App";
224
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225 // Used to indicate if an object (e.g. stack) that we are trying to get
226 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800227 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700229 // Used to indicate that windows of activities should be preserved during the resize.
230 static final boolean PRESERVE_WINDOWS = true;
231
Wale Ogunwale040b4702015-08-06 18:10:50 -0700232 // Used to indicate if an object (e.g. task) should be moved/created
233 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700234 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235
236 // Used to indicate that an objects (e.g. task) removal from its container
237 // (e.g. stack) is due to it moving to another container.
238 static final boolean MOVING = true;
239
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700240 // Force the focus to change to the stack we are moving a task to..
241 static final boolean FORCE_FOCUS = true;
242
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700243 // Restore task from the saved recents if it can't be found in any live stack.
244 static final boolean RESTORE_FROM_RECENTS = true;
245
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700246 // Don't execute any calls to resume.
247 static final boolean DEFER_RESUME = true;
248
Svetoslav7008b512015-06-24 18:47:07 -0700249 // Activity actions an app cannot start if it uses a permission which is not granted.
250 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
251 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700252
Svetoslav7008b512015-06-24 18:47:07 -0700253 static {
254 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
255 Manifest.permission.CAMERA);
256 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
257 Manifest.permission.CAMERA);
258 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
259 Manifest.permission.CALL_PHONE);
260 }
261
Svet Ganov99b60432015-06-27 13:15:22 -0700262 /** Action restriction: launching the activity is not restricted. */
263 private static final int ACTIVITY_RESTRICTION_NONE = 0;
264 /** Action restriction: launching the activity is restricted by a permission. */
265 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
266 /** Action restriction: launching the activity is restricted by an app op. */
267 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700268
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700269 // The height/width divide used when fitting a task within a bounds with method
270 // {@link #fitWithinBounds}.
271 // We always want the task to to be visible in the bounds without affecting its size when
272 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
273 // the input bounds right or bottom side minus the width or height divided by this value.
274 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
275
justinzhang5286d3f2014-05-12 17:06:01 -0400276 /** Status Bar Service **/
277 private IBinder mToken = new Binder();
278 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400279 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400280
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700281 // For debugging to make sure the caller when acquiring/releasing our
282 // wake lock is the system process.
283 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800284 /** The number of distinct task ids that can be assigned to the tasks of a single user */
285 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700286
Craig Mautner27084302013-03-25 08:05:25 -0700287 final ActivityManagerService mService;
288
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800289 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800290
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700291 final ActivityStackSupervisorHandler mHandler;
292
293 /** Short cut */
294 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800295 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700296
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700297 /** Counter for next free stack ID to use for dynamic activity stacks. */
298 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700299
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800300 /**
301 * Maps the task identifier that activities are currently being started in to the userId of the
302 * task. Each time a new task is created, the entry for the userId of the task is incremented
303 */
304 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700305
Craig Mautner2420ead2013-04-01 17:13:20 -0700306 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800307 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700308
Craig Mautnere0a38842013-12-16 16:14:02 -0800309 /** The stack containing the launcher app. Assumed to always be attached to
310 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800311 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700312
Craig Mautnere0a38842013-12-16 16:14:02 -0800313 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800314 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700315
Craig Mautner4a1cb222013-12-04 16:14:06 -0800316 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
317 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800318 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700320
321 /** List of activities that are waiting for a new activity to become visible before completing
322 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800323 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700324
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700325 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800326 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700327
328 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800329 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700330
Craig Mautnerde4ef022013-04-07 19:01:33 -0700331 /** List of activities that are ready to be stopped, but waiting for the next activity to
332 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800333 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700334
Craig Mautnerf3333272013-04-22 10:55:53 -0700335 /** List of activities that are ready to be finished, but waiting for the previous activity to
336 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800337 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700338
Craig Mautner0eea92c2013-05-16 13:35:39 -0700339 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800340 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700341
Wale Ogunwale22e25262016-02-01 10:32:02 -0800342 /** List of activities whose multi-window mode changed that we need to report to the
343 * application */
344 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
345
346 /** List of activities whose picture-in-picture mode changed that we need to report to the
347 * application */
348 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
349
Craig Mautnerf3333272013-04-22 10:55:53 -0700350 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700351 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700352
Craig Mautnerde4ef022013-04-07 19:01:33 -0700353 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
354 * is being brought in front of us. */
355 boolean mUserLeaving = false;
356
Craig Mautner0eea92c2013-05-16 13:35:39 -0700357 /** Set when we have taken too long waiting to go to sleep. */
358 boolean mSleepTimeout = false;
359
360 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700361 * We don't want to allow the device to go to sleep while in the process
362 * of launching an activity. This is primarily to allow alarm intent
363 * receivers to launch an activity and get that to run before the device
364 * goes back to sleep.
365 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700366 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700367
368 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700369 * Set when the system is going to sleep, until we have
370 * successfully paused the current activity and released our wake lock.
371 * At that point the system is allowed to actually sleep.
372 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700373 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700374
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700375 /** Stack id of the front stack when user switched, indexed by userId. */
376 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700377
Craig Mautner4504de52013-12-20 09:06:56 -0800378 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700380 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381
382 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700383 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800384
Jeff Brownca9bc702014-02-11 14:32:56 -0800385 InputManagerInternal mInputManagerInternal;
386
Craig Mautner15df08a2015-04-01 12:17:18 -0700387 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
388 * may be finished until there is only one entry left. If this is empty the system is not
389 * in lockTask mode. */
390 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000391 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700392 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
393 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000394 */
395 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400396 /**
397 * Notifies the user when entering/exiting lock-task.
398 */
399 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800400
Wale Ogunwaled046a012015-12-24 13:05:59 -0800401 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800402 boolean inResumeTopActivity;
403
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700404 // 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 -0700405 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700406 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700407
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700408 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
409 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800410 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700411
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.
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800414 int mDefaultMinimalSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700415
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
Jorim Jaggi275561a2016-02-23 10:11:02 -0500419 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800420
Jorim Jaggidc249c42015-12-15 14:57:31 -0800421 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
422
Wale Ogunwale39381972015-12-17 17:15:29 -0800423 static class FindTaskResult {
424 ActivityRecord r;
425 boolean matchedByRootAffinity;
426 }
427 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
428
Craig Mautneree36c772014-07-16 14:56:05 -0700429 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100430 * Used to keep track whether app visibilities got changed since the last pause. Useful to
431 * determine whether to invoke the task stack change listener after pausing.
432 */
433 boolean mAppVisibilitiesChangedSinceLastPause;
434
435 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100436 * Set of tasks that are in resizing mode during an app transition to fill the "void".
437 */
438 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
439
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700440
441 /**
442 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
443 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
444 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
445 * like the docked stack going empty.
446 */
447 private boolean mAllowDockedStackResize = true;
448
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100449 /**
Tony Mak853304c2016-04-18 15:17:41 +0100450 * Is dock currently minimized.
451 */
452 boolean mIsDockMinimized;
453
454 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700455 * Description of a request to start a new activity, which has been held
456 * due to app switches being disabled.
457 */
458 static class PendingActivityLaunch {
459 final ActivityRecord r;
460 final ActivityRecord sourceRecord;
461 final int startFlags;
462 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700463 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700464
465 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700466 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700467 r = _r;
468 sourceRecord = _sourceRecord;
469 startFlags = _startFlags;
470 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700471 callerApp = _callerApp;
472 }
473
474 void sendErrorResult(String message) {
475 try {
476 if (callerApp.thread != null) {
477 callerApp.thread.scheduleCrash(message);
478 }
479 } catch (RemoteException e) {
480 Slog.e(TAG, "Exception scheduling crash of failed "
481 + "activity launcher sourceRecord=" + sourceRecord, e);
482 }
Craig Mautneree36c772014-07-16 14:56:05 -0700483 }
484 }
485
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800486 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700487 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700488 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800489 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800490 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700491 }
492
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800493 void setRecentTasks(RecentTasks recentTasks) {
494 mRecentTasks = recentTasks;
495 }
496
Jeff Brown2c43c332014-06-12 22:38:59 -0700497 /**
498 * At the time when the constructor runs, the power manager has not yet been
499 * initialized. So we initialize our wakelocks afterwards.
500 */
501 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800502 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700503 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700504 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700505 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700506 }
507
justinzhang5286d3f2014-05-12 17:06:01 -0400508 // This function returns a IStatusBarService. The value is from ServiceManager.
509 // getService and is cached.
510 private IStatusBarService getStatusBarService() {
511 synchronized (mService) {
512 if (mStatusBarService == null) {
513 mStatusBarService = IStatusBarService.Stub.asInterface(
514 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
515 if (mStatusBarService == null) {
516 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
517 }
518 }
519 return mStatusBarService;
520 }
521 }
522
Jason Monk35c62a42014-06-17 10:24:47 -0400523 private IDevicePolicyManager getDevicePolicyManager() {
524 synchronized (mService) {
525 if (mDevicePolicyManager == null) {
526 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
527 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
528 if (mDevicePolicyManager == null) {
529 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
530 }
531 }
532 return mDevicePolicyManager;
533 }
534 }
535
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700536 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800537 synchronized (mService) {
538 mWindowManager = wm;
539
540 mDisplayManager =
541 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
542 mDisplayManager.registerDisplayListener(this, null);
543
544 Display[] displays = mDisplayManager.getDisplays();
545 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
546 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800547 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700548 if (activityDisplay.mDisplay == null) {
549 throw new IllegalStateException("Default Display does not exist");
550 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800551 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700552 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800553 }
554
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800555 mHomeStack = mFocusedStack = mLastFocusedStack =
556 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800557
558 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800559 }
Craig Mautner27084302013-03-25 08:05:25 -0700560 }
561
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200562 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700563 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200564 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700565 }
566
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700567 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700569 }
570
Craig Mautnerde4ef022013-04-07 19:01:33 -0700571 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700573 }
574
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700575 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700576 if (stack == null) {
577 return false;
578 }
579
Craig Mautnerdf88d732014-01-27 09:21:32 -0800580 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
581 if (parent != null) {
582 stack = parent.task.stack;
583 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800584 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700585 }
586
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700587 /** The top most stack. */
588 boolean isFrontStack(ActivityStack stack) {
589 if (stack == null) {
590 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700592
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700593 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
594 if (parent != null) {
595 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800596 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700597 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
598 }
599
Wale Ogunwaled046a012015-12-24 13:05:59 -0800600 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800601 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
602 if (!focusCandidate.isFocusable()) {
603 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
604 focusCandidate = focusCandidate.getNextFocusableStackLocked();
605 }
606
607 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800608 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800609 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800610
Wale Ogunwaled046a012015-12-24 13:05:59 -0800611 EventLogTags.writeAmFocusedStack(
612 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
613 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
614 }
615
616 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800617 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800618 // The focus activity should always be the top activity in the focused stack.
619 // There will be chaos and anarchy if it isn't...
620 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
621 }
Craig Mautnerde313752015-01-22 14:28:03 -0800622
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800623 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800624 if (r != null && r.idle) {
625 checkFinishBootingLocked();
626 }
627 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700628 }
629
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700630 void moveHomeStackToFront(String reason) {
631 mHomeStack.moveToFront(reason);
632 }
633
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700634 /** Returns true if the focus activity was adjusted to the home stack top activity. */
635 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700636 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700637 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700638 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700639 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700640
Craig Mautner84984fa2014-06-19 11:19:20 -0700641 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700642
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700643 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700644 if (top == null) {
645 return false;
646 }
647 mService.setFocusedActivityLocked(top, reason);
648 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700649 }
650
Craig Mautner299f9602015-01-26 09:47:33 -0800651 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700652 if (!mService.mBooting && !mService.mBooted) {
653 // Not ready yet!
654 return false;
655 }
656
Craig Mautner84984fa2014-06-19 11:19:20 -0700657 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700658 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700659 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700660 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700661
Craig Mautner84984fa2014-06-19 11:19:20 -0700662 if (prev != null) {
663 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
664 }
665
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700666 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
667 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800668 final String myReason = reason + " resumeHomeStackTask";
669
Mark Lua56ea122015-10-08 13:31:01 +0800670 // Only resume home activity if isn't finishing.
671 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800672 mService.setFocusedActivityLocked(r, myReason);
673 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700674 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800675 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700676 }
677
Craig Mautner8d341ef2013-03-26 09:03:27 -0700678 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700679 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700680 }
681
682 /**
683 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
684 * @param id Id of the task we would like returned.
685 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
686 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700687 * @param stackId The stack to restore the task to (default launch stack will be used if
688 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700689 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700690 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800691 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800692 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800693 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800694 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
695 ActivityStack stack = stacks.get(stackNdx);
696 TaskRecord task = stack.taskForIdLocked(id);
697 if (task != null) {
698 return task;
699 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700700 }
701 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800702
703 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700704 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800705 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800706 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700707 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800708 return null;
709 }
710
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700711 if (!restoreFromRecents) {
712 return task;
713 }
714
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700715 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700716 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
717 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800718 return null;
719 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700720 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800721 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700722 }
723
Craig Mautner6170f732013-04-02 13:05:23 -0700724 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800725 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800727 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
729 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
730 if (r != null) {
731 return r;
732 }
Craig Mautner6170f732013-04-02 13:05:23 -0700733 }
734 }
735 return null;
736 }
737
Tony Mak853304c2016-04-18 15:17:41 +0100738 /**
739 * TODO: Handle freefom mode.
740 * @return true when credential confirmation is needed for the user and there is any
741 * activity started by the user in any visible stack.
742 */
743 boolean isUserLockedProfile(@UserIdInt int userId) {
744 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
745 return false;
746 }
747 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
748 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
749 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
750 for (final ActivityStack activityStack : activityStacks) {
751 if (activityStack == null) {
752 continue;
753 }
754 if (activityStack.topRunningActivityLocked() == null) {
755 continue;
756 }
757 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
758 continue;
759 }
760 if (activityStack.isDockedStack() && mIsDockMinimized) {
761 continue;
762 }
763 final TaskRecord topTask = activityStack.topTask();
764 if (topTask == null) {
765 continue;
766 }
767 // To handle the case that work app is in the task but just is not the top one.
768 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
769 final ActivityRecord activityRecord = topTask.mActivities.get(i);
770 if (activityRecord.userId == userId) {
771 return true;
772 }
773 }
774 }
775 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000776 }
777
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800778 void setNextTaskIdForUserLocked(int taskId, int userId) {
779 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
780 if (taskId > currentTaskId) {
781 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700782 }
783 }
784
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800785 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800786 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
787 // for a userId u, a taskId can only be in the range
788 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
789 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
790 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800791 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
792 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
793 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800794 candidateTaskId++;
795 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
796 // Wrap around as there will be smaller task ids that are available now.
797 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700798 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800799 if (candidateTaskId == currentTaskId) {
800 // Something wrong!
801 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
802 throw new IllegalStateException("Cannot get an available task id."
803 + " Reached limit of " + MAX_TASK_IDS_PER_USER
804 + " running tasks per user.");
805 }
806 }
807 mCurTaskIdForUser.put(userId, candidateTaskId);
808 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700809 }
810
Craig Mautnerde4ef022013-04-07 19:01:33 -0700811 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800812 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700813 if (stack == null) {
814 return null;
815 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700816 ActivityRecord resumedActivity = stack.mResumedActivity;
817 if (resumedActivity == null || resumedActivity.app == null) {
818 resumedActivity = stack.mPausingActivity;
819 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700820 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700821 }
822 }
823 return resumedActivity;
824 }
825
Dianne Hackbornff072722014-09-24 10:56:28 -0700826 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700827 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800829 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
830 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800831 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
832 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700833 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800834 continue;
835 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700836 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 if (hr != null) {
838 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
839 && processName.equals(hr.processName)) {
840 try {
George Mount2c92c972014-03-20 09:38:23 -0700841 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800842 didSomething = true;
843 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700844 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800845 Slog.w(TAG, "Exception in new application when starting activity "
846 + hr.intent.getComponent().flattenToShortString(), e);
847 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700848 }
Craig Mautner20e72272013-04-01 13:45:53 -0700849 }
Craig Mautner20e72272013-04-01 13:45:53 -0700850 }
851 }
852 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700853 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700854 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700855 }
Craig Mautner20e72272013-04-01 13:45:53 -0700856 return didSomething;
857 }
858
859 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800860 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
861 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800862 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
863 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700864 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800865 continue;
866 }
867 final ActivityRecord resumedActivity = stack.mResumedActivity;
868 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700869 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800870 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800871 return false;
872 }
Craig Mautner20e72272013-04-01 13:45:53 -0700873 }
874 }
875 return true;
876 }
877
Craig Mautnerde4ef022013-04-07 19:01:33 -0700878 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800879 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
880 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800881 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
882 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700883 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800884 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700885 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 return false;
887 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700888 }
889 }
890 }
891 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700892 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800893 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
894 mLastFocusedStack + " to=" + mFocusedStack);
895 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700896 return true;
897 }
898
899 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800900 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800901 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
902 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800903 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
904 final ActivityStack stack = stacks.get(stackNdx);
905 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800906 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700907 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800908 return false;
909 }
910 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800911 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700912 }
913 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800914 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700915 }
916
Craig Mautner2acc3892013-09-23 10:28:14 -0700917 /**
918 * Pause all activities in either all of the stacks or just the back stacks.
919 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700920 * @return true if any activity was paused as a result of this call.
921 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700922 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700923 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
925 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
927 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700928 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700929 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800930 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700931 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
932 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800933 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700934 }
935 }
936 return someActivityPaused;
937 }
938
Craig Mautnerde4ef022013-04-07 19:01:33 -0700939 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700940 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
942 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800943 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
944 final ActivityStack stack = stacks.get(stackNdx);
945 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700946 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800947 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700948 Slog.d(TAG_STATES,
949 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800950 pausing = false;
951 } else {
952 return false;
953 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700954 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700955 }
956 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700957 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700958 }
959
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700960 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
961 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500962 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800963 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
964 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
965 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
966 final ActivityStack stack = stacks.get(stackNdx);
967 if (stack.mResumedActivity != null &&
968 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700969 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800970 }
971 }
972 }
973 }
974
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800975 void cancelInitializingActivities() {
976 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
977 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
978 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
979 stacks.get(stackNdx).cancelInitializingActivities();
980 }
981 }
982 }
983
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700984 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700985 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700986 }
987
988 void sendWaitingVisibleReportLocked(ActivityRecord r) {
989 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700990 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700991 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700992 if (w.who == null) {
993 changed = true;
994 w.timeout = false;
995 if (r != null) {
996 w.who = new ComponentName(r.info.packageName, r.info.name);
997 }
998 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
999 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001000 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001001 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001002 if (changed) {
1003 mService.notifyAll();
1004 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001005 }
1006
1007 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1008 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001009 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001010 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001011 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001012 if (w.who == null) {
1013 changed = true;
1014 w.timeout = timeout;
1015 if (r != null) {
1016 w.who = new ComponentName(r.info.packageName, r.info.name);
1017 }
1018 w.thisTime = thisTime;
1019 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001020 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001021 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001022 if (changed) {
1023 mService.notifyAll();
1024 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001025 }
1026
Craig Mautner29219d92013-04-16 20:19:12 -07001027 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001028 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001029 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001030 if (r != null) {
1031 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001032 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001033
Craig Mautner4a1cb222013-12-04 16:14:06 -08001034 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001035 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001036 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1037 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001038 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001039 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001040 if (r != null) {
1041 return r;
1042 }
1043 }
1044 }
1045 return null;
1046 }
1047
Dianne Hackborn09233282014-04-30 11:33:59 -07001048 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001049 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001050 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1051 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001052 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001053 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001054 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001055 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1056 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001057 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001058 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001059 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001060 }
1061 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001062
1063 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1064 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1065 while (maxNum > 0) {
1066 long mostRecentActiveTime = Long.MIN_VALUE;
1067 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001068 final int numTaskLists = runningTaskLists.size();
1069 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1070 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001071 if (!stackTaskList.isEmpty()) {
1072 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1073 if (lastActiveTime > mostRecentActiveTime) {
1074 mostRecentActiveTime = lastActiveTime;
1075 selectedStackList = stackTaskList;
1076 }
1077 }
1078 }
1079 if (selectedStackList != null) {
1080 list.add(selectedStackList.remove(0));
1081 --maxNum;
1082 } else {
1083 break;
1084 }
1085 }
Craig Mautner20e72272013-04-01 13:45:53 -07001086 }
1087
Todd Kennedy7440f172015-12-09 14:31:22 -08001088 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1089 ProfilerInfo profilerInfo) {
1090 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001091 if (aInfo != null) {
1092 // Store the found target back into the intent, because now that
1093 // we have it we never want to do this again. For example, if the
1094 // user navigates back to this point in the history, we should
1095 // always restart the exact same activity.
1096 intent.setComponent(new ComponentName(
1097 aInfo.applicationInfo.packageName, aInfo.name));
1098
1099 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001100 if (!aInfo.processName.equals("system")) {
1101 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001102 mService.setDebugApp(aInfo.processName, true, false);
1103 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001104
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001105 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1106 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1107 }
1108
Man Caocfa78b22015-06-11 20:14:34 -07001109 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1110 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1111 }
1112
1113 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001114 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001115 }
1116 }
1117 }
1118 return aInfo;
1119 }
1120
Todd Kennedy7440f172015-12-09 14:31:22 -08001121 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001122 return resolveIntent(intent, resolvedType, userId, 0);
1123 }
1124
1125 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001126 try {
1127 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001128 PackageManager.MATCH_DEFAULT_ONLY | flags
1129 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001130 } catch (RemoteException e) {
1131 }
1132 return null;
1133 }
1134
1135 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1136 ProfilerInfo profilerInfo, int userId) {
1137 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1138 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1139 }
1140
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001141 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1142 boolean andResume, boolean checkConfig) throws RemoteException {
1143
1144 if (!allPausedActivitiesComplete()) {
1145 // While there are activities pausing we skipping starting any new activities until
1146 // pauses are complete. NOTE: that we also do this for activities that are starting in
1147 // the paused state because they will first be resumed then paused on the client side.
1148 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1149 "realStartActivityLocked: Skipping start of r=" + r
1150 + " some activities pausing...");
1151 return false;
1152 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001153
Craig Mautner2568c3a2015-03-26 14:22:34 -07001154 if (andResume) {
1155 r.startFreezingScreenLocked(app, 0);
1156 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001157
Craig Mautner2568c3a2015-03-26 14:22:34 -07001158 // schedule launch ticks to collect information about slow apps.
1159 r.startLaunchTickingLocked();
1160 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001161
1162 // Have the window manager re-evaluate the orientation of
1163 // the screen based on the new activity order. Note that
1164 // as a result of this, it can call back into the activity
1165 // manager with a new orientation. We don't care about that,
1166 // because the activity is not currently running so we are
1167 // just restarting it anyway.
1168 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001169 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001170 mService.mConfiguration,
1171 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001172 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001173 }
1174
1175 r.app = app;
1176 app.waitingToKill = null;
1177 r.launchCount++;
1178 r.lastLaunchTime = SystemClock.uptimeMillis();
1179
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001180 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001181
1182 int idx = app.activities.indexOf(r);
1183 if (idx < 0) {
1184 app.activities.add(r);
1185 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001186 mService.updateLruProcessLocked(app, true, null);
1187 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001188
Craig Mautner15df08a2015-04-01 12:17:18 -07001189 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001190 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1191 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001192 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001193 }
1194
1195 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001196 try {
1197 if (app.thread == null) {
1198 throw new RemoteException();
1199 }
1200 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001201 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 if (andResume) {
1203 results = r.results;
1204 newIntents = r.newIntents;
1205 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001206 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1207 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1208 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001209 if (andResume) {
1210 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1211 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001212 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001213 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001214 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001215 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001216 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001217 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001218 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001219 r.sleeping = false;
1220 r.forceNewConfig = false;
1221 mService.showAskCompatModeDialogLocked(r);
1222 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001223 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001224 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1225 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1226 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001227 final String profileFile = mService.mProfileFile;
1228 if (profileFile != null) {
1229 ParcelFileDescriptor profileFd = mService.mProfileFd;
1230 if (profileFd != null) {
1231 try {
1232 profileFd = profileFd.dup();
1233 } catch (IOException e) {
1234 if (profileFd != null) {
1235 try {
1236 profileFd.close();
1237 } catch (IOException o) {
1238 }
1239 profileFd = null;
1240 }
1241 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001242 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001243
1244 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1245 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001246 }
1247 }
1248 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001249
Craig Mautner2568c3a2015-03-26 14:22:34 -07001250 if (andResume) {
1251 app.hasShownUi = true;
1252 app.pendingUiClean = true;
1253 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001254 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001256 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001257 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001258 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001259 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001260
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001261 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 // This may be a heavy-weight process! Note that the package
1263 // manager will ensure that only activity can run in the main
1264 // process of the .apk, which is the only thing that will be
1265 // considered heavy-weight.
1266 if (app.processName.equals(app.info.packageName)) {
1267 if (mService.mHeavyWeightProcess != null
1268 && mService.mHeavyWeightProcess != app) {
1269 Slog.w(TAG, "Starting new heavy weight process " + app
1270 + " when already running "
1271 + mService.mHeavyWeightProcess);
1272 }
1273 mService.mHeavyWeightProcess = app;
1274 Message msg = mService.mHandler.obtainMessage(
1275 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1276 msg.obj = r;
1277 mService.mHandler.sendMessage(msg);
1278 }
1279 }
1280
1281 } catch (RemoteException e) {
1282 if (r.launchFailed) {
1283 // This is the second time we failed -- finish activity
1284 // and give up.
1285 Slog.e(TAG, "Second failure launching "
1286 + r.intent.getComponent().flattenToShortString()
1287 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001288 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001289 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1290 "2nd-crash", false);
1291 return false;
1292 }
1293
1294 // This is the first time we failed -- restart process and
1295 // retry.
1296 app.activities.remove(r);
1297 throw e;
1298 }
1299
1300 r.launchFailed = false;
1301 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001302 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 }
1304
1305 if (andResume) {
1306 // As part of the process of launching, ActivityThread also performs
1307 // a resume.
1308 stack.minimalResumeActivityLocked(r);
1309 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001310 // This activity is not starting in the resumed state... which should look like we asked
1311 // it to pause+stop (but remain visible), and it has done so and reported back the
1312 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001313 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001314 "Moving to PAUSED: " + r + " (starting in paused state)");
1315 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001316 }
1317
1318 // Launch the new version setup screen if needed. We do this -after-
1319 // launching the initial activity (that is, home), so that it can have
1320 // a chance to initialize itself while in the background, making the
1321 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001322 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001323 mService.startSetupActivityLocked();
1324 }
1325
Dianne Hackborn465fa392014-09-14 14:21:18 -07001326 // Update any services we are bound to that might care about whether
1327 // their client may have activities.
1328 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1329
Craig Mautner2420ead2013-04-01 17:13:20 -07001330 return true;
1331 }
1332
Craig Mautnere79d42682013-04-01 19:01:53 -07001333 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001334 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001335 // Is this activity's application already running?
1336 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001337 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001338
1339 r.task.stack.setLaunchTime(r);
1340
1341 if (app != null && app.thread != null) {
1342 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001343 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1344 || !"android".equals(r.info.packageName)) {
1345 // Don't add this if it is a platform component that is marked
1346 // to run in multiple processes, because this is actually
1347 // part of the framework so doesn't make sense to track as a
1348 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001349 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1350 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001351 }
George Mount2c92c972014-03-20 09:38:23 -07001352 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001353 return;
1354 } catch (RemoteException e) {
1355 Slog.w(TAG, "Exception when starting activity "
1356 + r.intent.getComponent().flattenToShortString(), e);
1357 }
1358
1359 // If a dead object exception was thrown -- fall through to
1360 // restart the application.
1361 }
1362
1363 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001364 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001365 }
1366
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001367 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001368 String resultWho, int requestCode, int callingPid, int callingUid,
1369 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001370 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001371 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1372 callingUid);
1373 if (startAnyPerm == PERMISSION_GRANTED) {
1374 return true;
1375 }
1376 final int componentRestriction = getComponentRestrictionForCallingPackage(
1377 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1378 final int actionRestriction = getActionRestrictionForCallingPackage(
1379 intent.getAction(), callingPackage, callingPid, callingUid);
1380 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1381 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1382 if (resultRecord != null) {
1383 resultStack.sendActivityResultLocked(-1,
1384 resultRecord, resultWho, requestCode,
1385 Activity.RESULT_CANCELED, null);
1386 }
1387 final String msg;
1388 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1389 msg = "Permission Denial: starting " + intent.toString()
1390 + " from " + callerApp + " (pid=" + callingPid
1391 + ", uid=" + callingUid + ")" + " with revoked permission "
1392 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1393 } else if (!aInfo.exported) {
1394 msg = "Permission Denial: starting " + intent.toString()
1395 + " from " + callerApp + " (pid=" + callingPid
1396 + ", uid=" + callingUid + ")"
1397 + " not exported from uid " + aInfo.applicationInfo.uid;
1398 } else {
1399 msg = "Permission Denial: starting " + intent.toString()
1400 + " from " + callerApp + " (pid=" + callingPid
1401 + ", uid=" + callingUid + ")"
1402 + " requires " + aInfo.permission;
1403 }
1404 Slog.w(TAG, msg);
1405 throw new SecurityException(msg);
1406 }
1407
1408 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1409 final String message = "Appop Denial: starting " + intent.toString()
1410 + " from " + callerApp + " (pid=" + callingPid
1411 + ", uid=" + callingUid + ")"
1412 + " requires " + AppOpsManager.permissionToOp(
1413 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1414 Slog.w(TAG, message);
1415 return false;
1416 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1417 final String message = "Appop Denial: starting " + intent.toString()
1418 + " from " + callerApp + " (pid=" + callingPid
1419 + ", uid=" + callingUid + ")"
1420 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1421 Slog.w(TAG, message);
1422 return false;
1423 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001424 if (options != null && options.getLaunchTaskId() != -1) {
1425 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1426 callingPid, callingUid);
1427 if (startInTaskPerm != PERMISSION_GRANTED) {
1428 final String msg = "Permission Denial: starting " + intent.toString()
1429 + " from " + callerApp + " (pid=" + callingPid
1430 + ", uid=" + callingUid + ") with launchTaskId="
1431 + options.getLaunchTaskId();
1432 Slog.w(TAG, msg);
1433 throw new SecurityException(msg);
1434 }
1435 }
1436
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001437 return true;
1438 }
1439
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001440 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001441 final long identity = Binder.clearCallingIdentity();
1442 try {
1443 return UserManager.get(mService.mContext).getUserInfo(userId);
1444 } finally {
1445 Binder.restoreCallingIdentity(identity);
1446 }
1447 }
1448
Svet Ganov99b60432015-06-27 13:15:22 -07001449 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001450 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001451 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1452 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001453 == PackageManager.PERMISSION_DENIED) {
1454 return ACTIVITY_RESTRICTION_PERMISSION;
1455 }
1456
Christopher Tateff7add02015-08-17 10:23:22 -07001457 if (activityInfo.permission == null) {
1458 return ACTIVITY_RESTRICTION_NONE;
1459 }
1460
Svet Ganov99b60432015-06-27 13:15:22 -07001461 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1462 if (opCode == AppOpsManager.OP_NONE) {
1463 return ACTIVITY_RESTRICTION_NONE;
1464 }
1465
1466 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1467 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001468 if (!ignoreTargetSecurity) {
1469 return ACTIVITY_RESTRICTION_APPOP;
1470 }
Svet Ganov99b60432015-06-27 13:15:22 -07001471 }
1472
1473 return ACTIVITY_RESTRICTION_NONE;
1474 }
1475
Svetoslav7008b512015-06-24 18:47:07 -07001476 private int getActionRestrictionForCallingPackage(String action,
1477 String callingPackage, int callingPid, int callingUid) {
1478 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001479 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001480 }
1481
1482 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1483 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001484 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001485 }
1486
1487 final PackageInfo packageInfo;
1488 try {
1489 packageInfo = mService.mContext.getPackageManager()
1490 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1491 } catch (PackageManager.NameNotFoundException e) {
1492 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001493 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001494 }
1495
1496 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001497 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001498 }
1499
1500 if (mService.checkPermission(permission, callingPid, callingUid) ==
1501 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001502 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001503 }
1504
1505 final int opCode = AppOpsManager.permissionToOpCode(permission);
1506 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001507 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001508 }
1509
1510 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1511 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001512 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001513 }
1514
Svet Ganov99b60432015-06-27 13:15:22 -07001515 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001516 }
1517
Wale Ogunwaled046a012015-12-24 13:05:59 -08001518 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001519 if (r == null) {
1520 // Not sure what you are trying to do, but it is not going to work...
1521 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001522 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001523 final TaskRecord task = r.task;
1524 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001525 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001526 return false;
1527 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001528 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001529 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001530 }
1531
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001532 void setLaunchSource(int uid) {
1533 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1534 }
1535
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001536 void acquireLaunchWakelock() {
1537 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1538 throw new IllegalStateException("Calling must be system uid");
1539 }
1540 mLaunchingActivity.acquire();
1541 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1542 // To be safe, don't allow the wake lock to be held for too long.
1543 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1544 }
1545 }
1546
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001547 /**
1548 * Called when the frontmost task is idle.
1549 * @return the state of mService.mBooting before this was called.
1550 */
1551 private boolean checkFinishBootingLocked() {
1552 final boolean booting = mService.mBooting;
1553 boolean enableScreen = false;
1554 mService.mBooting = false;
1555 if (!mService.mBooted) {
1556 mService.mBooted = true;
1557 enableScreen = true;
1558 }
1559 if (booting || enableScreen) {
1560 mService.postFinishBooting(booting, enableScreen);
1561 }
1562 return booting;
1563 }
1564
Craig Mautnerf3333272013-04-22 10:55:53 -07001565 // Checked.
1566 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1567 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001568 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001569
Craig Mautnerf3333272013-04-22 10:55:53 -07001570 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001571 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001572 int NS = 0;
1573 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001574 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001575 boolean activityRemoved = false;
1576
Wale Ogunwale7d701172015-03-11 15:36:30 -07001577 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001578 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001579 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1580 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001581 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1582 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001583 if (fromTimeout) {
1584 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001585 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001586
1587 // This is a hack to semi-deal with a race condition
1588 // in the client where it can be constructed with a
1589 // newer configuration from when we asked it to launch.
1590 // We'll update with whatever configuration it now says
1591 // it used to launch.
1592 if (config != null) {
1593 r.configuration = config;
1594 }
1595
1596 // We are now idle. If someone is waiting for a thumbnail from
1597 // us, we can now deliver.
1598 r.idle = true;
1599
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001600 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001601 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001602 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001603 }
1604 }
1605
1606 if (allResumedActivitiesIdle()) {
1607 if (r != null) {
1608 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001609 }
1610
1611 if (mLaunchingActivity.isHeld()) {
1612 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1613 if (VALIDATE_WAKE_LOCK_CALLER &&
1614 Binder.getCallingUid() != Process.myUid()) {
1615 throw new IllegalStateException("Calling must be system uid");
1616 }
1617 mLaunchingActivity.release();
1618 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001619 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001620 }
1621
1622 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001623 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001624 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001625 if ((NF = mFinishingActivities.size()) > 0) {
1626 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001627 mFinishingActivities.clear();
1628 }
1629
Craig Mautnerf3333272013-04-22 10:55:53 -07001630 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001631 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001632 mStartingUsers.clear();
1633 }
1634
Craig Mautnerf3333272013-04-22 10:55:53 -07001635 // Stop any activities that are scheduled to do so but have been
1636 // waiting for the next one to start.
1637 for (int i = 0; i < NS; i++) {
1638 r = stops.get(i);
1639 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001640 if (stack != null) {
1641 if (r.finishing) {
1642 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1643 } else {
1644 stack.stopActivityLocked(r);
1645 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001646 }
1647 }
1648
1649 // Finish any activities that are scheduled to do so but have been
1650 // waiting for the next one to start.
1651 for (int i = 0; i < NF; i++) {
1652 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001653 final ActivityStack stack = r.task.stack;
1654 if (stack != null) {
1655 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1656 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001657 }
1658
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001659 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001660 // Complete user switch
1661 if (startingUsers != null) {
1662 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001663 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001664 }
1665 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001666 }
1667
1668 mService.trimApplications();
1669 //dump();
1670 //mWindowManager.dump();
1671
Craig Mautnerf3333272013-04-22 10:55:53 -07001672 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001673 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001674 }
1675
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001676 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001677 }
1678
Craig Mautner8e569572013-10-11 17:36:59 -07001679 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001680 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001681 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1682 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001683 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1684 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1685 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001686 }
Craig Mautner19091252013-10-05 00:03:53 -07001687 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001688 }
1689
1690 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001691 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1692 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001693 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1694 stacks.get(stackNdx).closeSystemDialogsLocked();
1695 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001696 }
1697 }
1698
Craig Mautner93529a42013-10-04 15:03:13 -07001699 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001700 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001701 }
1702
Craig Mautner8d341ef2013-03-26 09:03:27 -07001703 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001704 * Update the last used stack id for non-current user (current user's last
1705 * used stack is the focused stack)
1706 */
1707 void updateUserStackLocked(int userId, ActivityStack stack) {
1708 if (userId != mCurrentUser) {
1709 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1710 }
1711 }
1712
1713 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001714 * @return true if some activity was finished (or would have finished if doit were true).
1715 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001716 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1717 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001718 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001719 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1720 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001721 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001722 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001723 if (stack.finishDisabledPackageActivitiesLocked(
1724 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001725 didSomething = true;
1726 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001727 }
1728 }
1729 return didSomething;
1730 }
1731
Dianne Hackborna413dc02013-07-12 12:02:55 -07001732 void updatePreviousProcessLocked(ActivityRecord r) {
1733 // Now that this process has stopped, we may want to consider
1734 // it to be the previous app to try to keep around in case
1735 // the user wants to return to it.
1736
1737 // First, found out what is currently the foreground app, so that
1738 // we don't blow away the previous app if this activity is being
1739 // hosted by the process that is actually still the foreground.
1740 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001741 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1742 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001743 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1744 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001745 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001746 if (stack.mResumedActivity != null) {
1747 fgApp = stack.mResumedActivity.app;
1748 } else if (stack.mPausingActivity != null) {
1749 fgApp = stack.mPausingActivity.app;
1750 }
1751 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001752 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001753 }
1754 }
1755
1756 // Now set this one as the previous process, only if that really
1757 // makes sense to.
1758 if (r.app != null && fgApp != null && r.app != fgApp
1759 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001760 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001761 mService.mPreviousProcess = r.app;
1762 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1763 }
1764 }
1765
Wale Ogunwaled046a012015-12-24 13:05:59 -08001766 boolean resumeFocusedStackTopActivityLocked() {
1767 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001768 }
1769
Wale Ogunwaled046a012015-12-24 13:05:59 -08001770 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001771 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001772 if (targetStack != null && isFocusedStack(targetStack)) {
1773 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001774 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001775 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1776 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001777 }
1778
Todd Kennedy39bfee52016-02-24 10:28:21 -08001779 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1780 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1781 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1782 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1783 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1784 }
1785 }
1786 }
1787
Adrian Roos20d7df32016-01-12 18:59:43 +01001788 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1789 TaskRecord finishedTask = null;
1790 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001791 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1792 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001793 final int numStacks = stacks.size();
1794 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1795 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001796 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1797 if (stack == focusedStack || finishedTask == null) {
1798 finishedTask = t;
1799 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001800 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001801 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001802 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001803 }
1804
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001805 void finishVoiceTask(IVoiceInteractionSession session) {
1806 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1807 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1808 final int numStacks = stacks.size();
1809 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1810 final ActivityStack stack = stacks.get(stackNdx);
1811 stack.finishVoiceTask(session);
1812 }
1813 }
1814 }
1815
Andrii Kulianc27916642016-04-12 17:59:27 -07001816 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1817 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001818 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1819 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001820 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001821 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1822 // Caller wants the home activity moved with it. To accomplish this,
1823 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001824 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001825 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001826 if (task.stack == null) {
1827 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1828 + task + " to front. Stack is null");
1829 return;
1830 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001831
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001832 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001833 int stackId = options.getLaunchStackId();
1834 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001835 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001836 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001837 if (stackId == INVALID_STACK_ID) {
1838 stackId = task.getLaunchStackId();
1839 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001840 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001841 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1842 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1843 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001844 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1845 // still need moveTaskToFrontLocked() below for any transition settings.
1846 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001847 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1848 resizeStackLocked(stackId, bounds,
1849 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001850 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001851 } else {
1852 // WM resizeTask must be done after the task is moved to the correct stack,
1853 // because Task's setBounds() also updates dim layer's bounds, but that has
1854 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001855 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001856 false /* relayout */, false /* forced */);
1857 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001858 }
1859 }
1860
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001861 final ActivityRecord r = task.getTopActivity();
1862 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1863 r == null ? null : r.appTimeTracker, reason);
1864
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001865 if (DEBUG_STACK) Slog.d(TAG_STACK,
1866 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001867
Andrii Kulianc27916642016-04-12 17:59:27 -07001868 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1869 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001870 }
1871
Wale Ogunwale854809c2015-12-27 16:18:19 -08001872 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001873 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001874 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001875 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001876 return false;
1877 }
1878 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1879 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001880 }
1881
Craig Mautner967212c2013-04-13 21:10:58 -07001882 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001883 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001884 }
1885
1886 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001887 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1888 if (activityContainer != null) {
1889 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001890 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001891 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001892 return null;
1893 }
1894 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001895 }
1896
Craig Mautner967212c2013-04-13 21:10:58 -07001897 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001898 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001899 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1900 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001901 }
1902 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001903 }
1904
Craig Mautner4a1cb222013-12-04 16:14:06 -08001905 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001906 ActivityRecord homeActivity = getHomeActivity();
1907 if (homeActivity != null) {
1908 return homeActivity.appToken;
1909 }
1910 return null;
1911 }
1912
1913 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001914 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001915 }
1916
1917 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001918 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1919 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1920 final TaskRecord task = tasks.get(taskNdx);
1921 if (task.isHomeTask()) {
1922 final ArrayList<ActivityRecord> activities = task.mActivities;
1923 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1924 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001925 if (r.isHomeActivity()
1926 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001927 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001928 }
1929 }
1930 }
1931 }
1932 return null;
1933 }
1934
Chong Zhangb15758a2015-11-17 12:12:03 -08001935 /**
1936 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1937 * the docked stack itself, or if it's side-by-side to the docked stack.
1938 */
1939 boolean isStackDockedInEffect(int stackId) {
1940 return stackId == DOCKED_STACK_ID ||
1941 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1942 }
1943
Todd Kennedyca4d8422015-01-15 15:19:22 -08001944 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001945 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001946 ActivityContainer activityContainer =
1947 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001948 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001949 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1950 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001951 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001952 return activityContainer;
1953 }
1954
Craig Mautner34b73df2014-01-12 21:11:08 -08001955 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001956 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1957 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1958 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001959 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1960 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001961 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001962 }
1963 }
1964
Craig Mautner95da1082014-02-24 17:54:35 -08001965 void deleteActivityContainer(IActivityContainer container) {
1966 ActivityContainer activityContainer = (ActivityContainer)container;
1967 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001968 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1969 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001970 final int stackId = activityContainer.mStackId;
1971 mActivityContainers.remove(stackId);
1972 mWindowManager.removeStack(stackId);
1973 }
1974 }
1975
Jorim Jaggidc249c42015-12-15 14:57:31 -08001976 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001977 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001978 if (stackId == DOCKED_STACK_ID) {
1979 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1980 preserveWindows);
1981 return;
1982 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001983 final ActivityStack stack = getStack(stackId);
1984 if (stack == null) {
1985 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1986 return;
1987 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001988
Jorim Jaggidc249c42015-12-15 14:57:31 -08001989 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001990 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1991 // stack size changing so things don't get out of sync.
1992 return;
1993 }
1994
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001995 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001996 mWindowManager.deferSurfaceLayout();
1997 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001998 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001999 if (!deferResume) {
2000 ensureConfigurationAndResume(
2001 stack, stack.topRunningActivityLocked(), preserveWindows);
2002 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002003 } finally {
2004 mWindowManager.continueSurfaceLayout();
2005 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002006 }
2007 }
2008
Jorim Jaggi192086e2016-03-11 17:17:03 +01002009 void deferUpdateBounds(int stackId) {
2010 final ActivityStack stack = getStack(stackId);
2011 if (stack != null) {
2012 stack.deferUpdateBounds();
2013 }
2014 }
2015
2016 void continueUpdateBounds(int stackId) {
2017 final ActivityStack stack = getStack(stackId);
2018 if (stack != null) {
2019 stack.continueUpdateBounds();
2020 }
2021 }
2022
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002023 void notifyAppTransitionDone() {
2024 continueUpdateBounds(HOME_STACK_ID);
2025 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2026 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
2027 if (anyTaskForIdLocked(taskId) != null) {
2028 mWindowManager.setTaskDockedResizing(taskId, false);
2029 }
2030 }
2031 mResizingTasksDuringAnimation.clear();
2032 }
2033
Jorim Jaggi192086e2016-03-11 17:17:03 +01002034 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002035 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002036 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002037
Jorim Jaggi192086e2016-03-11 17:17:03 +01002038 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2039 return;
2040 }
2041
Jorim Jaggidc249c42015-12-15 14:57:31 -08002042 mTmpBounds.clear();
2043 mTmpConfigs.clear();
2044 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002045 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002046 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2047 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002048 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002049 final TaskRecord task = tasks.get(i);
2050 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002051 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2052 // For freeform stack we don't adjust the size of the tasks to match that
2053 // of the stack, but we do try to make sure the tasks are still contained
2054 // with the bounds of the stack.
2055 tempRect2.set(task.mBounds);
2056 fitWithinBounds(tempRect2, bounds);
2057 task.updateOverrideConfiguration(tempRect2);
2058 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002059 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002060 }
2061 }
2062
2063 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2064 mTmpBounds.put(task.taskId, task.mBounds);
2065 if (tempTaskInsetBounds != null) {
2066 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2067 }
2068 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002069
2070 // We might trigger a configuration change. Save the current task bounds for freezing.
2071 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002072 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2073 mTmpBounds, mTmpInsetBounds);
2074 stack.setBounds(bounds);
2075 }
2076
2077 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
2078 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08002079 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002080 return;
2081 }
2082 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
2083 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002084 if (!updated) {
2085 resumeFocusedStackTopActivityLocked();
2086 }
2087 }
2088
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002089 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2090 final ActivityStack stack = getStack(fromStackId);
2091 if (stack == null) {
2092 return;
2093 }
2094
2095 mWindowManager.deferSurfaceLayout();
2096 try {
2097 if (fromStackId == DOCKED_STACK_ID) {
2098
2099 // We are moving all tasks from the docked stack to the fullscreen stack,
2100 // which is dismissing the docked stack, so resize all other stacks to
2101 // fullscreen here already so we don't end up with resize trashing.
2102 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2103 if (StackId.isResizeableByDockedStack(i)) {
2104 ActivityStack otherStack = getStack(i);
2105 if (otherStack != null) {
2106 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2107 true /* allowResizeInDockedMode */, DEFER_RESUME);
2108 }
2109 }
2110 }
2111
2112 // Also disable docked stack resizing since we have manually adjusted the
2113 // size of other stacks above and we don't want to trigger a docked stack
2114 // resize when we remove task from it below and it is detached from the
2115 // display because it no longer contains any tasks.
2116 mAllowDockedStackResize = false;
2117 }
2118 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2119 final int size = tasks.size();
2120 if (onTop) {
2121 for (int i = 0; i < size; i++) {
2122 moveTaskToStackLocked(tasks.get(i).taskId,
2123 FULLSCREEN_WORKSPACE_STACK_ID, onTop, !FORCE_FOCUS,
2124 "moveTasksToFullscreenStack", ANIMATE);
2125 }
2126 } else {
2127 for (int i = size - 1; i >= 0; i--) {
2128 positionTaskInStackLocked(tasks.get(i).taskId,
2129 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2130 }
2131 }
2132 } finally {
2133 mAllowDockedStackResize = true;
2134 mWindowManager.continueSurfaceLayout();
2135 }
2136 }
2137
Jorim Jaggidc249c42015-12-15 14:57:31 -08002138 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2139 Rect tempDockedTaskInsetBounds,
2140 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002141
2142 if (!mAllowDockedStackResize) {
2143 // Docked stack resize currently disabled.
2144 return;
2145 }
2146
Jorim Jaggidc249c42015-12-15 14:57:31 -08002147 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2148 if (stack == null) {
2149 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2150 return;
2151 }
2152
2153 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2154 mWindowManager.deferSurfaceLayout();
2155 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002156 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2157 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002158 ActivityRecord r = stack.topRunningActivityLocked();
2159 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2160 tempDockedTaskInsetBounds);
2161
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002162 // TODO: Checking for isAttached might not be needed as if the user passes in null
2163 // dockedBounds then they want the docked stack to be dismissed.
2164 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2165 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002166 // In this case we make all other static stacks fullscreen and move all
2167 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002168 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002169
2170 // stack shouldn't contain anymore activities, so nothing to resume.
2171 r = null;
2172 } else {
2173 // Docked stacks occupy a dedicated region on screen so the size of all other
2174 // static stacks need to be adjusted so they don't overlap with the docked stack.
2175 // We get the bounds to use from window manager which has been adjusted for any
2176 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002177 mWindowManager.getStackDockedModeBounds(
2178 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002179 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002180 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2181 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2182 tempOtherTaskInsetBounds, preserveWindows,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002183 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002184 }
2185 }
2186 }
2187 ensureConfigurationAndResume(stack, r, preserveWindows);
2188 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002189 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002190 mWindowManager.continueSurfaceLayout();
2191 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2192 }
2193
2194 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2195 tempDockedTaskBounds != null
2196 || tempDockedTaskInsetBounds != null
2197 || tempOtherTaskBounds != null
2198 || tempOtherTaskInsetBounds != null);
2199 }
2200
Robert Carr0d00c2e2016-02-29 17:45:02 -08002201 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2202 final ActivityStack stack = getStack(PINNED_STACK_ID);
2203 if (stack == null) {
2204 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2205 return;
2206 }
2207 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2208 mWindowManager.deferSurfaceLayout();
2209 try {
2210 ActivityRecord r = stack.topRunningActivityLocked();
2211 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2212 null);
2213 ensureConfigurationAndResume(stack, r, false);
2214 } finally {
2215 mWindowManager.continueSurfaceLayout();
2216 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2217 }
2218 }
2219
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002220 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2221 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002222 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002223 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002224 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002225 }
2226
Chong Zhang87b21722015-09-21 15:39:51 -07002227 // If this is a forced resize, let it go through even if the bounds is not changing,
2228 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002229 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002230 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002231 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002232 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002233 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002234 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002235
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002236 if (!mWindowManager.isValidTaskId(task.taskId)) {
2237 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002238 // All we can do for now is update the bounds so it can be used when the task is
2239 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002240 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002241 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2242 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002243 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002244 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002245 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002246 }
2247
Chong Zhang6de2ae82015-09-30 18:25:21 -07002248 // Do not move the task to another stack here.
2249 // This method assumes that the task is already placed in the right stack.
2250 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002251
Chong Zhang6de2ae82015-09-30 18:25:21 -07002252 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002253
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002254 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002255 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002256 // way and the activity was kept the way it was. If it's false, it means the activity had
2257 // to be relaunched due to configuration change.
2258 boolean kept = true;
2259 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002260 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002261 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002262 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002263 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002264
2265 if (!deferResume) {
2266
2267 // All other activities must be made visible with their correct configuration.
2268 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2269 if (!kept) {
2270 resumeFocusedStackTopActivityLocked();
2271 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002272 }
2273 }
2274 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002275 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002276
2277 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002278 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002279 }
2280
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002281 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002282 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2283 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002284 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002285 }
2286
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002287 ActivityContainer activityContainer = new ActivityContainer(stackId);
2288 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002289 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002290 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002291 }
2292
2293 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002294 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002295 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2296 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002297 break;
2298 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002299 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002300 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002301 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002302 }
2303
Chong Zhang5dcb2752015-08-18 13:50:26 -07002304 /**
2305 * Restores a recent task to a stack
2306 * @param task The recent task to be restored.
2307 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002308 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002309 * @return true if the task has been restored successfully.
2310 */
2311 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2312 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002313 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002314 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2315 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2316 // Put it in the fullscreen stack.
2317 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002318 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002319
Wale Ogunwale706ed792015-08-02 10:29:44 -07002320 if (task.stack != null) {
2321 // Task has already been restored once. See if we need to do anything more
2322 if (task.stack.mStackId == stackId) {
2323 // Nothing else to do since it is already restored in the right stack.
2324 return true;
2325 }
2326 // Remove current stack association, so we can re-associate the task with the
2327 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002328 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002329 }
2330
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002331 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002332 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002333
2334 if (stack == null) {
2335 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002336 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2337 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002338 return false;
2339 }
2340
Wale Ogunwale5f986092015-12-04 15:35:38 -08002341 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002342 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2343 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002344 final ArrayList<ActivityRecord> activities = task.mActivities;
2345 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002346 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002347 }
2348 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002349 }
2350
Wale Ogunwale040b4702015-08-06 18:10:50 -07002351 /**
2352 * Moves the specified task record to the input stack id.
2353 * WARNING: This method performs an unchecked/raw move of the task and
2354 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002355 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002356 * @param task Task to move.
2357 * @param stackId Id of stack to move task to.
2358 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002359 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002360 * @param reason Reason the task is been moved.
2361 * @return The stack the task was moved to.
2362 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002363 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002364 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002365
2366 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2367 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2368 + "support multi-window task=" + task + " to stackId=" + stackId);
2369 }
2370
Chong Zhang02898352015-08-21 17:27:14 -07002371 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002372 final ActivityStack prevStack = task.stack;
2373 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002374 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002375 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2376 // Whenever we are moving the top activity from the front stack we want to make sure to move
2377 // the stack to the front.
2378 final boolean wasFront = isFrontStack(prevStack)
2379 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002380
Chong Zhangd545dce2016-02-29 18:09:17 -08002381 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002382 // We don't allow moving a unresizeable task to the docked stack since the docked
2383 // stack is used for split-screen mode and will cause things like the docked divider to
2384 // show up. We instead leave the task in its current stack or move it to the fullscreen
2385 // stack if it isn't currently in a stack.
2386 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002387 Slog.w(TAG, "Can not move unresizeable task=" + task
2388 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2389 }
2390
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002391 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2392 // if a docked stack is created below which will lead to the stack we are moving from and
2393 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002394 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002395 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002396 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002397 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002398 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002399
2400 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002401 // move focus to the new stack by moving the stack to the front.
2402 stack.moveToFrontAndResumeStateIfNeeded(
2403 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002404
Wale Ogunwale040b4702015-08-06 18:10:50 -07002405 return stack;
2406 }
2407
Chong Zhange4fbd322016-03-01 14:44:03 -08002408 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002409 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002410 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2411 false /* deferResume */);
2412 }
2413
2414 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2415 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002416 final TaskRecord task = anyTaskForIdLocked(taskId);
2417 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002418 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002419 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002420 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002421
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002422 if (task.stack != null && task.stack.mStackId == stackId) {
2423 // You are already in the right stack silly...
2424 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002425 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002426 }
2427
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002428 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2429 throw new IllegalArgumentException("moveTaskToStack:"
2430 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2431 }
2432
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002433 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002434 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002435 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002436 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002437 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002438 // We are about to relaunch the activity because its configuration changed due to
2439 // being maximized, i.e. size change. The activity will first remove the old window
2440 // and then add a new one. This call will tell window manager about this, so it can
2441 // preserve the old window until the new one is drawn. This prevents having a gap
2442 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002443 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002444 // Note here we always set the replacing window first, as the flags might be needed
2445 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002446 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002447 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002448
2449 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002450 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002451 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002452 try {
2453 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002454 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002455 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002456
Wale Ogunwale961f4852016-02-01 20:25:54 -08002457 if (!animate) {
2458 stack.mNoAnimActivities.add(topActivity);
2459 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002460
Wale Ogunwale961f4852016-02-01 20:25:54 -08002461 // We might trigger a configuration change. Save the current task bounds for freezing.
2462 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2463
2464 // Make sure the task has the appropriate bounds/size for the stack it is in.
2465 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002466 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2467 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002468 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2469 Rect bounds = task.getLaunchBounds();
2470 if (bounds == null) {
2471 stack.layoutTaskInStack(task, null);
2472 bounds = task.mBounds;
2473 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002474 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2475 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002476 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002477 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2478 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002479 }
2480 } finally {
2481 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002482 }
2483
2484 if (mightReplaceWindow) {
2485 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2486 // window), we need to clear the replace window settings. Otherwise, we schedule a
2487 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002488 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002489 }
2490
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002491 if (!deferResume) {
2492
2493 // The task might have already been running and its visibility needs to be synchronized with
2494 // the visibility of the stack / windows.
2495 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2496 resumeFocusedStackTopActivityLocked();
2497 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002498
Jorim Jaggid53f0922016-04-06 22:16:23 -07002499 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002500
2501 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002502 }
2503
Wale Ogunwale079a0042015-10-24 11:44:07 -07002504 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2505 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2506 if (stack == null) {
2507 throw new IllegalArgumentException(
2508 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2509 }
2510
2511 final ActivityRecord r = stack.topRunningActivityLocked();
2512 if (r == null) {
2513 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2514 + " in stack=" + stack);
2515 return false;
2516 }
2517
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002518 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002519 Slog.w(TAG,
2520 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002521 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002522 return false;
2523 }
2524
Wale Ogunwale480dca02016-02-06 13:58:29 -08002525 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002526 return true;
2527 }
2528
Wale Ogunwale480dca02016-02-06 13:58:29 -08002529 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2530 mWindowManager.deferSurfaceLayout();
2531 try {
2532 final TaskRecord task = r.task;
2533
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002534 if (r == task.stack.getVisibleBehindActivity()) {
2535 // An activity can't be pinned and visible behind at the same time. Go ahead and
2536 // release it from been visible behind before pinning.
2537 requestVisibleBehindLocked(r, false);
2538 }
2539
Wale Ogunwale480dca02016-02-06 13:58:29 -08002540 // Need to make sure the pinned stack exist so we can resize it below...
2541 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2542
2543 // Resize the pinned stack to match the current size of the task the activity we are
2544 // going to be moving is currently contained in. We do this to have the right starting
2545 // animation bounds for the pinned stack to the desired bounds the caller wants.
2546 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2547 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002548 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002549
2550 if (task.mActivities.size() == 1) {
2551 // There is only one activity in the task. So, we can just move the task over to
2552 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002553 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2554 // Move the home stack forward if the task we just moved to the pinned stack
2555 // was launched from home so home should be visible behind it.
2556 moveHomeStackToFront(reason);
2557 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002558 moveTaskToStackLocked(
2559 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2560 } else {
2561 stack.moveActivityToStack(r);
2562 }
2563 } finally {
2564 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002565 }
2566
Wale Ogunwale480dca02016-02-06 13:58:29 -08002567 // The task might have already been running and its visibility needs to be synchronized
2568 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002569 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002570 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002571
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002572 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002573 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002574 }
2575
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002576 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2577 final TaskRecord task = anyTaskForIdLocked(taskId);
2578 if (task == null) {
2579 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2580 return;
2581 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002582 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2583
2584 task.updateOverrideConfigurationForStack(stack);
2585
2586 mWindowManager.positionTaskInStack(
2587 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002588 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002589 // The task might have already been running and its visibility needs to be synchronized with
2590 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002591 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002592 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002593 }
2594
Craig Mautnerac6f8432013-07-17 13:24:59 -07002595 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002596 mTmpFindTaskResult.r = null;
2597 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002598 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002599 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2600 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002601 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2602 final ActivityStack stack = stacks.get(stackNdx);
2603 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002604 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002605 continue;
2606 }
2607 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002608 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2609 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002610 continue;
2611 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002612 stack.findTaskLocked(r, mTmpFindTaskResult);
2613 // It is possible to have task in multiple stacks with the same root affinity.
2614 // If the match we found was based on root affinity we keep on looking to see if
2615 // there is a better match in another stack. We eventually return the match based
2616 // on root affinity if we don't find a better match.
2617 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2618 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002619 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002620 }
2621 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002622 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2623 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002624 }
2625
2626 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002627 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2628 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002629 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2630 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2631 if (ar != null) {
2632 return ar;
2633 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002634 }
2635 }
2636 return null;
2637 }
2638
Craig Mautner8d341ef2013-03-26 09:03:27 -07002639 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002640 scheduleSleepTimeout();
2641 if (!mGoingToSleep.isHeld()) {
2642 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002643 if (mLaunchingActivity.isHeld()) {
2644 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2645 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002646 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002647 mLaunchingActivity.release();
2648 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002649 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002650 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002651 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002652 }
2653
2654 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002655 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002656
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002657 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002658 final long endTime = System.currentTimeMillis() + timeout;
2659 while (true) {
2660 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002661 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2662 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002663 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2664 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2665 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002666 }
2667 if (cantShutdown) {
2668 long timeRemaining = endTime - System.currentTimeMillis();
2669 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002670 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002671 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002672 } catch (InterruptedException e) {
2673 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002674 } else {
2675 Slog.w(TAG, "Activity manager shutdown timed out");
2676 timedout = true;
2677 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002678 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002679 } else {
2680 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002681 }
2682 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002683
2684 // Force checkReadyForSleep to complete.
2685 mSleepTimeout = true;
2686 checkReadyForSleepLocked();
2687
Craig Mautner8d341ef2013-03-26 09:03:27 -07002688 return timedout;
2689 }
2690
2691 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002692 removeSleepTimeouts();
2693 if (mGoingToSleep.isHeld()) {
2694 mGoingToSleep.release();
2695 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002696 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2697 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002698 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2699 final ActivityStack stack = stacks.get(stackNdx);
2700 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002701 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002702 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002703 }
Craig Mautner5314a402013-09-26 12:40:16 -07002704 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002705 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002706 mGoingToSleepActivities.clear();
2707 }
2708
2709 void activitySleptLocked(ActivityRecord r) {
2710 mGoingToSleepActivities.remove(r);
2711 checkReadyForSleepLocked();
2712 }
2713
2714 void checkReadyForSleepLocked() {
2715 if (!mService.isSleepingOrShuttingDown()) {
2716 // Do not care.
2717 return;
2718 }
2719
2720 if (!mSleepTimeout) {
2721 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2723 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2725 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2726 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002727 }
2728
2729 if (mStoppingActivities.size() > 0) {
2730 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002731 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002732 + mStoppingActivities.size() + " activities");
2733 scheduleIdleLocked();
2734 dontSleep = true;
2735 }
2736
2737 if (mGoingToSleepActivities.size() > 0) {
2738 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002739 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002740 + mGoingToSleepActivities.size() + " activities");
2741 dontSleep = true;
2742 }
2743
2744 if (dontSleep) {
2745 return;
2746 }
2747 }
2748
Craig Mautnere0a38842013-12-16 16:14:02 -08002749 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2750 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002751 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2752 stacks.get(stackNdx).goToSleep();
2753 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002754 }
2755
2756 removeSleepTimeouts();
2757
2758 if (mGoingToSleep.isHeld()) {
2759 mGoingToSleep.release();
2760 }
2761 if (mService.mShuttingDown) {
2762 mService.notifyAll();
2763 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002764 }
2765
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002766 boolean reportResumedActivityLocked(ActivityRecord r) {
2767 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002768 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002769 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002770 }
2771 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002772 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002773 mWindowManager.executeAppTransition();
2774 return true;
2775 }
2776 return false;
2777 }
2778
Craig Mautner8d341ef2013-03-26 09:03:27 -07002779 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002780 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2781 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002782 int stackNdx = stacks.size() - 1;
2783 while (stackNdx >= 0) {
2784 stacks.get(stackNdx).handleAppCrashLocked(app);
2785 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002786 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002787 }
2788 }
2789
Jose Lima4b6c6692014-08-12 17:41:12 -07002790 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002791 final ActivityStack stack = r.task.stack;
2792 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002793 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2794 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002795 return false;
2796 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002797
2798 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2799 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2800 + " visible=" + visible + " stackId=" + stack.mStackId
2801 + " can't contain visible behind activities");
2802 return false;
2803 }
2804
Jose Lima4b6c6692014-08-12 17:41:12 -07002805 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002806 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2807 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002808
2809 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002810 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002811 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002812 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002813 return true;
2814 }
2815
2816 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002817 if (visible && top.fullscreen) {
2818 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002819 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2820 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2821 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2822 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002823 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002824 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2825 // Only the activity set as currently visible behind should actively reset its
2826 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002827 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2828 "requestVisibleBehind: returning visible=" + visible
2829 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2830 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002831 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002832 }
2833
Jose Lima4b6c6692014-08-12 17:41:12 -07002834 stack.setVisibleBehindActivity(visible ? r : null);
2835 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002836 // If there is a translucent home activity, we need to force it stop being translucent,
2837 // because we can't depend on the application to necessarily perform that operation.
2838 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002839 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002840 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002841 mService.convertFromTranslucent(next.appToken);
2842 }
2843 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002844 if (top.app != null && top.app.thread != null) {
2845 // Notify the top app of the change.
2846 try {
2847 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2848 } catch (RemoteException e) {
2849 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002850 }
2851 return true;
2852 }
2853
Craig Mautnerbb742462014-07-07 15:28:55 -07002854 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2855 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002856 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002857 final ActivityStack stack = task.stack;
2858
2859 r.mLaunchTaskBehind = false;
2860 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002861 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002862 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002863 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002864
2865 // When launching tasks behind, update the last active time of the top task after the new
2866 // task has been shown briefly
2867 final ActivityRecord top = stack.topActivity();
2868 if (top != null) {
2869 top.task.touchActiveTime();
2870 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002871 }
2872
2873 void scheduleLaunchTaskBehindComplete(IBinder token) {
2874 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2875 }
2876
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002877 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2878 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002879 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002880 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2881 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002882 final int topStackNdx = stacks.size() - 1;
2883 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2884 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002885 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002886 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002887 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002888 }
2889
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002890 void invalidateTaskLayers() {
2891 mTaskLayersChanged = true;
2892 }
2893
2894 void rankTaskLayersIfNeeded() {
2895 if (!mTaskLayersChanged) {
2896 return;
2897 }
2898 mTaskLayersChanged = false;
2899 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2900 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2901 int baseLayer = 0;
2902 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2903 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2904 }
2905 }
2906 }
2907
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002908 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2909 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2910 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2911 final int topStackNdx = stacks.size() - 1;
2912 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2913 final ActivityStack stack = stacks.get(stackNdx);
2914 stack.clearOtherAppTimeTrackers(except);
2915 }
2916 }
2917 }
2918
Craig Mautner8d341ef2013-03-26 09:03:27 -07002919 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002920 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2921 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002922 final int numStacks = stacks.size();
2923 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2924 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002925 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002927 }
2928 }
2929
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002930 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2931 // Examine all activities currently running in the process.
2932 TaskRecord firstTask = null;
2933 // Tasks is non-null only if two or more tasks are found.
2934 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002935 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2936 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002937 ActivityRecord r = app.activities.get(i);
2938 // First, if we find an activity that is in the process of being destroyed,
2939 // then we just aren't going to do anything for now; we want things to settle
2940 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002941 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002942 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002943 return;
2944 }
2945 // Don't consider any activies that are currently not in a state where they
2946 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002947 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2948 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002949 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002950 continue;
2951 }
2952 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002953 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002954 + " from " + r);
2955 if (firstTask == null) {
2956 firstTask = r.task;
2957 } else if (firstTask != r.task) {
2958 if (tasks == null) {
2959 tasks = new ArraySet<>();
2960 tasks.add(firstTask);
2961 }
2962 tasks.add(r.task);
2963 }
2964 }
2965 }
2966 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002967 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002968 return;
2969 }
2970 // If we have activities in multiple tasks that are in a position to be destroyed,
2971 // let's iterate through the tasks and release the oldest one.
2972 final int numDisplays = mActivityDisplays.size();
2973 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2974 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2975 // Step through all stacks starting from behind, to hit the oldest things first.
2976 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2977 final ActivityStack stack = stacks.get(stackNdx);
2978 // Try to release activities in this stack; if we manage to, we are done.
2979 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2980 return;
2981 }
2982 }
2983 }
2984 }
2985
Amith Yamasani37a40c22015-06-17 13:25:42 -07002986 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002987 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002988 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002989 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002990
Craig Mautner858d8a62013-04-23 17:08:34 -07002991 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002992 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2993 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002994 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002995 final ActivityStack stack = stacks.get(stackNdx);
2996 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002997 TaskRecord task = stack.topTask();
2998 if (task != null) {
2999 mWindowManager.moveTaskToTop(task.taskId);
3000 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003001 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003002 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003003
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003004 ActivityStack stack = getStack(restoreStackId);
3005 if (stack == null) {
3006 stack = mHomeStack;
3007 }
3008 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003009 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003010 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003011 } else {
3012 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003013 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003014 }
Craig Mautner93529a42013-10-04 15:03:13 -07003015 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003016 }
3017
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003018 /** Checks whether the userid is a profile of the current user. */
3019 boolean isCurrentProfileLocked(int userId) {
3020 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003021 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003022 }
3023
Chong Zhang45c25ce2015-08-10 22:18:26 -07003024 /** Checks whether the activity should be shown for current user. */
3025 boolean okToShowLocked(ActivityRecord r) {
3026 return r != null && (isCurrentProfileLocked(r.userId)
3027 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3028 }
3029
Craig Mautnerde4ef022013-04-07 19:01:33 -07003030 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003031 ArrayList<ActivityRecord> stops = null;
3032
3033 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003034 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3035 ActivityRecord s = mStoppingActivities.get(activityNdx);
3036 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003037 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003038 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3039 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003040 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003041 if (s.finishing) {
3042 // If this activity is finishing, it is sitting on top of
3043 // everyone else but we now know it is no longer needed...
3044 // so get rid of it. Otherwise, we need to go through the
3045 // normal flow and hide it once we determine that it is
3046 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003047 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003048 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003049 }
3050 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003051 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003052 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003053 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003054 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003055 }
3056 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003057 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003058 }
3059 }
3060
3061 return stops;
3062 }
3063
Craig Mautnercf910b02013-04-23 11:23:27 -07003064 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003065 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3066 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3067 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3068 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003069 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003070 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003071 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003072 if (r == null) Slog.e(TAG,
3073 "validateTop...: null top activity, stack=" + stack);
3074 else {
3075 final ActivityRecord pausing = stack.mPausingActivity;
3076 if (pausing != null && pausing == r) Slog.e(TAG,
3077 "validateTop...: top stack has pausing activity r=" + r
3078 + " state=" + state);
3079 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3080 "validateTop...: activity in front not resumed r=" + r
3081 + " state=" + state);
3082 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003083 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003084 final ActivityRecord resumed = stack.mResumedActivity;
3085 if (resumed != null && resumed == r) Slog.e(TAG,
3086 "validateTop...: back stack has resumed activity r=" + r
3087 + " state=" + state);
3088 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3089 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003090 }
3091 }
3092 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003093 }
3094
Craig Mautnere0570202015-05-13 13:06:11 -07003095 private String lockTaskModeToString() {
3096 switch (mLockTaskModeState) {
3097 case LOCK_TASK_MODE_LOCKED:
3098 return "LOCKED";
3099 case LOCK_TASK_MODE_PINNED:
3100 return "PINNED";
3101 case LOCK_TASK_MODE_NONE:
3102 return "NONE";
3103 default: return "unknown=" + mLockTaskModeState;
3104 }
3105 }
3106
Craig Mautner27084302013-03-25 08:05:25 -07003107 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003108 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003109 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003110 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003111 pw.print(prefix);
3112 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003113 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003114 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003115 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3116 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3117 if (packages.size() > 0) {
3118 pw.println(" mLockTaskPackages (userId:packages)=");
3119 for (int i = 0; i < packages.size(); ++i) {
3120 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3121 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3122 }
3123 }
3124 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003125 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003126
Craig Mautner20e72272013-04-01 13:45:53 -07003127 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003128 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003129 }
3130
Dianne Hackborn390517b2013-05-30 15:03:32 -07003131 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3132 boolean needSep, String prefix) {
3133 if (activity != null) {
3134 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3135 if (needSep) {
3136 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003137 }
3138 pw.print(prefix);
3139 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003140 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003141 }
3142 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003143 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003144 }
3145
Craig Mautner8d341ef2013-03-26 09:03:27 -07003146 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3147 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003148 boolean printed = false;
3149 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003150 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3151 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003152 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003153 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003154 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003155 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003156 final ActivityStack stack = stacks.get(stackNdx);
3157 StringBuilder stackHeader = new StringBuilder(128);
3158 stackHeader.append(" Stack #");
3159 stackHeader.append(stack.mStackId);
3160 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003161 stackHeader.append("\n");
3162 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3163 stackHeader.append("\n");
3164 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003165 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3166 needSep, stackHeader.toString());
3167 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3168 !dumpAll, false, dumpPackage, true,
3169 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003170
Craig Mautner4a1cb222013-12-04 16:14:06 -08003171 needSep = printed;
3172 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3173 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003174 if (pr) {
3175 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003176 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003177 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003178 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3179 " mResumedActivity: ");
3180 if (pr) {
3181 printed = true;
3182 needSep = false;
3183 }
3184 if (dumpAll) {
3185 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3186 " mLastPausedActivity: ");
3187 if (pr) {
3188 printed = true;
3189 needSep = true;
3190 }
3191 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3192 needSep, " mLastNoHistoryActivity: ");
3193 }
3194 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003195 }
3196 }
3197
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003198 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3199 false, dumpPackage, true, " Activities waiting to finish:", null);
3200 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3201 false, dumpPackage, true, " Activities waiting to stop:", null);
3202 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3203 false, dumpPackage, true, " Activities waiting for another to become visible:",
3204 null);
3205 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3206 false, dumpPackage, true, " Activities waiting to sleep:", null);
3207 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3208 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003209
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003210 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003211 }
3212
Dianne Hackborn390517b2013-05-30 15:03:32 -07003213 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003214 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003215 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003216 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003217 String innerPrefix = null;
3218 String[] args = null;
3219 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003220 for (int i=list.size()-1; i>=0; i--) {
3221 final ActivityRecord r = list.get(i);
3222 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3223 continue;
3224 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003225 if (innerPrefix == null) {
3226 innerPrefix = prefix + " ";
3227 args = new String[0];
3228 }
3229 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003230 final boolean full = !brief && (complete || !r.isInHistory());
3231 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003232 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003233 needNL = false;
3234 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003235 if (header1 != null) {
3236 pw.println(header1);
3237 header1 = null;
3238 }
3239 if (header2 != null) {
3240 pw.println(header2);
3241 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003242 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003243 if (lastTask != r.task) {
3244 lastTask = r.task;
3245 pw.print(prefix);
3246 pw.print(full ? "* " : " ");
3247 pw.println(lastTask);
3248 if (full) {
3249 lastTask.dump(pw, prefix + " ");
3250 } else if (complete) {
3251 // Complete + brief == give a summary. Isn't that obvious?!?
3252 if (lastTask.intent != null) {
3253 pw.print(prefix); pw.print(" ");
3254 pw.println(lastTask.intent.toInsecureStringWithClip());
3255 }
3256 }
3257 }
3258 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3259 pw.print(" #"); pw.print(i); pw.print(": ");
3260 pw.println(r);
3261 if (full) {
3262 r.dump(pw, innerPrefix);
3263 } else if (complete) {
3264 // Complete + brief == give a summary. Isn't that obvious?!?
3265 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3266 if (r.app != null) {
3267 pw.print(innerPrefix); pw.println(r.app);
3268 }
3269 }
3270 if (client && r.app != null && r.app.thread != null) {
3271 // flush anything that is already in the PrintWriter since the thread is going
3272 // to write to the file descriptor directly
3273 pw.flush();
3274 try {
3275 TransferPipe tp = new TransferPipe();
3276 try {
3277 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3278 r.appToken, innerPrefix, args);
3279 // Short timeout, since blocking here can
3280 // deadlock with the application.
3281 tp.go(fd, 2000);
3282 } finally {
3283 tp.kill();
3284 }
3285 } catch (IOException e) {
3286 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3287 } catch (RemoteException e) {
3288 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3289 }
3290 needNL = true;
3291 }
3292 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003293 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003294 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003295
Craig Mautnerf3333272013-04-22 10:55:53 -07003296 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003297 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3298 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003299 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3300 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003301 }
3302
3303 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003304 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003305 }
3306
3307 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003308 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3309 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003310 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3311 }
3312
Craig Mautner05d29032013-05-03 13:40:13 -07003313 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003314 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3315 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3316 }
Craig Mautner05d29032013-05-03 13:40:13 -07003317 }
3318
Craig Mautner0eea92c2013-05-16 13:35:39 -07003319 void removeSleepTimeouts() {
3320 mSleepTimeout = false;
3321 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3322 }
3323
3324 final void scheduleSleepTimeout() {
3325 removeSleepTimeouts();
3326 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3327 }
3328
Craig Mautner4a1cb222013-12-04 16:14:06 -08003329 @Override
3330 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003331 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003332 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3333 }
3334
3335 @Override
3336 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003337 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003338 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3339 }
3340
3341 @Override
3342 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003343 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003344 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3345 }
3346
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003347 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003348 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003349 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003350 newDisplay = mActivityDisplays.get(displayId) == null;
3351 if (newDisplay) {
3352 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003353 if (activityDisplay.mDisplay == null) {
3354 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3355 return;
3356 }
Craig Mautner4504de52013-12-20 09:06:56 -08003357 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003358 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003359 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003360 }
Craig Mautner4504de52013-12-20 09:06:56 -08003361 if (newDisplay) {
3362 mWindowManager.onDisplayAdded(displayId);
3363 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003364 }
3365
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003366 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003367 mDefaultMinimalSizeOfResizeableTask =
3368 mService.mContext.getResources().getDimensionPixelSize(
3369 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003370 }
3371
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003372 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003373 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003374 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3375 if (activityDisplay != null) {
3376 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003377 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003378 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003379 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003380 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003381 }
3382 }
3383 mWindowManager.onDisplayRemoved(displayId);
3384 }
3385
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003386 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003387 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003388 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3389 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003390 // TODO: Update the bounds.
3391 }
3392 }
3393 mWindowManager.onDisplayChanged(displayId);
3394 }
3395
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003396 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003397 StackInfo info = new StackInfo();
3398 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3399 info.displayId = Display.DEFAULT_DISPLAY;
3400 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003401 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003402
3403 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3404 final int numTasks = tasks.size();
3405 int[] taskIds = new int[numTasks];
3406 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003407 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003408 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003409 for (int i = 0; i < numTasks; ++i) {
3410 final TaskRecord task = tasks.get(i);
3411 taskIds[i] = task.taskId;
3412 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3413 : task.realActivity != null ? task.realActivity.flattenToString()
3414 : task.getTopActivity() != null ? task.getTopActivity().packageName
3415 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003416 taskBounds[i] = new Rect();
3417 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003418 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003419 }
3420 info.taskIds = taskIds;
3421 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003422 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003423 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003424 return info;
3425 }
3426
3427 StackInfo getStackInfoLocked(int stackId) {
3428 ActivityStack stack = getStack(stackId);
3429 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003430 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003431 }
3432 return null;
3433 }
3434
3435 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003436 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003437 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3438 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003439 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003440 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003441 }
3442 }
3443 return list;
3444 }
3445
Craig Mautner15df08a2015-04-01 12:17:18 -07003446 TaskRecord getLockedTaskLocked() {
3447 final int top = mLockTaskModeTasks.size() - 1;
3448 if (top >= 0) {
3449 return mLockTaskModeTasks.get(top);
3450 }
3451 return null;
3452 }
3453
3454 boolean isLockedTask(TaskRecord task) {
3455 return mLockTaskModeTasks.contains(task);
3456 }
3457
3458 boolean isLastLockedTask(TaskRecord task) {
3459 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3460 }
3461
3462 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003463 if (!mLockTaskModeTasks.remove(task)) {
3464 return;
3465 }
3466 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3467 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003468 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003469 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3470 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003471 final Message lockTaskMsg = Message.obtain();
3472 lockTaskMsg.arg1 = task.userId;
3473 lockTaskMsg.what = LOCK_TASK_END_MSG;
3474 mHandler.sendMessage(lockTaskMsg);
3475 }
3476 }
3477
Andrii Kulianc27916642016-04-12 17:59:27 -07003478 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3479 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3480 false /* forceNonResizable */);
3481 }
3482
Jorim Jaggid53f0922016-04-06 22:16:23 -07003483 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003484 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003485 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3486 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003487 return;
3488 }
3489
Andrii Kulianc27916642016-04-12 17:59:27 -07003490 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003491 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003492 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3493
Andrii Kulianc27916642016-04-12 17:59:27 -07003494 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3495 // we need to move it to top of fullscreen stack, otherwise it will be covered.
3496 mService.moveTasksToFullscreenStack(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003497 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3498 String packageName = task.getTopActivity() != null
3499 ? task.getTopActivity().appInfo.packageName : null;
3500 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3501 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003502 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003503 }
3504
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003505 void showLockTaskToast() {
3506 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003507 }
3508
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003509 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3510 if (mLockTaskModeTasks.contains(task)) {
3511 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3512 }
3513 }
3514
Craig Mautner432f64e2015-05-20 14:59:57 -07003515 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3516 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003517 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003518 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003519 final TaskRecord lockedTask = getLockedTaskLocked();
3520 if (lockedTask != null) {
3521 removeLockedTaskLocked(lockedTask);
3522 if (!mLockTaskModeTasks.isEmpty()) {
3523 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003524 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3525 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003526 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003527 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003528 return;
3529 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003530 }
Craig Mautnere0570202015-05-13 13:06:11 -07003531 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3532 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003533 return;
3534 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003535
3536 // Should have already been checked, but do it again.
3537 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003538 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3539 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003540 return;
3541 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003542 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003543 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003544 return;
3545 }
3546
3547 if (mLockTaskModeTasks.isEmpty()) {
3548 // First locktask.
3549 final Message lockTaskMsg = Message.obtain();
3550 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3551 lockTaskMsg.arg1 = task.userId;
3552 lockTaskMsg.what = LOCK_TASK_START_MSG;
3553 lockTaskMsg.arg2 = lockTaskModeState;
3554 mHandler.sendMessage(lockTaskMsg);
3555 }
3556 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003557 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3558 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003559 mLockTaskModeTasks.remove(task);
3560 mLockTaskModeTasks.add(task);
3561
3562 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003563 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003564 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003565
3566 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003567 findTaskToMoveToFrontLocked(task, 0, null, reason,
3568 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003569 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003570 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3571 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3572 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003573 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003574 }
3575
3576 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003577 return isLockTaskModeViolation(task, false);
3578 }
3579
3580 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3581 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003582 return false;
3583 }
3584 final int lockTaskAuth = task.mLockTaskAuth;
3585 switch (lockTaskAuth) {
3586 case LOCK_TASK_AUTH_DONT_LOCK:
3587 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003588 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003589 case LOCK_TASK_AUTH_LAUNCHABLE:
3590 case LOCK_TASK_AUTH_WHITELISTED:
3591 return false;
3592 case LOCK_TASK_AUTH_PINNABLE:
3593 // Pinnable tasks can't be launched on top of locktask tasks.
3594 return !mLockTaskModeTasks.isEmpty();
3595 default:
3596 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3597 return true;
3598 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003599 }
3600
Craig Mautner15df08a2015-04-01 12:17:18 -07003601 void onLockTaskPackagesUpdatedLocked() {
3602 boolean didSomething = false;
3603 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3604 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003605 final boolean wasWhitelisted =
3606 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3607 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003608 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003609 final boolean isWhitelisted =
3610 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3611 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3612 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003613 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003614 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3615 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003616 removeLockedTaskLocked(lockedTask);
3617 lockedTask.performClearTaskLocked();
3618 didSomething = true;
3619 }
3620 }
3621 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3622 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3623 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3624 final ActivityStack stack = stacks.get(stackNdx);
3625 stack.onLockTaskPackagesUpdatedLocked();
3626 }
3627 }
Craig Mautnere0570202015-05-13 13:06:11 -07003628 final ActivityRecord r = topRunningActivityLocked();
3629 final TaskRecord task = r != null ? r.task : null;
3630 if (mLockTaskModeTasks.isEmpty() && task != null
3631 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3632 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003633 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3634 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3635 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3636 false);
3637 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003638 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003639 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003640 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003641 }
3642 }
3643
Benjamin Franz43261142015-02-11 15:59:44 +00003644 int getLockTaskModeState() {
3645 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003646 }
3647
Jorim Jaggife89d122015-12-22 16:28:44 +01003648 void activityRelaunchedLocked(IBinder token) {
3649 mWindowManager.notifyAppRelaunchingFinished(token);
3650 }
3651
3652 void activityRelaunchingLocked(ActivityRecord r) {
3653 mWindowManager.notifyAppRelaunching(r.appToken);
3654 }
3655
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003656 void logStackState() {
3657 mActivityMetricsLogger.logWindowState();
3658 }
3659
Andrii Kulian933076d2016-03-29 17:04:42 -07003660 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003661 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3662 final ActivityRecord r = task.mActivities.get(i);
3663 if (r.app != null && r.app.thread != null) {
3664 mMultiWindowModeChangedActivities.add(r);
3665 }
3666 }
3667
3668 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3669 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3670 }
3671 }
3672
Andrii Kulian933076d2016-03-29 17:04:42 -07003673 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003674 final ActivityStack stack = task.stack;
3675 if (prevStack == null || prevStack == stack
3676 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3677 return;
3678 }
3679
3680 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3681 final ActivityRecord r = task.mActivities.get(i);
3682 if (r.app != null && r.app.thread != null) {
3683 mPipModeChangedActivities.add(r);
3684 }
3685 }
3686
3687 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3688 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3689 }
3690 }
3691
Tony Mak853304c2016-04-18 15:17:41 +01003692 void setDockedStackMinimized(boolean minimized) {
3693 mIsDockMinimized = minimized;
3694 if (minimized) {
3695 // Docked stack is not visible, no need to confirm credentials for its top activity.
3696 return;
3697 }
3698 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3699 if (dockedStack == null) {
3700 return;
3701 }
3702 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3703 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3704 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3705 }
3706 }
3707
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003708 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003709
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003710 public ActivityStackSupervisorHandler(Looper looper) {
3711 super(looper);
3712 }
3713
Craig Mautnerf3333272013-04-22 10:55:53 -07003714 void activityIdleInternal(ActivityRecord r) {
3715 synchronized (mService) {
3716 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3717 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003718 }
3719
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003720 @Override
3721 public void handleMessage(Message msg) {
3722 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003723 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3724 synchronized (mService) {
3725 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3726 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003727 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003728 }
3729 }
3730 } break;
3731 case REPORT_PIP_MODE_CHANGED_MSG: {
3732 synchronized (mService) {
3733 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3734 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003735 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003736 }
3737 }
3738 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003739 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003740 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3741 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003742 if (mService.mDidDexOpt) {
3743 mService.mDidDexOpt = false;
3744 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3745 nmsg.obj = msg.obj;
3746 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3747 return;
3748 }
3749 // We don't at this point know if the activity is fullscreen,
3750 // so we need to be conservative and assume it isn't.
3751 activityIdleInternal((ActivityRecord)msg.obj);
3752 } break;
3753 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003754 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003755 activityIdleInternal((ActivityRecord)msg.obj);
3756 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003757 case RESUME_TOP_ACTIVITY_MSG: {
3758 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003759 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003760 }
3761 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003762 case SLEEP_TIMEOUT_MSG: {
3763 synchronized (mService) {
3764 if (mService.isSleepingOrShuttingDown()) {
3765 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3766 mSleepTimeout = true;
3767 checkReadyForSleepLocked();
3768 }
3769 }
3770 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003771 case LAUNCH_TIMEOUT_MSG: {
3772 if (mService.mDidDexOpt) {
3773 mService.mDidDexOpt = false;
3774 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3775 return;
3776 }
3777 synchronized (mService) {
3778 if (mLaunchingActivity.isHeld()) {
3779 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3780 if (VALIDATE_WAKE_LOCK_CALLER
3781 && Binder.getCallingUid() != Process.myUid()) {
3782 throw new IllegalStateException("Calling must be system uid");
3783 }
3784 mLaunchingActivity.release();
3785 }
3786 }
3787 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003788 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003789 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790 } break;
3791 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003792 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003793 } break;
3794 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003795 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003796 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003797 case CONTAINER_CALLBACK_VISIBILITY: {
3798 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003799 final IActivityContainerCallback callback = container.mCallback;
3800 if (callback != null) {
3801 try {
3802 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3803 } catch (RemoteException e) {
3804 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003805 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003806 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003807 case LOCK_TASK_START_MSG: {
3808 // When lock task starts, we disable the status bars.
3809 try {
Jason Monk62515be2014-05-21 16:06:19 -04003810 if (mLockTaskNotify == null) {
3811 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003812 }
Jason Monk62515be2014-05-21 16:06:19 -04003813 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003814 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003815 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003816 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003817 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003818 flags = StatusBarManager.DISABLE_MASK
3819 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003820 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003821 flags = StatusBarManager.DISABLE_MASK
3822 & (~StatusBarManager.DISABLE_BACK)
3823 & (~StatusBarManager.DISABLE_HOME)
3824 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003825 }
3826 getStatusBarService().disable(flags, mToken,
3827 mService.mContext.getPackageName());
3828 }
3829 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003830 if (getDevicePolicyManager() != null) {
3831 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003832 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003833 }
justinzhang5286d3f2014-05-12 17:06:01 -04003834 } catch (RemoteException ex) {
3835 throw new RuntimeException(ex);
3836 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003837 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003838 case LOCK_TASK_END_MSG: {
3839 // When lock task ends, we enable the status bars.
3840 try {
Jason Monk62515be2014-05-21 16:06:19 -04003841 if (getStatusBarService() != null) {
3842 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3843 mService.mContext.getPackageName());
3844 }
3845 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003846 if (getDevicePolicyManager() != null) {
3847 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3848 msg.arg1);
3849 }
Jason Monk62515be2014-05-21 16:06:19 -04003850 if (mLockTaskNotify == null) {
3851 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3852 }
3853 mLockTaskNotify.show(false);
3854 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003855 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003856 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003857 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003858 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003859 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003860 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003861 new LockPatternUtils(mService.mContext)
3862 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003863 }
3864 } catch (SettingNotFoundException e) {
3865 // No setting, don't lock.
3866 }
justinzhang5286d3f2014-05-12 17:06:01 -04003867 } catch (RemoteException ex) {
3868 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003869 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003870 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003871 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003872 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003873 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3874 if (mLockTaskNotify == null) {
3875 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3876 }
3877 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3878 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003879 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3880 final ActivityContainer container = (ActivityContainer) msg.obj;
3881 final IActivityContainerCallback callback = container.mCallback;
3882 if (callback != null) {
3883 try {
3884 callback.onAllActivitiesComplete(container.asBinder());
3885 } catch (RemoteException e) {
3886 }
3887 }
3888 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003889 case LAUNCH_TASK_BEHIND_COMPLETE: {
3890 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003891 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003892 if (r != null) {
3893 handleLaunchTaskBehindCompleteLocked(r);
3894 }
3895 }
3896 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003897
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003898 }
3899 }
3900 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003901
Ying Wangb081a592014-04-22 15:20:16 -07003902 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003903 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3904 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003905 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003906 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003907 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003908 ActivityRecord mParentActivity = null;
3909 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003910
Craig Mautnere3a00d72014-04-16 08:31:19 -07003911 boolean mVisible = true;
3912
Craig Mautner4a1cb222013-12-04 16:14:06 -08003913 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003914 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003915
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003916 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3917 final static int CONTAINER_STATE_NO_SURFACE = 1;
3918 final static int CONTAINER_STATE_FINISHING = 2;
3919 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3920
3921 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003922 synchronized (mService) {
3923 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003924 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003925 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003926 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003927 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003928 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003929
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003930 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003931 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003932 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003933 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003934 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003935 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003936 }
3937
3938 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003939 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003940 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003941 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3942 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003943 return;
3944 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003945 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003946 }
3947 }
3948
3949 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003950 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003951 synchronized (mService) {
3952 if (mActivityDisplay != null) {
3953 return mActivityDisplay.mDisplayId;
3954 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003955 }
3956 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003957 }
3958
Jeff Brownca9bc702014-02-11 14:32:56 -08003959 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003960 public int getStackId() {
3961 synchronized (mService) {
3962 return mStackId;
3963 }
3964 }
3965
3966 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003967 public boolean injectEvent(InputEvent event) {
3968 final long origId = Binder.clearCallingIdentity();
3969 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003970 synchronized (mService) {
3971 if (mActivityDisplay != null) {
3972 return mInputManagerInternal.injectInputEvent(event,
3973 mActivityDisplay.mDisplayId,
3974 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3975 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003976 }
3977 return false;
3978 } finally {
3979 Binder.restoreCallingIdentity(origId);
3980 }
3981 }
3982
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003983 @Override
3984 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003985 synchronized (mService) {
3986 if (mContainerState == CONTAINER_STATE_FINISHING) {
3987 return;
3988 }
3989 mContainerState = CONTAINER_STATE_FINISHING;
3990
Craig Mautnerd163e752014-06-13 17:18:47 -07003991 long origId = Binder.clearCallingIdentity();
3992 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003993 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003994 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003995 } finally {
3996 Binder.restoreCallingIdentity(origId);
3997 }
3998 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003999 }
4000
Craig Mautner60257702014-09-17 15:02:33 -07004001 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004002 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004003 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004004 if (mActivityDisplay != null) {
4005 mActivityDisplay.detachActivitiesLocked(mStack);
4006 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004007 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004008 }
4009 }
4010
4011 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004012 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004013 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004014 }
4015
4016 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004017 public final int startActivityIntentSender(IIntentSender intentSender)
4018 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004019 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4020
4021 if (!(intentSender instanceof PendingIntentRecord)) {
4022 throw new IllegalArgumentException("Bad PendingIntent object");
4023 }
4024
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004025 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004026 Binder.getCallingUid(), mCurrentUser, false,
4027 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004028
4029 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4030 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4031 pendingIntent.key.requestResolvedType);
4032
4033 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4034 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4035 }
4036
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004037 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004038 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004039 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004040 throw new SecurityException(
4041 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4042 }
4043 }
4044
Craig Mautnerdf88d732014-01-27 09:21:32 -08004045 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004046 public IBinder asBinder() {
4047 return this;
4048 }
4049
Craig Mautner4504de52013-12-20 09:06:56 -08004050 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004051 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004052 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004053 }
4054
Craig Mautner4a1cb222013-12-04 16:14:06 -08004055 ActivityStackSupervisor getOuter() {
4056 return ActivityStackSupervisor.this;
4057 }
4058
Craig Mautnerd163e752014-06-13 17:18:47 -07004059 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004060 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004061 }
4062
Craig Mautner6985bad2014-04-21 15:22:06 -07004063 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004064 void setVisible(boolean visible) {
4065 if (mVisible != visible) {
4066 mVisible = visible;
4067 if (mCallback != null) {
4068 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4069 0 /* unused */, this).sendToTarget();
4070 }
4071 }
4072 }
4073
Craig Mautner6985bad2014-04-21 15:22:06 -07004074 void setDrawn() {
4075 }
4076
Craig Mautner1b4bf852014-05-26 15:06:32 -07004077 // You can always start a new task on a regular ActivityStack.
4078 boolean isEligibleForNewTasks() {
4079 return true;
4080 }
4081
Craig Mautnerd163e752014-06-13 17:18:47 -07004082 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004083 detachLocked();
4084 deleteActivityContainer(this);
4085 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004086 }
4087
Craig Mautner34b73df2014-01-12 21:11:08 -08004088 @Override
4089 public String toString() {
4090 return mIdString + (mActivityDisplay == null ? "N" : "A");
4091 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004092 }
4093
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004094 private class VirtualActivityContainer extends ActivityContainer {
4095 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004096 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004097
4098 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4099 super(getNextStackId());
4100 mParentActivity = parent;
4101 mCallback = callback;
4102 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004103 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004104 }
4105
4106 @Override
4107 public void setSurface(Surface surface, int width, int height, int density) {
4108 super.setSurface(surface, width, height, density);
4109
4110 synchronized (mService) {
4111 final long origId = Binder.clearCallingIdentity();
4112 try {
4113 setSurfaceLocked(surface, width, height, density);
4114 } finally {
4115 Binder.restoreCallingIdentity(origId);
4116 }
4117 }
4118 }
4119
4120 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4121 if (mContainerState == CONTAINER_STATE_FINISHING) {
4122 return;
4123 }
4124 VirtualActivityDisplay virtualActivityDisplay =
4125 (VirtualActivityDisplay) mActivityDisplay;
4126 if (virtualActivityDisplay == null) {
4127 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004128 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004129 mActivityDisplay = virtualActivityDisplay;
4130 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004131 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004132 }
4133
4134 if (mSurface != null) {
4135 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004136 }
4137
Craig Mautner6985bad2014-04-21 15:22:06 -07004138 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004139 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004140 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004141 } else {
4142 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004143 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004144 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004145 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004146 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004147 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004148
Craig Mautnerd163e752014-06-13 17:18:47 -07004149 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004150
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004151 if (DEBUG_STACK) Slog.d(TAG_STACK,
4152 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004153 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004154
Craig Mautner6985bad2014-04-21 15:22:06 -07004155 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004156 boolean isAttachedLocked() {
4157 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004158 }
4159
4160 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004161 void setDrawn() {
4162 synchronized (mService) {
4163 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004164 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004165 }
4166 }
4167
Craig Mautner1b4bf852014-05-26 15:06:32 -07004168 // Never start a new task on an ActivityView if it isn't explicitly specified.
4169 @Override
4170 boolean isEligibleForNewTasks() {
4171 return false;
4172 }
4173
Craig Mautnerd163e752014-06-13 17:18:47 -07004174 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004175 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004176 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4177 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4178 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4179 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4180 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004181 }
4182 }
4183
Craig Mautner4a1cb222013-12-04 16:14:06 -08004184 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4185 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004186 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004187 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004188 int mDisplayId;
4189 Display mDisplay;
4190 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004191
Craig Mautner4a1cb222013-12-04 16:14:06 -08004192 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4193 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004194 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004195
Jose Lima4b6c6692014-08-12 17:41:12 -07004196 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004197
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004198 ActivityDisplay() {
4199 }
Craig Mautner4504de52013-12-20 09:06:56 -08004200
Craig Mautner1a70a162014-09-13 12:09:31 -07004201 // After instantiation, check that mDisplay is not null before using this. The alternative
4202 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004203 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004204 final Display display = mDisplayManager.getDisplay(displayId);
4205 if (display == null) {
4206 return;
4207 }
4208 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004209 }
4210
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004211 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004212 mDisplay = display;
4213 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004214 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004215 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004216
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004217 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004218 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004219 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4220 + " onTop=" + onTop);
4221 if (onTop) {
4222 mStacks.add(stack);
4223 } else {
4224 mStacks.add(0, stack);
4225 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004226 }
4227
Craig Mautnere0a38842013-12-16 16:14:02 -08004228 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004229 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004230 + " from displayId=" + mDisplayId);
4231 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004232 }
4233
Jose Lima4b6c6692014-08-12 17:41:12 -07004234 void setVisibleBehindActivity(ActivityRecord r) {
4235 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004236 }
4237
Jose Lima4b6c6692014-08-12 17:41:12 -07004238 boolean hasVisibleBehindActivity() {
4239 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004240 }
4241
Craig Mautner34b73df2014-01-12 21:11:08 -08004242 @Override
4243 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004244 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4245 }
4246 }
4247
4248 class VirtualActivityDisplay extends ActivityDisplay {
4249 VirtualDisplay mVirtualDisplay;
4250
Craig Mautner6985bad2014-04-21 15:22:06 -07004251 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004252 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004253 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4254 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4255 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4256 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004257
4258 init(mVirtualDisplay.getDisplay());
4259
4260 mWindowManager.handleDisplayAdded(mDisplayId);
4261 }
4262
4263 void setSurface(Surface surface) {
4264 if (mVirtualDisplay != null) {
4265 mVirtualDisplay.setSurface(surface);
4266 }
4267 }
4268
4269 @Override
4270 void detachActivitiesLocked(ActivityStack stack) {
4271 super.detachActivitiesLocked(stack);
4272 if (mVirtualDisplay != null) {
4273 mVirtualDisplay.release();
4274 mVirtualDisplay = null;
4275 }
4276 }
4277
4278 @Override
4279 public String toString() {
4280 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004281 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004282 }
Jose Lima58e66d62014-05-27 20:00:27 -07004283
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004284 /**
4285 * Adjust bounds to stay within stack bounds.
4286 *
4287 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4288 * that keep them unchanged, but be contained within the stack bounds.
4289 *
4290 * @param bounds Bounds to be adjusted.
4291 * @param stackBounds Bounds within which the other bounds should remain.
4292 */
4293 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4294 if (stackBounds == null || stackBounds.contains(bounds)) {
4295 return;
4296 }
4297
4298 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4299 final int maxRight = stackBounds.right
4300 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4301 int horizontalDiff = stackBounds.left - bounds.left;
4302 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4303 || (bounds.left + horizontalDiff >= maxRight)) {
4304 horizontalDiff = maxRight - bounds.left;
4305 }
4306 bounds.left += horizontalDiff;
4307 bounds.right += horizontalDiff;
4308 }
4309
4310 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4311 final int maxBottom = stackBounds.bottom
4312 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4313 int verticalDiff = stackBounds.top - bounds.top;
4314 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4315 || (bounds.top + verticalDiff >= maxBottom)) {
4316 verticalDiff = maxBottom - bounds.top;
4317 }
4318 bounds.top += verticalDiff;
4319 bounds.bottom += verticalDiff;
4320 }
4321 }
4322
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004323 ActivityStack findStackBehind(ActivityStack stack) {
4324 // TODO(multi-display): We are only looking for stacks on the default display.
4325 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4326 if (display == null) {
4327 return null;
4328 }
4329 final ArrayList<ActivityStack> stacks = display.mStacks;
4330 for (int i = stacks.size() - 1; i >= 0; i--) {
4331 if (stacks.get(i) == stack && i > 0) {
4332 return stacks.get(i - 1);
4333 }
4334 }
4335 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4336 + " in=" + stacks);
4337 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004338
Jorim Jaggic69bd222016-03-15 14:38:37 +01004339 /**
4340 * Puts a task into resizing mode during the next app transition.
4341 *
4342 * @param taskId the id of the task to put into resizing mode
4343 */
4344 private void setResizingDuringAnimation(int taskId) {
4345 mResizingTasksDuringAnimation.add(taskId);
4346 mWindowManager.setTaskDockedResizing(taskId, true);
4347 }
4348
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004349 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4350 final TaskRecord task;
4351 final int callingUid;
4352 final String callingPackage;
4353 final Intent intent;
4354 final int userId;
4355 final ActivityOptions activityOptions = (bOptions != null)
4356 ? new ActivityOptions(bOptions) : null;
4357 final int launchStackId = (activityOptions != null)
4358 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004359 if (launchStackId == HOME_STACK_ID) {
4360 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4361 + taskId + " can't be launch in the home stack.");
4362 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004363
4364 if (launchStackId == DOCKED_STACK_ID) {
4365 mWindowManager.setDockedStackCreateState(
4366 activityOptions.getDockCreateMode(), null /* initialBounds */);
4367
4368 // Defer updating the stack in which recents is until the app transition is done, to
4369 // not run into issues where we still need to draw the task in recents but the
4370 // docked stack is already created.
4371 deferUpdateBounds(HOME_STACK_ID);
4372 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4373 }
4374
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004375 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4376 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004377 continueUpdateBounds(HOME_STACK_ID);
4378 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004379 throw new IllegalArgumentException(
4380 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4381 }
4382
4383 if (launchStackId != INVALID_STACK_ID) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004384 if (task.stack.mStackId != launchStackId) {
4385 moveTaskToStackLocked(
4386 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4387 ANIMATE);
4388 }
4389 }
4390
4391 // If the user must confirm credentials (e.g. when first launching a work app and the
4392 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4393 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4394 && task.getRootActivity() != null) {
Jorim Jaggi09c49542016-04-09 01:39:40 -07004395 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004396 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
4397
4398 // If we are launching the task in the docked stack, put it into resizing mode so
4399 // the window renders full-screen with the background filling the void. Also only
4400 // call this at the end to make sure that tasks exists on the window manager side.
4401 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004402 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004403 }
4404 return ActivityManager.START_TASK_TO_FRONT;
4405 }
4406 callingUid = task.mCallingUid;
4407 callingPackage = task.mCallingPackage;
4408 intent = task.intent;
4409 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4410 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004411 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4412 null, null, 0, 0, bOptions, userId, null, task);
4413 if (launchStackId == DOCKED_STACK_ID) {
4414 setResizingDuringAnimation(task.taskId);
4415 }
4416 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004417 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004418
4419 /**
4420 * @return a list of activities which are the top ones in each visible stack. The first
4421 * entry will be the focused activity.
4422 */
4423 public List<IBinder> getTopVisibleActivities() {
4424 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4425 if (display == null) {
4426 return Collections.EMPTY_LIST;
4427 }
4428 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4429 final ArrayList<ActivityStack> stacks = display.mStacks;
4430 for (int i = stacks.size() - 1; i >= 0; i--) {
4431 ActivityStack stack = stacks.get(i);
4432 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4433 ActivityRecord top = stack.topActivity();
4434 if (top != null) {
4435 if (stack == mFocusedStack) {
4436 topActivityTokens.add(0, top.appToken);
4437 } else {
4438 topActivityTokens.add(top.appToken);
4439 }
4440 }
4441 }
4442 }
4443 return topActivityTokens;
4444 }
Craig Mautner27084302013-03-25 08:05:25 -07004445}