blob: f285cea745d1d121ac8da84a3af158cccc98f4a9 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Svetoslav7008b512015-06-24 18:47:07 -070019import android.Manifest;
Tony Mak853304c2016-04-18 15:17:41 +010020import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -070021import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070022import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080023import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070024import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080025import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070026import android.app.ActivityOptions;
27import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070028import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080029import android.app.IActivityContainer;
30import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070031import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080032import android.app.IActivityManager.WaitResult;
Tony Mak853304c2016-04-18 15:17:41 +010033import android.app.KeyguardManager;
34import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070035import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070036import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040037import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040038import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070040import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070042import android.content.Intent;
Tony Mak853304c2016-04-18 15:17:41 +010043import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070044import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070045import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070046import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.content.pm.PackageManager;
48import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010049import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080051import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080052import android.hardware.display.DisplayManager;
53import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080054import android.hardware.display.DisplayManagerGlobal;
55import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080056import android.hardware.input.InputManager;
57import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010059import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070060import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070061import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070063import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070064import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070066import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070067import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070068import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040069import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070070import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070071import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070072import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070073import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +010074import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070075import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070076import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040077import android.provider.Settings;
78import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070079import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070080import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070081import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070082import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070083import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080084import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -080085import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080086import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080087import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080088import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080089import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -080090
Dianne Hackborn85d558c2014-11-04 10:31:54 -080091import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070092import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040093import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -070094import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -040095import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -080096import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -070097import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070098import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import java.io.FileDescriptor;
101import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700102import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700103import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700104import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700105import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700106import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800107import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700108import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700109
Jorim Jaggife89d122015-12-22 16:28:44 +0100110import static android.Manifest.permission.START_ANY_ACTIVITY;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100111import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100112import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
113import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
114import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
115import static android.app.ActivityManager.RESIZE_MODE_FORCED;
116import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
117import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
118import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
119import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
120import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
121import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
122import static android.app.ActivityManager.StackId.HOME_STACK_ID;
123import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
124import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
125import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
126import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
127import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
128import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggi51605272016-02-24 18:06:49 -0500129import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100130import static android.content.pm.PackageManager.PERMISSION_GRANTED;
131import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
140import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
141import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
142import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
143import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
144import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
151import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
152import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
153import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
154import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
155import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
156import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
157import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800158import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100159import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggid53f0922016-04-06 22:16:23 -0700160import static com.android.server.am.ActivityManagerService.NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100161import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100162import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
163import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
164import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
165import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
166import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
167import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
168import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
169import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
170import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
171import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
172import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Tony Mak853304c2016-04-18 15:17:41 +0100173import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100174import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
175import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
176import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
177import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
178import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100179import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100180
Craig Mautner4a1cb222013-12-04 16:14:06 -0800181public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800182 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700185 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700186 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700191 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800192 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800194
Craig Mautnerf3333272013-04-22 10:55:53 -0700195 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700197
Craig Mautner0eea92c2013-05-16 13:35:39 -0700198 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700200
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700201 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700203
Craig Mautner05d29032013-05-03 13:40:13 -0700204 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
205 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
206 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700208 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800209 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
210 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
211 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700212 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400213 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
214 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700215 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700216 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700217 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800218 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
219 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800220
Wale Ogunwale040b4702015-08-06 18:10:50 -0700221 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700222
Jason Monk62515be2014-05-21 16:06:19 -0400223 private static final String LOCK_TASK_TAG = "Lock-to-App";
224
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225 // Used to indicate if an object (e.g. stack) that we are trying to get
226 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800227 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700229 // Used to indicate that windows of activities should be preserved during the resize.
230 static final boolean PRESERVE_WINDOWS = true;
231
Wale Ogunwale040b4702015-08-06 18:10:50 -0700232 // Used to indicate if an object (e.g. task) should be moved/created
233 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700234 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235
236 // Used to indicate that an objects (e.g. task) removal from its container
237 // (e.g. stack) is due to it moving to another container.
238 static final boolean MOVING = true;
239
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700240 // Force the focus to change to the stack we are moving a task to..
241 static final boolean FORCE_FOCUS = true;
242
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700243 // Restore task from the saved recents if it can't be found in any live stack.
244 static final boolean RESTORE_FROM_RECENTS = true;
245
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700246 // Don't execute any calls to resume.
247 static final boolean DEFER_RESUME = true;
248
Svetoslav7008b512015-06-24 18:47:07 -0700249 // Activity actions an app cannot start if it uses a permission which is not granted.
250 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
251 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700252
Svetoslav7008b512015-06-24 18:47:07 -0700253 static {
254 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
255 Manifest.permission.CAMERA);
256 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
257 Manifest.permission.CAMERA);
258 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
259 Manifest.permission.CALL_PHONE);
260 }
261
Svet Ganov99b60432015-06-27 13:15:22 -0700262 /** Action restriction: launching the activity is not restricted. */
263 private static final int ACTIVITY_RESTRICTION_NONE = 0;
264 /** Action restriction: launching the activity is restricted by a permission. */
265 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
266 /** Action restriction: launching the activity is restricted by an app op. */
267 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700268
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700269 // The height/width divide used when fitting a task within a bounds with method
270 // {@link #fitWithinBounds}.
271 // We always want the task to to be visible in the bounds without affecting its size when
272 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
273 // the input bounds right or bottom side minus the width or height divided by this value.
274 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
275
justinzhang5286d3f2014-05-12 17:06:01 -0400276 /** Status Bar Service **/
277 private IBinder mToken = new Binder();
278 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400279 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400280
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700281 // For debugging to make sure the caller when acquiring/releasing our
282 // wake lock is the system process.
283 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800284 /** The number of distinct task ids that can be assigned to the tasks of a single user */
285 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700286
Craig Mautner27084302013-03-25 08:05:25 -0700287 final ActivityManagerService mService;
288
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800289 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800290
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700291 final ActivityStackSupervisorHandler mHandler;
292
293 /** Short cut */
294 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800295 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700296
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700297 /** Counter for next free stack ID to use for dynamic activity stacks. */
298 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700299
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800300 /**
301 * Maps the task identifier that activities are currently being started in to the userId of the
302 * task. Each time a new task is created, the entry for the userId of the task is incremented
303 */
304 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700305
Craig Mautner2420ead2013-04-01 17:13:20 -0700306 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800307 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700308
Craig Mautnere0a38842013-12-16 16:14:02 -0800309 /** The stack containing the launcher app. Assumed to always be attached to
310 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800311 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700312
Craig Mautnere0a38842013-12-16 16:14:02 -0800313 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800314 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700315
Craig Mautner4a1cb222013-12-04 16:14:06 -0800316 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
317 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800318 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700320
321 /** List of activities that are waiting for a new activity to become visible before completing
322 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800323 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700324
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700325 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800326 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700327
328 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800329 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700330
Craig Mautnerde4ef022013-04-07 19:01:33 -0700331 /** List of activities that are ready to be stopped, but waiting for the next activity to
332 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800333 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700334
Craig Mautnerf3333272013-04-22 10:55:53 -0700335 /** List of activities that are ready to be finished, but waiting for the previous activity to
336 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800337 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700338
Craig Mautner0eea92c2013-05-16 13:35:39 -0700339 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800340 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700341
Wale Ogunwale22e25262016-02-01 10:32:02 -0800342 /** List of activities whose multi-window mode changed that we need to report to the
343 * application */
344 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
345
346 /** List of activities whose picture-in-picture mode changed that we need to report to the
347 * application */
348 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
349
Craig Mautnerf3333272013-04-22 10:55:53 -0700350 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700351 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700352
Craig Mautnerde4ef022013-04-07 19:01:33 -0700353 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
354 * is being brought in front of us. */
355 boolean mUserLeaving = false;
356
Craig Mautner0eea92c2013-05-16 13:35:39 -0700357 /** Set when we have taken too long waiting to go to sleep. */
358 boolean mSleepTimeout = false;
359
360 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700361 * We don't want to allow the device to go to sleep while in the process
362 * of launching an activity. This is primarily to allow alarm intent
363 * receivers to launch an activity and get that to run before the device
364 * goes back to sleep.
365 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700366 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700367
368 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700369 * Set when the system is going to sleep, until we have
370 * successfully paused the current activity and released our wake lock.
371 * At that point the system is allowed to actually sleep.
372 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700373 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700374
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700375 /** Stack id of the front stack when user switched, indexed by userId. */
376 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700377
Craig Mautner4504de52013-12-20 09:06:56 -0800378 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700380 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381
382 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700383 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800384
Jeff Brownca9bc702014-02-11 14:32:56 -0800385 InputManagerInternal mInputManagerInternal;
386
Craig Mautner15df08a2015-04-01 12:17:18 -0700387 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
388 * may be finished until there is only one entry left. If this is empty the system is not
389 * in lockTask mode. */
390 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000391 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700392 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
393 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000394 */
395 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400396 /**
397 * Notifies the user when entering/exiting lock-task.
398 */
399 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800400
Wale Ogunwaled046a012015-12-24 13:05:59 -0800401 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800402 boolean inResumeTopActivity;
403
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700404 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700405 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700406 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700407
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700408 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
409 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800410 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700411
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700412 // The default minimal size that will be used if the activity doesn't specify its minimal size.
413 // It will be calculated when the default display gets added.
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800414 int mDefaultMinimalSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700415
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700416 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
417 private boolean mTaskLayersChanged = true;
418
Jorim Jaggi275561a2016-02-23 10:11:02 -0500419 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800420
Jorim Jaggidc249c42015-12-15 14:57:31 -0800421 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
422
Wale Ogunwale39381972015-12-17 17:15:29 -0800423 static class FindTaskResult {
424 ActivityRecord r;
425 boolean matchedByRootAffinity;
426 }
427 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
428
Craig Mautneree36c772014-07-16 14:56:05 -0700429 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100430 * Used to keep track whether app visibilities got changed since the last pause. Useful to
431 * determine whether to invoke the task stack change listener after pausing.
432 */
433 boolean mAppVisibilitiesChangedSinceLastPause;
434
435 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100436 * Set of tasks that are in resizing mode during an app transition to fill the "void".
437 */
438 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
439
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700440
441 /**
442 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
443 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
444 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
445 * like the docked stack going empty.
446 */
447 private boolean mAllowDockedStackResize = true;
448
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100449 /**
Tony Mak853304c2016-04-18 15:17:41 +0100450 * Is dock currently minimized.
451 */
452 boolean mIsDockMinimized;
453
454 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700455 * Description of a request to start a new activity, which has been held
456 * due to app switches being disabled.
457 */
458 static class PendingActivityLaunch {
459 final ActivityRecord r;
460 final ActivityRecord sourceRecord;
461 final int startFlags;
462 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700463 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700464
465 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700466 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700467 r = _r;
468 sourceRecord = _sourceRecord;
469 startFlags = _startFlags;
470 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700471 callerApp = _callerApp;
472 }
473
474 void sendErrorResult(String message) {
475 try {
476 if (callerApp.thread != null) {
477 callerApp.thread.scheduleCrash(message);
478 }
479 } catch (RemoteException e) {
480 Slog.e(TAG, "Exception scheduling crash of failed "
481 + "activity launcher sourceRecord=" + sourceRecord, e);
482 }
Craig Mautneree36c772014-07-16 14:56:05 -0700483 }
484 }
485
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800486 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700487 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700488 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800489 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800490 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700491 }
492
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800493 void setRecentTasks(RecentTasks recentTasks) {
494 mRecentTasks = recentTasks;
495 }
496
Jeff Brown2c43c332014-06-12 22:38:59 -0700497 /**
498 * At the time when the constructor runs, the power manager has not yet been
499 * initialized. So we initialize our wakelocks afterwards.
500 */
501 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800502 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700503 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700504 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700505 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700506 }
507
justinzhang5286d3f2014-05-12 17:06:01 -0400508 // This function returns a IStatusBarService. The value is from ServiceManager.
509 // getService and is cached.
510 private IStatusBarService getStatusBarService() {
511 synchronized (mService) {
512 if (mStatusBarService == null) {
513 mStatusBarService = IStatusBarService.Stub.asInterface(
514 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
515 if (mStatusBarService == null) {
516 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
517 }
518 }
519 return mStatusBarService;
520 }
521 }
522
Jason Monk35c62a42014-06-17 10:24:47 -0400523 private IDevicePolicyManager getDevicePolicyManager() {
524 synchronized (mService) {
525 if (mDevicePolicyManager == null) {
526 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
527 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
528 if (mDevicePolicyManager == null) {
529 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
530 }
531 }
532 return mDevicePolicyManager;
533 }
534 }
535
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700536 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800537 synchronized (mService) {
538 mWindowManager = wm;
539
540 mDisplayManager =
541 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
542 mDisplayManager.registerDisplayListener(this, null);
543
544 Display[] displays = mDisplayManager.getDisplays();
545 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
546 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800547 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700548 if (activityDisplay.mDisplay == null) {
549 throw new IllegalStateException("Default Display does not exist");
550 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800551 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700552 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800553 }
554
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800555 mHomeStack = mFocusedStack = mLastFocusedStack =
556 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800557
558 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800559 }
Craig Mautner27084302013-03-25 08:05:25 -0700560 }
561
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200562 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700563 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200564 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700565 }
566
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700567 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700569 }
570
Craig Mautnerde4ef022013-04-07 19:01:33 -0700571 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700573 }
574
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700575 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700576 if (stack == null) {
577 return false;
578 }
579
Craig Mautnerdf88d732014-01-27 09:21:32 -0800580 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
581 if (parent != null) {
582 stack = parent.task.stack;
583 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800584 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700585 }
586
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700587 /** The top most stack. */
588 boolean isFrontStack(ActivityStack stack) {
589 if (stack == null) {
590 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700592
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700593 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
594 if (parent != null) {
595 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800596 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700597 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
598 }
599
Wale Ogunwaled046a012015-12-24 13:05:59 -0800600 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800601 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
602 if (!focusCandidate.isFocusable()) {
603 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
604 focusCandidate = focusCandidate.getNextFocusableStackLocked();
605 }
606
607 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800608 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800609 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800610
Wale Ogunwaled046a012015-12-24 13:05:59 -0800611 EventLogTags.writeAmFocusedStack(
612 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
613 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
614 }
615
616 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800617 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800618 // The focus activity should always be the top activity in the focused stack.
619 // There will be chaos and anarchy if it isn't...
620 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
621 }
Craig Mautnerde313752015-01-22 14:28:03 -0800622
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800623 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800624 if (r != null && r.idle) {
625 checkFinishBootingLocked();
626 }
627 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700628 }
629
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700630 void moveHomeStackToFront(String reason) {
631 mHomeStack.moveToFront(reason);
632 }
633
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700634 /** Returns true if the focus activity was adjusted to the home stack top activity. */
635 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700636 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700637 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700638 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700639 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700640
Craig Mautner84984fa2014-06-19 11:19:20 -0700641 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700642
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700643 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700644 if (top == null) {
645 return false;
646 }
647 mService.setFocusedActivityLocked(top, reason);
648 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700649 }
650
Craig Mautner299f9602015-01-26 09:47:33 -0800651 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700652 if (!mService.mBooting && !mService.mBooted) {
653 // Not ready yet!
654 return false;
655 }
656
Craig Mautner84984fa2014-06-19 11:19:20 -0700657 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700658 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700659 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700660 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700661
Craig Mautner84984fa2014-06-19 11:19:20 -0700662 if (prev != null) {
663 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
664 }
665
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700666 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
667 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800668 final String myReason = reason + " resumeHomeStackTask";
669
Mark Lua56ea122015-10-08 13:31:01 +0800670 // Only resume home activity if isn't finishing.
671 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800672 mService.setFocusedActivityLocked(r, myReason);
673 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700674 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800675 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700676 }
677
Craig Mautner8d341ef2013-03-26 09:03:27 -0700678 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700679 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700680 }
681
682 /**
683 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
684 * @param id Id of the task we would like returned.
685 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
686 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700687 * @param stackId The stack to restore the task to (default launch stack will be used if
688 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700689 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700690 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800691 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800692 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800693 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800694 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
695 ActivityStack stack = stacks.get(stackNdx);
696 TaskRecord task = stack.taskForIdLocked(id);
697 if (task != null) {
698 return task;
699 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700700 }
701 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800702
703 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700704 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800705 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800706 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700707 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800708 return null;
709 }
710
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700711 if (!restoreFromRecents) {
712 return task;
713 }
714
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700715 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700716 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
717 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800718 return null;
719 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700720 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800721 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700722 }
723
Craig Mautner6170f732013-04-02 13:05:23 -0700724 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800725 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800727 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
729 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
730 if (r != null) {
731 return r;
732 }
Craig Mautner6170f732013-04-02 13:05:23 -0700733 }
734 }
735 return null;
736 }
737
Tony Mak853304c2016-04-18 15:17:41 +0100738 /**
739 * TODO: Handle freefom mode.
740 * @return true when credential confirmation is needed for the user and there is any
741 * activity started by the user in any visible stack.
742 */
743 boolean isUserLockedProfile(@UserIdInt int userId) {
744 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
745 return false;
746 }
747 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
748 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
749 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
750 for (final ActivityStack activityStack : activityStacks) {
751 if (activityStack == null) {
752 continue;
753 }
754 if (activityStack.topRunningActivityLocked() == null) {
755 continue;
756 }
757 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
758 continue;
759 }
760 if (activityStack.isDockedStack() && mIsDockMinimized) {
761 continue;
762 }
763 final TaskRecord topTask = activityStack.topTask();
764 if (topTask == null) {
765 continue;
766 }
767 // To handle the case that work app is in the task but just is not the top one.
768 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
769 final ActivityRecord activityRecord = topTask.mActivities.get(i);
770 if (activityRecord.userId == userId) {
771 return true;
772 }
773 }
774 }
775 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000776 }
777
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800778 void setNextTaskIdForUserLocked(int taskId, int userId) {
779 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
780 if (taskId > currentTaskId) {
781 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700782 }
783 }
784
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800785 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800786 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
787 // for a userId u, a taskId can only be in the range
788 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
789 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
790 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800791 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
792 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
793 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800794 candidateTaskId++;
795 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
796 // Wrap around as there will be smaller task ids that are available now.
797 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700798 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800799 if (candidateTaskId == currentTaskId) {
800 // Something wrong!
801 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
802 throw new IllegalStateException("Cannot get an available task id."
803 + " Reached limit of " + MAX_TASK_IDS_PER_USER
804 + " running tasks per user.");
805 }
806 }
807 mCurTaskIdForUser.put(userId, candidateTaskId);
808 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700809 }
810
Craig Mautnerde4ef022013-04-07 19:01:33 -0700811 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800812 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700813 if (stack == null) {
814 return null;
815 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700816 ActivityRecord resumedActivity = stack.mResumedActivity;
817 if (resumedActivity == null || resumedActivity.app == null) {
818 resumedActivity = stack.mPausingActivity;
819 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700820 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700821 }
822 }
823 return resumedActivity;
824 }
825
Dianne Hackbornff072722014-09-24 10:56:28 -0700826 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700827 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800829 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
830 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800831 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
832 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700833 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800834 continue;
835 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700836 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 if (hr != null) {
838 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
839 && processName.equals(hr.processName)) {
840 try {
George Mount2c92c972014-03-20 09:38:23 -0700841 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800842 didSomething = true;
843 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700844 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800845 Slog.w(TAG, "Exception in new application when starting activity "
846 + hr.intent.getComponent().flattenToShortString(), e);
847 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700848 }
Craig Mautner20e72272013-04-01 13:45:53 -0700849 }
Craig Mautner20e72272013-04-01 13:45:53 -0700850 }
851 }
852 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700853 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700854 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700855 }
Craig Mautner20e72272013-04-01 13:45:53 -0700856 return didSomething;
857 }
858
859 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800860 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
861 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800862 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
863 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700864 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800865 continue;
866 }
867 final ActivityRecord resumedActivity = stack.mResumedActivity;
868 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700869 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800870 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800871 return false;
872 }
Craig Mautner20e72272013-04-01 13:45:53 -0700873 }
874 }
875 return true;
876 }
877
Craig Mautnerde4ef022013-04-07 19:01:33 -0700878 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800879 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
880 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800881 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
882 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700883 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800884 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700885 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 return false;
887 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700888 }
889 }
890 }
891 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700892 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800893 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
894 mLastFocusedStack + " to=" + mFocusedStack);
895 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700896 return true;
897 }
898
899 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800900 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800901 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
902 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800903 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
904 final ActivityStack stack = stacks.get(stackNdx);
905 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800906 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700907 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800908 return false;
909 }
910 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800911 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700912 }
913 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800914 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700915 }
916
Craig Mautner2acc3892013-09-23 10:28:14 -0700917 /**
918 * Pause all activities in either all of the stacks or just the back stacks.
919 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700920 * @return true if any activity was paused as a result of this call.
921 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700922 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700923 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
925 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
927 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700928 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700929 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800930 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700931 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
932 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800933 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700934 }
935 }
936 return someActivityPaused;
937 }
938
Craig Mautnerde4ef022013-04-07 19:01:33 -0700939 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700940 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
942 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800943 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
944 final ActivityStack stack = stacks.get(stackNdx);
945 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700946 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800947 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700948 Slog.d(TAG_STATES,
949 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800950 pausing = false;
951 } else {
952 return false;
953 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700954 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700955 }
956 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700957 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700958 }
959
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700960 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
961 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500962 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800963 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
964 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
965 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
966 final ActivityStack stack = stacks.get(stackNdx);
967 if (stack.mResumedActivity != null &&
968 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700969 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800970 }
971 }
972 }
973 }
974
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800975 void cancelInitializingActivities() {
976 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
977 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
978 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
979 stacks.get(stackNdx).cancelInitializingActivities();
980 }
981 }
982 }
983
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700984 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700985 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700986 }
987
988 void sendWaitingVisibleReportLocked(ActivityRecord r) {
989 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700990 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700991 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700992 if (w.who == null) {
993 changed = true;
994 w.timeout = false;
995 if (r != null) {
996 w.who = new ComponentName(r.info.packageName, r.info.name);
997 }
998 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
999 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001000 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001001 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001002 if (changed) {
1003 mService.notifyAll();
1004 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001005 }
1006
1007 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1008 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001009 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001010 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001011 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001012 if (w.who == null) {
1013 changed = true;
1014 w.timeout = timeout;
1015 if (r != null) {
1016 w.who = new ComponentName(r.info.packageName, r.info.name);
1017 }
1018 w.thisTime = thisTime;
1019 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001020 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001021 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001022 if (changed) {
1023 mService.notifyAll();
1024 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001025 }
1026
Craig Mautner29219d92013-04-16 20:19:12 -07001027 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001028 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001029 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001030 if (r != null) {
1031 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001032 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001033
Craig Mautner4a1cb222013-12-04 16:14:06 -08001034 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001035 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001036 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1037 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001038 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001039 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001040 if (r != null) {
1041 return r;
1042 }
1043 }
1044 }
1045 return null;
1046 }
1047
Dianne Hackborn09233282014-04-30 11:33:59 -07001048 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001049 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001050 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1051 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001052 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001053 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001054 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001055 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1056 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001057 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001058 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001059 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001060 }
1061 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001062
1063 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1064 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1065 while (maxNum > 0) {
1066 long mostRecentActiveTime = Long.MIN_VALUE;
1067 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001068 final int numTaskLists = runningTaskLists.size();
1069 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1070 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001071 if (!stackTaskList.isEmpty()) {
1072 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1073 if (lastActiveTime > mostRecentActiveTime) {
1074 mostRecentActiveTime = lastActiveTime;
1075 selectedStackList = stackTaskList;
1076 }
1077 }
1078 }
1079 if (selectedStackList != null) {
1080 list.add(selectedStackList.remove(0));
1081 --maxNum;
1082 } else {
1083 break;
1084 }
1085 }
Craig Mautner20e72272013-04-01 13:45:53 -07001086 }
1087
Todd Kennedy7440f172015-12-09 14:31:22 -08001088 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1089 ProfilerInfo profilerInfo) {
1090 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001091 if (aInfo != null) {
1092 // Store the found target back into the intent, because now that
1093 // we have it we never want to do this again. For example, if the
1094 // user navigates back to this point in the history, we should
1095 // always restart the exact same activity.
1096 intent.setComponent(new ComponentName(
1097 aInfo.applicationInfo.packageName, aInfo.name));
1098
1099 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001100 if (!aInfo.processName.equals("system")) {
1101 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001102 mService.setDebugApp(aInfo.processName, true, false);
1103 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001104
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001105 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1106 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1107 }
1108
Man Caocfa78b22015-06-11 20:14:34 -07001109 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1110 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1111 }
1112
1113 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001114 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001115 }
1116 }
1117 }
1118 return aInfo;
1119 }
1120
Todd Kennedy7440f172015-12-09 14:31:22 -08001121 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001122 return resolveIntent(intent, resolvedType, userId, 0);
1123 }
1124
1125 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001126 try {
1127 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001128 PackageManager.MATCH_DEFAULT_ONLY | flags
1129 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001130 } catch (RemoteException e) {
1131 }
1132 return null;
1133 }
1134
1135 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1136 ProfilerInfo profilerInfo, int userId) {
1137 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1138 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1139 }
1140
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001141 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1142 boolean andResume, boolean checkConfig) throws RemoteException {
1143
1144 if (!allPausedActivitiesComplete()) {
1145 // While there are activities pausing we skipping starting any new activities until
1146 // pauses are complete. NOTE: that we also do this for activities that are starting in
1147 // the paused state because they will first be resumed then paused on the client side.
1148 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1149 "realStartActivityLocked: Skipping start of r=" + r
1150 + " some activities pausing...");
1151 return false;
1152 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001153
Craig Mautner2568c3a2015-03-26 14:22:34 -07001154 if (andResume) {
1155 r.startFreezingScreenLocked(app, 0);
1156 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001157
Craig Mautner2568c3a2015-03-26 14:22:34 -07001158 // schedule launch ticks to collect information about slow apps.
1159 r.startLaunchTickingLocked();
1160 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001161
1162 // Have the window manager re-evaluate the orientation of
1163 // the screen based on the new activity order. Note that
1164 // as a result of this, it can call back into the activity
1165 // manager with a new orientation. We don't care about that,
1166 // because the activity is not currently running so we are
1167 // just restarting it anyway.
1168 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001169 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001170 mService.mConfiguration,
1171 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001172 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001173 }
1174
1175 r.app = app;
1176 app.waitingToKill = null;
1177 r.launchCount++;
1178 r.lastLaunchTime = SystemClock.uptimeMillis();
1179
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001180 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001181
1182 int idx = app.activities.indexOf(r);
1183 if (idx < 0) {
1184 app.activities.add(r);
1185 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001186 mService.updateLruProcessLocked(app, true, null);
1187 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001188
Craig Mautner15df08a2015-04-01 12:17:18 -07001189 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001190 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1191 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001192 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001193 }
1194
1195 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001196 try {
1197 if (app.thread == null) {
1198 throw new RemoteException();
1199 }
1200 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001201 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 if (andResume) {
1203 results = r.results;
1204 newIntents = r.newIntents;
1205 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001206 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1207 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1208 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001209 if (andResume) {
1210 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1211 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001212 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001213 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001214 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001215 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001216 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001217 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001218 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1219 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001220 r.sleeping = false;
1221 r.forceNewConfig = false;
1222 mService.showAskCompatModeDialogLocked(r);
1223 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001224 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001225 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1226 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1227 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001228 final String profileFile = mService.mProfileFile;
1229 if (profileFile != null) {
1230 ParcelFileDescriptor profileFd = mService.mProfileFd;
1231 if (profileFd != null) {
1232 try {
1233 profileFd = profileFd.dup();
1234 } catch (IOException e) {
1235 if (profileFd != null) {
1236 try {
1237 profileFd.close();
1238 } catch (IOException o) {
1239 }
1240 profileFd = null;
1241 }
1242 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001243 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001244
1245 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1246 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 }
1248 }
1249 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001250
Craig Mautner2568c3a2015-03-26 14:22:34 -07001251 if (andResume) {
1252 app.hasShownUi = true;
1253 app.pendingUiClean = true;
1254 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001255 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001256 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001257 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001258 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001259 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001260 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001261
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001262 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001263 // This may be a heavy-weight process! Note that the package
1264 // manager will ensure that only activity can run in the main
1265 // process of the .apk, which is the only thing that will be
1266 // considered heavy-weight.
1267 if (app.processName.equals(app.info.packageName)) {
1268 if (mService.mHeavyWeightProcess != null
1269 && mService.mHeavyWeightProcess != app) {
1270 Slog.w(TAG, "Starting new heavy weight process " + app
1271 + " when already running "
1272 + mService.mHeavyWeightProcess);
1273 }
1274 mService.mHeavyWeightProcess = app;
1275 Message msg = mService.mHandler.obtainMessage(
1276 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1277 msg.obj = r;
1278 mService.mHandler.sendMessage(msg);
1279 }
1280 }
1281
1282 } catch (RemoteException e) {
1283 if (r.launchFailed) {
1284 // This is the second time we failed -- finish activity
1285 // and give up.
1286 Slog.e(TAG, "Second failure launching "
1287 + r.intent.getComponent().flattenToShortString()
1288 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001289 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001290 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1291 "2nd-crash", false);
1292 return false;
1293 }
1294
1295 // This is the first time we failed -- restart process and
1296 // retry.
1297 app.activities.remove(r);
1298 throw e;
1299 }
1300
1301 r.launchFailed = false;
1302 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001303 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001304 }
1305
1306 if (andResume) {
1307 // As part of the process of launching, ActivityThread also performs
1308 // a resume.
1309 stack.minimalResumeActivityLocked(r);
1310 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001311 // This activity is not starting in the resumed state... which should look like we asked
1312 // it to pause+stop (but remain visible), and it has done so and reported back the
1313 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001314 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001315 "Moving to PAUSED: " + r + " (starting in paused state)");
1316 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001317 }
1318
1319 // Launch the new version setup screen if needed. We do this -after-
1320 // launching the initial activity (that is, home), so that it can have
1321 // a chance to initialize itself while in the background, making the
1322 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001323 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 mService.startSetupActivityLocked();
1325 }
1326
Dianne Hackborn465fa392014-09-14 14:21:18 -07001327 // Update any services we are bound to that might care about whether
1328 // their client may have activities.
1329 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1330
Craig Mautner2420ead2013-04-01 17:13:20 -07001331 return true;
1332 }
1333
Craig Mautnere79d42682013-04-01 19:01:53 -07001334 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001335 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001336 // Is this activity's application already running?
1337 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001338 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001339
1340 r.task.stack.setLaunchTime(r);
1341
1342 if (app != null && app.thread != null) {
1343 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001344 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1345 || !"android".equals(r.info.packageName)) {
1346 // Don't add this if it is a platform component that is marked
1347 // to run in multiple processes, because this is actually
1348 // part of the framework so doesn't make sense to track as a
1349 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001350 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1351 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001352 }
George Mount2c92c972014-03-20 09:38:23 -07001353 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001354 return;
1355 } catch (RemoteException e) {
1356 Slog.w(TAG, "Exception when starting activity "
1357 + r.intent.getComponent().flattenToShortString(), e);
1358 }
1359
1360 // If a dead object exception was thrown -- fall through to
1361 // restart the application.
1362 }
1363
1364 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001365 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001366 }
1367
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001368 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001369 String resultWho, int requestCode, int callingPid, int callingUid,
1370 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001371 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001372 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1373 callingUid);
1374 if (startAnyPerm == PERMISSION_GRANTED) {
1375 return true;
1376 }
1377 final int componentRestriction = getComponentRestrictionForCallingPackage(
1378 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1379 final int actionRestriction = getActionRestrictionForCallingPackage(
1380 intent.getAction(), callingPackage, callingPid, callingUid);
1381 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1382 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1383 if (resultRecord != null) {
1384 resultStack.sendActivityResultLocked(-1,
1385 resultRecord, resultWho, requestCode,
1386 Activity.RESULT_CANCELED, null);
1387 }
1388 final String msg;
1389 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1390 msg = "Permission Denial: starting " + intent.toString()
1391 + " from " + callerApp + " (pid=" + callingPid
1392 + ", uid=" + callingUid + ")" + " with revoked permission "
1393 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1394 } else if (!aInfo.exported) {
1395 msg = "Permission Denial: starting " + intent.toString()
1396 + " from " + callerApp + " (pid=" + callingPid
1397 + ", uid=" + callingUid + ")"
1398 + " not exported from uid " + aInfo.applicationInfo.uid;
1399 } else {
1400 msg = "Permission Denial: starting " + intent.toString()
1401 + " from " + callerApp + " (pid=" + callingPid
1402 + ", uid=" + callingUid + ")"
1403 + " requires " + aInfo.permission;
1404 }
1405 Slog.w(TAG, msg);
1406 throw new SecurityException(msg);
1407 }
1408
1409 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1410 final String message = "Appop Denial: starting " + intent.toString()
1411 + " from " + callerApp + " (pid=" + callingPid
1412 + ", uid=" + callingUid + ")"
1413 + " requires " + AppOpsManager.permissionToOp(
1414 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1415 Slog.w(TAG, message);
1416 return false;
1417 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1418 final String message = "Appop Denial: starting " + intent.toString()
1419 + " from " + callerApp + " (pid=" + callingPid
1420 + ", uid=" + callingUid + ")"
1421 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1422 Slog.w(TAG, message);
1423 return false;
1424 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001425 if (options != null && options.getLaunchTaskId() != -1) {
1426 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1427 callingPid, callingUid);
1428 if (startInTaskPerm != PERMISSION_GRANTED) {
1429 final String msg = "Permission Denial: starting " + intent.toString()
1430 + " from " + callerApp + " (pid=" + callingPid
1431 + ", uid=" + callingUid + ") with launchTaskId="
1432 + options.getLaunchTaskId();
1433 Slog.w(TAG, msg);
1434 throw new SecurityException(msg);
1435 }
1436 }
1437
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001438 return true;
1439 }
1440
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001441 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001442 final long identity = Binder.clearCallingIdentity();
1443 try {
1444 return UserManager.get(mService.mContext).getUserInfo(userId);
1445 } finally {
1446 Binder.restoreCallingIdentity(identity);
1447 }
1448 }
1449
Svet Ganov99b60432015-06-27 13:15:22 -07001450 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001451 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001452 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1453 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001454 == PackageManager.PERMISSION_DENIED) {
1455 return ACTIVITY_RESTRICTION_PERMISSION;
1456 }
1457
Christopher Tateff7add02015-08-17 10:23:22 -07001458 if (activityInfo.permission == null) {
1459 return ACTIVITY_RESTRICTION_NONE;
1460 }
1461
Svet Ganov99b60432015-06-27 13:15:22 -07001462 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1463 if (opCode == AppOpsManager.OP_NONE) {
1464 return ACTIVITY_RESTRICTION_NONE;
1465 }
1466
1467 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1468 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001469 if (!ignoreTargetSecurity) {
1470 return ACTIVITY_RESTRICTION_APPOP;
1471 }
Svet Ganov99b60432015-06-27 13:15:22 -07001472 }
1473
1474 return ACTIVITY_RESTRICTION_NONE;
1475 }
1476
Svetoslav7008b512015-06-24 18:47:07 -07001477 private int getActionRestrictionForCallingPackage(String action,
1478 String callingPackage, int callingPid, int callingUid) {
1479 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001480 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001481 }
1482
1483 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1484 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001485 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001486 }
1487
1488 final PackageInfo packageInfo;
1489 try {
1490 packageInfo = mService.mContext.getPackageManager()
1491 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1492 } catch (PackageManager.NameNotFoundException e) {
1493 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001494 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001495 }
1496
1497 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001498 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001499 }
1500
1501 if (mService.checkPermission(permission, callingPid, callingUid) ==
1502 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001503 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001504 }
1505
1506 final int opCode = AppOpsManager.permissionToOpCode(permission);
1507 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001508 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001509 }
1510
1511 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1512 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001513 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001514 }
1515
Svet Ganov99b60432015-06-27 13:15:22 -07001516 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001517 }
1518
Wale Ogunwaled046a012015-12-24 13:05:59 -08001519 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001520 if (r == null) {
1521 // Not sure what you are trying to do, but it is not going to work...
1522 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001523 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001524 final TaskRecord task = r.task;
1525 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001526 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001527 return false;
1528 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001529 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001530 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001531 }
1532
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001533 void setLaunchSource(int uid) {
1534 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1535 }
1536
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001537 void acquireLaunchWakelock() {
1538 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1539 throw new IllegalStateException("Calling must be system uid");
1540 }
1541 mLaunchingActivity.acquire();
1542 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1543 // To be safe, don't allow the wake lock to be held for too long.
1544 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1545 }
1546 }
1547
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001548 /**
1549 * Called when the frontmost task is idle.
1550 * @return the state of mService.mBooting before this was called.
1551 */
1552 private boolean checkFinishBootingLocked() {
1553 final boolean booting = mService.mBooting;
1554 boolean enableScreen = false;
1555 mService.mBooting = false;
1556 if (!mService.mBooted) {
1557 mService.mBooted = true;
1558 enableScreen = true;
1559 }
1560 if (booting || enableScreen) {
1561 mService.postFinishBooting(booting, enableScreen);
1562 }
1563 return booting;
1564 }
1565
Craig Mautnerf3333272013-04-22 10:55:53 -07001566 // Checked.
1567 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1568 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001569 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001570
Craig Mautnerf3333272013-04-22 10:55:53 -07001571 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001572 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001573 int NS = 0;
1574 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001575 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001576 boolean activityRemoved = false;
1577
Wale Ogunwale7d701172015-03-11 15:36:30 -07001578 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001579 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001580 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1581 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001582 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1583 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001584 if (fromTimeout) {
1585 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001586 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001587
1588 // This is a hack to semi-deal with a race condition
1589 // in the client where it can be constructed with a
1590 // newer configuration from when we asked it to launch.
1591 // We'll update with whatever configuration it now says
1592 // it used to launch.
1593 if (config != null) {
1594 r.configuration = config;
1595 }
1596
1597 // We are now idle. If someone is waiting for a thumbnail from
1598 // us, we can now deliver.
1599 r.idle = true;
1600
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001601 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001602 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001603 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001604 }
1605 }
1606
1607 if (allResumedActivitiesIdle()) {
1608 if (r != null) {
1609 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001610 }
1611
1612 if (mLaunchingActivity.isHeld()) {
1613 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1614 if (VALIDATE_WAKE_LOCK_CALLER &&
1615 Binder.getCallingUid() != Process.myUid()) {
1616 throw new IllegalStateException("Calling must be system uid");
1617 }
1618 mLaunchingActivity.release();
1619 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001620 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001621 }
1622
1623 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001624 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001625 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001626 if ((NF = mFinishingActivities.size()) > 0) {
1627 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001628 mFinishingActivities.clear();
1629 }
1630
Craig Mautnerf3333272013-04-22 10:55:53 -07001631 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001632 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001633 mStartingUsers.clear();
1634 }
1635
Craig Mautnerf3333272013-04-22 10:55:53 -07001636 // Stop any activities that are scheduled to do so but have been
1637 // waiting for the next one to start.
1638 for (int i = 0; i < NS; i++) {
1639 r = stops.get(i);
1640 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001641 if (stack != null) {
1642 if (r.finishing) {
1643 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1644 } else {
1645 stack.stopActivityLocked(r);
1646 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001647 }
1648 }
1649
1650 // Finish any activities that are scheduled to do so but have been
1651 // waiting for the next one to start.
1652 for (int i = 0; i < NF; i++) {
1653 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001654 final ActivityStack stack = r.task.stack;
1655 if (stack != null) {
1656 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1657 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001658 }
1659
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001660 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001661 // Complete user switch
1662 if (startingUsers != null) {
1663 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001664 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001665 }
1666 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001667 }
1668
1669 mService.trimApplications();
1670 //dump();
1671 //mWindowManager.dump();
1672
Craig Mautnerf3333272013-04-22 10:55:53 -07001673 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001674 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001675 }
1676
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001677 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001678 }
1679
Craig Mautner8e569572013-10-11 17:36:59 -07001680 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001681 boolean hasVisibleActivities = false;
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 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1686 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001687 }
Craig Mautner19091252013-10-05 00:03:53 -07001688 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001689 }
1690
1691 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001692 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1693 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001694 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1695 stacks.get(stackNdx).closeSystemDialogsLocked();
1696 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001697 }
1698 }
1699
Craig Mautner93529a42013-10-04 15:03:13 -07001700 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001701 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001702 }
1703
Craig Mautner8d341ef2013-03-26 09:03:27 -07001704 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001705 * Update the last used stack id for non-current user (current user's last
1706 * used stack is the focused stack)
1707 */
1708 void updateUserStackLocked(int userId, ActivityStack stack) {
1709 if (userId != mCurrentUser) {
1710 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1711 }
1712 }
1713
1714 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001715 * @return true if some activity was finished (or would have finished if doit were true).
1716 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001717 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1718 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001719 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001720 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1721 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001722 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001723 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001724 if (stack.finishDisabledPackageActivitiesLocked(
1725 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001726 didSomething = true;
1727 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001728 }
1729 }
1730 return didSomething;
1731 }
1732
Dianne Hackborna413dc02013-07-12 12:02:55 -07001733 void updatePreviousProcessLocked(ActivityRecord r) {
1734 // Now that this process has stopped, we may want to consider
1735 // it to be the previous app to try to keep around in case
1736 // the user wants to return to it.
1737
1738 // First, found out what is currently the foreground app, so that
1739 // we don't blow away the previous app if this activity is being
1740 // hosted by the process that is actually still the foreground.
1741 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001742 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1743 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001744 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1745 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001746 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001747 if (stack.mResumedActivity != null) {
1748 fgApp = stack.mResumedActivity.app;
1749 } else if (stack.mPausingActivity != null) {
1750 fgApp = stack.mPausingActivity.app;
1751 }
1752 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001753 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001754 }
1755 }
1756
1757 // Now set this one as the previous process, only if that really
1758 // makes sense to.
1759 if (r.app != null && fgApp != null && r.app != fgApp
1760 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001761 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001762 mService.mPreviousProcess = r.app;
1763 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1764 }
1765 }
1766
Wale Ogunwaled046a012015-12-24 13:05:59 -08001767 boolean resumeFocusedStackTopActivityLocked() {
1768 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001769 }
1770
Wale Ogunwaled046a012015-12-24 13:05:59 -08001771 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001772 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001773 if (targetStack != null && isFocusedStack(targetStack)) {
1774 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001775 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001776 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1777 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001778 }
1779
Todd Kennedy39bfee52016-02-24 10:28:21 -08001780 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1781 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1782 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1783 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1784 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1785 }
1786 }
1787 }
1788
Adrian Roos20d7df32016-01-12 18:59:43 +01001789 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1790 TaskRecord finishedTask = null;
1791 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001792 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1793 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001794 final int numStacks = stacks.size();
1795 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1796 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001797 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1798 if (stack == focusedStack || finishedTask == null) {
1799 finishedTask = t;
1800 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001801 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001802 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001803 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001804 }
1805
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001806 void finishVoiceTask(IVoiceInteractionSession session) {
1807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1808 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1809 final int numStacks = stacks.size();
1810 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1811 final ActivityStack stack = stacks.get(stackNdx);
1812 stack.finishVoiceTask(session);
1813 }
1814 }
1815 }
1816
Andrii Kulianc27916642016-04-12 17:59:27 -07001817 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1818 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001819 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1820 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001821 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001822 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1823 // Caller wants the home activity moved with it. To accomplish this,
1824 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001825 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001826 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001827 if (task.stack == null) {
1828 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1829 + task + " to front. Stack is null");
1830 return;
1831 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001832
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001833 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001834 int stackId = options.getLaunchStackId();
1835 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001836 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001837 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001838 if (stackId == INVALID_STACK_ID) {
1839 stackId = task.getLaunchStackId();
1840 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001841 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001842 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1843 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1844 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001845 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1846 // still need moveTaskToFrontLocked() below for any transition settings.
1847 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001848 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1849 resizeStackLocked(stackId, bounds,
1850 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001851 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001852 } else {
1853 // WM resizeTask must be done after the task is moved to the correct stack,
1854 // because Task's setBounds() also updates dim layer's bounds, but that has
1855 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001856 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001857 false /* relayout */, false /* forced */);
1858 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001859 }
1860 }
1861
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001862 final ActivityRecord r = task.getTopActivity();
1863 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1864 r == null ? null : r.appTimeTracker, reason);
1865
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001866 if (DEBUG_STACK) Slog.d(TAG_STACK,
1867 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001868
Andrii Kulianc27916642016-04-12 17:59:27 -07001869 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1870 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001871 }
1872
Wale Ogunwale854809c2015-12-27 16:18:19 -08001873 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001874 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001875 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001876 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001877 return false;
1878 }
1879 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1880 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001881 }
1882
Craig Mautner967212c2013-04-13 21:10:58 -07001883 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001884 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001885 }
1886
1887 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001888 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1889 if (activityContainer != null) {
1890 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001891 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001892 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001893 return null;
1894 }
1895 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001896 }
1897
Craig Mautner967212c2013-04-13 21:10:58 -07001898 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001899 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001900 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1901 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001902 }
1903 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001904 }
1905
Craig Mautner4a1cb222013-12-04 16:14:06 -08001906 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001907 ActivityRecord homeActivity = getHomeActivity();
1908 if (homeActivity != null) {
1909 return homeActivity.appToken;
1910 }
1911 return null;
1912 }
1913
1914 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001915 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001916 }
1917
1918 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001919 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1920 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1921 final TaskRecord task = tasks.get(taskNdx);
1922 if (task.isHomeTask()) {
1923 final ArrayList<ActivityRecord> activities = task.mActivities;
1924 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1925 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001926 if (r.isHomeActivity()
1927 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001928 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001929 }
1930 }
1931 }
1932 }
1933 return null;
1934 }
1935
Chong Zhangb15758a2015-11-17 12:12:03 -08001936 /**
1937 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1938 * the docked stack itself, or if it's side-by-side to the docked stack.
1939 */
1940 boolean isStackDockedInEffect(int stackId) {
1941 return stackId == DOCKED_STACK_ID ||
1942 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1943 }
1944
Todd Kennedyca4d8422015-01-15 15:19:22 -08001945 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001946 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001947 ActivityContainer activityContainer =
1948 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001949 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001950 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1951 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001952 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001953 return activityContainer;
1954 }
1955
Craig Mautner34b73df2014-01-12 21:11:08 -08001956 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001957 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1958 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1959 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001960 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1961 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001962 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001963 }
1964 }
1965
Craig Mautner95da1082014-02-24 17:54:35 -08001966 void deleteActivityContainer(IActivityContainer container) {
1967 ActivityContainer activityContainer = (ActivityContainer)container;
1968 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001969 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1970 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001971 final int stackId = activityContainer.mStackId;
1972 mActivityContainers.remove(stackId);
1973 mWindowManager.removeStack(stackId);
1974 }
1975 }
1976
Jorim Jaggidc249c42015-12-15 14:57:31 -08001977 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001978 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001979 if (stackId == DOCKED_STACK_ID) {
1980 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1981 preserveWindows);
1982 return;
1983 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001984 final ActivityStack stack = getStack(stackId);
1985 if (stack == null) {
1986 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1987 return;
1988 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001989
Jorim Jaggidc249c42015-12-15 14:57:31 -08001990 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001991 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1992 // stack size changing so things don't get out of sync.
1993 return;
1994 }
1995
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001996 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001997 mWindowManager.deferSurfaceLayout();
1998 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001999 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002000 if (!deferResume) {
2001 ensureConfigurationAndResume(
2002 stack, stack.topRunningActivityLocked(), preserveWindows);
2003 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002004 } finally {
2005 mWindowManager.continueSurfaceLayout();
2006 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002007 }
2008 }
2009
Jorim Jaggi192086e2016-03-11 17:17:03 +01002010 void deferUpdateBounds(int stackId) {
2011 final ActivityStack stack = getStack(stackId);
2012 if (stack != null) {
2013 stack.deferUpdateBounds();
2014 }
2015 }
2016
2017 void continueUpdateBounds(int stackId) {
2018 final ActivityStack stack = getStack(stackId);
2019 if (stack != null) {
2020 stack.continueUpdateBounds();
2021 }
2022 }
2023
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002024 void notifyAppTransitionDone() {
2025 continueUpdateBounds(HOME_STACK_ID);
2026 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2027 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
2028 if (anyTaskForIdLocked(taskId) != null) {
2029 mWindowManager.setTaskDockedResizing(taskId, false);
2030 }
2031 }
2032 mResizingTasksDuringAnimation.clear();
2033 }
2034
Jorim Jaggi192086e2016-03-11 17:17:03 +01002035 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002036 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002037 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002038
Jorim Jaggi192086e2016-03-11 17:17:03 +01002039 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2040 return;
2041 }
2042
Jorim Jaggidc249c42015-12-15 14:57:31 -08002043 mTmpBounds.clear();
2044 mTmpConfigs.clear();
2045 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002046 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002047 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2048 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002049 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002050 final TaskRecord task = tasks.get(i);
2051 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002052 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2053 // For freeform stack we don't adjust the size of the tasks to match that
2054 // of the stack, but we do try to make sure the tasks are still contained
2055 // with the bounds of the stack.
2056 tempRect2.set(task.mBounds);
2057 fitWithinBounds(tempRect2, bounds);
2058 task.updateOverrideConfiguration(tempRect2);
2059 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002060 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002061 }
2062 }
2063
2064 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2065 mTmpBounds.put(task.taskId, task.mBounds);
2066 if (tempTaskInsetBounds != null) {
2067 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2068 }
2069 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002070
2071 // We might trigger a configuration change. Save the current task bounds for freezing.
2072 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002073 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2074 mTmpBounds, mTmpInsetBounds);
2075 stack.setBounds(bounds);
2076 }
2077
2078 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
2079 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08002080 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002081 return;
2082 }
2083 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
2084 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002085 if (!updated) {
2086 resumeFocusedStackTopActivityLocked();
2087 }
2088 }
2089
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002090 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2091 final ActivityStack stack = getStack(fromStackId);
2092 if (stack == null) {
2093 return;
2094 }
2095
2096 mWindowManager.deferSurfaceLayout();
2097 try {
2098 if (fromStackId == DOCKED_STACK_ID) {
2099
2100 // We are moving all tasks from the docked stack to the fullscreen stack,
2101 // which is dismissing the docked stack, so resize all other stacks to
2102 // fullscreen here already so we don't end up with resize trashing.
2103 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2104 if (StackId.isResizeableByDockedStack(i)) {
2105 ActivityStack otherStack = getStack(i);
2106 if (otherStack != null) {
2107 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2108 true /* allowResizeInDockedMode */, DEFER_RESUME);
2109 }
2110 }
2111 }
2112
2113 // Also disable docked stack resizing since we have manually adjusted the
2114 // size of other stacks above and we don't want to trigger a docked stack
2115 // resize when we remove task from it below and it is detached from the
2116 // display because it no longer contains any tasks.
2117 mAllowDockedStackResize = false;
2118 }
2119 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2120 final int size = tasks.size();
2121 if (onTop) {
2122 for (int i = 0; i < size; i++) {
2123 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002124 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002125 "moveTasksToFullscreenStack", ANIMATE);
2126 }
2127 } else {
2128 for (int i = size - 1; i >= 0; i--) {
2129 positionTaskInStackLocked(tasks.get(i).taskId,
2130 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2131 }
2132 }
2133 } finally {
2134 mAllowDockedStackResize = true;
2135 mWindowManager.continueSurfaceLayout();
2136 }
2137 }
2138
Jorim Jaggidc249c42015-12-15 14:57:31 -08002139 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2140 Rect tempDockedTaskInsetBounds,
2141 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002142
2143 if (!mAllowDockedStackResize) {
2144 // Docked stack resize currently disabled.
2145 return;
2146 }
2147
Jorim Jaggidc249c42015-12-15 14:57:31 -08002148 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2149 if (stack == null) {
2150 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2151 return;
2152 }
2153
2154 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2155 mWindowManager.deferSurfaceLayout();
2156 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002157 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2158 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002159 ActivityRecord r = stack.topRunningActivityLocked();
2160 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2161 tempDockedTaskInsetBounds);
2162
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002163 // TODO: Checking for isAttached might not be needed as if the user passes in null
2164 // dockedBounds then they want the docked stack to be dismissed.
2165 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2166 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002167 // In this case we make all other static stacks fullscreen and move all
2168 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002169 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002170
2171 // stack shouldn't contain anymore activities, so nothing to resume.
2172 r = null;
2173 } else {
2174 // Docked stacks occupy a dedicated region on screen so the size of all other
2175 // static stacks need to be adjusted so they don't overlap with the docked stack.
2176 // We get the bounds to use from window manager which has been adjusted for any
2177 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002178 mWindowManager.getStackDockedModeBounds(
2179 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002180 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002181 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2182 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2183 tempOtherTaskInsetBounds, preserveWindows,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002184 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002185 }
2186 }
2187 }
2188 ensureConfigurationAndResume(stack, r, preserveWindows);
2189 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002190 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002191 mWindowManager.continueSurfaceLayout();
2192 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2193 }
2194
2195 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2196 tempDockedTaskBounds != null
2197 || tempDockedTaskInsetBounds != null
2198 || tempOtherTaskBounds != null
2199 || tempOtherTaskInsetBounds != null);
2200 }
2201
Robert Carr0d00c2e2016-02-29 17:45:02 -08002202 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2203 final ActivityStack stack = getStack(PINNED_STACK_ID);
2204 if (stack == null) {
2205 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2206 return;
2207 }
2208 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2209 mWindowManager.deferSurfaceLayout();
2210 try {
2211 ActivityRecord r = stack.topRunningActivityLocked();
2212 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2213 null);
2214 ensureConfigurationAndResume(stack, r, false);
2215 } finally {
2216 mWindowManager.continueSurfaceLayout();
2217 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2218 }
2219 }
2220
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002221 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2222 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002223 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002224 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002225 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002226 }
2227
Chong Zhang87b21722015-09-21 15:39:51 -07002228 // If this is a forced resize, let it go through even if the bounds is not changing,
2229 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002230 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002231 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002232 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002233 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002234 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002235 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002236
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002237 if (!mWindowManager.isValidTaskId(task.taskId)) {
2238 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002239 // All we can do for now is update the bounds so it can be used when the task is
2240 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002241 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002242 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2243 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002244 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002245 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002246 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002247 }
2248
Chong Zhang6de2ae82015-09-30 18:25:21 -07002249 // Do not move the task to another stack here.
2250 // This method assumes that the task is already placed in the right stack.
2251 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002252
Chong Zhang6de2ae82015-09-30 18:25:21 -07002253 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002254
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002255 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002256 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002257 // way and the activity was kept the way it was. If it's false, it means the activity had
2258 // to be relaunched due to configuration change.
2259 boolean kept = true;
2260 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002261 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002262 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002263 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002264 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002265
2266 if (!deferResume) {
2267
2268 // All other activities must be made visible with their correct configuration.
2269 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2270 if (!kept) {
2271 resumeFocusedStackTopActivityLocked();
2272 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002273 }
2274 }
2275 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002276 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002277
2278 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002279 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002280 }
2281
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002282 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002283 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2284 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002285 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002286 }
2287
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002288 ActivityContainer activityContainer = new ActivityContainer(stackId);
2289 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002290 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002291 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002292 }
2293
2294 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002295 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002296 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2297 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002298 break;
2299 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002300 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002301 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002302 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002303 }
2304
Chong Zhang5dcb2752015-08-18 13:50:26 -07002305 /**
2306 * Restores a recent task to a stack
2307 * @param task The recent task to be restored.
2308 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002309 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002310 * @return true if the task has been restored successfully.
2311 */
2312 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2313 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002314 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002315 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2316 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2317 // Put it in the fullscreen stack.
2318 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002319 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002320
Wale Ogunwale706ed792015-08-02 10:29:44 -07002321 if (task.stack != null) {
2322 // Task has already been restored once. See if we need to do anything more
2323 if (task.stack.mStackId == stackId) {
2324 // Nothing else to do since it is already restored in the right stack.
2325 return true;
2326 }
2327 // Remove current stack association, so we can re-associate the task with the
2328 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002329 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002330 }
2331
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002332 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002333 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002334
2335 if (stack == null) {
2336 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002337 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2338 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002339 return false;
2340 }
2341
Wale Ogunwale5f986092015-12-04 15:35:38 -08002342 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002343 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2344 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002345 final ArrayList<ActivityRecord> activities = task.mActivities;
2346 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002347 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002348 }
2349 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002350 }
2351
Wale Ogunwale040b4702015-08-06 18:10:50 -07002352 /**
2353 * Moves the specified task record to the input stack id.
2354 * WARNING: This method performs an unchecked/raw move of the task and
2355 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002356 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002357 * @param task Task to move.
2358 * @param stackId Id of stack to move task to.
2359 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002360 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002361 * @param reason Reason the task is been moved.
2362 * @return The stack the task was moved to.
2363 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002364 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002365 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002366
2367 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2368 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2369 + "support multi-window task=" + task + " to stackId=" + stackId);
2370 }
2371
Chong Zhang02898352015-08-21 17:27:14 -07002372 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002373 final ActivityStack prevStack = task.stack;
2374 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002375 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002376 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2377 // Whenever we are moving the top activity from the front stack we want to make sure to move
2378 // the stack to the front.
2379 final boolean wasFront = isFrontStack(prevStack)
2380 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002381
Chong Zhangd545dce2016-02-29 18:09:17 -08002382 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002383 // We don't allow moving a unresizeable task to the docked stack since the docked
2384 // stack is used for split-screen mode and will cause things like the docked divider to
2385 // show up. We instead leave the task in its current stack or move it to the fullscreen
2386 // stack if it isn't currently in a stack.
2387 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002388 Slog.w(TAG, "Can not move unresizeable task=" + task
2389 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2390 }
2391
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002392 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2393 // if a docked stack is created below which will lead to the stack we are moving from and
2394 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002395 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002396 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002397 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002398 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002399 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002400
2401 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002402 // move focus to the new stack by moving the stack to the front.
2403 stack.moveToFrontAndResumeStateIfNeeded(
2404 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002405
Wale Ogunwale040b4702015-08-06 18:10:50 -07002406 return stack;
2407 }
2408
Chong Zhange4fbd322016-03-01 14:44:03 -08002409 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002410 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002411 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2412 false /* deferResume */);
2413 }
2414
2415 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2416 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002417 final TaskRecord task = anyTaskForIdLocked(taskId);
2418 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002419 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002420 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002421 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002422
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002423 if (task.stack != null && task.stack.mStackId == stackId) {
2424 // You are already in the right stack silly...
2425 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002426 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002427 }
2428
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002429 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2430 throw new IllegalArgumentException("moveTaskToStack:"
2431 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2432 }
2433
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002434 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002435 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002436 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002437 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002438 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002439 // We are about to relaunch the activity because its configuration changed due to
2440 // being maximized, i.e. size change. The activity will first remove the old window
2441 // and then add a new one. This call will tell window manager about this, so it can
2442 // preserve the old window until the new one is drawn. This prevents having a gap
2443 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002444 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002445 // Note here we always set the replacing window first, as the flags might be needed
2446 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002447 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002448 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002449
2450 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002451 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002452 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002453 try {
2454 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002455 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002456 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002457
Wale Ogunwale961f4852016-02-01 20:25:54 -08002458 if (!animate) {
2459 stack.mNoAnimActivities.add(topActivity);
2460 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002461
Wale Ogunwale961f4852016-02-01 20:25:54 -08002462 // We might trigger a configuration change. Save the current task bounds for freezing.
2463 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2464
2465 // Make sure the task has the appropriate bounds/size for the stack it is in.
2466 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002467 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2468 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002469 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2470 Rect bounds = task.getLaunchBounds();
2471 if (bounds == null) {
2472 stack.layoutTaskInStack(task, null);
2473 bounds = task.mBounds;
2474 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002475 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2476 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002477 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002478 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2479 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002480 }
2481 } finally {
2482 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002483 }
2484
2485 if (mightReplaceWindow) {
2486 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2487 // window), we need to clear the replace window settings. Otherwise, we schedule a
2488 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002489 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002490 }
2491
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002492 if (!deferResume) {
2493
2494 // The task might have already been running and its visibility needs to be synchronized with
2495 // the visibility of the stack / windows.
2496 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2497 resumeFocusedStackTopActivityLocked();
2498 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002499
Jorim Jaggid53f0922016-04-06 22:16:23 -07002500 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002501
2502 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002503 }
2504
Wale Ogunwale079a0042015-10-24 11:44:07 -07002505 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2506 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2507 if (stack == null) {
2508 throw new IllegalArgumentException(
2509 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2510 }
2511
2512 final ActivityRecord r = stack.topRunningActivityLocked();
2513 if (r == null) {
2514 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2515 + " in stack=" + stack);
2516 return false;
2517 }
2518
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002519 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002520 Slog.w(TAG,
2521 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002522 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002523 return false;
2524 }
2525
Wale Ogunwale480dca02016-02-06 13:58:29 -08002526 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002527 return true;
2528 }
2529
Wale Ogunwale480dca02016-02-06 13:58:29 -08002530 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2531 mWindowManager.deferSurfaceLayout();
2532 try {
2533 final TaskRecord task = r.task;
2534
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002535 if (r == task.stack.getVisibleBehindActivity()) {
2536 // An activity can't be pinned and visible behind at the same time. Go ahead and
2537 // release it from been visible behind before pinning.
2538 requestVisibleBehindLocked(r, false);
2539 }
2540
Wale Ogunwale480dca02016-02-06 13:58:29 -08002541 // Need to make sure the pinned stack exist so we can resize it below...
2542 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2543
2544 // Resize the pinned stack to match the current size of the task the activity we are
2545 // going to be moving is currently contained in. We do this to have the right starting
2546 // animation bounds for the pinned stack to the desired bounds the caller wants.
2547 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2548 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002549 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002550
2551 if (task.mActivities.size() == 1) {
2552 // There is only one activity in the task. So, we can just move the task over to
2553 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002554 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2555 // Move the home stack forward if the task we just moved to the pinned stack
2556 // was launched from home so home should be visible behind it.
2557 moveHomeStackToFront(reason);
2558 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002559 moveTaskToStackLocked(
2560 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2561 } else {
2562 stack.moveActivityToStack(r);
2563 }
2564 } finally {
2565 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002566 }
2567
Wale Ogunwale480dca02016-02-06 13:58:29 -08002568 // The task might have already been running and its visibility needs to be synchronized
2569 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002570 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002571 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002572
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002573 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002574 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002575 }
2576
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002577 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2578 final TaskRecord task = anyTaskForIdLocked(taskId);
2579 if (task == null) {
2580 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2581 return;
2582 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002583 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2584
2585 task.updateOverrideConfigurationForStack(stack);
2586
2587 mWindowManager.positionTaskInStack(
2588 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002589 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002590 // The task might have already been running and its visibility needs to be synchronized with
2591 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002592 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002593 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002594 }
2595
Craig Mautnerac6f8432013-07-17 13:24:59 -07002596 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002597 mTmpFindTaskResult.r = null;
2598 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002599 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002600 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2601 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002602 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2603 final ActivityStack stack = stacks.get(stackNdx);
2604 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002605 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002606 continue;
2607 }
2608 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002609 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2610 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002611 continue;
2612 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002613 stack.findTaskLocked(r, mTmpFindTaskResult);
2614 // It is possible to have task in multiple stacks with the same root affinity.
2615 // If the match we found was based on root affinity we keep on looking to see if
2616 // there is a better match in another stack. We eventually return the match based
2617 // on root affinity if we don't find a better match.
2618 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2619 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002620 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002621 }
2622 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002623 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2624 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002625 }
2626
2627 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002628 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2629 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002630 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2631 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2632 if (ar != null) {
2633 return ar;
2634 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002635 }
2636 }
2637 return null;
2638 }
2639
Craig Mautner8d341ef2013-03-26 09:03:27 -07002640 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002641 scheduleSleepTimeout();
2642 if (!mGoingToSleep.isHeld()) {
2643 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002644 if (mLaunchingActivity.isHeld()) {
2645 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2646 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002647 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002648 mLaunchingActivity.release();
2649 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002650 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002651 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002652 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002653 }
2654
2655 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002656 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002657
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002658 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002659 final long endTime = System.currentTimeMillis() + timeout;
2660 while (true) {
2661 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002662 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2663 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002664 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2665 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2666 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002667 }
2668 if (cantShutdown) {
2669 long timeRemaining = endTime - System.currentTimeMillis();
2670 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002671 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002672 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002673 } catch (InterruptedException e) {
2674 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002675 } else {
2676 Slog.w(TAG, "Activity manager shutdown timed out");
2677 timedout = true;
2678 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002680 } else {
2681 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002682 }
2683 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002684
2685 // Force checkReadyForSleep to complete.
2686 mSleepTimeout = true;
2687 checkReadyForSleepLocked();
2688
Craig Mautner8d341ef2013-03-26 09:03:27 -07002689 return timedout;
2690 }
2691
2692 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002693 removeSleepTimeouts();
2694 if (mGoingToSleep.isHeld()) {
2695 mGoingToSleep.release();
2696 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002697 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2698 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002699 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2700 final ActivityStack stack = stacks.get(stackNdx);
2701 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002702 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002703 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002704 }
Craig Mautner5314a402013-09-26 12:40:16 -07002705 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002706 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002707 mGoingToSleepActivities.clear();
2708 }
2709
2710 void activitySleptLocked(ActivityRecord r) {
2711 mGoingToSleepActivities.remove(r);
2712 checkReadyForSleepLocked();
2713 }
2714
2715 void checkReadyForSleepLocked() {
2716 if (!mService.isSleepingOrShuttingDown()) {
2717 // Do not care.
2718 return;
2719 }
2720
2721 if (!mSleepTimeout) {
2722 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002723 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2724 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002725 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2726 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2727 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002728 }
2729
2730 if (mStoppingActivities.size() > 0) {
2731 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002732 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002733 + mStoppingActivities.size() + " activities");
2734 scheduleIdleLocked();
2735 dontSleep = true;
2736 }
2737
2738 if (mGoingToSleepActivities.size() > 0) {
2739 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002740 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002741 + mGoingToSleepActivities.size() + " activities");
2742 dontSleep = true;
2743 }
2744
2745 if (dontSleep) {
2746 return;
2747 }
2748 }
2749
Craig Mautnere0a38842013-12-16 16:14:02 -08002750 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2751 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002752 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2753 stacks.get(stackNdx).goToSleep();
2754 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002755 }
2756
2757 removeSleepTimeouts();
2758
2759 if (mGoingToSleep.isHeld()) {
2760 mGoingToSleep.release();
2761 }
2762 if (mService.mShuttingDown) {
2763 mService.notifyAll();
2764 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002765 }
2766
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002767 boolean reportResumedActivityLocked(ActivityRecord r) {
2768 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002769 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002770 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002771 }
2772 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002773 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002774 mWindowManager.executeAppTransition();
2775 return true;
2776 }
2777 return false;
2778 }
2779
Craig Mautner8d341ef2013-03-26 09:03:27 -07002780 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002781 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2782 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002783 int stackNdx = stacks.size() - 1;
2784 while (stackNdx >= 0) {
2785 stacks.get(stackNdx).handleAppCrashLocked(app);
2786 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002787 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002788 }
2789 }
2790
Jose Lima4b6c6692014-08-12 17:41:12 -07002791 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002792 final ActivityStack stack = r.task.stack;
2793 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002794 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2795 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002796 return false;
2797 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002798
2799 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2800 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2801 + " visible=" + visible + " stackId=" + stack.mStackId
2802 + " can't contain visible behind activities");
2803 return false;
2804 }
2805
Jose Lima4b6c6692014-08-12 17:41:12 -07002806 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002807 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2808 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002809
2810 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002811 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002812 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002813 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002814 return true;
2815 }
2816
2817 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002818 if (visible && top.fullscreen) {
2819 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002820 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2821 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2822 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2823 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002824 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002825 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2826 // Only the activity set as currently visible behind should actively reset its
2827 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002828 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2829 "requestVisibleBehind: returning visible=" + visible
2830 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2831 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002832 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002833 }
2834
Jose Lima4b6c6692014-08-12 17:41:12 -07002835 stack.setVisibleBehindActivity(visible ? r : null);
2836 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002837 // If there is a translucent home activity, we need to force it stop being translucent,
2838 // because we can't depend on the application to necessarily perform that operation.
2839 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002840 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002841 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002842 mService.convertFromTranslucent(next.appToken);
2843 }
2844 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002845 if (top.app != null && top.app.thread != null) {
2846 // Notify the top app of the change.
2847 try {
2848 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2849 } catch (RemoteException e) {
2850 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002851 }
2852 return true;
2853 }
2854
Craig Mautnerbb742462014-07-07 15:28:55 -07002855 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2856 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002857 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002858 final ActivityStack stack = task.stack;
2859
2860 r.mLaunchTaskBehind = false;
2861 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002862 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002863 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002864 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002865
2866 // When launching tasks behind, update the last active time of the top task after the new
2867 // task has been shown briefly
2868 final ActivityRecord top = stack.topActivity();
2869 if (top != null) {
2870 top.task.touchActiveTime();
2871 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002872 }
2873
2874 void scheduleLaunchTaskBehindComplete(IBinder token) {
2875 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2876 }
2877
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002878 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2879 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002880 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002881 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2882 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002883 final int topStackNdx = stacks.size() - 1;
2884 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2885 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002886 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002887 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002888 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002889 }
2890
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002891 void invalidateTaskLayers() {
2892 mTaskLayersChanged = true;
2893 }
2894
2895 void rankTaskLayersIfNeeded() {
2896 if (!mTaskLayersChanged) {
2897 return;
2898 }
2899 mTaskLayersChanged = false;
2900 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2901 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2902 int baseLayer = 0;
2903 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2904 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2905 }
2906 }
2907 }
2908
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002909 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2910 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2911 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2912 final int topStackNdx = stacks.size() - 1;
2913 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2914 final ActivityStack stack = stacks.get(stackNdx);
2915 stack.clearOtherAppTimeTrackers(except);
2916 }
2917 }
2918 }
2919
Craig Mautner8d341ef2013-03-26 09:03:27 -07002920 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002921 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2922 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002923 final int numStacks = stacks.size();
2924 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2925 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002926 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002927 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002928 }
2929 }
2930
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002931 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2932 // Examine all activities currently running in the process.
2933 TaskRecord firstTask = null;
2934 // Tasks is non-null only if two or more tasks are found.
2935 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002936 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2937 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002938 ActivityRecord r = app.activities.get(i);
2939 // First, if we find an activity that is in the process of being destroyed,
2940 // then we just aren't going to do anything for now; we want things to settle
2941 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002942 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002943 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002944 return;
2945 }
2946 // Don't consider any activies that are currently not in a state where they
2947 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002948 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2949 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002950 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002951 continue;
2952 }
2953 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002954 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002955 + " from " + r);
2956 if (firstTask == null) {
2957 firstTask = r.task;
2958 } else if (firstTask != r.task) {
2959 if (tasks == null) {
2960 tasks = new ArraySet<>();
2961 tasks.add(firstTask);
2962 }
2963 tasks.add(r.task);
2964 }
2965 }
2966 }
2967 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002968 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002969 return;
2970 }
2971 // If we have activities in multiple tasks that are in a position to be destroyed,
2972 // let's iterate through the tasks and release the oldest one.
2973 final int numDisplays = mActivityDisplays.size();
2974 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2975 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2976 // Step through all stacks starting from behind, to hit the oldest things first.
2977 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2978 final ActivityStack stack = stacks.get(stackNdx);
2979 // Try to release activities in this stack; if we manage to, we are done.
2980 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2981 return;
2982 }
2983 }
2984 }
2985 }
2986
Amith Yamasani37a40c22015-06-17 13:25:42 -07002987 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002988 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002989 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002990 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002991
Craig Mautner858d8a62013-04-23 17:08:34 -07002992 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002993 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2994 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002995 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002996 final ActivityStack stack = stacks.get(stackNdx);
2997 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002998 TaskRecord task = stack.topTask();
2999 if (task != null) {
3000 mWindowManager.moveTaskToTop(task.taskId);
3001 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003002 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003003 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003004
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003005 ActivityStack stack = getStack(restoreStackId);
3006 if (stack == null) {
3007 stack = mHomeStack;
3008 }
3009 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003010 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003011 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003012 } else {
3013 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003014 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003015 }
Craig Mautner93529a42013-10-04 15:03:13 -07003016 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003017 }
3018
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003019 /** Checks whether the userid is a profile of the current user. */
3020 boolean isCurrentProfileLocked(int userId) {
3021 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003022 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003023 }
3024
Chong Zhang45c25ce2015-08-10 22:18:26 -07003025 /** Checks whether the activity should be shown for current user. */
3026 boolean okToShowLocked(ActivityRecord r) {
3027 return r != null && (isCurrentProfileLocked(r.userId)
3028 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3029 }
3030
Craig Mautnerde4ef022013-04-07 19:01:33 -07003031 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003032 ArrayList<ActivityRecord> stops = null;
3033
3034 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003035 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3036 ActivityRecord s = mStoppingActivities.get(activityNdx);
3037 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003038 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003039 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3040 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003041 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003042 if (s.finishing) {
3043 // If this activity is finishing, it is sitting on top of
3044 // everyone else but we now know it is no longer needed...
3045 // so get rid of it. Otherwise, we need to go through the
3046 // normal flow and hide it once we determine that it is
3047 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003048 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003049 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003050 }
3051 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003052 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003053 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003054 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003055 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003056 }
3057 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003058 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003059 }
3060 }
3061
3062 return stops;
3063 }
3064
Craig Mautnercf910b02013-04-23 11:23:27 -07003065 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003066 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3067 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3068 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3069 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003070 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003071 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003072 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003073 if (r == null) Slog.e(TAG,
3074 "validateTop...: null top activity, stack=" + stack);
3075 else {
3076 final ActivityRecord pausing = stack.mPausingActivity;
3077 if (pausing != null && pausing == r) Slog.e(TAG,
3078 "validateTop...: top stack has pausing activity r=" + r
3079 + " state=" + state);
3080 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3081 "validateTop...: activity in front not resumed r=" + r
3082 + " state=" + state);
3083 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003084 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003085 final ActivityRecord resumed = stack.mResumedActivity;
3086 if (resumed != null && resumed == r) Slog.e(TAG,
3087 "validateTop...: back stack has resumed activity r=" + r
3088 + " state=" + state);
3089 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3090 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003091 }
3092 }
3093 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003094 }
3095
Craig Mautnere0570202015-05-13 13:06:11 -07003096 private String lockTaskModeToString() {
3097 switch (mLockTaskModeState) {
3098 case LOCK_TASK_MODE_LOCKED:
3099 return "LOCKED";
3100 case LOCK_TASK_MODE_PINNED:
3101 return "PINNED";
3102 case LOCK_TASK_MODE_NONE:
3103 return "NONE";
3104 default: return "unknown=" + mLockTaskModeState;
3105 }
3106 }
3107
Craig Mautner27084302013-03-25 08:05:25 -07003108 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003109 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003110 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003111 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003112 pw.print(prefix);
3113 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003114 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003115 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003116 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3117 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3118 if (packages.size() > 0) {
3119 pw.println(" mLockTaskPackages (userId:packages)=");
3120 for (int i = 0; i < packages.size(); ++i) {
3121 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3122 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3123 }
3124 }
3125 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003126 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003127
Craig Mautner20e72272013-04-01 13:45:53 -07003128 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003129 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003130 }
3131
Dianne Hackborn390517b2013-05-30 15:03:32 -07003132 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3133 boolean needSep, String prefix) {
3134 if (activity != null) {
3135 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3136 if (needSep) {
3137 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003138 }
3139 pw.print(prefix);
3140 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003141 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003142 }
3143 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003144 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003145 }
3146
Craig Mautner8d341ef2013-03-26 09:03:27 -07003147 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3148 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003149 boolean printed = false;
3150 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003151 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3152 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003153 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003154 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003155 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003156 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003157 final ActivityStack stack = stacks.get(stackNdx);
3158 StringBuilder stackHeader = new StringBuilder(128);
3159 stackHeader.append(" Stack #");
3160 stackHeader.append(stack.mStackId);
3161 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003162 stackHeader.append("\n");
3163 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3164 stackHeader.append("\n");
3165 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003166 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3167 needSep, stackHeader.toString());
3168 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3169 !dumpAll, false, dumpPackage, true,
3170 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003171
Craig Mautner4a1cb222013-12-04 16:14:06 -08003172 needSep = printed;
3173 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3174 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003175 if (pr) {
3176 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003177 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003178 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003179 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3180 " mResumedActivity: ");
3181 if (pr) {
3182 printed = true;
3183 needSep = false;
3184 }
3185 if (dumpAll) {
3186 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3187 " mLastPausedActivity: ");
3188 if (pr) {
3189 printed = true;
3190 needSep = true;
3191 }
3192 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3193 needSep, " mLastNoHistoryActivity: ");
3194 }
3195 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003196 }
3197 }
3198
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003199 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3200 false, dumpPackage, true, " Activities waiting to finish:", null);
3201 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3202 false, dumpPackage, true, " Activities waiting to stop:", null);
3203 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3204 false, dumpPackage, true, " Activities waiting for another to become visible:",
3205 null);
3206 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3207 false, dumpPackage, true, " Activities waiting to sleep:", null);
3208 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3209 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003210
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003211 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003212 }
3213
Dianne Hackborn390517b2013-05-30 15:03:32 -07003214 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003215 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003216 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003217 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003218 String innerPrefix = null;
3219 String[] args = null;
3220 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003221 for (int i=list.size()-1; i>=0; i--) {
3222 final ActivityRecord r = list.get(i);
3223 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3224 continue;
3225 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003226 if (innerPrefix == null) {
3227 innerPrefix = prefix + " ";
3228 args = new String[0];
3229 }
3230 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003231 final boolean full = !brief && (complete || !r.isInHistory());
3232 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003233 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003234 needNL = false;
3235 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003236 if (header1 != null) {
3237 pw.println(header1);
3238 header1 = null;
3239 }
3240 if (header2 != null) {
3241 pw.println(header2);
3242 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003243 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003244 if (lastTask != r.task) {
3245 lastTask = r.task;
3246 pw.print(prefix);
3247 pw.print(full ? "* " : " ");
3248 pw.println(lastTask);
3249 if (full) {
3250 lastTask.dump(pw, prefix + " ");
3251 } else if (complete) {
3252 // Complete + brief == give a summary. Isn't that obvious?!?
3253 if (lastTask.intent != null) {
3254 pw.print(prefix); pw.print(" ");
3255 pw.println(lastTask.intent.toInsecureStringWithClip());
3256 }
3257 }
3258 }
3259 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3260 pw.print(" #"); pw.print(i); pw.print(": ");
3261 pw.println(r);
3262 if (full) {
3263 r.dump(pw, innerPrefix);
3264 } else if (complete) {
3265 // Complete + brief == give a summary. Isn't that obvious?!?
3266 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3267 if (r.app != null) {
3268 pw.print(innerPrefix); pw.println(r.app);
3269 }
3270 }
3271 if (client && r.app != null && r.app.thread != null) {
3272 // flush anything that is already in the PrintWriter since the thread is going
3273 // to write to the file descriptor directly
3274 pw.flush();
3275 try {
3276 TransferPipe tp = new TransferPipe();
3277 try {
3278 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3279 r.appToken, innerPrefix, args);
3280 // Short timeout, since blocking here can
3281 // deadlock with the application.
3282 tp.go(fd, 2000);
3283 } finally {
3284 tp.kill();
3285 }
3286 } catch (IOException e) {
3287 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3288 } catch (RemoteException e) {
3289 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3290 }
3291 needNL = true;
3292 }
3293 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003294 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003295 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003296
Craig Mautnerf3333272013-04-22 10:55:53 -07003297 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003298 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3299 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003300 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3301 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003302 }
3303
3304 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003305 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003306 }
3307
3308 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003309 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3310 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003311 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3312 }
3313
Craig Mautner05d29032013-05-03 13:40:13 -07003314 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003315 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3316 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3317 }
Craig Mautner05d29032013-05-03 13:40:13 -07003318 }
3319
Craig Mautner0eea92c2013-05-16 13:35:39 -07003320 void removeSleepTimeouts() {
3321 mSleepTimeout = false;
3322 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3323 }
3324
3325 final void scheduleSleepTimeout() {
3326 removeSleepTimeouts();
3327 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3328 }
3329
Craig Mautner4a1cb222013-12-04 16:14:06 -08003330 @Override
3331 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003332 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003333 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3334 }
3335
3336 @Override
3337 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003338 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003339 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3340 }
3341
3342 @Override
3343 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003344 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003345 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3346 }
3347
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003348 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003349 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003350 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003351 newDisplay = mActivityDisplays.get(displayId) == null;
3352 if (newDisplay) {
3353 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003354 if (activityDisplay.mDisplay == null) {
3355 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3356 return;
3357 }
Craig Mautner4504de52013-12-20 09:06:56 -08003358 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003359 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003360 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003361 }
Craig Mautner4504de52013-12-20 09:06:56 -08003362 if (newDisplay) {
3363 mWindowManager.onDisplayAdded(displayId);
3364 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003365 }
3366
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003367 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003368 mDefaultMinimalSizeOfResizeableTask =
3369 mService.mContext.getResources().getDimensionPixelSize(
3370 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003371 }
3372
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003373 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003374 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003375 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3376 if (activityDisplay != null) {
3377 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003378 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003379 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003380 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003381 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003382 }
3383 }
3384 mWindowManager.onDisplayRemoved(displayId);
3385 }
3386
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003387 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003388 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003389 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3390 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003391 // TODO: Update the bounds.
3392 }
3393 }
3394 mWindowManager.onDisplayChanged(displayId);
3395 }
3396
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003397 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003398 StackInfo info = new StackInfo();
3399 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3400 info.displayId = Display.DEFAULT_DISPLAY;
3401 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003402 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003403
3404 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3405 final int numTasks = tasks.size();
3406 int[] taskIds = new int[numTasks];
3407 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003408 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003409 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003410 for (int i = 0; i < numTasks; ++i) {
3411 final TaskRecord task = tasks.get(i);
3412 taskIds[i] = task.taskId;
3413 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3414 : task.realActivity != null ? task.realActivity.flattenToString()
3415 : task.getTopActivity() != null ? task.getTopActivity().packageName
3416 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003417 taskBounds[i] = new Rect();
3418 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003419 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003420 }
3421 info.taskIds = taskIds;
3422 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003423 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003424 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003425 return info;
3426 }
3427
3428 StackInfo getStackInfoLocked(int stackId) {
3429 ActivityStack stack = getStack(stackId);
3430 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003431 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003432 }
3433 return null;
3434 }
3435
3436 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003437 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003438 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3439 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003440 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003441 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003442 }
3443 }
3444 return list;
3445 }
3446
Craig Mautner15df08a2015-04-01 12:17:18 -07003447 TaskRecord getLockedTaskLocked() {
3448 final int top = mLockTaskModeTasks.size() - 1;
3449 if (top >= 0) {
3450 return mLockTaskModeTasks.get(top);
3451 }
3452 return null;
3453 }
3454
3455 boolean isLockedTask(TaskRecord task) {
3456 return mLockTaskModeTasks.contains(task);
3457 }
3458
3459 boolean isLastLockedTask(TaskRecord task) {
3460 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3461 }
3462
3463 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003464 if (!mLockTaskModeTasks.remove(task)) {
3465 return;
3466 }
3467 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3468 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003469 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003470 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3471 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003472 final Message lockTaskMsg = Message.obtain();
3473 lockTaskMsg.arg1 = task.userId;
3474 lockTaskMsg.what = LOCK_TASK_END_MSG;
3475 mHandler.sendMessage(lockTaskMsg);
3476 }
3477 }
3478
Andrii Kulianc27916642016-04-12 17:59:27 -07003479 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3480 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3481 false /* forceNonResizable */);
3482 }
3483
Jorim Jaggid53f0922016-04-06 22:16:23 -07003484 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003485 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003486 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3487 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003488 return;
3489 }
3490
Andrii Kulianc27916642016-04-12 17:59:27 -07003491 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003492 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003493 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3494
Andrii Kulianc27916642016-04-12 17:59:27 -07003495 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3496 // we need to move it to top of fullscreen stack, otherwise it will be covered.
3497 mService.moveTasksToFullscreenStack(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003498 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3499 String packageName = task.getTopActivity() != null
3500 ? task.getTopActivity().appInfo.packageName : null;
3501 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3502 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003503 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003504 }
3505
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003506 void showLockTaskToast() {
3507 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003508 }
3509
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003510 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3511 if (mLockTaskModeTasks.contains(task)) {
3512 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3513 }
3514 }
3515
Craig Mautner432f64e2015-05-20 14:59:57 -07003516 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3517 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003518 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003519 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003520 final TaskRecord lockedTask = getLockedTaskLocked();
3521 if (lockedTask != null) {
3522 removeLockedTaskLocked(lockedTask);
3523 if (!mLockTaskModeTasks.isEmpty()) {
3524 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003525 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3526 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003527 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003528 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003529 return;
3530 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003531 }
Craig Mautnere0570202015-05-13 13:06:11 -07003532 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3533 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003534 return;
3535 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003536
3537 // Should have already been checked, but do it again.
3538 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003539 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3540 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003541 return;
3542 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003543 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003544 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003545 return;
3546 }
3547
3548 if (mLockTaskModeTasks.isEmpty()) {
3549 // First locktask.
3550 final Message lockTaskMsg = Message.obtain();
3551 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3552 lockTaskMsg.arg1 = task.userId;
3553 lockTaskMsg.what = LOCK_TASK_START_MSG;
3554 lockTaskMsg.arg2 = lockTaskModeState;
3555 mHandler.sendMessage(lockTaskMsg);
3556 }
3557 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003558 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3559 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003560 mLockTaskModeTasks.remove(task);
3561 mLockTaskModeTasks.add(task);
3562
3563 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003564 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003565 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003566
3567 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003568 findTaskToMoveToFrontLocked(task, 0, null, reason,
3569 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003570 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003571 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3572 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3573 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003574 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003575 }
3576
3577 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003578 return isLockTaskModeViolation(task, false);
3579 }
3580
3581 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3582 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003583 return false;
3584 }
3585 final int lockTaskAuth = task.mLockTaskAuth;
3586 switch (lockTaskAuth) {
3587 case LOCK_TASK_AUTH_DONT_LOCK:
3588 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003589 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003590 case LOCK_TASK_AUTH_LAUNCHABLE:
3591 case LOCK_TASK_AUTH_WHITELISTED:
3592 return false;
3593 case LOCK_TASK_AUTH_PINNABLE:
3594 // Pinnable tasks can't be launched on top of locktask tasks.
3595 return !mLockTaskModeTasks.isEmpty();
3596 default:
3597 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3598 return true;
3599 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003600 }
3601
Craig Mautner15df08a2015-04-01 12:17:18 -07003602 void onLockTaskPackagesUpdatedLocked() {
3603 boolean didSomething = false;
3604 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3605 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003606 final boolean wasWhitelisted =
3607 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3608 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003609 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003610 final boolean isWhitelisted =
3611 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3612 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3613 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003614 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003615 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3616 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003617 removeLockedTaskLocked(lockedTask);
3618 lockedTask.performClearTaskLocked();
3619 didSomething = true;
3620 }
3621 }
3622 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3623 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3624 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3625 final ActivityStack stack = stacks.get(stackNdx);
3626 stack.onLockTaskPackagesUpdatedLocked();
3627 }
3628 }
Craig Mautnere0570202015-05-13 13:06:11 -07003629 final ActivityRecord r = topRunningActivityLocked();
3630 final TaskRecord task = r != null ? r.task : null;
3631 if (mLockTaskModeTasks.isEmpty() && task != null
3632 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3633 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003634 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3635 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3636 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3637 false);
3638 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003639 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003640 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003641 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003642 }
3643 }
3644
Benjamin Franz43261142015-02-11 15:59:44 +00003645 int getLockTaskModeState() {
3646 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003647 }
3648
Jorim Jaggife89d122015-12-22 16:28:44 +01003649 void activityRelaunchedLocked(IBinder token) {
3650 mWindowManager.notifyAppRelaunchingFinished(token);
3651 }
3652
3653 void activityRelaunchingLocked(ActivityRecord r) {
3654 mWindowManager.notifyAppRelaunching(r.appToken);
3655 }
3656
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003657 void logStackState() {
3658 mActivityMetricsLogger.logWindowState();
3659 }
3660
Andrii Kulian933076d2016-03-29 17:04:42 -07003661 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003662 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3663 final ActivityRecord r = task.mActivities.get(i);
3664 if (r.app != null && r.app.thread != null) {
3665 mMultiWindowModeChangedActivities.add(r);
3666 }
3667 }
3668
3669 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3670 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3671 }
3672 }
3673
Andrii Kulian933076d2016-03-29 17:04:42 -07003674 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003675 final ActivityStack stack = task.stack;
3676 if (prevStack == null || prevStack == stack
3677 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3678 return;
3679 }
3680
3681 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3682 final ActivityRecord r = task.mActivities.get(i);
3683 if (r.app != null && r.app.thread != null) {
3684 mPipModeChangedActivities.add(r);
3685 }
3686 }
3687
3688 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3689 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3690 }
3691 }
3692
Tony Mak853304c2016-04-18 15:17:41 +01003693 void setDockedStackMinimized(boolean minimized) {
3694 mIsDockMinimized = minimized;
3695 if (minimized) {
3696 // Docked stack is not visible, no need to confirm credentials for its top activity.
3697 return;
3698 }
3699 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3700 if (dockedStack == null) {
3701 return;
3702 }
3703 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3704 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3705 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3706 }
3707 }
3708
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003709 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003710
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003711 public ActivityStackSupervisorHandler(Looper looper) {
3712 super(looper);
3713 }
3714
Craig Mautnerf3333272013-04-22 10:55:53 -07003715 void activityIdleInternal(ActivityRecord r) {
3716 synchronized (mService) {
3717 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3718 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003719 }
3720
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003721 @Override
3722 public void handleMessage(Message msg) {
3723 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003724 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3725 synchronized (mService) {
3726 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3727 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003728 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003729 }
3730 }
3731 } break;
3732 case REPORT_PIP_MODE_CHANGED_MSG: {
3733 synchronized (mService) {
3734 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3735 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003736 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003737 }
3738 }
3739 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003740 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003741 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3742 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003743 if (mService.mDidDexOpt) {
3744 mService.mDidDexOpt = false;
3745 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3746 nmsg.obj = msg.obj;
3747 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3748 return;
3749 }
3750 // We don't at this point know if the activity is fullscreen,
3751 // so we need to be conservative and assume it isn't.
3752 activityIdleInternal((ActivityRecord)msg.obj);
3753 } break;
3754 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003755 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003756 activityIdleInternal((ActivityRecord)msg.obj);
3757 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003758 case RESUME_TOP_ACTIVITY_MSG: {
3759 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003760 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003761 }
3762 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003763 case SLEEP_TIMEOUT_MSG: {
3764 synchronized (mService) {
3765 if (mService.isSleepingOrShuttingDown()) {
3766 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3767 mSleepTimeout = true;
3768 checkReadyForSleepLocked();
3769 }
3770 }
3771 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003772 case LAUNCH_TIMEOUT_MSG: {
3773 if (mService.mDidDexOpt) {
3774 mService.mDidDexOpt = false;
3775 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3776 return;
3777 }
3778 synchronized (mService) {
3779 if (mLaunchingActivity.isHeld()) {
3780 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3781 if (VALIDATE_WAKE_LOCK_CALLER
3782 && Binder.getCallingUid() != Process.myUid()) {
3783 throw new IllegalStateException("Calling must be system uid");
3784 }
3785 mLaunchingActivity.release();
3786 }
3787 }
3788 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003789 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003790 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003791 } break;
3792 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003793 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794 } break;
3795 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003796 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003798 case CONTAINER_CALLBACK_VISIBILITY: {
3799 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003800 final IActivityContainerCallback callback = container.mCallback;
3801 if (callback != null) {
3802 try {
3803 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3804 } catch (RemoteException e) {
3805 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003806 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003807 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003808 case LOCK_TASK_START_MSG: {
3809 // When lock task starts, we disable the status bars.
3810 try {
Jason Monk62515be2014-05-21 16:06:19 -04003811 if (mLockTaskNotify == null) {
3812 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003813 }
Jason Monk62515be2014-05-21 16:06:19 -04003814 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003815 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003816 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003817 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003818 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003819 flags = StatusBarManager.DISABLE_MASK
3820 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003821 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003822 flags = StatusBarManager.DISABLE_MASK
3823 & (~StatusBarManager.DISABLE_BACK)
3824 & (~StatusBarManager.DISABLE_HOME)
3825 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003826 }
3827 getStatusBarService().disable(flags, mToken,
3828 mService.mContext.getPackageName());
3829 }
3830 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003831 if (getDevicePolicyManager() != null) {
3832 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003833 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003834 }
justinzhang5286d3f2014-05-12 17:06:01 -04003835 } catch (RemoteException ex) {
3836 throw new RuntimeException(ex);
3837 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003838 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003839 case LOCK_TASK_END_MSG: {
3840 // When lock task ends, we enable the status bars.
3841 try {
Jason Monk62515be2014-05-21 16:06:19 -04003842 if (getStatusBarService() != null) {
3843 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3844 mService.mContext.getPackageName());
3845 }
3846 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003847 if (getDevicePolicyManager() != null) {
3848 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3849 msg.arg1);
3850 }
Jason Monk62515be2014-05-21 16:06:19 -04003851 if (mLockTaskNotify == null) {
3852 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3853 }
3854 mLockTaskNotify.show(false);
3855 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003856 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003857 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003858 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003859 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003860 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003861 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003862 new LockPatternUtils(mService.mContext)
3863 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003864 }
3865 } catch (SettingNotFoundException e) {
3866 // No setting, don't lock.
3867 }
justinzhang5286d3f2014-05-12 17:06:01 -04003868 } catch (RemoteException ex) {
3869 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003870 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003871 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003872 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003873 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003874 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3875 if (mLockTaskNotify == null) {
3876 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3877 }
3878 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3879 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003880 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3881 final ActivityContainer container = (ActivityContainer) msg.obj;
3882 final IActivityContainerCallback callback = container.mCallback;
3883 if (callback != null) {
3884 try {
3885 callback.onAllActivitiesComplete(container.asBinder());
3886 } catch (RemoteException e) {
3887 }
3888 }
3889 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003890 case LAUNCH_TASK_BEHIND_COMPLETE: {
3891 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003892 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003893 if (r != null) {
3894 handleLaunchTaskBehindCompleteLocked(r);
3895 }
3896 }
3897 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003898
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003899 }
3900 }
3901 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003902
Ying Wangb081a592014-04-22 15:20:16 -07003903 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003904 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3905 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003906 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003907 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003908 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003909 ActivityRecord mParentActivity = null;
3910 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003911
Craig Mautnere3a00d72014-04-16 08:31:19 -07003912 boolean mVisible = true;
3913
Craig Mautner4a1cb222013-12-04 16:14:06 -08003914 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003915 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003916
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003917 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3918 final static int CONTAINER_STATE_NO_SURFACE = 1;
3919 final static int CONTAINER_STATE_FINISHING = 2;
3920 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3921
3922 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003923 synchronized (mService) {
3924 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003925 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003926 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003927 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003928 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003929 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003930
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003931 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003932 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003933 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003934 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003935 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003936 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003937 }
3938
3939 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003940 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003941 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003942 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3943 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003944 return;
3945 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003946 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003947 }
3948 }
3949
3950 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003951 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003952 synchronized (mService) {
3953 if (mActivityDisplay != null) {
3954 return mActivityDisplay.mDisplayId;
3955 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003956 }
3957 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003958 }
3959
Jeff Brownca9bc702014-02-11 14:32:56 -08003960 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003961 public int getStackId() {
3962 synchronized (mService) {
3963 return mStackId;
3964 }
3965 }
3966
3967 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003968 public boolean injectEvent(InputEvent event) {
3969 final long origId = Binder.clearCallingIdentity();
3970 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003971 synchronized (mService) {
3972 if (mActivityDisplay != null) {
3973 return mInputManagerInternal.injectInputEvent(event,
3974 mActivityDisplay.mDisplayId,
3975 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3976 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003977 }
3978 return false;
3979 } finally {
3980 Binder.restoreCallingIdentity(origId);
3981 }
3982 }
3983
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003984 @Override
3985 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003986 synchronized (mService) {
3987 if (mContainerState == CONTAINER_STATE_FINISHING) {
3988 return;
3989 }
3990 mContainerState = CONTAINER_STATE_FINISHING;
3991
Craig Mautnerd163e752014-06-13 17:18:47 -07003992 long origId = Binder.clearCallingIdentity();
3993 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003994 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003995 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003996 } finally {
3997 Binder.restoreCallingIdentity(origId);
3998 }
3999 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004000 }
4001
Craig Mautner60257702014-09-17 15:02:33 -07004002 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004003 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004004 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004005 if (mActivityDisplay != null) {
4006 mActivityDisplay.detachActivitiesLocked(mStack);
4007 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004008 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 }
4010 }
4011
4012 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004013 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004014 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004015 }
4016
4017 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004018 public final int startActivityIntentSender(IIntentSender intentSender)
4019 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004020 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4021
4022 if (!(intentSender instanceof PendingIntentRecord)) {
4023 throw new IllegalArgumentException("Bad PendingIntent object");
4024 }
4025
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004026 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004027 Binder.getCallingUid(), mCurrentUser, false,
4028 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004029
4030 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4031 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4032 pendingIntent.key.requestResolvedType);
4033
4034 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4035 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4036 }
4037
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004038 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004039 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004040 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004041 throw new SecurityException(
4042 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4043 }
4044 }
4045
Craig Mautnerdf88d732014-01-27 09:21:32 -08004046 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004047 public IBinder asBinder() {
4048 return this;
4049 }
4050
Craig Mautner4504de52013-12-20 09:06:56 -08004051 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004052 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004053 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004054 }
4055
Craig Mautner4a1cb222013-12-04 16:14:06 -08004056 ActivityStackSupervisor getOuter() {
4057 return ActivityStackSupervisor.this;
4058 }
4059
Craig Mautnerd163e752014-06-13 17:18:47 -07004060 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004061 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004062 }
4063
Craig Mautner6985bad2014-04-21 15:22:06 -07004064 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004065 void setVisible(boolean visible) {
4066 if (mVisible != visible) {
4067 mVisible = visible;
4068 if (mCallback != null) {
4069 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4070 0 /* unused */, this).sendToTarget();
4071 }
4072 }
4073 }
4074
Craig Mautner6985bad2014-04-21 15:22:06 -07004075 void setDrawn() {
4076 }
4077
Craig Mautner1b4bf852014-05-26 15:06:32 -07004078 // You can always start a new task on a regular ActivityStack.
4079 boolean isEligibleForNewTasks() {
4080 return true;
4081 }
4082
Craig Mautnerd163e752014-06-13 17:18:47 -07004083 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004084 detachLocked();
4085 deleteActivityContainer(this);
4086 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004087 }
4088
Craig Mautner34b73df2014-01-12 21:11:08 -08004089 @Override
4090 public String toString() {
4091 return mIdString + (mActivityDisplay == null ? "N" : "A");
4092 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004093 }
4094
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004095 private class VirtualActivityContainer extends ActivityContainer {
4096 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004097 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004098
4099 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4100 super(getNextStackId());
4101 mParentActivity = parent;
4102 mCallback = callback;
4103 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004104 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004105 }
4106
4107 @Override
4108 public void setSurface(Surface surface, int width, int height, int density) {
4109 super.setSurface(surface, width, height, density);
4110
4111 synchronized (mService) {
4112 final long origId = Binder.clearCallingIdentity();
4113 try {
4114 setSurfaceLocked(surface, width, height, density);
4115 } finally {
4116 Binder.restoreCallingIdentity(origId);
4117 }
4118 }
4119 }
4120
4121 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4122 if (mContainerState == CONTAINER_STATE_FINISHING) {
4123 return;
4124 }
4125 VirtualActivityDisplay virtualActivityDisplay =
4126 (VirtualActivityDisplay) mActivityDisplay;
4127 if (virtualActivityDisplay == null) {
4128 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004129 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004130 mActivityDisplay = virtualActivityDisplay;
4131 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004132 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004133 }
4134
4135 if (mSurface != null) {
4136 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004137 }
4138
Craig Mautner6985bad2014-04-21 15:22:06 -07004139 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004140 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004141 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004142 } else {
4143 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004144 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004145 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004146 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004147 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004148 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004149
Craig Mautnerd163e752014-06-13 17:18:47 -07004150 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004151
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004152 if (DEBUG_STACK) Slog.d(TAG_STACK,
4153 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004154 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004155
Craig Mautner6985bad2014-04-21 15:22:06 -07004156 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004157 boolean isAttachedLocked() {
4158 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004159 }
4160
4161 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004162 void setDrawn() {
4163 synchronized (mService) {
4164 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004165 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004166 }
4167 }
4168
Craig Mautner1b4bf852014-05-26 15:06:32 -07004169 // Never start a new task on an ActivityView if it isn't explicitly specified.
4170 @Override
4171 boolean isEligibleForNewTasks() {
4172 return false;
4173 }
4174
Craig Mautnerd163e752014-06-13 17:18:47 -07004175 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004176 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004177 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4178 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4179 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4180 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4181 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004182 }
4183 }
4184
Craig Mautner4a1cb222013-12-04 16:14:06 -08004185 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4186 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004187 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004188 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004189 int mDisplayId;
4190 Display mDisplay;
4191 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004192
Craig Mautner4a1cb222013-12-04 16:14:06 -08004193 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4194 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004195 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004196
Jose Lima4b6c6692014-08-12 17:41:12 -07004197 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004198
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004199 ActivityDisplay() {
4200 }
Craig Mautner4504de52013-12-20 09:06:56 -08004201
Craig Mautner1a70a162014-09-13 12:09:31 -07004202 // After instantiation, check that mDisplay is not null before using this. The alternative
4203 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004204 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004205 final Display display = mDisplayManager.getDisplay(displayId);
4206 if (display == null) {
4207 return;
4208 }
4209 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004210 }
4211
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004212 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004213 mDisplay = display;
4214 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004215 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004216 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004217
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004218 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004219 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004220 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4221 + " onTop=" + onTop);
4222 if (onTop) {
4223 mStacks.add(stack);
4224 } else {
4225 mStacks.add(0, stack);
4226 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004227 }
4228
Craig Mautnere0a38842013-12-16 16:14:02 -08004229 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004230 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004231 + " from displayId=" + mDisplayId);
4232 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004233 }
4234
Jose Lima4b6c6692014-08-12 17:41:12 -07004235 void setVisibleBehindActivity(ActivityRecord r) {
4236 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004237 }
4238
Jose Lima4b6c6692014-08-12 17:41:12 -07004239 boolean hasVisibleBehindActivity() {
4240 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004241 }
4242
Craig Mautner34b73df2014-01-12 21:11:08 -08004243 @Override
4244 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004245 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4246 }
4247 }
4248
4249 class VirtualActivityDisplay extends ActivityDisplay {
4250 VirtualDisplay mVirtualDisplay;
4251
Craig Mautner6985bad2014-04-21 15:22:06 -07004252 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004253 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004254 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4255 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4256 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4257 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004258
4259 init(mVirtualDisplay.getDisplay());
4260
4261 mWindowManager.handleDisplayAdded(mDisplayId);
4262 }
4263
4264 void setSurface(Surface surface) {
4265 if (mVirtualDisplay != null) {
4266 mVirtualDisplay.setSurface(surface);
4267 }
4268 }
4269
4270 @Override
4271 void detachActivitiesLocked(ActivityStack stack) {
4272 super.detachActivitiesLocked(stack);
4273 if (mVirtualDisplay != null) {
4274 mVirtualDisplay.release();
4275 mVirtualDisplay = null;
4276 }
4277 }
4278
4279 @Override
4280 public String toString() {
4281 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004282 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004283 }
Jose Lima58e66d62014-05-27 20:00:27 -07004284
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004285 /**
4286 * Adjust bounds to stay within stack bounds.
4287 *
4288 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4289 * that keep them unchanged, but be contained within the stack bounds.
4290 *
4291 * @param bounds Bounds to be adjusted.
4292 * @param stackBounds Bounds within which the other bounds should remain.
4293 */
4294 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4295 if (stackBounds == null || stackBounds.contains(bounds)) {
4296 return;
4297 }
4298
4299 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4300 final int maxRight = stackBounds.right
4301 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4302 int horizontalDiff = stackBounds.left - bounds.left;
4303 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4304 || (bounds.left + horizontalDiff >= maxRight)) {
4305 horizontalDiff = maxRight - bounds.left;
4306 }
4307 bounds.left += horizontalDiff;
4308 bounds.right += horizontalDiff;
4309 }
4310
4311 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4312 final int maxBottom = stackBounds.bottom
4313 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4314 int verticalDiff = stackBounds.top - bounds.top;
4315 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4316 || (bounds.top + verticalDiff >= maxBottom)) {
4317 verticalDiff = maxBottom - bounds.top;
4318 }
4319 bounds.top += verticalDiff;
4320 bounds.bottom += verticalDiff;
4321 }
4322 }
4323
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004324 ActivityStack findStackBehind(ActivityStack stack) {
4325 // TODO(multi-display): We are only looking for stacks on the default display.
4326 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4327 if (display == null) {
4328 return null;
4329 }
4330 final ArrayList<ActivityStack> stacks = display.mStacks;
4331 for (int i = stacks.size() - 1; i >= 0; i--) {
4332 if (stacks.get(i) == stack && i > 0) {
4333 return stacks.get(i - 1);
4334 }
4335 }
4336 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4337 + " in=" + stacks);
4338 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004339
Jorim Jaggic69bd222016-03-15 14:38:37 +01004340 /**
4341 * Puts a task into resizing mode during the next app transition.
4342 *
4343 * @param taskId the id of the task to put into resizing mode
4344 */
4345 private void setResizingDuringAnimation(int taskId) {
4346 mResizingTasksDuringAnimation.add(taskId);
4347 mWindowManager.setTaskDockedResizing(taskId, true);
4348 }
4349
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004350 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4351 final TaskRecord task;
4352 final int callingUid;
4353 final String callingPackage;
4354 final Intent intent;
4355 final int userId;
4356 final ActivityOptions activityOptions = (bOptions != null)
4357 ? new ActivityOptions(bOptions) : null;
4358 final int launchStackId = (activityOptions != null)
4359 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004360 if (launchStackId == HOME_STACK_ID) {
4361 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4362 + taskId + " can't be launch in the home stack.");
4363 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004364
4365 if (launchStackId == DOCKED_STACK_ID) {
4366 mWindowManager.setDockedStackCreateState(
4367 activityOptions.getDockCreateMode(), null /* initialBounds */);
4368
4369 // Defer updating the stack in which recents is until the app transition is done, to
4370 // not run into issues where we still need to draw the task in recents but the
4371 // docked stack is already created.
4372 deferUpdateBounds(HOME_STACK_ID);
4373 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4374 }
4375
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004376 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4377 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004378 continueUpdateBounds(HOME_STACK_ID);
4379 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004380 throw new IllegalArgumentException(
4381 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4382 }
4383
4384 if (launchStackId != INVALID_STACK_ID) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004385 if (task.stack.mStackId != launchStackId) {
4386 moveTaskToStackLocked(
4387 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4388 ANIMATE);
4389 }
4390 }
4391
4392 // If the user must confirm credentials (e.g. when first launching a work app and the
4393 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4394 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4395 && task.getRootActivity() != null) {
Jorim Jaggi09c49542016-04-09 01:39:40 -07004396 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004397 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
4398
4399 // If we are launching the task in the docked stack, put it into resizing mode so
4400 // the window renders full-screen with the background filling the void. Also only
4401 // call this at the end to make sure that tasks exists on the window manager side.
4402 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004403 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004404 }
4405 return ActivityManager.START_TASK_TO_FRONT;
4406 }
4407 callingUid = task.mCallingUid;
4408 callingPackage = task.mCallingPackage;
4409 intent = task.intent;
4410 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4411 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004412 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4413 null, null, 0, 0, bOptions, userId, null, task);
4414 if (launchStackId == DOCKED_STACK_ID) {
4415 setResizingDuringAnimation(task.taskId);
4416 }
4417 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004418 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004419
4420 /**
4421 * @return a list of activities which are the top ones in each visible stack. The first
4422 * entry will be the focused activity.
4423 */
4424 public List<IBinder> getTopVisibleActivities() {
4425 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4426 if (display == null) {
4427 return Collections.EMPTY_LIST;
4428 }
4429 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4430 final ArrayList<ActivityStack> stacks = display.mStacks;
4431 for (int i = stacks.size() - 1; i >= 0; i--) {
4432 ActivityStack stack = stacks.get(i);
4433 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4434 ActivityRecord top = stack.topActivity();
4435 if (top != null) {
4436 if (stack == mFocusedStack) {
4437 topActivityTokens.add(0, top.appToken);
4438 } else {
4439 topActivityTokens.add(top.appToken);
4440 }
4441 }
4442 }
4443 }
4444 return topActivityTokens;
4445 }
Craig Mautner27084302013-03-25 08:05:25 -07004446}