blob: 7e1662ebaea36b3b1213ab3f97f50307faadb7e6 [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
Svetoslav7008b512015-06-24 18:47:07 -0700246 // Activity actions an app cannot start if it uses a permission which is not granted.
247 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
248 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700249
Svetoslav7008b512015-06-24 18:47:07 -0700250 static {
251 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
252 Manifest.permission.CAMERA);
253 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
254 Manifest.permission.CAMERA);
255 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
256 Manifest.permission.CALL_PHONE);
257 }
258
Svet Ganov99b60432015-06-27 13:15:22 -0700259 /** Action restriction: launching the activity is not restricted. */
260 private static final int ACTIVITY_RESTRICTION_NONE = 0;
261 /** Action restriction: launching the activity is restricted by a permission. */
262 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
263 /** Action restriction: launching the activity is restricted by an app op. */
264 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700265
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700266 // The height/width divide used when fitting a task within a bounds with method
267 // {@link #fitWithinBounds}.
268 // We always want the task to to be visible in the bounds without affecting its size when
269 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
270 // the input bounds right or bottom side minus the width or height divided by this value.
271 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
272
justinzhang5286d3f2014-05-12 17:06:01 -0400273 /** Status Bar Service **/
274 private IBinder mToken = new Binder();
275 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400276 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400277
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700278 // For debugging to make sure the caller when acquiring/releasing our
279 // wake lock is the system process.
280 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800281 /** The number of distinct task ids that can be assigned to the tasks of a single user */
282 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700283
Craig Mautner27084302013-03-25 08:05:25 -0700284 final ActivityManagerService mService;
285
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800286 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800287
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700288 final ActivityStackSupervisorHandler mHandler;
289
290 /** Short cut */
291 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800292 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700293
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700294 /** Counter for next free stack ID to use for dynamic activity stacks. */
295 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700296
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800297 /**
298 * Maps the task identifier that activities are currently being started in to the userId of the
299 * task. Each time a new task is created, the entry for the userId of the task is incremented
300 */
301 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700302
Craig Mautner2420ead2013-04-01 17:13:20 -0700303 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800304 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700305
Craig Mautnere0a38842013-12-16 16:14:02 -0800306 /** The stack containing the launcher app. Assumed to always be attached to
307 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800308 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700309
Craig Mautnere0a38842013-12-16 16:14:02 -0800310 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800311 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700312
Craig Mautner4a1cb222013-12-04 16:14:06 -0800313 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
314 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800315 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800316 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700317
318 /** List of activities that are waiting for a new activity to become visible before completing
319 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800320 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800323 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324
325 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800326 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700327
Craig Mautnerde4ef022013-04-07 19:01:33 -0700328 /** List of activities that are ready to be stopped, but waiting for the next activity to
329 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800330 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700331
Craig Mautnerf3333272013-04-22 10:55:53 -0700332 /** List of activities that are ready to be finished, but waiting for the previous activity to
333 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800334 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700335
Craig Mautner0eea92c2013-05-16 13:35:39 -0700336 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800337 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700338
Wale Ogunwale22e25262016-02-01 10:32:02 -0800339 /** List of activities whose multi-window mode changed that we need to report to the
340 * application */
341 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
342
343 /** List of activities whose picture-in-picture mode changed that we need to report to the
344 * application */
345 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
346
Craig Mautnerf3333272013-04-22 10:55:53 -0700347 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700348 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700349
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700350 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700351 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -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
440 /**
Tony Mak853304c2016-04-18 15:17:41 +0100441 * Is dock currently minimized.
442 */
443 boolean mIsDockMinimized;
444
445 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700446 * Description of a request to start a new activity, which has been held
447 * due to app switches being disabled.
448 */
449 static class PendingActivityLaunch {
450 final ActivityRecord r;
451 final ActivityRecord sourceRecord;
452 final int startFlags;
453 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700454 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700455
456 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700457 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700458 r = _r;
459 sourceRecord = _sourceRecord;
460 startFlags = _startFlags;
461 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700462 callerApp = _callerApp;
463 }
464
465 void sendErrorResult(String message) {
466 try {
467 if (callerApp.thread != null) {
468 callerApp.thread.scheduleCrash(message);
469 }
470 } catch (RemoteException e) {
471 Slog.e(TAG, "Exception scheduling crash of failed "
472 + "activity launcher sourceRecord=" + sourceRecord, e);
473 }
Craig Mautneree36c772014-07-16 14:56:05 -0700474 }
475 }
476
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800477 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700478 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700479 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800480 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800481 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700482 }
483
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800484 void setRecentTasks(RecentTasks recentTasks) {
485 mRecentTasks = recentTasks;
486 }
487
Jeff Brown2c43c332014-06-12 22:38:59 -0700488 /**
489 * At the time when the constructor runs, the power manager has not yet been
490 * initialized. So we initialize our wakelocks afterwards.
491 */
492 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800493 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700494 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700495 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700496 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700497 }
498
justinzhang5286d3f2014-05-12 17:06:01 -0400499 // This function returns a IStatusBarService. The value is from ServiceManager.
500 // getService and is cached.
501 private IStatusBarService getStatusBarService() {
502 synchronized (mService) {
503 if (mStatusBarService == null) {
504 mStatusBarService = IStatusBarService.Stub.asInterface(
505 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
506 if (mStatusBarService == null) {
507 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
508 }
509 }
510 return mStatusBarService;
511 }
512 }
513
Jason Monk35c62a42014-06-17 10:24:47 -0400514 private IDevicePolicyManager getDevicePolicyManager() {
515 synchronized (mService) {
516 if (mDevicePolicyManager == null) {
517 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
518 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
519 if (mDevicePolicyManager == null) {
520 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
521 }
522 }
523 return mDevicePolicyManager;
524 }
525 }
526
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700527 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800528 synchronized (mService) {
529 mWindowManager = wm;
530
531 mDisplayManager =
532 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
533 mDisplayManager.registerDisplayListener(this, null);
534
535 Display[] displays = mDisplayManager.getDisplays();
536 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
537 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800538 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700539 if (activityDisplay.mDisplay == null) {
540 throw new IllegalStateException("Default Display does not exist");
541 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800542 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700543 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800544 }
545
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800546 mHomeStack = mFocusedStack = mLastFocusedStack =
547 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800548
549 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 }
Craig Mautner27084302013-03-25 08:05:25 -0700551 }
552
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200553 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700554 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200555 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700556 }
557
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700558 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800559 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700560 }
561
Craig Mautnerde4ef022013-04-07 19:01:33 -0700562 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800563 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700564 }
565
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700566 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700567 if (stack == null) {
568 return false;
569 }
570
Craig Mautnerdf88d732014-01-27 09:21:32 -0800571 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
572 if (parent != null) {
573 stack = parent.task.stack;
574 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800575 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700576 }
577
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700578 /** The top most stack. */
579 boolean isFrontStack(ActivityStack stack) {
580 if (stack == null) {
581 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800582 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700583
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700584 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
585 if (parent != null) {
586 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800587 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700588 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
589 }
590
Wale Ogunwaled046a012015-12-24 13:05:59 -0800591 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800592 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
593 if (!focusCandidate.isFocusable()) {
594 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
595 focusCandidate = focusCandidate.getNextFocusableStackLocked();
596 }
597
598 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800599 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800600 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800601
Wale Ogunwaled046a012015-12-24 13:05:59 -0800602 EventLogTags.writeAmFocusedStack(
603 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
604 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
605 }
606
607 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800608 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800609 // The focus activity should always be the top activity in the focused stack.
610 // There will be chaos and anarchy if it isn't...
611 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
612 }
Craig Mautnerde313752015-01-22 14:28:03 -0800613
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800614 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800615 if (r != null && r.idle) {
616 checkFinishBootingLocked();
617 }
618 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700619 }
620
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700621 void moveHomeStackToFront(String reason) {
622 mHomeStack.moveToFront(reason);
623 }
624
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700625 /** Returns true if the focus activity was adjusted to the home stack top activity. */
626 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700627 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700628 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700629 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700630 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700631
Craig Mautner84984fa2014-06-19 11:19:20 -0700632 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700633
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700634 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700635 if (top == null) {
636 return false;
637 }
638 mService.setFocusedActivityLocked(top, reason);
639 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700640 }
641
Craig Mautner299f9602015-01-26 09:47:33 -0800642 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700643 if (!mService.mBooting && !mService.mBooted) {
644 // Not ready yet!
645 return false;
646 }
647
Craig Mautner84984fa2014-06-19 11:19:20 -0700648 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700649 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700650 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700651 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700652
Craig Mautner84984fa2014-06-19 11:19:20 -0700653 if (prev != null) {
654 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
655 }
656
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700657 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
658 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800659 final String myReason = reason + " resumeHomeStackTask";
660
Mark Lua56ea122015-10-08 13:31:01 +0800661 // Only resume home activity if isn't finishing.
662 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800663 mService.setFocusedActivityLocked(r, myReason);
664 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700665 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800666 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700667 }
668
Craig Mautner8d341ef2013-03-26 09:03:27 -0700669 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700670 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700671 }
672
673 /**
674 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
675 * @param id Id of the task we would like returned.
676 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
677 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700678 * @param stackId The stack to restore the task to (default launch stack will be used if
679 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700680 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700681 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800682 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800683 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800684 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
686 ActivityStack stack = stacks.get(stackNdx);
687 TaskRecord task = stack.taskForIdLocked(id);
688 if (task != null) {
689 return task;
690 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700691 }
692 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800693
694 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700695 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800696 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800697 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700698 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800699 return null;
700 }
701
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700702 if (!restoreFromRecents) {
703 return task;
704 }
705
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700706 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700707 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
708 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800709 return null;
710 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700711 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800712 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700713 }
714
Craig Mautner6170f732013-04-02 13:05:23 -0700715 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800716 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800717 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800718 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800719 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
720 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
721 if (r != null) {
722 return r;
723 }
Craig Mautner6170f732013-04-02 13:05:23 -0700724 }
725 }
726 return null;
727 }
728
Tony Mak853304c2016-04-18 15:17:41 +0100729 /**
730 * TODO: Handle freefom mode.
731 * @return true when credential confirmation is needed for the user and there is any
732 * activity started by the user in any visible stack.
733 */
734 boolean isUserLockedProfile(@UserIdInt int userId) {
735 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
736 return false;
737 }
738 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
739 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
740 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
741 for (final ActivityStack activityStack : activityStacks) {
742 if (activityStack == null) {
743 continue;
744 }
745 if (activityStack.topRunningActivityLocked() == null) {
746 continue;
747 }
748 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
749 continue;
750 }
751 if (activityStack.isDockedStack() && mIsDockMinimized) {
752 continue;
753 }
754 final TaskRecord topTask = activityStack.topTask();
755 if (topTask == null) {
756 continue;
757 }
758 // To handle the case that work app is in the task but just is not the top one.
759 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
760 final ActivityRecord activityRecord = topTask.mActivities.get(i);
761 if (activityRecord.userId == userId) {
762 return true;
763 }
764 }
765 }
766 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000767 }
768
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800769 void setNextTaskIdForUserLocked(int taskId, int userId) {
770 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
771 if (taskId > currentTaskId) {
772 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700773 }
774 }
775
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800776 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800777 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
778 // for a userId u, a taskId can only be in the range
779 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
780 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
781 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800782 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
783 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
784 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800785 candidateTaskId++;
786 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
787 // Wrap around as there will be smaller task ids that are available now.
788 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700789 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800790 if (candidateTaskId == currentTaskId) {
791 // Something wrong!
792 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
793 throw new IllegalStateException("Cannot get an available task id."
794 + " Reached limit of " + MAX_TASK_IDS_PER_USER
795 + " running tasks per user.");
796 }
797 }
798 mCurTaskIdForUser.put(userId, candidateTaskId);
799 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700800 }
801
Craig Mautnerde4ef022013-04-07 19:01:33 -0700802 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800803 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700804 if (stack == null) {
805 return null;
806 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700807 ActivityRecord resumedActivity = stack.mResumedActivity;
808 if (resumedActivity == null || resumedActivity.app == null) {
809 resumedActivity = stack.mPausingActivity;
810 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700811 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700812 }
813 }
814 return resumedActivity;
815 }
816
Dianne Hackbornff072722014-09-24 10:56:28 -0700817 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700818 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800819 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800820 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
821 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800822 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
823 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700824 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800825 continue;
826 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700827 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 if (hr != null) {
829 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
830 && processName.equals(hr.processName)) {
831 try {
George Mount2c92c972014-03-20 09:38:23 -0700832 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 didSomething = true;
834 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700835 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800836 Slog.w(TAG, "Exception in new application when starting activity "
837 + hr.intent.getComponent().flattenToShortString(), e);
838 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700839 }
Craig Mautner20e72272013-04-01 13:45:53 -0700840 }
Craig Mautner20e72272013-04-01 13:45:53 -0700841 }
842 }
843 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700844 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700845 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700846 }
Craig Mautner20e72272013-04-01 13:45:53 -0700847 return didSomething;
848 }
849
850 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800851 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
852 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
854 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700855 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800856 continue;
857 }
858 final ActivityRecord resumedActivity = stack.mResumedActivity;
859 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700860 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800861 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800862 return false;
863 }
Craig Mautner20e72272013-04-01 13:45:53 -0700864 }
865 }
866 return true;
867 }
868
Craig Mautnerde4ef022013-04-07 19:01:33 -0700869 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800870 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
871 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
873 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700874 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800875 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700876 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800877 return false;
878 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700879 }
880 }
881 }
882 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700883 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800884 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
885 mLastFocusedStack + " to=" + mFocusedStack);
886 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700887 return true;
888 }
889
890 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800891 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800892 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
893 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800894 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
895 final ActivityStack stack = stacks.get(stackNdx);
896 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800897 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700898 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800899 return false;
900 }
901 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800902 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700903 }
904 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800905 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700906 }
907
Craig Mautner2acc3892013-09-23 10:28:14 -0700908 /**
909 * Pause all activities in either all of the stacks or just the back stacks.
910 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700911 * @return true if any activity was paused as a result of this call.
912 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700913 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700914 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800915 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
916 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800917 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
918 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700919 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700920 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800921 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700922 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
923 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800924 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700925 }
926 }
927 return someActivityPaused;
928 }
929
Craig Mautnerde4ef022013-04-07 19:01:33 -0700930 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700931 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800932 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
933 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800934 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
935 final ActivityStack stack = stacks.get(stackNdx);
936 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700937 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800938 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700939 Slog.d(TAG_STATES,
940 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800941 pausing = false;
942 } else {
943 return false;
944 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700945 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700946 }
947 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700948 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700949 }
950
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700951 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
952 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500953 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800954 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
955 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
956 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
957 final ActivityStack stack = stacks.get(stackNdx);
958 if (stack.mResumedActivity != null &&
959 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700960 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800961 }
962 }
963 }
964 }
965
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800966 void cancelInitializingActivities() {
967 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
968 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
969 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
970 stacks.get(stackNdx).cancelInitializingActivities();
971 }
972 }
973 }
974
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700975 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700976 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700977 }
978
979 void sendWaitingVisibleReportLocked(ActivityRecord r) {
980 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700981 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700982 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700983 if (w.who == null) {
984 changed = true;
985 w.timeout = false;
986 if (r != null) {
987 w.who = new ComponentName(r.info.packageName, r.info.name);
988 }
989 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
990 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700991 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700992 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700993 if (changed) {
994 mService.notifyAll();
995 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700996 }
997
998 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
999 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001000 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001001 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001002 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001003 if (w.who == null) {
1004 changed = true;
1005 w.timeout = timeout;
1006 if (r != null) {
1007 w.who = new ComponentName(r.info.packageName, r.info.name);
1008 }
1009 w.thisTime = thisTime;
1010 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001011 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001012 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001013 if (changed) {
1014 mService.notifyAll();
1015 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001016 }
1017
Craig Mautner29219d92013-04-16 20:19:12 -07001018 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001019 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001020 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001021 if (r != null) {
1022 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001023 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001024
Craig Mautner4a1cb222013-12-04 16:14:06 -08001025 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001026 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001027 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1028 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001029 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001030 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001031 if (r != null) {
1032 return r;
1033 }
1034 }
1035 }
1036 return null;
1037 }
1038
Dianne Hackborn09233282014-04-30 11:33:59 -07001039 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001040 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001041 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1042 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001043 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001044 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001045 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001046 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1047 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001048 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001049 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001050 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001051 }
1052 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001053
1054 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1055 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1056 while (maxNum > 0) {
1057 long mostRecentActiveTime = Long.MIN_VALUE;
1058 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001059 final int numTaskLists = runningTaskLists.size();
1060 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1061 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001062 if (!stackTaskList.isEmpty()) {
1063 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1064 if (lastActiveTime > mostRecentActiveTime) {
1065 mostRecentActiveTime = lastActiveTime;
1066 selectedStackList = stackTaskList;
1067 }
1068 }
1069 }
1070 if (selectedStackList != null) {
1071 list.add(selectedStackList.remove(0));
1072 --maxNum;
1073 } else {
1074 break;
1075 }
1076 }
Craig Mautner20e72272013-04-01 13:45:53 -07001077 }
1078
Todd Kennedy7440f172015-12-09 14:31:22 -08001079 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1080 ProfilerInfo profilerInfo) {
1081 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001082 if (aInfo != null) {
1083 // Store the found target back into the intent, because now that
1084 // we have it we never want to do this again. For example, if the
1085 // user navigates back to this point in the history, we should
1086 // always restart the exact same activity.
1087 intent.setComponent(new ComponentName(
1088 aInfo.applicationInfo.packageName, aInfo.name));
1089
1090 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001091 if (!aInfo.processName.equals("system")) {
1092 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001093 mService.setDebugApp(aInfo.processName, true, false);
1094 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001096 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1097 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1098 }
1099
Man Caocfa78b22015-06-11 20:14:34 -07001100 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1101 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1102 }
1103
1104 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001105 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001106 }
1107 }
1108 }
1109 return aInfo;
1110 }
1111
Todd Kennedy7440f172015-12-09 14:31:22 -08001112 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001113 return resolveIntent(intent, resolvedType, userId, 0);
1114 }
1115
1116 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001117 try {
1118 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001119 PackageManager.MATCH_DEFAULT_ONLY | flags
1120 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001121 } catch (RemoteException e) {
1122 }
1123 return null;
1124 }
1125
1126 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1127 ProfilerInfo profilerInfo, int userId) {
1128 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1129 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1130 }
1131
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001132 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1133 boolean andResume, boolean checkConfig) throws RemoteException {
1134
1135 if (!allPausedActivitiesComplete()) {
1136 // While there are activities pausing we skipping starting any new activities until
1137 // pauses are complete. NOTE: that we also do this for activities that are starting in
1138 // the paused state because they will first be resumed then paused on the client side.
1139 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1140 "realStartActivityLocked: Skipping start of r=" + r
1141 + " some activities pausing...");
1142 return false;
1143 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001144
Craig Mautner2568c3a2015-03-26 14:22:34 -07001145 if (andResume) {
1146 r.startFreezingScreenLocked(app, 0);
1147 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001148
Craig Mautner2568c3a2015-03-26 14:22:34 -07001149 // schedule launch ticks to collect information about slow apps.
1150 r.startLaunchTickingLocked();
1151 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001152
1153 // Have the window manager re-evaluate the orientation of
1154 // the screen based on the new activity order. Note that
1155 // as a result of this, it can call back into the activity
1156 // manager with a new orientation. We don't care about that,
1157 // because the activity is not currently running so we are
1158 // just restarting it anyway.
1159 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001160 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001161 mService.mConfiguration,
1162 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001163 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001164 }
1165
1166 r.app = app;
1167 app.waitingToKill = null;
1168 r.launchCount++;
1169 r.lastLaunchTime = SystemClock.uptimeMillis();
1170
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001171 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001172
1173 int idx = app.activities.indexOf(r);
1174 if (idx < 0) {
1175 app.activities.add(r);
1176 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001177 mService.updateLruProcessLocked(app, true, null);
1178 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001179
Craig Mautner15df08a2015-04-01 12:17:18 -07001180 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001181 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1182 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001183 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001184 }
1185
1186 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001187 try {
1188 if (app.thread == null) {
1189 throw new RemoteException();
1190 }
1191 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001192 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001193 if (andResume) {
1194 results = r.results;
1195 newIntents = r.newIntents;
1196 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001197 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1198 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1199 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001200 if (andResume) {
1201 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1202 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001203 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001204 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001205 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001206 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001207 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001209 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001210 r.sleeping = false;
1211 r.forceNewConfig = false;
1212 mService.showAskCompatModeDialogLocked(r);
1213 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001214 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1216 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1217 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001218 final String profileFile = mService.mProfileFile;
1219 if (profileFile != null) {
1220 ParcelFileDescriptor profileFd = mService.mProfileFd;
1221 if (profileFd != null) {
1222 try {
1223 profileFd = profileFd.dup();
1224 } catch (IOException e) {
1225 if (profileFd != null) {
1226 try {
1227 profileFd.close();
1228 } catch (IOException o) {
1229 }
1230 profileFd = null;
1231 }
1232 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001234
1235 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1236 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001237 }
1238 }
1239 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001240
Craig Mautner2568c3a2015-03-26 14:22:34 -07001241 if (andResume) {
1242 app.hasShownUi = true;
1243 app.pendingUiClean = true;
1244 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001245 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001246 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001247 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001248 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001249 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001250 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001251
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001252 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 // This may be a heavy-weight process! Note that the package
1254 // manager will ensure that only activity can run in the main
1255 // process of the .apk, which is the only thing that will be
1256 // considered heavy-weight.
1257 if (app.processName.equals(app.info.packageName)) {
1258 if (mService.mHeavyWeightProcess != null
1259 && mService.mHeavyWeightProcess != app) {
1260 Slog.w(TAG, "Starting new heavy weight process " + app
1261 + " when already running "
1262 + mService.mHeavyWeightProcess);
1263 }
1264 mService.mHeavyWeightProcess = app;
1265 Message msg = mService.mHandler.obtainMessage(
1266 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1267 msg.obj = r;
1268 mService.mHandler.sendMessage(msg);
1269 }
1270 }
1271
1272 } catch (RemoteException e) {
1273 if (r.launchFailed) {
1274 // This is the second time we failed -- finish activity
1275 // and give up.
1276 Slog.e(TAG, "Second failure launching "
1277 + r.intent.getComponent().flattenToShortString()
1278 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001279 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1281 "2nd-crash", false);
1282 return false;
1283 }
1284
1285 // This is the first time we failed -- restart process and
1286 // retry.
1287 app.activities.remove(r);
1288 throw e;
1289 }
1290
1291 r.launchFailed = false;
1292 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001293 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001294 }
1295
1296 if (andResume) {
1297 // As part of the process of launching, ActivityThread also performs
1298 // a resume.
1299 stack.minimalResumeActivityLocked(r);
1300 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001301 // This activity is not starting in the resumed state... which should look like we asked
1302 // it to pause+stop (but remain visible), and it has done so and reported back the
1303 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001304 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001305 "Moving to PAUSED: " + r + " (starting in paused state)");
1306 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001307 }
1308
1309 // Launch the new version setup screen if needed. We do this -after-
1310 // launching the initial activity (that is, home), so that it can have
1311 // a chance to initialize itself while in the background, making the
1312 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001313 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001314 mService.startSetupActivityLocked();
1315 }
1316
Dianne Hackborn465fa392014-09-14 14:21:18 -07001317 // Update any services we are bound to that might care about whether
1318 // their client may have activities.
1319 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1320
Craig Mautner2420ead2013-04-01 17:13:20 -07001321 return true;
1322 }
1323
Craig Mautnere79d42682013-04-01 19:01:53 -07001324 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001325 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001326 // Is this activity's application already running?
1327 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001328 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001329
1330 r.task.stack.setLaunchTime(r);
1331
1332 if (app != null && app.thread != null) {
1333 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001334 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1335 || !"android".equals(r.info.packageName)) {
1336 // Don't add this if it is a platform component that is marked
1337 // to run in multiple processes, because this is actually
1338 // part of the framework so doesn't make sense to track as a
1339 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001340 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1341 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001342 }
George Mount2c92c972014-03-20 09:38:23 -07001343 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001344 return;
1345 } catch (RemoteException e) {
1346 Slog.w(TAG, "Exception when starting activity "
1347 + r.intent.getComponent().flattenToShortString(), e);
1348 }
1349
1350 // If a dead object exception was thrown -- fall through to
1351 // restart the application.
1352 }
1353
1354 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001355 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001356 }
1357
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001358 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001359 String resultWho, int requestCode, int callingPid, int callingUid,
1360 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001361 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001362 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1363 callingUid);
1364 if (startAnyPerm == PERMISSION_GRANTED) {
1365 return true;
1366 }
1367 final int componentRestriction = getComponentRestrictionForCallingPackage(
1368 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1369 final int actionRestriction = getActionRestrictionForCallingPackage(
1370 intent.getAction(), callingPackage, callingPid, callingUid);
1371 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1372 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1373 if (resultRecord != null) {
1374 resultStack.sendActivityResultLocked(-1,
1375 resultRecord, resultWho, requestCode,
1376 Activity.RESULT_CANCELED, null);
1377 }
1378 final String msg;
1379 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1380 msg = "Permission Denial: starting " + intent.toString()
1381 + " from " + callerApp + " (pid=" + callingPid
1382 + ", uid=" + callingUid + ")" + " with revoked permission "
1383 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1384 } else if (!aInfo.exported) {
1385 msg = "Permission Denial: starting " + intent.toString()
1386 + " from " + callerApp + " (pid=" + callingPid
1387 + ", uid=" + callingUid + ")"
1388 + " not exported from uid " + aInfo.applicationInfo.uid;
1389 } else {
1390 msg = "Permission Denial: starting " + intent.toString()
1391 + " from " + callerApp + " (pid=" + callingPid
1392 + ", uid=" + callingUid + ")"
1393 + " requires " + aInfo.permission;
1394 }
1395 Slog.w(TAG, msg);
1396 throw new SecurityException(msg);
1397 }
1398
1399 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1400 final String message = "Appop Denial: starting " + intent.toString()
1401 + " from " + callerApp + " (pid=" + callingPid
1402 + ", uid=" + callingUid + ")"
1403 + " requires " + AppOpsManager.permissionToOp(
1404 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1405 Slog.w(TAG, message);
1406 return false;
1407 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1408 final String message = "Appop Denial: starting " + intent.toString()
1409 + " from " + callerApp + " (pid=" + callingPid
1410 + ", uid=" + callingUid + ")"
1411 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1412 Slog.w(TAG, message);
1413 return false;
1414 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001415 if (options != null && options.getLaunchTaskId() != -1) {
1416 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1417 callingPid, callingUid);
1418 if (startInTaskPerm != PERMISSION_GRANTED) {
1419 final String msg = "Permission Denial: starting " + intent.toString()
1420 + " from " + callerApp + " (pid=" + callingPid
1421 + ", uid=" + callingUid + ") with launchTaskId="
1422 + options.getLaunchTaskId();
1423 Slog.w(TAG, msg);
1424 throw new SecurityException(msg);
1425 }
1426 }
1427
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001428 return true;
1429 }
1430
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001431 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001432 final long identity = Binder.clearCallingIdentity();
1433 try {
1434 return UserManager.get(mService.mContext).getUserInfo(userId);
1435 } finally {
1436 Binder.restoreCallingIdentity(identity);
1437 }
1438 }
1439
Svet Ganov99b60432015-06-27 13:15:22 -07001440 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001441 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001442 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1443 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001444 == PackageManager.PERMISSION_DENIED) {
1445 return ACTIVITY_RESTRICTION_PERMISSION;
1446 }
1447
Christopher Tateff7add02015-08-17 10:23:22 -07001448 if (activityInfo.permission == null) {
1449 return ACTIVITY_RESTRICTION_NONE;
1450 }
1451
Svet Ganov99b60432015-06-27 13:15:22 -07001452 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1453 if (opCode == AppOpsManager.OP_NONE) {
1454 return ACTIVITY_RESTRICTION_NONE;
1455 }
1456
1457 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1458 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001459 if (!ignoreTargetSecurity) {
1460 return ACTIVITY_RESTRICTION_APPOP;
1461 }
Svet Ganov99b60432015-06-27 13:15:22 -07001462 }
1463
1464 return ACTIVITY_RESTRICTION_NONE;
1465 }
1466
Svetoslav7008b512015-06-24 18:47:07 -07001467 private int getActionRestrictionForCallingPackage(String action,
1468 String callingPackage, int callingPid, int callingUid) {
1469 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001470 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001471 }
1472
1473 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1474 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001475 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001476 }
1477
1478 final PackageInfo packageInfo;
1479 try {
1480 packageInfo = mService.mContext.getPackageManager()
1481 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1482 } catch (PackageManager.NameNotFoundException e) {
1483 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001484 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001485 }
1486
1487 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001488 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001489 }
1490
1491 if (mService.checkPermission(permission, callingPid, callingUid) ==
1492 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001493 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001494 }
1495
1496 final int opCode = AppOpsManager.permissionToOpCode(permission);
1497 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001498 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001499 }
1500
1501 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1502 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001503 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001504 }
1505
Svet Ganov99b60432015-06-27 13:15:22 -07001506 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001507 }
1508
Wale Ogunwaled046a012015-12-24 13:05:59 -08001509 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001510 if (r == null) {
1511 // Not sure what you are trying to do, but it is not going to work...
1512 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001513 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001514 final TaskRecord task = r.task;
1515 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001516 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001517 return false;
1518 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001519 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001520 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001521 }
1522
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001523 void setLaunchSource(int uid) {
1524 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1525 }
1526
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001527 void acquireLaunchWakelock() {
1528 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1529 throw new IllegalStateException("Calling must be system uid");
1530 }
1531 mLaunchingActivity.acquire();
1532 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1533 // To be safe, don't allow the wake lock to be held for too long.
1534 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1535 }
1536 }
1537
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001538 /**
1539 * Called when the frontmost task is idle.
1540 * @return the state of mService.mBooting before this was called.
1541 */
1542 private boolean checkFinishBootingLocked() {
1543 final boolean booting = mService.mBooting;
1544 boolean enableScreen = false;
1545 mService.mBooting = false;
1546 if (!mService.mBooted) {
1547 mService.mBooted = true;
1548 enableScreen = true;
1549 }
1550 if (booting || enableScreen) {
1551 mService.postFinishBooting(booting, enableScreen);
1552 }
1553 return booting;
1554 }
1555
Craig Mautnerf3333272013-04-22 10:55:53 -07001556 // Checked.
1557 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1558 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001559 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001560
Craig Mautnerf3333272013-04-22 10:55:53 -07001561 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001562 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001563 int NS = 0;
1564 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001565 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001566 boolean activityRemoved = false;
1567
Wale Ogunwale7d701172015-03-11 15:36:30 -07001568 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001569 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001570 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1571 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001572 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1573 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001574 if (fromTimeout) {
1575 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001576 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001577
1578 // This is a hack to semi-deal with a race condition
1579 // in the client where it can be constructed with a
1580 // newer configuration from when we asked it to launch.
1581 // We'll update with whatever configuration it now says
1582 // it used to launch.
1583 if (config != null) {
1584 r.configuration = config;
1585 }
1586
1587 // We are now idle. If someone is waiting for a thumbnail from
1588 // us, we can now deliver.
1589 r.idle = true;
1590
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001591 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001592 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001593 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001594 }
1595 }
1596
1597 if (allResumedActivitiesIdle()) {
1598 if (r != null) {
1599 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001600 }
1601
1602 if (mLaunchingActivity.isHeld()) {
1603 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1604 if (VALIDATE_WAKE_LOCK_CALLER &&
1605 Binder.getCallingUid() != Process.myUid()) {
1606 throw new IllegalStateException("Calling must be system uid");
1607 }
1608 mLaunchingActivity.release();
1609 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001610 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001611 }
1612
1613 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001614 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001615 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001616 if ((NF = mFinishingActivities.size()) > 0) {
1617 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001618 mFinishingActivities.clear();
1619 }
1620
Craig Mautnerf3333272013-04-22 10:55:53 -07001621 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001622 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001623 mStartingUsers.clear();
1624 }
1625
Craig Mautnerf3333272013-04-22 10:55:53 -07001626 // Stop any activities that are scheduled to do so but have been
1627 // waiting for the next one to start.
1628 for (int i = 0; i < NS; i++) {
1629 r = stops.get(i);
1630 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001631 if (stack != null) {
1632 if (r.finishing) {
1633 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1634 } else {
1635 stack.stopActivityLocked(r);
1636 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001637 }
1638 }
1639
1640 // Finish any activities that are scheduled to do so but have been
1641 // waiting for the next one to start.
1642 for (int i = 0; i < NF; i++) {
1643 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001644 final ActivityStack stack = r.task.stack;
1645 if (stack != null) {
1646 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1647 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001648 }
1649
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001650 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001651 // Complete user switch
1652 if (startingUsers != null) {
1653 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001654 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001655 }
1656 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001657 }
1658
1659 mService.trimApplications();
1660 //dump();
1661 //mWindowManager.dump();
1662
Craig Mautnerf3333272013-04-22 10:55:53 -07001663 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001664 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001665 }
1666
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001667 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001668 }
1669
Craig Mautner8e569572013-10-11 17:36:59 -07001670 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001671 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001672 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1673 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001674 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1675 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1676 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001677 }
Craig Mautner19091252013-10-05 00:03:53 -07001678 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001679 }
1680
1681 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001682 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1683 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001684 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1685 stacks.get(stackNdx).closeSystemDialogsLocked();
1686 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001687 }
1688 }
1689
Craig Mautner93529a42013-10-04 15:03:13 -07001690 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001691 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001692 }
1693
Craig Mautner8d341ef2013-03-26 09:03:27 -07001694 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001695 * Update the last used stack id for non-current user (current user's last
1696 * used stack is the focused stack)
1697 */
1698 void updateUserStackLocked(int userId, ActivityStack stack) {
1699 if (userId != mCurrentUser) {
1700 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1701 }
1702 }
1703
1704 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001705 * @return true if some activity was finished (or would have finished if doit were true).
1706 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001707 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1708 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001709 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001710 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1711 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001712 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001713 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001714 if (stack.finishDisabledPackageActivitiesLocked(
1715 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001716 didSomething = true;
1717 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001718 }
1719 }
1720 return didSomething;
1721 }
1722
Dianne Hackborna413dc02013-07-12 12:02:55 -07001723 void updatePreviousProcessLocked(ActivityRecord r) {
1724 // Now that this process has stopped, we may want to consider
1725 // it to be the previous app to try to keep around in case
1726 // the user wants to return to it.
1727
1728 // First, found out what is currently the foreground app, so that
1729 // we don't blow away the previous app if this activity is being
1730 // hosted by the process that is actually still the foreground.
1731 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001732 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1733 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001734 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1735 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001736 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001737 if (stack.mResumedActivity != null) {
1738 fgApp = stack.mResumedActivity.app;
1739 } else if (stack.mPausingActivity != null) {
1740 fgApp = stack.mPausingActivity.app;
1741 }
1742 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001743 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001744 }
1745 }
1746
1747 // Now set this one as the previous process, only if that really
1748 // makes sense to.
1749 if (r.app != null && fgApp != null && r.app != fgApp
1750 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001751 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001752 mService.mPreviousProcess = r.app;
1753 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1754 }
1755 }
1756
Wale Ogunwaled046a012015-12-24 13:05:59 -08001757 boolean resumeFocusedStackTopActivityLocked() {
1758 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001759 }
1760
Wale Ogunwaled046a012015-12-24 13:05:59 -08001761 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001762 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001763 if (targetStack != null && isFocusedStack(targetStack)) {
1764 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001765 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001766 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1767 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001768 }
1769
Todd Kennedy39bfee52016-02-24 10:28:21 -08001770 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1771 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1772 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1773 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1774 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1775 }
1776 }
1777 }
1778
Adrian Roos20d7df32016-01-12 18:59:43 +01001779 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1780 TaskRecord finishedTask = null;
1781 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001782 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1783 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001784 final int numStacks = stacks.size();
1785 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1786 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001787 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1788 if (stack == focusedStack || finishedTask == null) {
1789 finishedTask = t;
1790 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001791 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001792 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001793 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001794 }
1795
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001796 void finishVoiceTask(IVoiceInteractionSession session) {
1797 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1798 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1799 final int numStacks = stacks.size();
1800 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1801 final ActivityStack stack = stacks.get(stackNdx);
1802 stack.finishVoiceTask(session);
1803 }
1804 }
1805 }
1806
Andrii Kulianc27916642016-04-12 17:59:27 -07001807 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1808 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001809 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1810 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001811 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001812 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1813 // Caller wants the home activity moved with it. To accomplish this,
1814 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001815 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001816 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001817 if (task.stack == null) {
1818 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1819 + task + " to front. Stack is null");
1820 return;
1821 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001822
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001823 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001824 int stackId = options.getLaunchStackId();
1825 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001826 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001827 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001828 if (stackId == INVALID_STACK_ID) {
1829 stackId = task.getLaunchStackId();
1830 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001831 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001832 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1833 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1834 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001835 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1836 // still need moveTaskToFrontLocked() below for any transition settings.
1837 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001838 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1839 resizeStackLocked(stackId, bounds,
1840 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1841 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1842 } else {
1843 // WM resizeTask must be done after the task is moved to the correct stack,
1844 // because Task's setBounds() also updates dim layer's bounds, but that has
1845 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001846 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001847 false /* relayout */, false /* forced */);
1848 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001849 }
1850 }
1851
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001852 final ActivityRecord r = task.getTopActivity();
1853 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1854 r == null ? null : r.appTimeTracker, reason);
1855
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001856 if (DEBUG_STACK) Slog.d(TAG_STACK,
1857 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001858
Andrii Kulianc27916642016-04-12 17:59:27 -07001859 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1860 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001861 }
1862
Wale Ogunwale854809c2015-12-27 16:18:19 -08001863 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001864 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001865 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001866 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001867 return false;
1868 }
1869 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1870 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001871 }
1872
Craig Mautner967212c2013-04-13 21:10:58 -07001873 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001874 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001875 }
1876
1877 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001878 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1879 if (activityContainer != null) {
1880 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001881 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001882 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001883 return null;
1884 }
1885 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001886 }
1887
Craig Mautner967212c2013-04-13 21:10:58 -07001888 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001889 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001890 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1891 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001892 }
1893 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001894 }
1895
Craig Mautner4a1cb222013-12-04 16:14:06 -08001896 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001897 ActivityRecord homeActivity = getHomeActivity();
1898 if (homeActivity != null) {
1899 return homeActivity.appToken;
1900 }
1901 return null;
1902 }
1903
1904 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001905 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001906 }
1907
1908 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001909 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1910 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1911 final TaskRecord task = tasks.get(taskNdx);
1912 if (task.isHomeTask()) {
1913 final ArrayList<ActivityRecord> activities = task.mActivities;
1914 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1915 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001916 if (r.isHomeActivity()
1917 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001918 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001919 }
1920 }
1921 }
1922 }
1923 return null;
1924 }
1925
Chong Zhangb15758a2015-11-17 12:12:03 -08001926 /**
1927 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1928 * the docked stack itself, or if it's side-by-side to the docked stack.
1929 */
1930 boolean isStackDockedInEffect(int stackId) {
1931 return stackId == DOCKED_STACK_ID ||
1932 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1933 }
1934
Todd Kennedyca4d8422015-01-15 15:19:22 -08001935 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001936 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001937 ActivityContainer activityContainer =
1938 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001939 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001940 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1941 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001942 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001943 return activityContainer;
1944 }
1945
Craig Mautner34b73df2014-01-12 21:11:08 -08001946 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001947 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1948 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1949 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001950 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1951 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001952 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001953 }
1954 }
1955
Craig Mautner95da1082014-02-24 17:54:35 -08001956 void deleteActivityContainer(IActivityContainer container) {
1957 ActivityContainer activityContainer = (ActivityContainer)container;
1958 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001959 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1960 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001961 final int stackId = activityContainer.mStackId;
1962 mActivityContainers.remove(stackId);
1963 mWindowManager.removeStack(stackId);
1964 }
1965 }
1966
Jorim Jaggidc249c42015-12-15 14:57:31 -08001967 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1968 boolean preserveWindows, boolean allowResizeInDockedMode) {
1969 if (stackId == DOCKED_STACK_ID) {
1970 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1971 preserveWindows);
1972 return;
1973 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001974 final ActivityStack stack = getStack(stackId);
1975 if (stack == null) {
1976 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1977 return;
1978 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001979
Jorim Jaggidc249c42015-12-15 14:57:31 -08001980 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001981 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1982 // stack size changing so things don't get out of sync.
1983 return;
1984 }
1985
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001986 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001987 mWindowManager.deferSurfaceLayout();
1988 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001989 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1990 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001991 } finally {
1992 mWindowManager.continueSurfaceLayout();
1993 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001994 }
1995 }
1996
Jorim Jaggi192086e2016-03-11 17:17:03 +01001997 void deferUpdateBounds(int stackId) {
1998 final ActivityStack stack = getStack(stackId);
1999 if (stack != null) {
2000 stack.deferUpdateBounds();
2001 }
2002 }
2003
2004 void continueUpdateBounds(int stackId) {
2005 final ActivityStack stack = getStack(stackId);
2006 if (stack != null) {
2007 stack.continueUpdateBounds();
2008 }
2009 }
2010
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002011 void notifyAppTransitionDone() {
2012 continueUpdateBounds(HOME_STACK_ID);
2013 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2014 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
2015 if (anyTaskForIdLocked(taskId) != null) {
2016 mWindowManager.setTaskDockedResizing(taskId, false);
2017 }
2018 }
2019 mResizingTasksDuringAnimation.clear();
2020 }
2021
Jorim Jaggi192086e2016-03-11 17:17:03 +01002022 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002023 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002024 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002025
Jorim Jaggi192086e2016-03-11 17:17:03 +01002026 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2027 return;
2028 }
2029
Jorim Jaggidc249c42015-12-15 14:57:31 -08002030 mTmpBounds.clear();
2031 mTmpConfigs.clear();
2032 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002033 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002034 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2035 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002036 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002037 final TaskRecord task = tasks.get(i);
2038 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002039 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2040 // For freeform stack we don't adjust the size of the tasks to match that
2041 // of the stack, but we do try to make sure the tasks are still contained
2042 // with the bounds of the stack.
2043 tempRect2.set(task.mBounds);
2044 fitWithinBounds(tempRect2, bounds);
2045 task.updateOverrideConfiguration(tempRect2);
2046 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002047 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002048 }
2049 }
2050
2051 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2052 mTmpBounds.put(task.taskId, task.mBounds);
2053 if (tempTaskInsetBounds != null) {
2054 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2055 }
2056 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002057
2058 // We might trigger a configuration change. Save the current task bounds for freezing.
2059 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002060 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2061 mTmpBounds, mTmpInsetBounds);
2062 stack.setBounds(bounds);
2063 }
2064
2065 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
2066 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08002067 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002068 return;
2069 }
2070 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
2071 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002072 if (!updated) {
2073 resumeFocusedStackTopActivityLocked();
2074 }
2075 }
2076
2077 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2078 Rect tempDockedTaskInsetBounds,
2079 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
2080 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2081 if (stack == null) {
2082 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2083 return;
2084 }
2085
2086 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2087 mWindowManager.deferSurfaceLayout();
2088 try {
2089 ActivityRecord r = stack.topRunningActivityLocked();
2090 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2091 tempDockedTaskInsetBounds);
2092
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002093 // TODO: Checking for isAttached might not be needed as if the user passes in null
2094 // dockedBounds then they want the docked stack to be dismissed.
2095 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2096 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002097 // In this case we make all other static stacks fullscreen and move all
2098 // docked stack tasks to the fullscreen stack.
2099 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2100 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2101 resizeStackLocked(i, null, null, null, preserveWindows,
2102 true /* allowResizeInDockedMode */);
2103 }
2104 }
2105
2106 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2107 final int count = tasks.size();
2108 for (int i = 0; i < count; i++) {
2109 moveTaskToStackLocked(tasks.get(i).taskId,
2110 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2111 false /* animate */);
2112 }
2113
2114 // stack shouldn't contain anymore activities, so nothing to resume.
2115 r = null;
2116 } else {
2117 // Docked stacks occupy a dedicated region on screen so the size of all other
2118 // static stacks need to be adjusted so they don't overlap with the docked stack.
2119 // We get the bounds to use from window manager which has been adjusted for any
2120 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002121 mWindowManager.getStackDockedModeBounds(
2122 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002123 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002124 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2125 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2126 tempOtherTaskInsetBounds, preserveWindows,
2127 true /* allowResizeInDockedMode */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002128 }
2129 }
2130 }
2131 ensureConfigurationAndResume(stack, r, preserveWindows);
2132 } finally {
2133 mWindowManager.continueSurfaceLayout();
2134 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2135 }
2136
2137 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2138 tempDockedTaskBounds != null
2139 || tempDockedTaskInsetBounds != null
2140 || tempOtherTaskBounds != null
2141 || tempOtherTaskInsetBounds != null);
2142 }
2143
Robert Carr0d00c2e2016-02-29 17:45:02 -08002144 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2145 final ActivityStack stack = getStack(PINNED_STACK_ID);
2146 if (stack == null) {
2147 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2148 return;
2149 }
2150 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2151 mWindowManager.deferSurfaceLayout();
2152 try {
2153 ActivityRecord r = stack.topRunningActivityLocked();
2154 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2155 null);
2156 ensureConfigurationAndResume(stack, r, false);
2157 } finally {
2158 mWindowManager.continueSurfaceLayout();
2159 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2160 }
2161 }
2162
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002163 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2164 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002165 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002166 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002167 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002168 }
2169
Chong Zhang87b21722015-09-21 15:39:51 -07002170 // If this is a forced resize, let it go through even if the bounds is not changing,
2171 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002172 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002173 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002174 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002175 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002176 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002177 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002178
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002179 if (!mWindowManager.isValidTaskId(task.taskId)) {
2180 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002181 // All we can do for now is update the bounds so it can be used when the task is
2182 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002183 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002184 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2185 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002186 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002187 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002188 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002189 }
2190
Chong Zhang6de2ae82015-09-30 18:25:21 -07002191 // Do not move the task to another stack here.
2192 // This method assumes that the task is already placed in the right stack.
2193 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002194
Chong Zhang6de2ae82015-09-30 18:25:21 -07002195 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002196
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002197 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002198 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002199 // way and the activity was kept the way it was. If it's false, it means the activity had
2200 // to be relaunched due to configuration change.
2201 boolean kept = true;
2202 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002203 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002204 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002205 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002206 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002207
2208 if (!deferResume) {
2209
2210 // All other activities must be made visible with their correct configuration.
2211 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2212 if (!kept) {
2213 resumeFocusedStackTopActivityLocked();
2214 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002215 }
2216 }
2217 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002218 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002219
2220 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002221 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002222 }
2223
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002224 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002225 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2226 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002227 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002228 }
2229
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002230 ActivityContainer activityContainer = new ActivityContainer(stackId);
2231 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002232 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002233 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002234 }
2235
2236 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002237 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002238 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2239 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002240 break;
2241 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002242 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002243 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002244 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002245 }
2246
Chong Zhang5dcb2752015-08-18 13:50:26 -07002247 /**
2248 * Restores a recent task to a stack
2249 * @param task The recent task to be restored.
2250 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002251 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002252 * @return true if the task has been restored successfully.
2253 */
2254 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2255 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002256 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002257 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2258 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2259 // Put it in the fullscreen stack.
2260 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002261 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002262
Wale Ogunwale706ed792015-08-02 10:29:44 -07002263 if (task.stack != null) {
2264 // Task has already been restored once. See if we need to do anything more
2265 if (task.stack.mStackId == stackId) {
2266 // Nothing else to do since it is already restored in the right stack.
2267 return true;
2268 }
2269 // Remove current stack association, so we can re-associate the task with the
2270 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002271 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002272 }
2273
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002274 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002275 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002276
2277 if (stack == null) {
2278 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002279 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2280 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002281 return false;
2282 }
2283
Wale Ogunwale5f986092015-12-04 15:35:38 -08002284 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002285 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2286 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002287 final ArrayList<ActivityRecord> activities = task.mActivities;
2288 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002289 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002290 }
2291 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002292 }
2293
Wale Ogunwale040b4702015-08-06 18:10:50 -07002294 /**
2295 * Moves the specified task record to the input stack id.
2296 * WARNING: This method performs an unchecked/raw move of the task and
2297 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002298 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002299 * @param task Task to move.
2300 * @param stackId Id of stack to move task to.
2301 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002302 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002303 * @param reason Reason the task is been moved.
2304 * @return The stack the task was moved to.
2305 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002306 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002307 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002308
2309 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2310 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2311 + "support multi-window task=" + task + " to stackId=" + stackId);
2312 }
2313
Chong Zhang02898352015-08-21 17:27:14 -07002314 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002315 final ActivityStack prevStack = task.stack;
2316 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002317 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002318 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2319 // Whenever we are moving the top activity from the front stack we want to make sure to move
2320 // the stack to the front.
2321 final boolean wasFront = isFrontStack(prevStack)
2322 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002323
Chong Zhangd545dce2016-02-29 18:09:17 -08002324 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002325 // We don't allow moving a unresizeable task to the docked stack since the docked
2326 // stack is used for split-screen mode and will cause things like the docked divider to
2327 // show up. We instead leave the task in its current stack or move it to the fullscreen
2328 // stack if it isn't currently in a stack.
2329 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002330 Slog.w(TAG, "Can not move unresizeable task=" + task
2331 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2332 }
2333
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002334 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2335 // if a docked stack is created below which will lead to the stack we are moving from and
2336 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002337 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002338 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002339 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002340 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002341 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002342
2343 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002344 // move focus to the new stack by moving the stack to the front.
2345 stack.moveToFrontAndResumeStateIfNeeded(
2346 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002347
Wale Ogunwale040b4702015-08-06 18:10:50 -07002348 return stack;
2349 }
2350
Chong Zhange4fbd322016-03-01 14:44:03 -08002351 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002352 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002353 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2354 false /* deferResume */);
2355 }
2356
2357 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2358 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002359 final TaskRecord task = anyTaskForIdLocked(taskId);
2360 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002361 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002362 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002363 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002364
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002365 if (task.stack != null && task.stack.mStackId == stackId) {
2366 // You are already in the right stack silly...
2367 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002368 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002369 }
2370
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002371 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2372 throw new IllegalArgumentException("moveTaskToStack:"
2373 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2374 }
2375
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002376 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002377 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002378 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002379 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002380 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002381 // We are about to relaunch the activity because its configuration changed due to
2382 // being maximized, i.e. size change. The activity will first remove the old window
2383 // and then add a new one. This call will tell window manager about this, so it can
2384 // preserve the old window until the new one is drawn. This prevents having a gap
2385 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002386 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002387 // Note here we always set the replacing window first, as the flags might be needed
2388 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002389 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002390 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002391
2392 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002393 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002394 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002395 try {
2396 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002397 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002398 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002399
Wale Ogunwale961f4852016-02-01 20:25:54 -08002400 if (!animate) {
2401 stack.mNoAnimActivities.add(topActivity);
2402 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002403
Wale Ogunwale961f4852016-02-01 20:25:54 -08002404 // We might trigger a configuration change. Save the current task bounds for freezing.
2405 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2406
2407 // Make sure the task has the appropriate bounds/size for the stack it is in.
2408 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002409 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2410 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002411 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2412 Rect bounds = task.getLaunchBounds();
2413 if (bounds == null) {
2414 stack.layoutTaskInStack(task, null);
2415 bounds = task.mBounds;
2416 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002417 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2418 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002419 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002420 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2421 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002422 }
2423 } finally {
2424 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002425 }
2426
2427 if (mightReplaceWindow) {
2428 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2429 // window), we need to clear the replace window settings. Otherwise, we schedule a
2430 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002431 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002432 }
2433
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002434 if (!deferResume) {
2435
2436 // The task might have already been running and its visibility needs to be synchronized with
2437 // the visibility of the stack / windows.
2438 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2439 resumeFocusedStackTopActivityLocked();
2440 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002441
Jorim Jaggid53f0922016-04-06 22:16:23 -07002442 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002443
2444 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002445 }
2446
Wale Ogunwale079a0042015-10-24 11:44:07 -07002447 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2448 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2449 if (stack == null) {
2450 throw new IllegalArgumentException(
2451 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2452 }
2453
2454 final ActivityRecord r = stack.topRunningActivityLocked();
2455 if (r == null) {
2456 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2457 + " in stack=" + stack);
2458 return false;
2459 }
2460
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002461 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002462 Slog.w(TAG,
2463 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002464 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002465 return false;
2466 }
2467
Wale Ogunwale480dca02016-02-06 13:58:29 -08002468 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002469 return true;
2470 }
2471
Wale Ogunwale480dca02016-02-06 13:58:29 -08002472 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2473 mWindowManager.deferSurfaceLayout();
2474 try {
2475 final TaskRecord task = r.task;
2476
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002477 if (r == task.stack.getVisibleBehindActivity()) {
2478 // An activity can't be pinned and visible behind at the same time. Go ahead and
2479 // release it from been visible behind before pinning.
2480 requestVisibleBehindLocked(r, false);
2481 }
2482
Wale Ogunwale480dca02016-02-06 13:58:29 -08002483 // Need to make sure the pinned stack exist so we can resize it below...
2484 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2485
2486 // Resize the pinned stack to match the current size of the task the activity we are
2487 // going to be moving is currently contained in. We do this to have the right starting
2488 // animation bounds for the pinned stack to the desired bounds the caller wants.
2489 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2490 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2491 true /* allowResizeInDockedMode */);
2492
2493 if (task.mActivities.size() == 1) {
2494 // There is only one activity in the task. So, we can just move the task over to
2495 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002496 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2497 // Move the home stack forward if the task we just moved to the pinned stack
2498 // was launched from home so home should be visible behind it.
2499 moveHomeStackToFront(reason);
2500 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002501 moveTaskToStackLocked(
2502 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2503 } else {
2504 stack.moveActivityToStack(r);
2505 }
2506 } finally {
2507 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002508 }
2509
Wale Ogunwale480dca02016-02-06 13:58:29 -08002510 // The task might have already been running and its visibility needs to be synchronized
2511 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002512 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002513 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002514
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002515 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002516 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002517 }
2518
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002519 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2520 final TaskRecord task = anyTaskForIdLocked(taskId);
2521 if (task == null) {
2522 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2523 return;
2524 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002525 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2526
2527 task.updateOverrideConfigurationForStack(stack);
2528
2529 mWindowManager.positionTaskInStack(
2530 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002531 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002532 // The task might have already been running and its visibility needs to be synchronized with
2533 // the visibility of the stack / windows.
Jorim Jaggi8fa45222016-02-19 19:54:39 -08002534 stack.ensureActivityConfigurationLocked(task.topRunningActivityLocked(), 0,
2535 !PRESERVE_WINDOWS);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002536 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002537 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002538 }
2539
Craig Mautnerac6f8432013-07-17 13:24:59 -07002540 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002541 mTmpFindTaskResult.r = null;
2542 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002543 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002544 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2545 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002546 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2547 final ActivityStack stack = stacks.get(stackNdx);
2548 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002549 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002550 continue;
2551 }
2552 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002553 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2554 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002555 continue;
2556 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002557 stack.findTaskLocked(r, mTmpFindTaskResult);
2558 // It is possible to have task in multiple stacks with the same root affinity.
2559 // If the match we found was based on root affinity we keep on looking to see if
2560 // there is a better match in another stack. We eventually return the match based
2561 // on root affinity if we don't find a better match.
2562 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2563 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002564 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002565 }
2566 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002567 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2568 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002569 }
2570
2571 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002572 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2573 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002574 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2575 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2576 if (ar != null) {
2577 return ar;
2578 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002579 }
2580 }
2581 return null;
2582 }
2583
Craig Mautner8d341ef2013-03-26 09:03:27 -07002584 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002585 scheduleSleepTimeout();
2586 if (!mGoingToSleep.isHeld()) {
2587 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002588 if (mLaunchingActivity.isHeld()) {
2589 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2590 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002591 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002592 mLaunchingActivity.release();
2593 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002594 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002595 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002596 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002597 }
2598
2599 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002600 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002601
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002602 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002603 final long endTime = System.currentTimeMillis() + timeout;
2604 while (true) {
2605 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002606 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2607 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002608 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2609 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2610 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002611 }
2612 if (cantShutdown) {
2613 long timeRemaining = endTime - System.currentTimeMillis();
2614 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002615 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002616 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002617 } catch (InterruptedException e) {
2618 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002619 } else {
2620 Slog.w(TAG, "Activity manager shutdown timed out");
2621 timedout = true;
2622 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002623 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002624 } else {
2625 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002626 }
2627 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002628
2629 // Force checkReadyForSleep to complete.
2630 mSleepTimeout = true;
2631 checkReadyForSleepLocked();
2632
Craig Mautner8d341ef2013-03-26 09:03:27 -07002633 return timedout;
2634 }
2635
2636 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002637 removeSleepTimeouts();
2638 if (mGoingToSleep.isHeld()) {
2639 mGoingToSleep.release();
2640 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002641 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2642 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002643 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2644 final ActivityStack stack = stacks.get(stackNdx);
2645 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002646 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002647 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002648 }
Craig Mautner5314a402013-09-26 12:40:16 -07002649 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002650 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002651 mGoingToSleepActivities.clear();
2652 }
2653
2654 void activitySleptLocked(ActivityRecord r) {
2655 mGoingToSleepActivities.remove(r);
2656 checkReadyForSleepLocked();
2657 }
2658
2659 void checkReadyForSleepLocked() {
2660 if (!mService.isSleepingOrShuttingDown()) {
2661 // Do not care.
2662 return;
2663 }
2664
2665 if (!mSleepTimeout) {
2666 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002667 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2668 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002669 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2670 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2671 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002672 }
2673
2674 if (mStoppingActivities.size() > 0) {
2675 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002676 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002677 + mStoppingActivities.size() + " activities");
2678 scheduleIdleLocked();
2679 dontSleep = true;
2680 }
2681
2682 if (mGoingToSleepActivities.size() > 0) {
2683 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002684 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002685 + mGoingToSleepActivities.size() + " activities");
2686 dontSleep = true;
2687 }
2688
2689 if (dontSleep) {
2690 return;
2691 }
2692 }
2693
Craig Mautnere0a38842013-12-16 16:14:02 -08002694 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2695 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002696 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2697 stacks.get(stackNdx).goToSleep();
2698 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002699 }
2700
2701 removeSleepTimeouts();
2702
2703 if (mGoingToSleep.isHeld()) {
2704 mGoingToSleep.release();
2705 }
2706 if (mService.mShuttingDown) {
2707 mService.notifyAll();
2708 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002709 }
2710
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002711 boolean reportResumedActivityLocked(ActivityRecord r) {
2712 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002713 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002714 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002715 }
2716 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002717 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002718 mWindowManager.executeAppTransition();
2719 return true;
2720 }
2721 return false;
2722 }
2723
Craig Mautner8d341ef2013-03-26 09:03:27 -07002724 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2726 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002727 int stackNdx = stacks.size() - 1;
2728 while (stackNdx >= 0) {
2729 stacks.get(stackNdx).handleAppCrashLocked(app);
2730 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002731 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002732 }
2733 }
2734
Jose Lima4b6c6692014-08-12 17:41:12 -07002735 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002736 final ActivityStack stack = r.task.stack;
2737 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002738 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2739 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002740 return false;
2741 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002742
2743 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2744 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2745 + " visible=" + visible + " stackId=" + stack.mStackId
2746 + " can't contain visible behind activities");
2747 return false;
2748 }
2749
Jose Lima4b6c6692014-08-12 17:41:12 -07002750 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002751 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2752 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002753
2754 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002755 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002756 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002757 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002758 return true;
2759 }
2760
2761 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002762 if (visible && top.fullscreen) {
2763 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002764 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2765 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2766 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2767 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002768 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002769 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2770 // Only the activity set as currently visible behind should actively reset its
2771 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002772 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2773 "requestVisibleBehind: returning visible=" + visible
2774 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2775 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002776 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002777 }
2778
Jose Lima4b6c6692014-08-12 17:41:12 -07002779 stack.setVisibleBehindActivity(visible ? r : null);
2780 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002781 // If there is a translucent home activity, we need to force it stop being translucent,
2782 // because we can't depend on the application to necessarily perform that operation.
2783 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002784 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002785 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002786 mService.convertFromTranslucent(next.appToken);
2787 }
2788 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002789 if (top.app != null && top.app.thread != null) {
2790 // Notify the top app of the change.
2791 try {
2792 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2793 } catch (RemoteException e) {
2794 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002795 }
2796 return true;
2797 }
2798
Craig Mautnerbb742462014-07-07 15:28:55 -07002799 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2800 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002801 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002802 final ActivityStack stack = task.stack;
2803
2804 r.mLaunchTaskBehind = false;
2805 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002806 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002807 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002808 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002809
2810 // When launching tasks behind, update the last active time of the top task after the new
2811 // task has been shown briefly
2812 final ActivityRecord top = stack.topActivity();
2813 if (top != null) {
2814 top.task.touchActiveTime();
2815 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002816 }
2817
2818 void scheduleLaunchTaskBehindComplete(IBinder token) {
2819 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2820 }
2821
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002822 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2823 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002824 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002825 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2826 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002827 final int topStackNdx = stacks.size() - 1;
2828 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2829 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002830 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002831 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002832 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002833 }
2834
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002835 void invalidateTaskLayers() {
2836 mTaskLayersChanged = true;
2837 }
2838
2839 void rankTaskLayersIfNeeded() {
2840 if (!mTaskLayersChanged) {
2841 return;
2842 }
2843 mTaskLayersChanged = false;
2844 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2845 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2846 int baseLayer = 0;
2847 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2848 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2849 }
2850 }
2851 }
2852
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002853 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2854 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2855 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2856 final int topStackNdx = stacks.size() - 1;
2857 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2858 final ActivityStack stack = stacks.get(stackNdx);
2859 stack.clearOtherAppTimeTrackers(except);
2860 }
2861 }
2862 }
2863
Craig Mautner8d341ef2013-03-26 09:03:27 -07002864 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002865 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2866 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002867 final int numStacks = stacks.size();
2868 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2869 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002870 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002871 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002872 }
2873 }
2874
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002875 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2876 // Examine all activities currently running in the process.
2877 TaskRecord firstTask = null;
2878 // Tasks is non-null only if two or more tasks are found.
2879 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002880 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2881 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002882 ActivityRecord r = app.activities.get(i);
2883 // First, if we find an activity that is in the process of being destroyed,
2884 // then we just aren't going to do anything for now; we want things to settle
2885 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002886 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002887 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002888 return;
2889 }
2890 // Don't consider any activies that are currently not in a state where they
2891 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002892 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2893 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002894 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002895 continue;
2896 }
2897 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002898 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002899 + " from " + r);
2900 if (firstTask == null) {
2901 firstTask = r.task;
2902 } else if (firstTask != r.task) {
2903 if (tasks == null) {
2904 tasks = new ArraySet<>();
2905 tasks.add(firstTask);
2906 }
2907 tasks.add(r.task);
2908 }
2909 }
2910 }
2911 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002912 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002913 return;
2914 }
2915 // If we have activities in multiple tasks that are in a position to be destroyed,
2916 // let's iterate through the tasks and release the oldest one.
2917 final int numDisplays = mActivityDisplays.size();
2918 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2919 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2920 // Step through all stacks starting from behind, to hit the oldest things first.
2921 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2922 final ActivityStack stack = stacks.get(stackNdx);
2923 // Try to release activities in this stack; if we manage to, we are done.
2924 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2925 return;
2926 }
2927 }
2928 }
2929 }
2930
Amith Yamasani37a40c22015-06-17 13:25:42 -07002931 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002932 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002933 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002934 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002935
Craig Mautner858d8a62013-04-23 17:08:34 -07002936 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002937 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2938 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002939 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002940 final ActivityStack stack = stacks.get(stackNdx);
2941 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002942 TaskRecord task = stack.topTask();
2943 if (task != null) {
2944 mWindowManager.moveTaskToTop(task.taskId);
2945 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002947 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002948
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002949 ActivityStack stack = getStack(restoreStackId);
2950 if (stack == null) {
2951 stack = mHomeStack;
2952 }
2953 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002954 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002955 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002956 } else {
2957 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002958 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002959 }
Craig Mautner93529a42013-10-04 15:03:13 -07002960 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002961 }
2962
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002963 /** Checks whether the userid is a profile of the current user. */
2964 boolean isCurrentProfileLocked(int userId) {
2965 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002966 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002967 }
2968
Chong Zhang45c25ce2015-08-10 22:18:26 -07002969 /** Checks whether the activity should be shown for current user. */
2970 boolean okToShowLocked(ActivityRecord r) {
2971 return r != null && (isCurrentProfileLocked(r.userId)
2972 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2973 }
2974
Craig Mautnerde4ef022013-04-07 19:01:33 -07002975 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002976 ArrayList<ActivityRecord> stops = null;
2977
2978 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002979 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2980 ActivityRecord s = mStoppingActivities.get(activityNdx);
2981 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002982 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002983 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2984 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002985 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002986 if (s.finishing) {
2987 // If this activity is finishing, it is sitting on top of
2988 // everyone else but we now know it is no longer needed...
2989 // so get rid of it. Otherwise, we need to go through the
2990 // normal flow and hide it once we determine that it is
2991 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002992 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002993 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002994 }
2995 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002996 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002997 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002998 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002999 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003000 }
3001 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003002 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003003 }
3004 }
3005
3006 return stops;
3007 }
3008
Craig Mautnercf910b02013-04-23 11:23:27 -07003009 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003010 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3011 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3012 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3013 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003014 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003015 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003016 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003017 if (r == null) Slog.e(TAG,
3018 "validateTop...: null top activity, stack=" + stack);
3019 else {
3020 final ActivityRecord pausing = stack.mPausingActivity;
3021 if (pausing != null && pausing == r) Slog.e(TAG,
3022 "validateTop...: top stack has pausing activity r=" + r
3023 + " state=" + state);
3024 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3025 "validateTop...: activity in front not resumed r=" + r
3026 + " state=" + state);
3027 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003028 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003029 final ActivityRecord resumed = stack.mResumedActivity;
3030 if (resumed != null && resumed == r) Slog.e(TAG,
3031 "validateTop...: back stack has resumed activity r=" + r
3032 + " state=" + state);
3033 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3034 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003035 }
3036 }
3037 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003038 }
3039
Craig Mautnere0570202015-05-13 13:06:11 -07003040 private String lockTaskModeToString() {
3041 switch (mLockTaskModeState) {
3042 case LOCK_TASK_MODE_LOCKED:
3043 return "LOCKED";
3044 case LOCK_TASK_MODE_PINNED:
3045 return "PINNED";
3046 case LOCK_TASK_MODE_NONE:
3047 return "NONE";
3048 default: return "unknown=" + mLockTaskModeState;
3049 }
3050 }
3051
Craig Mautner27084302013-03-25 08:05:25 -07003052 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003053 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003054 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003055 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003056 pw.print(prefix);
3057 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003058 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003059 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003060 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3061 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3062 if (packages.size() > 0) {
3063 pw.println(" mLockTaskPackages (userId:packages)=");
3064 for (int i = 0; i < packages.size(); ++i) {
3065 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3066 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3067 }
3068 }
3069 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003070 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003071
Craig Mautner20e72272013-04-01 13:45:53 -07003072 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003073 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003074 }
3075
Dianne Hackborn390517b2013-05-30 15:03:32 -07003076 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3077 boolean needSep, String prefix) {
3078 if (activity != null) {
3079 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3080 if (needSep) {
3081 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003082 }
3083 pw.print(prefix);
3084 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003085 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003086 }
3087 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003088 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003089 }
3090
Craig Mautner8d341ef2013-03-26 09:03:27 -07003091 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3092 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003093 boolean printed = false;
3094 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003095 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3096 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003097 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003098 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003099 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003100 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003101 final ActivityStack stack = stacks.get(stackNdx);
3102 StringBuilder stackHeader = new StringBuilder(128);
3103 stackHeader.append(" Stack #");
3104 stackHeader.append(stack.mStackId);
3105 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003106 stackHeader.append("\n");
3107 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3108 stackHeader.append("\n");
3109 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003110 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3111 needSep, stackHeader.toString());
3112 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3113 !dumpAll, false, dumpPackage, true,
3114 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003115
Craig Mautner4a1cb222013-12-04 16:14:06 -08003116 needSep = printed;
3117 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3118 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003119 if (pr) {
3120 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003121 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003122 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003123 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3124 " mResumedActivity: ");
3125 if (pr) {
3126 printed = true;
3127 needSep = false;
3128 }
3129 if (dumpAll) {
3130 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3131 " mLastPausedActivity: ");
3132 if (pr) {
3133 printed = true;
3134 needSep = true;
3135 }
3136 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3137 needSep, " mLastNoHistoryActivity: ");
3138 }
3139 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003140 }
3141 }
3142
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003143 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3144 false, dumpPackage, true, " Activities waiting to finish:", null);
3145 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3146 false, dumpPackage, true, " Activities waiting to stop:", null);
3147 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3148 false, dumpPackage, true, " Activities waiting for another to become visible:",
3149 null);
3150 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3151 false, dumpPackage, true, " Activities waiting to sleep:", null);
3152 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3153 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003154
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003155 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003156 }
3157
Dianne Hackborn390517b2013-05-30 15:03:32 -07003158 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003159 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003160 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003161 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003162 String innerPrefix = null;
3163 String[] args = null;
3164 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003165 for (int i=list.size()-1; i>=0; i--) {
3166 final ActivityRecord r = list.get(i);
3167 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3168 continue;
3169 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003170 if (innerPrefix == null) {
3171 innerPrefix = prefix + " ";
3172 args = new String[0];
3173 }
3174 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003175 final boolean full = !brief && (complete || !r.isInHistory());
3176 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003177 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003178 needNL = false;
3179 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003180 if (header1 != null) {
3181 pw.println(header1);
3182 header1 = null;
3183 }
3184 if (header2 != null) {
3185 pw.println(header2);
3186 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003187 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003188 if (lastTask != r.task) {
3189 lastTask = r.task;
3190 pw.print(prefix);
3191 pw.print(full ? "* " : " ");
3192 pw.println(lastTask);
3193 if (full) {
3194 lastTask.dump(pw, prefix + " ");
3195 } else if (complete) {
3196 // Complete + brief == give a summary. Isn't that obvious?!?
3197 if (lastTask.intent != null) {
3198 pw.print(prefix); pw.print(" ");
3199 pw.println(lastTask.intent.toInsecureStringWithClip());
3200 }
3201 }
3202 }
3203 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3204 pw.print(" #"); pw.print(i); pw.print(": ");
3205 pw.println(r);
3206 if (full) {
3207 r.dump(pw, innerPrefix);
3208 } else if (complete) {
3209 // Complete + brief == give a summary. Isn't that obvious?!?
3210 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3211 if (r.app != null) {
3212 pw.print(innerPrefix); pw.println(r.app);
3213 }
3214 }
3215 if (client && r.app != null && r.app.thread != null) {
3216 // flush anything that is already in the PrintWriter since the thread is going
3217 // to write to the file descriptor directly
3218 pw.flush();
3219 try {
3220 TransferPipe tp = new TransferPipe();
3221 try {
3222 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3223 r.appToken, innerPrefix, args);
3224 // Short timeout, since blocking here can
3225 // deadlock with the application.
3226 tp.go(fd, 2000);
3227 } finally {
3228 tp.kill();
3229 }
3230 } catch (IOException e) {
3231 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3232 } catch (RemoteException e) {
3233 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3234 }
3235 needNL = true;
3236 }
3237 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003238 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003239 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003240
Craig Mautnerf3333272013-04-22 10:55:53 -07003241 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003242 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3243 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003244 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3245 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003246 }
3247
3248 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003249 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003250 }
3251
3252 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003253 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3254 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003255 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3256 }
3257
Craig Mautner05d29032013-05-03 13:40:13 -07003258 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003259 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3260 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3261 }
Craig Mautner05d29032013-05-03 13:40:13 -07003262 }
3263
Craig Mautner0eea92c2013-05-16 13:35:39 -07003264 void removeSleepTimeouts() {
3265 mSleepTimeout = false;
3266 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3267 }
3268
3269 final void scheduleSleepTimeout() {
3270 removeSleepTimeouts();
3271 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3272 }
3273
Craig Mautner4a1cb222013-12-04 16:14:06 -08003274 @Override
3275 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003276 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003277 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3278 }
3279
3280 @Override
3281 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003282 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003283 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3284 }
3285
3286 @Override
3287 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003288 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003289 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3290 }
3291
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003292 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003293 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003294 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003295 newDisplay = mActivityDisplays.get(displayId) == null;
3296 if (newDisplay) {
3297 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003298 if (activityDisplay.mDisplay == null) {
3299 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3300 return;
3301 }
Craig Mautner4504de52013-12-20 09:06:56 -08003302 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003303 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003304 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003305 }
Craig Mautner4504de52013-12-20 09:06:56 -08003306 if (newDisplay) {
3307 mWindowManager.onDisplayAdded(displayId);
3308 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003309 }
3310
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003311 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003312 mDefaultMinimalSizeOfResizeableTask =
3313 mService.mContext.getResources().getDimensionPixelSize(
3314 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003315 }
3316
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003317 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003318 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003319 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3320 if (activityDisplay != null) {
3321 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003322 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003323 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003324 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003325 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003326 }
3327 }
3328 mWindowManager.onDisplayRemoved(displayId);
3329 }
3330
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003331 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003332 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003333 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3334 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003335 // TODO: Update the bounds.
3336 }
3337 }
3338 mWindowManager.onDisplayChanged(displayId);
3339 }
3340
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003341 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003342 StackInfo info = new StackInfo();
3343 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3344 info.displayId = Display.DEFAULT_DISPLAY;
3345 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003346 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003347
3348 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3349 final int numTasks = tasks.size();
3350 int[] taskIds = new int[numTasks];
3351 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003352 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003353 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003354 for (int i = 0; i < numTasks; ++i) {
3355 final TaskRecord task = tasks.get(i);
3356 taskIds[i] = task.taskId;
3357 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3358 : task.realActivity != null ? task.realActivity.flattenToString()
3359 : task.getTopActivity() != null ? task.getTopActivity().packageName
3360 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003361 taskBounds[i] = new Rect();
3362 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003363 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003364 }
3365 info.taskIds = taskIds;
3366 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003367 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003368 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003369 return info;
3370 }
3371
3372 StackInfo getStackInfoLocked(int stackId) {
3373 ActivityStack stack = getStack(stackId);
3374 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003375 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003376 }
3377 return null;
3378 }
3379
3380 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003381 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003382 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3383 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003384 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003385 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003386 }
3387 }
3388 return list;
3389 }
3390
Craig Mautner15df08a2015-04-01 12:17:18 -07003391 TaskRecord getLockedTaskLocked() {
3392 final int top = mLockTaskModeTasks.size() - 1;
3393 if (top >= 0) {
3394 return mLockTaskModeTasks.get(top);
3395 }
3396 return null;
3397 }
3398
3399 boolean isLockedTask(TaskRecord task) {
3400 return mLockTaskModeTasks.contains(task);
3401 }
3402
3403 boolean isLastLockedTask(TaskRecord task) {
3404 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3405 }
3406
3407 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003408 if (!mLockTaskModeTasks.remove(task)) {
3409 return;
3410 }
3411 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3412 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003413 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003414 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3415 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003416 final Message lockTaskMsg = Message.obtain();
3417 lockTaskMsg.arg1 = task.userId;
3418 lockTaskMsg.what = LOCK_TASK_END_MSG;
3419 mHandler.sendMessage(lockTaskMsg);
3420 }
3421 }
3422
Andrii Kulianc27916642016-04-12 17:59:27 -07003423 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3424 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3425 false /* forceNonResizable */);
3426 }
3427
Jorim Jaggid53f0922016-04-06 22:16:23 -07003428 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003429 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003430 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3431 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003432 return;
3433 }
3434
Andrii Kulianc27916642016-04-12 17:59:27 -07003435 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003436 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003437 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3438
Andrii Kulianc27916642016-04-12 17:59:27 -07003439 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3440 // we need to move it to top of fullscreen stack, otherwise it will be covered.
3441 mService.moveTasksToFullscreenStack(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003442 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3443 String packageName = task.getTopActivity() != null
3444 ? task.getTopActivity().appInfo.packageName : null;
3445 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3446 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003447 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003448 }
3449
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003450 void showLockTaskToast() {
3451 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003452 }
3453
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003454 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3455 if (mLockTaskModeTasks.contains(task)) {
3456 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3457 }
3458 }
3459
Craig Mautner432f64e2015-05-20 14:59:57 -07003460 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3461 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003462 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003463 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003464 final TaskRecord lockedTask = getLockedTaskLocked();
3465 if (lockedTask != null) {
3466 removeLockedTaskLocked(lockedTask);
3467 if (!mLockTaskModeTasks.isEmpty()) {
3468 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003469 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3470 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003471 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003472 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003473 return;
3474 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003475 }
Craig Mautnere0570202015-05-13 13:06:11 -07003476 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3477 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003478 return;
3479 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003480
3481 // Should have already been checked, but do it again.
3482 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003483 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3484 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003485 return;
3486 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003487 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003488 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003489 return;
3490 }
3491
3492 if (mLockTaskModeTasks.isEmpty()) {
3493 // First locktask.
3494 final Message lockTaskMsg = Message.obtain();
3495 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3496 lockTaskMsg.arg1 = task.userId;
3497 lockTaskMsg.what = LOCK_TASK_START_MSG;
3498 lockTaskMsg.arg2 = lockTaskModeState;
3499 mHandler.sendMessage(lockTaskMsg);
3500 }
3501 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003502 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3503 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003504 mLockTaskModeTasks.remove(task);
3505 mLockTaskModeTasks.add(task);
3506
3507 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003508 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003509 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003510
3511 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003512 findTaskToMoveToFrontLocked(task, 0, null, reason,
3513 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003514 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003515 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3516 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3517 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003518 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003519 }
3520
3521 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003522 return isLockTaskModeViolation(task, false);
3523 }
3524
3525 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3526 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003527 return false;
3528 }
3529 final int lockTaskAuth = task.mLockTaskAuth;
3530 switch (lockTaskAuth) {
3531 case LOCK_TASK_AUTH_DONT_LOCK:
3532 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003533 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003534 case LOCK_TASK_AUTH_LAUNCHABLE:
3535 case LOCK_TASK_AUTH_WHITELISTED:
3536 return false;
3537 case LOCK_TASK_AUTH_PINNABLE:
3538 // Pinnable tasks can't be launched on top of locktask tasks.
3539 return !mLockTaskModeTasks.isEmpty();
3540 default:
3541 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3542 return true;
3543 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003544 }
3545
Craig Mautner15df08a2015-04-01 12:17:18 -07003546 void onLockTaskPackagesUpdatedLocked() {
3547 boolean didSomething = false;
3548 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3549 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003550 final boolean wasWhitelisted =
3551 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3552 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003553 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003554 final boolean isWhitelisted =
3555 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3556 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3557 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003558 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003559 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3560 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003561 removeLockedTaskLocked(lockedTask);
3562 lockedTask.performClearTaskLocked();
3563 didSomething = true;
3564 }
3565 }
3566 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3567 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3568 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3569 final ActivityStack stack = stacks.get(stackNdx);
3570 stack.onLockTaskPackagesUpdatedLocked();
3571 }
3572 }
Craig Mautnere0570202015-05-13 13:06:11 -07003573 final ActivityRecord r = topRunningActivityLocked();
3574 final TaskRecord task = r != null ? r.task : null;
3575 if (mLockTaskModeTasks.isEmpty() && task != null
3576 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3577 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003578 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3579 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3580 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3581 false);
3582 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003583 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003584 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003585 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003586 }
3587 }
3588
Benjamin Franz43261142015-02-11 15:59:44 +00003589 int getLockTaskModeState() {
3590 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003591 }
3592
Jorim Jaggife89d122015-12-22 16:28:44 +01003593 void activityRelaunchedLocked(IBinder token) {
3594 mWindowManager.notifyAppRelaunchingFinished(token);
3595 }
3596
3597 void activityRelaunchingLocked(ActivityRecord r) {
3598 mWindowManager.notifyAppRelaunching(r.appToken);
3599 }
3600
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003601 void logStackState() {
3602 mActivityMetricsLogger.logWindowState();
3603 }
3604
Andrii Kulian933076d2016-03-29 17:04:42 -07003605 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003606 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3607 final ActivityRecord r = task.mActivities.get(i);
3608 if (r.app != null && r.app.thread != null) {
3609 mMultiWindowModeChangedActivities.add(r);
3610 }
3611 }
3612
3613 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3614 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3615 }
3616 }
3617
Andrii Kulian933076d2016-03-29 17:04:42 -07003618 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003619 final ActivityStack stack = task.stack;
3620 if (prevStack == null || prevStack == stack
3621 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3622 return;
3623 }
3624
3625 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3626 final ActivityRecord r = task.mActivities.get(i);
3627 if (r.app != null && r.app.thread != null) {
3628 mPipModeChangedActivities.add(r);
3629 }
3630 }
3631
3632 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3633 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3634 }
3635 }
3636
Tony Mak853304c2016-04-18 15:17:41 +01003637 void setDockedStackMinimized(boolean minimized) {
3638 mIsDockMinimized = minimized;
3639 if (minimized) {
3640 // Docked stack is not visible, no need to confirm credentials for its top activity.
3641 return;
3642 }
3643 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3644 if (dockedStack == null) {
3645 return;
3646 }
3647 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3648 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3649 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3650 }
3651 }
3652
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003653 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003654
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003655 public ActivityStackSupervisorHandler(Looper looper) {
3656 super(looper);
3657 }
3658
Craig Mautnerf3333272013-04-22 10:55:53 -07003659 void activityIdleInternal(ActivityRecord r) {
3660 synchronized (mService) {
3661 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3662 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003663 }
3664
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003665 @Override
3666 public void handleMessage(Message msg) {
3667 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003668 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3669 synchronized (mService) {
3670 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3671 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003672 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003673 }
3674 }
3675 } break;
3676 case REPORT_PIP_MODE_CHANGED_MSG: {
3677 synchronized (mService) {
3678 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3679 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003680 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003681 }
3682 }
3683 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003684 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003685 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3686 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003687 if (mService.mDidDexOpt) {
3688 mService.mDidDexOpt = false;
3689 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3690 nmsg.obj = msg.obj;
3691 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3692 return;
3693 }
3694 // We don't at this point know if the activity is fullscreen,
3695 // so we need to be conservative and assume it isn't.
3696 activityIdleInternal((ActivityRecord)msg.obj);
3697 } break;
3698 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003699 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003700 activityIdleInternal((ActivityRecord)msg.obj);
3701 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003702 case RESUME_TOP_ACTIVITY_MSG: {
3703 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003704 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003705 }
3706 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003707 case SLEEP_TIMEOUT_MSG: {
3708 synchronized (mService) {
3709 if (mService.isSleepingOrShuttingDown()) {
3710 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3711 mSleepTimeout = true;
3712 checkReadyForSleepLocked();
3713 }
3714 }
3715 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003716 case LAUNCH_TIMEOUT_MSG: {
3717 if (mService.mDidDexOpt) {
3718 mService.mDidDexOpt = false;
3719 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3720 return;
3721 }
3722 synchronized (mService) {
3723 if (mLaunchingActivity.isHeld()) {
3724 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3725 if (VALIDATE_WAKE_LOCK_CALLER
3726 && Binder.getCallingUid() != Process.myUid()) {
3727 throw new IllegalStateException("Calling must be system uid");
3728 }
3729 mLaunchingActivity.release();
3730 }
3731 }
3732 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003733 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003734 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003735 } break;
3736 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003737 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003738 } break;
3739 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003740 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003741 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003742 case CONTAINER_CALLBACK_VISIBILITY: {
3743 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003744 final IActivityContainerCallback callback = container.mCallback;
3745 if (callback != null) {
3746 try {
3747 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3748 } catch (RemoteException e) {
3749 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003750 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003751 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003752 case LOCK_TASK_START_MSG: {
3753 // When lock task starts, we disable the status bars.
3754 try {
Jason Monk62515be2014-05-21 16:06:19 -04003755 if (mLockTaskNotify == null) {
3756 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003757 }
Jason Monk62515be2014-05-21 16:06:19 -04003758 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003759 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003760 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003761 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003762 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003763 flags = StatusBarManager.DISABLE_MASK
3764 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003765 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003766 flags = StatusBarManager.DISABLE_MASK
3767 & (~StatusBarManager.DISABLE_BACK)
3768 & (~StatusBarManager.DISABLE_HOME)
3769 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003770 }
3771 getStatusBarService().disable(flags, mToken,
3772 mService.mContext.getPackageName());
3773 }
3774 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003775 if (getDevicePolicyManager() != null) {
3776 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003777 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003778 }
justinzhang5286d3f2014-05-12 17:06:01 -04003779 } catch (RemoteException ex) {
3780 throw new RuntimeException(ex);
3781 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003782 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003783 case LOCK_TASK_END_MSG: {
3784 // When lock task ends, we enable the status bars.
3785 try {
Jason Monk62515be2014-05-21 16:06:19 -04003786 if (getStatusBarService() != null) {
3787 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3788 mService.mContext.getPackageName());
3789 }
3790 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003791 if (getDevicePolicyManager() != null) {
3792 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3793 msg.arg1);
3794 }
Jason Monk62515be2014-05-21 16:06:19 -04003795 if (mLockTaskNotify == null) {
3796 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3797 }
3798 mLockTaskNotify.show(false);
3799 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003800 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003801 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003802 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003803 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003804 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003805 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003806 new LockPatternUtils(mService.mContext)
3807 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003808 }
3809 } catch (SettingNotFoundException e) {
3810 // No setting, don't lock.
3811 }
justinzhang5286d3f2014-05-12 17:06:01 -04003812 } catch (RemoteException ex) {
3813 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003814 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003815 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003816 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003817 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003818 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3819 if (mLockTaskNotify == null) {
3820 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3821 }
3822 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3823 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003824 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3825 final ActivityContainer container = (ActivityContainer) msg.obj;
3826 final IActivityContainerCallback callback = container.mCallback;
3827 if (callback != null) {
3828 try {
3829 callback.onAllActivitiesComplete(container.asBinder());
3830 } catch (RemoteException e) {
3831 }
3832 }
3833 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003834 case LAUNCH_TASK_BEHIND_COMPLETE: {
3835 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003836 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003837 if (r != null) {
3838 handleLaunchTaskBehindCompleteLocked(r);
3839 }
3840 }
3841 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003842
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003843 }
3844 }
3845 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003846
Ying Wangb081a592014-04-22 15:20:16 -07003847 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003848 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3849 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003850 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003851 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003852 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003853 ActivityRecord mParentActivity = null;
3854 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003855
Craig Mautnere3a00d72014-04-16 08:31:19 -07003856 boolean mVisible = true;
3857
Craig Mautner4a1cb222013-12-04 16:14:06 -08003858 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003859 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003860
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003861 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3862 final static int CONTAINER_STATE_NO_SURFACE = 1;
3863 final static int CONTAINER_STATE_FINISHING = 2;
3864 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3865
3866 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003867 synchronized (mService) {
3868 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003869 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003870 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003871 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003872 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003873 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003874
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003875 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003876 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003877 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003878 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003879 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003880 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003881 }
3882
3883 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003884 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003885 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003886 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3887 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003888 return;
3889 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003890 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003891 }
3892 }
3893
3894 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003895 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003896 synchronized (mService) {
3897 if (mActivityDisplay != null) {
3898 return mActivityDisplay.mDisplayId;
3899 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003900 }
3901 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003902 }
3903
Jeff Brownca9bc702014-02-11 14:32:56 -08003904 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003905 public int getStackId() {
3906 synchronized (mService) {
3907 return mStackId;
3908 }
3909 }
3910
3911 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003912 public boolean injectEvent(InputEvent event) {
3913 final long origId = Binder.clearCallingIdentity();
3914 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003915 synchronized (mService) {
3916 if (mActivityDisplay != null) {
3917 return mInputManagerInternal.injectInputEvent(event,
3918 mActivityDisplay.mDisplayId,
3919 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3920 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003921 }
3922 return false;
3923 } finally {
3924 Binder.restoreCallingIdentity(origId);
3925 }
3926 }
3927
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003928 @Override
3929 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003930 synchronized (mService) {
3931 if (mContainerState == CONTAINER_STATE_FINISHING) {
3932 return;
3933 }
3934 mContainerState = CONTAINER_STATE_FINISHING;
3935
Craig Mautnerd163e752014-06-13 17:18:47 -07003936 long origId = Binder.clearCallingIdentity();
3937 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003938 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003939 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003940 } finally {
3941 Binder.restoreCallingIdentity(origId);
3942 }
3943 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003944 }
3945
Craig Mautner60257702014-09-17 15:02:33 -07003946 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003947 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003948 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003949 if (mActivityDisplay != null) {
3950 mActivityDisplay.detachActivitiesLocked(mStack);
3951 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003952 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003953 }
3954 }
3955
3956 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003957 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003958 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003959 }
3960
3961 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003962 public final int startActivityIntentSender(IIntentSender intentSender)
3963 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003964 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3965
3966 if (!(intentSender instanceof PendingIntentRecord)) {
3967 throw new IllegalArgumentException("Bad PendingIntent object");
3968 }
3969
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003970 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003971 Binder.getCallingUid(), mCurrentUser, false,
3972 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003973
3974 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3975 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3976 pendingIntent.key.requestResolvedType);
3977
3978 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3979 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3980 }
3981
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003982 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003983 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003984 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003985 throw new SecurityException(
3986 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3987 }
3988 }
3989
Craig Mautnerdf88d732014-01-27 09:21:32 -08003990 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003991 public IBinder asBinder() {
3992 return this;
3993 }
3994
Craig Mautner4504de52013-12-20 09:06:56 -08003995 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003996 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003997 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003998 }
3999
Craig Mautner4a1cb222013-12-04 16:14:06 -08004000 ActivityStackSupervisor getOuter() {
4001 return ActivityStackSupervisor.this;
4002 }
4003
Craig Mautnerd163e752014-06-13 17:18:47 -07004004 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004005 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004006 }
4007
Craig Mautner6985bad2014-04-21 15:22:06 -07004008 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004009 void setVisible(boolean visible) {
4010 if (mVisible != visible) {
4011 mVisible = visible;
4012 if (mCallback != null) {
4013 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4014 0 /* unused */, this).sendToTarget();
4015 }
4016 }
4017 }
4018
Craig Mautner6985bad2014-04-21 15:22:06 -07004019 void setDrawn() {
4020 }
4021
Craig Mautner1b4bf852014-05-26 15:06:32 -07004022 // You can always start a new task on a regular ActivityStack.
4023 boolean isEligibleForNewTasks() {
4024 return true;
4025 }
4026
Craig Mautnerd163e752014-06-13 17:18:47 -07004027 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004028 detachLocked();
4029 deleteActivityContainer(this);
4030 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004031 }
4032
Craig Mautner34b73df2014-01-12 21:11:08 -08004033 @Override
4034 public String toString() {
4035 return mIdString + (mActivityDisplay == null ? "N" : "A");
4036 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004037 }
4038
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004039 private class VirtualActivityContainer extends ActivityContainer {
4040 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004041 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004042
4043 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4044 super(getNextStackId());
4045 mParentActivity = parent;
4046 mCallback = callback;
4047 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004048 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004049 }
4050
4051 @Override
4052 public void setSurface(Surface surface, int width, int height, int density) {
4053 super.setSurface(surface, width, height, density);
4054
4055 synchronized (mService) {
4056 final long origId = Binder.clearCallingIdentity();
4057 try {
4058 setSurfaceLocked(surface, width, height, density);
4059 } finally {
4060 Binder.restoreCallingIdentity(origId);
4061 }
4062 }
4063 }
4064
4065 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4066 if (mContainerState == CONTAINER_STATE_FINISHING) {
4067 return;
4068 }
4069 VirtualActivityDisplay virtualActivityDisplay =
4070 (VirtualActivityDisplay) mActivityDisplay;
4071 if (virtualActivityDisplay == null) {
4072 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004073 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004074 mActivityDisplay = virtualActivityDisplay;
4075 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004076 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004077 }
4078
4079 if (mSurface != null) {
4080 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004081 }
4082
Craig Mautner6985bad2014-04-21 15:22:06 -07004083 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004084 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004085 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004086 } else {
4087 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004088 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004089 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004090 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004091 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004092 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004093
Craig Mautnerd163e752014-06-13 17:18:47 -07004094 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004095
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004096 if (DEBUG_STACK) Slog.d(TAG_STACK,
4097 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004098 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004099
Craig Mautner6985bad2014-04-21 15:22:06 -07004100 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004101 boolean isAttachedLocked() {
4102 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004103 }
4104
4105 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004106 void setDrawn() {
4107 synchronized (mService) {
4108 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004109 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004110 }
4111 }
4112
Craig Mautner1b4bf852014-05-26 15:06:32 -07004113 // Never start a new task on an ActivityView if it isn't explicitly specified.
4114 @Override
4115 boolean isEligibleForNewTasks() {
4116 return false;
4117 }
4118
Craig Mautnerd163e752014-06-13 17:18:47 -07004119 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004120 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004121 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4122 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4123 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4124 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4125 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004126 }
4127 }
4128
Craig Mautner4a1cb222013-12-04 16:14:06 -08004129 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4130 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004131 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004132 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004133 int mDisplayId;
4134 Display mDisplay;
4135 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004136
Craig Mautner4a1cb222013-12-04 16:14:06 -08004137 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4138 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004139 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004140
Jose Lima4b6c6692014-08-12 17:41:12 -07004141 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004142
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004143 ActivityDisplay() {
4144 }
Craig Mautner4504de52013-12-20 09:06:56 -08004145
Craig Mautner1a70a162014-09-13 12:09:31 -07004146 // After instantiation, check that mDisplay is not null before using this. The alternative
4147 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004148 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004149 final Display display = mDisplayManager.getDisplay(displayId);
4150 if (display == null) {
4151 return;
4152 }
4153 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004154 }
4155
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004156 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004157 mDisplay = display;
4158 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004159 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004160 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004161
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004162 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004163 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004164 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4165 + " onTop=" + onTop);
4166 if (onTop) {
4167 mStacks.add(stack);
4168 } else {
4169 mStacks.add(0, stack);
4170 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004171 }
4172
Craig Mautnere0a38842013-12-16 16:14:02 -08004173 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004174 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004175 + " from displayId=" + mDisplayId);
4176 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004177 }
4178
Jose Lima4b6c6692014-08-12 17:41:12 -07004179 void setVisibleBehindActivity(ActivityRecord r) {
4180 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004181 }
4182
Jose Lima4b6c6692014-08-12 17:41:12 -07004183 boolean hasVisibleBehindActivity() {
4184 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004185 }
4186
Craig Mautner34b73df2014-01-12 21:11:08 -08004187 @Override
4188 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004189 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4190 }
4191 }
4192
4193 class VirtualActivityDisplay extends ActivityDisplay {
4194 VirtualDisplay mVirtualDisplay;
4195
Craig Mautner6985bad2014-04-21 15:22:06 -07004196 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004197 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004198 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4199 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4200 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4201 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004202
4203 init(mVirtualDisplay.getDisplay());
4204
4205 mWindowManager.handleDisplayAdded(mDisplayId);
4206 }
4207
4208 void setSurface(Surface surface) {
4209 if (mVirtualDisplay != null) {
4210 mVirtualDisplay.setSurface(surface);
4211 }
4212 }
4213
4214 @Override
4215 void detachActivitiesLocked(ActivityStack stack) {
4216 super.detachActivitiesLocked(stack);
4217 if (mVirtualDisplay != null) {
4218 mVirtualDisplay.release();
4219 mVirtualDisplay = null;
4220 }
4221 }
4222
4223 @Override
4224 public String toString() {
4225 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004226 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004227 }
Jose Lima58e66d62014-05-27 20:00:27 -07004228
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004229 /**
4230 * Adjust bounds to stay within stack bounds.
4231 *
4232 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4233 * that keep them unchanged, but be contained within the stack bounds.
4234 *
4235 * @param bounds Bounds to be adjusted.
4236 * @param stackBounds Bounds within which the other bounds should remain.
4237 */
4238 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4239 if (stackBounds == null || stackBounds.contains(bounds)) {
4240 return;
4241 }
4242
4243 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4244 final int maxRight = stackBounds.right
4245 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4246 int horizontalDiff = stackBounds.left - bounds.left;
4247 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4248 || (bounds.left + horizontalDiff >= maxRight)) {
4249 horizontalDiff = maxRight - bounds.left;
4250 }
4251 bounds.left += horizontalDiff;
4252 bounds.right += horizontalDiff;
4253 }
4254
4255 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4256 final int maxBottom = stackBounds.bottom
4257 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4258 int verticalDiff = stackBounds.top - bounds.top;
4259 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4260 || (bounds.top + verticalDiff >= maxBottom)) {
4261 verticalDiff = maxBottom - bounds.top;
4262 }
4263 bounds.top += verticalDiff;
4264 bounds.bottom += verticalDiff;
4265 }
4266 }
4267
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004268 ActivityStack findStackBehind(ActivityStack stack) {
4269 // TODO(multi-display): We are only looking for stacks on the default display.
4270 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4271 if (display == null) {
4272 return null;
4273 }
4274 final ArrayList<ActivityStack> stacks = display.mStacks;
4275 for (int i = stacks.size() - 1; i >= 0; i--) {
4276 if (stacks.get(i) == stack && i > 0) {
4277 return stacks.get(i - 1);
4278 }
4279 }
4280 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4281 + " in=" + stacks);
4282 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004283
Jorim Jaggic69bd222016-03-15 14:38:37 +01004284 /**
4285 * Puts a task into resizing mode during the next app transition.
4286 *
4287 * @param taskId the id of the task to put into resizing mode
4288 */
4289 private void setResizingDuringAnimation(int taskId) {
4290 mResizingTasksDuringAnimation.add(taskId);
4291 mWindowManager.setTaskDockedResizing(taskId, true);
4292 }
4293
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004294 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4295 final TaskRecord task;
4296 final int callingUid;
4297 final String callingPackage;
4298 final Intent intent;
4299 final int userId;
4300 final ActivityOptions activityOptions = (bOptions != null)
4301 ? new ActivityOptions(bOptions) : null;
4302 final int launchStackId = (activityOptions != null)
4303 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
4304
4305 if (launchStackId == HOME_STACK_ID) {
4306 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4307 + taskId + " can't be launch in the home stack.");
4308 }
4309 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4310 if (task == null) {
4311 throw new IllegalArgumentException(
4312 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4313 }
4314
4315 if (launchStackId != INVALID_STACK_ID) {
4316 if (launchStackId == DOCKED_STACK_ID) {
4317 mWindowManager.setDockedStackCreateState(
4318 activityOptions.getDockCreateMode(), null /* initialBounds */);
4319
4320 // Defer updating the stack in which recents is until the app transition is done, to
4321 // not run into issues where we still need to draw the task in recents but the
4322 // docked stack is already created.
4323 deferUpdateBounds(HOME_STACK_ID);
4324 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4325 }
4326 if (task.stack.mStackId != launchStackId) {
4327 moveTaskToStackLocked(
4328 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4329 ANIMATE);
4330 }
4331 }
4332
4333 // If the user must confirm credentials (e.g. when first launching a work app and the
4334 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4335 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4336 && task.getRootActivity() != null) {
Jorim Jaggi09c49542016-04-09 01:39:40 -07004337 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004338 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
4339
4340 // If we are launching the task in the docked stack, put it into resizing mode so
4341 // the window renders full-screen with the background filling the void. Also only
4342 // call this at the end to make sure that tasks exists on the window manager side.
4343 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004344 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004345 }
4346 return ActivityManager.START_TASK_TO_FRONT;
4347 }
4348 callingUid = task.mCallingUid;
4349 callingPackage = task.mCallingPackage;
4350 intent = task.intent;
4351 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4352 userId = task.userId;
Jorim Jaggic69bd222016-03-15 14:38:37 +01004353 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4354 null, null, 0, 0, bOptions, userId, null, task);
4355 if (launchStackId == DOCKED_STACK_ID) {
4356 setResizingDuringAnimation(task.taskId);
4357 }
4358 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004359 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004360
4361 /**
4362 * @return a list of activities which are the top ones in each visible stack. The first
4363 * entry will be the focused activity.
4364 */
4365 public List<IBinder> getTopVisibleActivities() {
4366 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4367 if (display == null) {
4368 return Collections.EMPTY_LIST;
4369 }
4370 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4371 final ArrayList<ActivityStack> stacks = display.mStacks;
4372 for (int i = stacks.size() - 1; i >= 0; i--) {
4373 ActivityStack stack = stacks.get(i);
4374 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4375 ActivityRecord top = stack.topActivity();
4376 if (top != null) {
4377 if (stack == mFocusedStack) {
4378 topActivityTokens.add(0, top.appToken);
4379 } else {
4380 topActivityTokens.add(top.appToken);
4381 }
4382 }
4383 }
4384 }
4385 return topActivityTokens;
4386 }
Craig Mautner27084302013-03-25 08:05:25 -07004387}