blob: e1fe0d2314fe94c02e094eb98712343b369c7b3c [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;
Jeff Hao1b012d32014-08-20 10:35:34 -070033import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070034import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040035import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040036import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070037import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070038import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070040import android.content.Intent;
41import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070043import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.content.pm.PackageManager;
45import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010046import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080048import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080049import android.hardware.display.DisplayManager;
50import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080051import android.hardware.display.DisplayManagerGlobal;
52import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080053import android.hardware.input.InputManager;
54import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010056import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070057import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070058import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070061import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070063import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070064import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070065import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040066import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070067import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070068import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070069import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070070import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +010071import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070072import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070073import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040074import android.provider.Settings;
75import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070076import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070077import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070078import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070079import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070080import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080081import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -080082import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080083import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080084import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080085import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080086import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -080087
Dianne Hackborn85d558c2014-11-04 10:31:54 -080088import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070089import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040090import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -070091import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -040092import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -080093import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -070094import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070095import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070096
Craig Mautner8d341ef2013-03-26 09:03:27 -070097import java.io.FileDescriptor;
98import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070099import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700100import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700101import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700102import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700103import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800104import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700105import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700106
Jorim Jaggife89d122015-12-22 16:28:44 +0100107import static android.Manifest.permission.START_ANY_ACTIVITY;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100108import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100109import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
110import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
111import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
112import static android.app.ActivityManager.RESIZE_MODE_FORCED;
113import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
Chong Zhang5022da32016-06-21 16:31:37 -0700114import static android.app.ActivityManager.START_TASK_TO_FRONT;
Jorim Jaggife89d122015-12-22 16:28:44 +0100115import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
116import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
117import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
118import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
119import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
120import static android.app.ActivityManager.StackId.HOME_STACK_ID;
121import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
122import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
123import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
124import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
125import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Jorim Jaggife89d122015-12-22 16:28:44 +0100126import static android.content.pm.PackageManager.PERMISSION_GRANTED;
127import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
140import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
141import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
151import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
152import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
153import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
154import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
155import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800156import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100157import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggid53f0922016-04-06 22:16:23 -0700158import static com.android.server.am.ActivityManagerService.NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100159import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100160import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
161import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
162import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
163import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
164import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
165import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
166import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
167import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
168import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
169import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
170import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700171import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Tony Mak853304c2016-04-18 15:17:41 +0100172import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Winsond46b7272016-04-20 11:54:27 -0700173import static com.android.server.am.ActivityStack.STACK_VISIBLE;
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;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700184 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700186 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700188 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700192 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800193 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800195
Craig Mautnerf3333272013-04-22 10:55:53 -0700196 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700198
Craig Mautner0eea92c2013-05-16 13:35:39 -0700199 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700201
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700202 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700204
Craig Mautner05d29032013-05-03 13:40:13 -0700205 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
206 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
207 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700208 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700209 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800210 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
211 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
212 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700213 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400214 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
215 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700216 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700217 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700218 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800219 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
220 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800221
Wale Ogunwale040b4702015-08-06 18:10:50 -0700222 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700223
Jason Monk62515be2014-05-21 16:06:19 -0400224 private static final String LOCK_TASK_TAG = "Lock-to-App";
225
Wale Ogunwale040b4702015-08-06 18:10:50 -0700226 // Used to indicate if an object (e.g. stack) that we are trying to get
227 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800228 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700230 // Used to indicate that windows of activities should be preserved during the resize.
231 static final boolean PRESERVE_WINDOWS = true;
232
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233 // Used to indicate if an object (e.g. task) should be moved/created
234 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700235 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236
237 // Used to indicate that an objects (e.g. task) removal from its container
238 // (e.g. stack) is due to it moving to another container.
239 static final boolean MOVING = true;
240
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700241 // Force the focus to change to the stack we are moving a task to..
242 static final boolean FORCE_FOCUS = true;
243
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700244 // Restore task from the saved recents if it can't be found in any live stack.
245 static final boolean RESTORE_FROM_RECENTS = true;
246
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700247 // Don't execute any calls to resume.
248 static final boolean DEFER_RESUME = true;
249
Svetoslav7008b512015-06-24 18:47:07 -0700250 // Activity actions an app cannot start if it uses a permission which is not granted.
251 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
252 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700253
Svetoslav7008b512015-06-24 18:47:07 -0700254 static {
255 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
256 Manifest.permission.CAMERA);
257 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
258 Manifest.permission.CAMERA);
259 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
260 Manifest.permission.CALL_PHONE);
261 }
262
Svet Ganov99b60432015-06-27 13:15:22 -0700263 /** Action restriction: launching the activity is not restricted. */
264 private static final int ACTIVITY_RESTRICTION_NONE = 0;
265 /** Action restriction: launching the activity is restricted by a permission. */
266 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
267 /** Action restriction: launching the activity is restricted by an app op. */
268 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700269
justinzhang5286d3f2014-05-12 17:06:01 -0400270 /** Status Bar Service **/
271 private IBinder mToken = new Binder();
272 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400273 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400274
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700275 // For debugging to make sure the caller when acquiring/releasing our
276 // wake lock is the system process.
277 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800278 /** The number of distinct task ids that can be assigned to the tasks of a single user */
279 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700280
Craig Mautner27084302013-03-25 08:05:25 -0700281 final ActivityManagerService mService;
282
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800283 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800284
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700285 final ActivityStackSupervisorHandler mHandler;
286
287 /** Short cut */
288 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800289 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700290
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700291 /** Counter for next free stack ID to use for dynamic activity stacks. */
292 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700293
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800294 /**
295 * Maps the task identifier that activities are currently being started in to the userId of the
296 * task. Each time a new task is created, the entry for the userId of the task is incremented
297 */
298 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700299
Craig Mautner2420ead2013-04-01 17:13:20 -0700300 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800301 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700302
Craig Mautnere0a38842013-12-16 16:14:02 -0800303 /** The stack containing the launcher app. Assumed to always be attached to
304 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800305 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700306
Craig Mautnere0a38842013-12-16 16:14:02 -0800307 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800308 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700309
Craig Mautner4a1cb222013-12-04 16:14:06 -0800310 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
311 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800313 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700314
315 /** List of activities that are waiting for a new activity to become visible before completing
316 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800317 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700318
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700319 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800320 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700321
322 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800323 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324
Craig Mautnerde4ef022013-04-07 19:01:33 -0700325 /** List of activities that are ready to be stopped, but waiting for the next activity to
326 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800327 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700328
Craig Mautnerf3333272013-04-22 10:55:53 -0700329 /** List of activities that are ready to be finished, but waiting for the previous activity to
330 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800331 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700332
Craig Mautner0eea92c2013-05-16 13:35:39 -0700333 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800334 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700335
Wale Ogunwale22e25262016-02-01 10:32:02 -0800336 /** List of activities whose multi-window mode changed that we need to report to the
337 * application */
338 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
339
340 /** List of activities whose picture-in-picture mode changed that we need to report to the
341 * application */
342 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
343
Craig Mautnerf3333272013-04-22 10:55:53 -0700344 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700345 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700346
Craig Mautnerde4ef022013-04-07 19:01:33 -0700347 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
348 * is being brought in front of us. */
349 boolean mUserLeaving = false;
350
Craig Mautner0eea92c2013-05-16 13:35:39 -0700351 /** Set when we have taken too long waiting to go to sleep. */
352 boolean mSleepTimeout = false;
353
354 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700355 * We don't want to allow the device to go to sleep while in the process
356 * of launching an activity. This is primarily to allow alarm intent
357 * receivers to launch an activity and get that to run before the device
358 * goes back to sleep.
359 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700360 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700361
362 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363 * Set when the system is going to sleep, until we have
364 * successfully paused the current activity and released our wake lock.
365 * At that point the system is allowed to actually sleep.
366 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700367 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700369 /** Stack id of the front stack when user switched, indexed by userId. */
370 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700371
Craig Mautner4504de52013-12-20 09:06:56 -0800372 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800373 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700374 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375
376 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700377 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378
Jeff Brownca9bc702014-02-11 14:32:56 -0800379 InputManagerInternal mInputManagerInternal;
380
Craig Mautner15df08a2015-04-01 12:17:18 -0700381 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
382 * may be finished until there is only one entry left. If this is empty the system is not
383 * in lockTask mode. */
384 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000385 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700386 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
387 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000388 */
389 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400390 /**
391 * Notifies the user when entering/exiting lock-task.
392 */
393 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800394
Wale Ogunwaled046a012015-12-24 13:05:59 -0800395 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800396 boolean inResumeTopActivity;
397
Andrii Kulian1e32e022016-09-16 15:29:34 -0700398 /**
399 * Temporary rect used during docked stack resize calculation so we don't need to create a new
400 * object each time.
401 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700402 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700403
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700404 // The default minimal size that will be used if the activity doesn't specify its minimal size.
405 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700406 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700407
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700408 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
409 private boolean mTaskLayersChanged = true;
410
Jorim Jaggi275561a2016-02-23 10:11:02 -0500411 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800412
Jorim Jaggidc249c42015-12-15 14:57:31 -0800413 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
414
Wale Ogunwale39381972015-12-17 17:15:29 -0800415 static class FindTaskResult {
416 ActivityRecord r;
417 boolean matchedByRootAffinity;
418 }
419 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
420
Craig Mautneree36c772014-07-16 14:56:05 -0700421 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100422 * Used to keep track whether app visibilities got changed since the last pause. Useful to
423 * determine whether to invoke the task stack change listener after pausing.
424 */
425 boolean mAppVisibilitiesChangedSinceLastPause;
426
427 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100428 * Set of tasks that are in resizing mode during an app transition to fill the "void".
429 */
430 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
431
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700432
433 /**
434 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
435 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
436 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
437 * like the docked stack going empty.
438 */
439 private boolean mAllowDockedStackResize = true;
440
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100441 /**
Tony Mak853304c2016-04-18 15:17:41 +0100442 * Is dock currently minimized.
443 */
444 boolean mIsDockMinimized;
445
446 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700447 * Description of a request to start a new activity, which has been held
448 * due to app switches being disabled.
449 */
450 static class PendingActivityLaunch {
451 final ActivityRecord r;
452 final ActivityRecord sourceRecord;
453 final int startFlags;
454 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700455 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700456
457 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700458 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700459 r = _r;
460 sourceRecord = _sourceRecord;
461 startFlags = _startFlags;
462 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700463 callerApp = _callerApp;
464 }
465
466 void sendErrorResult(String message) {
467 try {
468 if (callerApp.thread != null) {
469 callerApp.thread.scheduleCrash(message);
470 }
471 } catch (RemoteException e) {
472 Slog.e(TAG, "Exception scheduling crash of failed "
473 + "activity launcher sourceRecord=" + sourceRecord, e);
474 }
Craig Mautneree36c772014-07-16 14:56:05 -0700475 }
476 }
477
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800478 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700479 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700480 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800481 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800482 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700483 }
484
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800485 void setRecentTasks(RecentTasks recentTasks) {
486 mRecentTasks = recentTasks;
487 }
488
Jeff Brown2c43c332014-06-12 22:38:59 -0700489 /**
490 * At the time when the constructor runs, the power manager has not yet been
491 * initialized. So we initialize our wakelocks afterwards.
492 */
493 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800494 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700495 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700496 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700497 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700498 }
499
justinzhang5286d3f2014-05-12 17:06:01 -0400500 // This function returns a IStatusBarService. The value is from ServiceManager.
501 // getService and is cached.
502 private IStatusBarService getStatusBarService() {
503 synchronized (mService) {
504 if (mStatusBarService == null) {
505 mStatusBarService = IStatusBarService.Stub.asInterface(
506 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
507 if (mStatusBarService == null) {
508 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
509 }
510 }
511 return mStatusBarService;
512 }
513 }
514
Jason Monk35c62a42014-06-17 10:24:47 -0400515 private IDevicePolicyManager getDevicePolicyManager() {
516 synchronized (mService) {
517 if (mDevicePolicyManager == null) {
518 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
519 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
520 if (mDevicePolicyManager == null) {
521 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
522 }
523 }
524 return mDevicePolicyManager;
525 }
526 }
527
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700528 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800529 synchronized (mService) {
530 mWindowManager = wm;
531
532 mDisplayManager =
533 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
534 mDisplayManager.registerDisplayListener(this, null);
535
536 Display[] displays = mDisplayManager.getDisplays();
537 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
538 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800539 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700540 if (activityDisplay.mDisplay == null) {
541 throw new IllegalStateException("Default Display does not exist");
542 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800543 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700544 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 }
546
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800547 mHomeStack = mFocusedStack = mLastFocusedStack =
548 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800549
550 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800551 }
Craig Mautner27084302013-03-25 08:05:25 -0700552 }
553
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200554 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700555 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200556 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700557 }
558
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700559 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800560 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700561 }
562
Craig Mautnerde4ef022013-04-07 19:01:33 -0700563 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800564 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700565 }
566
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700567 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700568 if (stack == null) {
569 return false;
570 }
571
Craig Mautnerdf88d732014-01-27 09:21:32 -0800572 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
573 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700574 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800575 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800576 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700577 }
578
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700579 /** The top most stack. */
580 boolean isFrontStack(ActivityStack stack) {
581 if (stack == null) {
582 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800583 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700584
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700585 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
586 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700587 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800588 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700589 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
590 }
591
Wale Ogunwaled046a012015-12-24 13:05:59 -0800592 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800593 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
594 if (!focusCandidate.isFocusable()) {
595 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
596 focusCandidate = focusCandidate.getNextFocusableStackLocked();
597 }
598
599 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800600 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800601 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800602
Wale Ogunwaled046a012015-12-24 13:05:59 -0800603 EventLogTags.writeAmFocusedStack(
604 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
605 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
606 }
607
608 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800609 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800610 if (r != null && r.idle) {
611 checkFinishBootingLocked();
612 }
613 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700614 }
615
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700616 void moveHomeStackToFront(String reason) {
617 mHomeStack.moveToFront(reason);
618 }
619
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700620 /** Returns true if the focus activity was adjusted to the home stack top activity. */
621 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700622 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700623 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700624 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700625 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700626
Craig Mautner84984fa2014-06-19 11:19:20 -0700627 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700628
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700629 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700630 if (top == null) {
631 return false;
632 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700633 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700634 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700635 }
636
Craig Mautner299f9602015-01-26 09:47:33 -0800637 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700638 if (!mService.mBooting && !mService.mBooted) {
639 // Not ready yet!
640 return false;
641 }
642
Craig Mautner84984fa2014-06-19 11:19:20 -0700643 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700644 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700645 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700646 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700647
Craig Mautner84984fa2014-06-19 11:19:20 -0700648 if (prev != null) {
649 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
650 }
651
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700652 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
653 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800654 final String myReason = reason + " resumeHomeStackTask";
655
Mark Lua56ea122015-10-08 13:31:01 +0800656 // Only resume home activity if isn't finishing.
657 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700658 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800659 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700660 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800661 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700662 }
663
Craig Mautner8d341ef2013-03-26 09:03:27 -0700664 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700665 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700666 }
667
668 /**
669 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
670 * @param id Id of the task we would like returned.
671 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
672 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700673 * @param stackId The stack to restore the task to (default launch stack will be used if
674 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700675 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700676 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800677 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800679 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800680 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
681 ActivityStack stack = stacks.get(stackNdx);
682 TaskRecord task = stack.taskForIdLocked(id);
683 if (task != null) {
684 return task;
685 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700686 }
687 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800688
689 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700690 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800691 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800692 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700693 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800694 return null;
695 }
696
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700697 if (!restoreFromRecents) {
698 return task;
699 }
700
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700701 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700702 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
703 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800704 return null;
705 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700706 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800707 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700708 }
709
Craig Mautner6170f732013-04-02 13:05:23 -0700710 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800711 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800713 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800714 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
715 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
716 if (r != null) {
717 return r;
718 }
Craig Mautner6170f732013-04-02 13:05:23 -0700719 }
720 }
721 return null;
722 }
723
Tony Mak853304c2016-04-18 15:17:41 +0100724 /**
725 * TODO: Handle freefom mode.
726 * @return true when credential confirmation is needed for the user and there is any
727 * activity started by the user in any visible stack.
728 */
729 boolean isUserLockedProfile(@UserIdInt int userId) {
730 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
731 return false;
732 }
733 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
734 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
735 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
736 for (final ActivityStack activityStack : activityStacks) {
737 if (activityStack == null) {
738 continue;
739 }
740 if (activityStack.topRunningActivityLocked() == null) {
741 continue;
742 }
743 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
744 continue;
745 }
746 if (activityStack.isDockedStack() && mIsDockMinimized) {
747 continue;
748 }
749 final TaskRecord topTask = activityStack.topTask();
750 if (topTask == null) {
751 continue;
752 }
753 // To handle the case that work app is in the task but just is not the top one.
754 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
755 final ActivityRecord activityRecord = topTask.mActivities.get(i);
756 if (activityRecord.userId == userId) {
757 return true;
758 }
759 }
760 }
761 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000762 }
763
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800764 void setNextTaskIdForUserLocked(int taskId, int userId) {
765 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
766 if (taskId > currentTaskId) {
767 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700768 }
769 }
770
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700771 static int nextTaskIdForUser(int taskId, int userId) {
772 int nextTaskId = taskId + 1;
773 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
774 // Wrap around as there will be smaller task ids that are available now.
775 nextTaskId -= MAX_TASK_IDS_PER_USER;
776 }
777 return nextTaskId;
778 }
779
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800780 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800781 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
782 // for a userId u, a taskId can only be in the range
783 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
784 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700785 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800786 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
787 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
788 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700789 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800790 if (candidateTaskId == currentTaskId) {
791 // Something wrong!
792 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
793 throw new IllegalStateException("Cannot get an available task id."
794 + " Reached limit of " + MAX_TASK_IDS_PER_USER
795 + " running tasks per user.");
796 }
797 }
798 mCurTaskIdForUser.put(userId, candidateTaskId);
799 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700800 }
801
Chong Zhang6cda19c2016-06-14 19:07:56 -0700802 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800803 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700804 if (stack == null) {
805 return null;
806 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700807 ActivityRecord resumedActivity = stack.mResumedActivity;
808 if (resumedActivity == null || resumedActivity.app == null) {
809 resumedActivity = stack.mPausingActivity;
810 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700811 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700812 }
813 }
814 return resumedActivity;
815 }
816
Dianne Hackbornff072722014-09-24 10:56:28 -0700817 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700818 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800819 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800820 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
821 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800822 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
823 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700824 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800825 continue;
826 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700827 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 if (hr != null) {
829 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
830 && processName.equals(hr.processName)) {
831 try {
George Mount2c92c972014-03-20 09:38:23 -0700832 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 didSomething = true;
834 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700835 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800836 Slog.w(TAG, "Exception in new application when starting activity "
837 + hr.intent.getComponent().flattenToShortString(), e);
838 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700839 }
Craig Mautner20e72272013-04-01 13:45:53 -0700840 }
Craig Mautner20e72272013-04-01 13:45:53 -0700841 }
842 }
843 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700844 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700845 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700846 }
Craig Mautner20e72272013-04-01 13:45:53 -0700847 return didSomething;
848 }
849
850 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800851 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
852 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
854 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700855 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800856 continue;
857 }
858 final ActivityRecord resumedActivity = stack.mResumedActivity;
859 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700860 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800861 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800862 return false;
863 }
Craig Mautner20e72272013-04-01 13:45:53 -0700864 }
865 }
Wei Wang65c7a152016-06-02 18:51:22 -0700866 // Send launch end powerhint when idle
867 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700868 return true;
869 }
870
Craig Mautnerde4ef022013-04-07 19:01:33 -0700871 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800872 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
873 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800874 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
875 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700876 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800877 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700878 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800879 return false;
880 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700881 }
882 }
883 }
884 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700885 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
887 mLastFocusedStack + " to=" + mFocusedStack);
888 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700889 return true;
890 }
891
892 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800893 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800894 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
895 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800896 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
897 final ActivityStack stack = stacks.get(stackNdx);
898 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800899 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700900 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800901 return false;
902 }
903 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800904 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700905 }
906 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800907 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700908 }
909
Craig Mautner2acc3892013-09-23 10:28:14 -0700910 /**
911 * Pause all activities in either all of the stacks or just the back stacks.
912 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700913 * @param resuming The resuming activity.
914 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
915 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700916 * @return true if any activity was paused as a result of this call.
917 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700918 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700919 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800920 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
921 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800922 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
923 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700924 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700925 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700927 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
928 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700930 }
931 }
932 return someActivityPaused;
933 }
934
Craig Mautnerde4ef022013-04-07 19:01:33 -0700935 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700936 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800937 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
938 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800939 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
940 final ActivityStack stack = stacks.get(stackNdx);
941 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700942 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800943 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700944 Slog.d(TAG_STATES,
945 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800946 pausing = false;
947 } else {
948 return false;
949 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700950 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700951 }
952 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700953 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700954 }
955
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700956 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -0700957 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500958 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800959 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
960 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
961 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
962 final ActivityStack stack = stacks.get(stackNdx);
963 if (stack.mResumedActivity != null &&
964 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700965 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800966 }
967 }
968 }
969 }
970
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800971 void cancelInitializingActivities() {
972 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
973 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
974 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
975 stacks.get(stackNdx).cancelInitializingActivities();
976 }
977 }
978 }
979
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700980 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700981 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700982 }
983
984 void sendWaitingVisibleReportLocked(ActivityRecord r) {
985 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700986 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700987 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700988 if (w.who == null) {
989 changed = true;
990 w.timeout = false;
991 if (r != null) {
992 w.who = new ComponentName(r.info.packageName, r.info.name);
993 }
994 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
995 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700996 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700997 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700998 if (changed) {
999 mService.notifyAll();
1000 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001001 }
1002
Chong Zhang5022da32016-06-21 16:31:37 -07001003 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1004 boolean changed = false;
1005 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1006 WaitResult w = mWaitingActivityLaunched.remove(i);
1007 if (w.who == null) {
1008 changed = true;
1009 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1010 // the starting activity ends up moving another activity to front, and it should
1011 // wait for this new activity to become visible instead.
1012 // Do not modify other fields.
1013 w.result = START_TASK_TO_FRONT;
1014 }
1015 }
1016 if (changed) {
1017 mService.notifyAll();
1018 }
1019 }
1020
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001021 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1022 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001023 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001024 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001025 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001026 if (w.who == null) {
1027 changed = true;
1028 w.timeout = timeout;
1029 if (r != null) {
1030 w.who = new ComponentName(r.info.packageName, r.info.name);
1031 }
1032 w.thisTime = thisTime;
1033 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001034 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001035 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001036 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001037 if (changed) {
1038 mService.notifyAll();
1039 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 }
1041
Craig Mautner29219d92013-04-16 20:19:12 -07001042 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001043 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001044 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001045 if (r != null) {
1046 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001047 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001048
Andrii Kulian7318d632016-07-20 18:59:28 -07001049 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001050 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001051 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1052 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001053 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001054 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001055 if (r != null) {
1056 return r;
1057 }
1058 }
1059 }
1060 return null;
1061 }
1062
Dianne Hackborn09233282014-04-30 11:33:59 -07001063 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001064 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001065 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1066 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001067 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001068 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001069 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001070 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1071 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001072 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001073 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001074 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001075 }
1076 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001077
1078 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1079 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1080 while (maxNum > 0) {
1081 long mostRecentActiveTime = Long.MIN_VALUE;
1082 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001083 final int numTaskLists = runningTaskLists.size();
1084 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1085 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001086 if (!stackTaskList.isEmpty()) {
1087 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1088 if (lastActiveTime > mostRecentActiveTime) {
1089 mostRecentActiveTime = lastActiveTime;
1090 selectedStackList = stackTaskList;
1091 }
1092 }
1093 }
1094 if (selectedStackList != null) {
1095 list.add(selectedStackList.remove(0));
1096 --maxNum;
1097 } else {
1098 break;
1099 }
1100 }
Craig Mautner20e72272013-04-01 13:45:53 -07001101 }
1102
Todd Kennedy7440f172015-12-09 14:31:22 -08001103 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1104 ProfilerInfo profilerInfo) {
1105 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001106 if (aInfo != null) {
1107 // Store the found target back into the intent, because now that
1108 // we have it we never want to do this again. For example, if the
1109 // user navigates back to this point in the history, we should
1110 // always restart the exact same activity.
1111 intent.setComponent(new ComponentName(
1112 aInfo.applicationInfo.packageName, aInfo.name));
1113
1114 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001115 if (!aInfo.processName.equals("system")) {
1116 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001117 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001118 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001119
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001120 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1121 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1122 }
1123
Man Caocfa78b22015-06-11 20:14:34 -07001124 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1125 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1126 }
1127
1128 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001129 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001130 }
1131 }
1132 }
1133 return aInfo;
1134 }
1135
Todd Kennedy7440f172015-12-09 14:31:22 -08001136 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001137 return resolveIntent(intent, resolvedType, userId, 0);
1138 }
1139
1140 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001141 try {
1142 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001143 PackageManager.MATCH_DEFAULT_ONLY | flags
1144 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001145 } catch (RemoteException e) {
1146 }
1147 return null;
1148 }
1149
1150 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1151 ProfilerInfo profilerInfo, int userId) {
1152 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1153 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1154 }
1155
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001156 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1157 boolean andResume, boolean checkConfig) throws RemoteException {
1158
1159 if (!allPausedActivitiesComplete()) {
1160 // While there are activities pausing we skipping starting any new activities until
1161 // pauses are complete. NOTE: that we also do this for activities that are starting in
1162 // the paused state because they will first be resumed then paused on the client side.
1163 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1164 "realStartActivityLocked: Skipping start of r=" + r
1165 + " some activities pausing...");
1166 return false;
1167 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001168
Craig Mautner2568c3a2015-03-26 14:22:34 -07001169 if (andResume) {
1170 r.startFreezingScreenLocked(app, 0);
1171 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001172
Craig Mautner2568c3a2015-03-26 14:22:34 -07001173 // schedule launch ticks to collect information about slow apps.
1174 r.startLaunchTickingLocked();
1175 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001176
1177 // Have the window manager re-evaluate the orientation of
1178 // the screen based on the new activity order. Note that
1179 // as a result of this, it can call back into the activity
1180 // manager with a new orientation. We don't care about that,
1181 // because the activity is not currently running so we are
1182 // just restarting it anyway.
1183 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001184 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Andrii Kulian8072d112016-09-16 11:11:01 -07001185 mService.mGlobalConfiguration,
Craig Mautner2420ead2013-04-01 17:13:20 -07001186 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001187 // Deferring resume here because we're going to launch new activity shortly.
1188 // We don't want to perform a redundant launch of the same record while ensuring
1189 // configurations and trying to resume top activity of focused stack.
1190 mService.updateConfigurationLocked(config, r, false, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -07001191 }
1192
1193 r.app = app;
1194 app.waitingToKill = null;
1195 r.launchCount++;
1196 r.lastLaunchTime = SystemClock.uptimeMillis();
1197
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001198 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001199
1200 int idx = app.activities.indexOf(r);
1201 if (idx < 0) {
1202 app.activities.add(r);
1203 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001204 mService.updateLruProcessLocked(app, true, null);
1205 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001206
Craig Mautner15df08a2015-04-01 12:17:18 -07001207 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001208 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1209 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001210 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001211 }
1212
Andrii Kulian02b7a832016-10-06 23:11:56 -07001213 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001214 try {
1215 if (app.thread == null) {
1216 throw new RemoteException();
1217 }
1218 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001219 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001220 if (andResume) {
1221 results = r.results;
1222 newIntents = r.newIntents;
1223 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001224 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1225 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1226 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001227 if (andResume) {
1228 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1229 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001230 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001231 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001232 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001233 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001234 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001235 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001236 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1237 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001238 r.sleeping = false;
1239 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001240 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001241 mService.showAskCompatModeDialogLocked(r);
1242 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001243 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001244 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1245 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1246 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001247 final String profileFile = mService.mProfileFile;
1248 if (profileFile != null) {
1249 ParcelFileDescriptor profileFd = mService.mProfileFd;
1250 if (profileFd != null) {
1251 try {
1252 profileFd = profileFd.dup();
1253 } catch (IOException e) {
1254 if (profileFd != null) {
1255 try {
1256 profileFd.close();
1257 } catch (IOException o) {
1258 }
1259 profileFd = null;
1260 }
1261 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001263
1264 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1265 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001266 }
1267 }
1268 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001269
Craig Mautner2568c3a2015-03-26 14:22:34 -07001270 if (andResume) {
1271 app.hasShownUi = true;
1272 app.pendingUiClean = true;
1273 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001274 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001276 System.identityHashCode(r), r.info,
1277 new Configuration(mService.mGlobalConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001278 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001279 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001280 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001281
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001282 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001283 // This may be a heavy-weight process! Note that the package
1284 // manager will ensure that only activity can run in the main
1285 // process of the .apk, which is the only thing that will be
1286 // considered heavy-weight.
1287 if (app.processName.equals(app.info.packageName)) {
1288 if (mService.mHeavyWeightProcess != null
1289 && mService.mHeavyWeightProcess != app) {
1290 Slog.w(TAG, "Starting new heavy weight process " + app
1291 + " when already running "
1292 + mService.mHeavyWeightProcess);
1293 }
1294 mService.mHeavyWeightProcess = app;
1295 Message msg = mService.mHandler.obtainMessage(
1296 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1297 msg.obj = r;
1298 mService.mHandler.sendMessage(msg);
1299 }
1300 }
1301
1302 } catch (RemoteException e) {
1303 if (r.launchFailed) {
1304 // This is the second time we failed -- finish activity
1305 // and give up.
1306 Slog.e(TAG, "Second failure launching "
1307 + r.intent.getComponent().flattenToShortString()
1308 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001309 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001310 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1311 "2nd-crash", false);
1312 return false;
1313 }
1314
1315 // This is the first time we failed -- restart process and
1316 // retry.
1317 app.activities.remove(r);
1318 throw e;
1319 }
1320
1321 r.launchFailed = false;
1322 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001323 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 }
1325
1326 if (andResume) {
1327 // As part of the process of launching, ActivityThread also performs
1328 // a resume.
1329 stack.minimalResumeActivityLocked(r);
1330 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001331 // This activity is not starting in the resumed state... which should look like we asked
1332 // it to pause+stop (but remain visible), and it has done so and reported back the
1333 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001334 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001335 "Moving to PAUSED: " + r + " (starting in paused state)");
1336 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001337 }
1338
1339 // Launch the new version setup screen if needed. We do this -after-
1340 // launching the initial activity (that is, home), so that it can have
1341 // a chance to initialize itself while in the background, making the
1342 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001343 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001344 mService.startSetupActivityLocked();
1345 }
1346
Dianne Hackborn465fa392014-09-14 14:21:18 -07001347 // Update any services we are bound to that might care about whether
1348 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001349 if (r.app != null) {
1350 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1351 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001352
Craig Mautner2420ead2013-04-01 17:13:20 -07001353 return true;
1354 }
1355
Craig Mautnere79d42682013-04-01 19:01:53 -07001356 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001357 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001358 // Is this activity's application already running?
1359 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001360 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001361
Andrii Kulian02b7a832016-10-06 23:11:56 -07001362 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001363
1364 if (app != null && app.thread != null) {
1365 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001366 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1367 || !"android".equals(r.info.packageName)) {
1368 // Don't add this if it is a platform component that is marked
1369 // to run in multiple processes, because this is actually
1370 // part of the framework so doesn't make sense to track as a
1371 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001372 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1373 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001374 }
George Mount2c92c972014-03-20 09:38:23 -07001375 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001376 return;
1377 } catch (RemoteException e) {
1378 Slog.w(TAG, "Exception when starting activity "
1379 + r.intent.getComponent().flattenToShortString(), e);
1380 }
1381
1382 // If a dead object exception was thrown -- fall through to
1383 // restart the application.
1384 }
1385
1386 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001387 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001388 }
1389
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001390 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001391 String resultWho, int requestCode, int callingPid, int callingUid,
1392 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001393 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001394 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1395 callingUid);
1396 if (startAnyPerm == PERMISSION_GRANTED) {
1397 return true;
1398 }
1399 final int componentRestriction = getComponentRestrictionForCallingPackage(
1400 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1401 final int actionRestriction = getActionRestrictionForCallingPackage(
1402 intent.getAction(), callingPackage, callingPid, callingUid);
1403 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1404 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1405 if (resultRecord != null) {
1406 resultStack.sendActivityResultLocked(-1,
1407 resultRecord, resultWho, requestCode,
1408 Activity.RESULT_CANCELED, null);
1409 }
1410 final String msg;
1411 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1412 msg = "Permission Denial: starting " + intent.toString()
1413 + " from " + callerApp + " (pid=" + callingPid
1414 + ", uid=" + callingUid + ")" + " with revoked permission "
1415 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1416 } else if (!aInfo.exported) {
1417 msg = "Permission Denial: starting " + intent.toString()
1418 + " from " + callerApp + " (pid=" + callingPid
1419 + ", uid=" + callingUid + ")"
1420 + " not exported from uid " + aInfo.applicationInfo.uid;
1421 } else {
1422 msg = "Permission Denial: starting " + intent.toString()
1423 + " from " + callerApp + " (pid=" + callingPid
1424 + ", uid=" + callingUid + ")"
1425 + " requires " + aInfo.permission;
1426 }
1427 Slog.w(TAG, msg);
1428 throw new SecurityException(msg);
1429 }
1430
1431 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1432 final String message = "Appop Denial: starting " + intent.toString()
1433 + " from " + callerApp + " (pid=" + callingPid
1434 + ", uid=" + callingUid + ")"
1435 + " requires " + AppOpsManager.permissionToOp(
1436 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1437 Slog.w(TAG, message);
1438 return false;
1439 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1440 final String message = "Appop Denial: starting " + intent.toString()
1441 + " from " + callerApp + " (pid=" + callingPid
1442 + ", uid=" + callingUid + ")"
1443 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1444 Slog.w(TAG, message);
1445 return false;
1446 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001447 if (options != null && options.getLaunchTaskId() != -1) {
1448 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1449 callingPid, callingUid);
1450 if (startInTaskPerm != PERMISSION_GRANTED) {
1451 final String msg = "Permission Denial: starting " + intent.toString()
1452 + " from " + callerApp + " (pid=" + callingPid
1453 + ", uid=" + callingUid + ") with launchTaskId="
1454 + options.getLaunchTaskId();
1455 Slog.w(TAG, msg);
1456 throw new SecurityException(msg);
1457 }
1458 }
1459
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001460 return true;
1461 }
1462
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001463 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001464 final long identity = Binder.clearCallingIdentity();
1465 try {
1466 return UserManager.get(mService.mContext).getUserInfo(userId);
1467 } finally {
1468 Binder.restoreCallingIdentity(identity);
1469 }
1470 }
1471
Svet Ganov99b60432015-06-27 13:15:22 -07001472 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001473 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001474 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1475 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001476 == PackageManager.PERMISSION_DENIED) {
1477 return ACTIVITY_RESTRICTION_PERMISSION;
1478 }
1479
Christopher Tateff7add02015-08-17 10:23:22 -07001480 if (activityInfo.permission == null) {
1481 return ACTIVITY_RESTRICTION_NONE;
1482 }
1483
Svet Ganov99b60432015-06-27 13:15:22 -07001484 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1485 if (opCode == AppOpsManager.OP_NONE) {
1486 return ACTIVITY_RESTRICTION_NONE;
1487 }
1488
1489 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1490 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001491 if (!ignoreTargetSecurity) {
1492 return ACTIVITY_RESTRICTION_APPOP;
1493 }
Svet Ganov99b60432015-06-27 13:15:22 -07001494 }
1495
1496 return ACTIVITY_RESTRICTION_NONE;
1497 }
1498
Svetoslav7008b512015-06-24 18:47:07 -07001499 private int getActionRestrictionForCallingPackage(String action,
1500 String callingPackage, int callingPid, int callingUid) {
1501 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001502 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001503 }
1504
1505 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1506 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001507 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001508 }
1509
1510 final PackageInfo packageInfo;
1511 try {
1512 packageInfo = mService.mContext.getPackageManager()
1513 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1514 } catch (PackageManager.NameNotFoundException e) {
1515 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001516 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001517 }
1518
1519 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001520 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001521 }
1522
1523 if (mService.checkPermission(permission, callingPid, callingUid) ==
1524 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001525 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001526 }
1527
1528 final int opCode = AppOpsManager.permissionToOpCode(permission);
1529 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001530 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001531 }
1532
1533 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1534 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001535 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001536 }
1537
Svet Ganov99b60432015-06-27 13:15:22 -07001538 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001539 }
1540
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001541 void setLaunchSource(int uid) {
1542 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1543 }
1544
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001545 void acquireLaunchWakelock() {
1546 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1547 throw new IllegalStateException("Calling must be system uid");
1548 }
1549 mLaunchingActivity.acquire();
1550 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1551 // To be safe, don't allow the wake lock to be held for too long.
1552 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1553 }
1554 }
1555
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001556 /**
1557 * Called when the frontmost task is idle.
1558 * @return the state of mService.mBooting before this was called.
1559 */
1560 private boolean checkFinishBootingLocked() {
1561 final boolean booting = mService.mBooting;
1562 boolean enableScreen = false;
1563 mService.mBooting = false;
1564 if (!mService.mBooted) {
1565 mService.mBooted = true;
1566 enableScreen = true;
1567 }
1568 if (booting || enableScreen) {
1569 mService.postFinishBooting(booting, enableScreen);
1570 }
1571 return booting;
1572 }
1573
Craig Mautnerf3333272013-04-22 10:55:53 -07001574 // Checked.
1575 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1576 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001577 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001578
Craig Mautnerf3333272013-04-22 10:55:53 -07001579 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001580 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001581 int NS = 0;
1582 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001583 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001584 boolean activityRemoved = false;
1585
Wale Ogunwale7d701172015-03-11 15:36:30 -07001586 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001587 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001588 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1589 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001590 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1591 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001592 if (fromTimeout) {
1593 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001594 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001595
1596 // This is a hack to semi-deal with a race condition
1597 // in the client where it can be constructed with a
1598 // newer configuration from when we asked it to launch.
1599 // We'll update with whatever configuration it now says
1600 // it used to launch.
1601 if (config != null) {
1602 r.configuration = config;
1603 }
1604
1605 // We are now idle. If someone is waiting for a thumbnail from
1606 // us, we can now deliver.
1607 r.idle = true;
1608
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001609 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001610 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001611 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001612 }
1613 }
1614
1615 if (allResumedActivitiesIdle()) {
1616 if (r != null) {
1617 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001618 }
1619
1620 if (mLaunchingActivity.isHeld()) {
1621 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1622 if (VALIDATE_WAKE_LOCK_CALLER &&
1623 Binder.getCallingUid() != Process.myUid()) {
1624 throw new IllegalStateException("Calling must be system uid");
1625 }
1626 mLaunchingActivity.release();
1627 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001628 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001629 }
1630
1631 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001632 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001633 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001634 if ((NF = mFinishingActivities.size()) > 0) {
1635 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001636 mFinishingActivities.clear();
1637 }
1638
Craig Mautnerf3333272013-04-22 10:55:53 -07001639 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001640 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001641 mStartingUsers.clear();
1642 }
1643
Craig Mautnerf3333272013-04-22 10:55:53 -07001644 // Stop any activities that are scheduled to do so but have been
1645 // waiting for the next one to start.
1646 for (int i = 0; i < NS; i++) {
1647 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001648 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001649 if (stack != null) {
1650 if (r.finishing) {
1651 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1652 } else {
1653 stack.stopActivityLocked(r);
1654 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001655 }
1656 }
1657
1658 // Finish any activities that are scheduled to do so but have been
1659 // waiting for the next one to start.
1660 for (int i = 0; i < NF; i++) {
1661 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001662 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001663 if (stack != null) {
1664 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1665 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001666 }
1667
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001668 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001669 // Complete user switch
1670 if (startingUsers != null) {
1671 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001672 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001673 }
1674 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001675 }
1676
1677 mService.trimApplications();
1678 //dump();
1679 //mWindowManager.dump();
1680
Craig Mautnerf3333272013-04-22 10:55:53 -07001681 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001682 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001683 }
1684
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001685 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001686 }
1687
Craig Mautner8e569572013-10-11 17:36:59 -07001688 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001689 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001690 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1691 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001692 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1693 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1694 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001695 }
Craig Mautner19091252013-10-05 00:03:53 -07001696 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001697 }
1698
1699 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001700 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1701 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001702 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1703 stacks.get(stackNdx).closeSystemDialogsLocked();
1704 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001705 }
1706 }
1707
Craig Mautner93529a42013-10-04 15:03:13 -07001708 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001709 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001710 }
1711
Craig Mautner8d341ef2013-03-26 09:03:27 -07001712 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001713 * Update the last used stack id for non-current user (current user's last
1714 * used stack is the focused stack)
1715 */
1716 void updateUserStackLocked(int userId, ActivityStack stack) {
1717 if (userId != mCurrentUser) {
1718 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1719 }
1720 }
1721
1722 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001723 * @return true if some activity was finished (or would have finished if doit were true).
1724 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001725 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1726 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001727 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001728 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1729 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001730 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001731 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001732 if (stack.finishDisabledPackageActivitiesLocked(
1733 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001734 didSomething = true;
1735 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001736 }
1737 }
1738 return didSomething;
1739 }
1740
Dianne Hackborna413dc02013-07-12 12:02:55 -07001741 void updatePreviousProcessLocked(ActivityRecord r) {
1742 // Now that this process has stopped, we may want to consider
1743 // it to be the previous app to try to keep around in case
1744 // the user wants to return to it.
1745
1746 // First, found out what is currently the foreground app, so that
1747 // we don't blow away the previous app if this activity is being
1748 // hosted by the process that is actually still the foreground.
1749 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001750 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1751 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001752 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1753 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001754 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001755 if (stack.mResumedActivity != null) {
1756 fgApp = stack.mResumedActivity.app;
1757 } else if (stack.mPausingActivity != null) {
1758 fgApp = stack.mPausingActivity.app;
1759 }
1760 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001761 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001762 }
1763 }
1764
1765 // Now set this one as the previous process, only if that really
1766 // makes sense to.
1767 if (r.app != null && fgApp != null && r.app != fgApp
1768 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001769 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001770 mService.mPreviousProcess = r.app;
1771 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1772 }
1773 }
1774
Wale Ogunwaled046a012015-12-24 13:05:59 -08001775 boolean resumeFocusedStackTopActivityLocked() {
1776 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001777 }
1778
Wale Ogunwaled046a012015-12-24 13:05:59 -08001779 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001780 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001781 if (targetStack != null && isFocusedStack(targetStack)) {
1782 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001783 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001784 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1785 if (r == null || r.state != RESUMED) {
1786 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1787 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001788 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001789 }
1790
Todd Kennedy39bfee52016-02-24 10:28:21 -08001791 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1792 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1793 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1794 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1795 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1796 }
1797 }
1798 }
1799
Adrian Roos20d7df32016-01-12 18:59:43 +01001800 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1801 TaskRecord finishedTask = null;
1802 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001803 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1804 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001805 final int numStacks = stacks.size();
1806 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1807 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001808 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1809 if (stack == focusedStack || finishedTask == null) {
1810 finishedTask = t;
1811 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001812 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001813 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001814 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001815 }
1816
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001817 void finishVoiceTask(IVoiceInteractionSession session) {
1818 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1819 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1820 final int numStacks = stacks.size();
1821 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1822 final ActivityStack stack = stacks.get(stackNdx);
1823 stack.finishVoiceTask(session);
1824 }
1825 }
1826 }
1827
Andrii Kulianc27916642016-04-12 17:59:27 -07001828 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1829 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001830 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1831 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001832 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001833 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1834 // Caller wants the home activity moved with it. To accomplish this,
1835 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001836 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001837 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001838 ActivityStack currentStack = task.getStack();
1839 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001840 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1841 + task + " to front. Stack is null");
1842 return;
1843 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001844
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001845 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001846 int stackId = options.getLaunchStackId();
1847 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001848 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001849 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001850 if (stackId == INVALID_STACK_ID) {
1851 stackId = task.getLaunchStackId();
1852 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001853 if (stackId != currentStack.mStackId) {
1854 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
1855 !FORCE_FOCUS, reason);
1856 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001857 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1858 // still need moveTaskToFrontLocked() below for any transition settings.
1859 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001860 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1861 resizeStackLocked(stackId, bounds,
1862 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001863 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001864 } else {
1865 // WM resizeTask must be done after the task is moved to the correct stack,
1866 // because Task's setBounds() also updates dim layer's bounds, but that has
1867 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001868 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001869 false /* relayout */, false /* forced */);
1870 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001871 }
1872 }
1873
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001874 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001875 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001876 r == null ? null : r.appTimeTracker, reason);
1877
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001878 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001879 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001880
Andrii Kulian02b7a832016-10-06 23:11:56 -07001881 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07001882 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001883 }
1884
Wale Ogunwale854809c2015-12-27 16:18:19 -08001885 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001886 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001887 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001888 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001889 return false;
1890 }
1891 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1892 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001893 }
1894
Craig Mautner967212c2013-04-13 21:10:58 -07001895 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001896 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001897 }
1898
1899 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001900 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1901 if (activityContainer != null) {
1902 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001903 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001904 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001905 return null;
1906 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07001907 // TODO(multi-display): Allow creating stacks on secondary displays.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001908 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001909 }
1910
Craig Mautner967212c2013-04-13 21:10:58 -07001911 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001912 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001913 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1914 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001915 }
1916 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001917 }
1918
Craig Mautneree2e45a2014-06-27 12:10:03 -07001919 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001920 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001921 }
1922
1923 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001924 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1925 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1926 final TaskRecord task = tasks.get(taskNdx);
1927 if (task.isHomeTask()) {
1928 final ArrayList<ActivityRecord> activities = task.mActivities;
1929 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1930 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001931 if (r.isHomeActivity()
1932 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001933 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001934 }
1935 }
1936 }
1937 }
1938 return null;
1939 }
1940
Chong Zhangb15758a2015-11-17 12:12:03 -08001941 /**
1942 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1943 * the docked stack itself, or if it's side-by-side to the docked stack.
1944 */
1945 boolean isStackDockedInEffect(int stackId) {
1946 return stackId == DOCKED_STACK_ID ||
1947 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1948 }
1949
Todd Kennedyca4d8422015-01-15 15:19:22 -08001950 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001951 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001952 ActivityContainer activityContainer =
1953 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001954 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001955 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1956 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001957 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001958 return activityContainer;
1959 }
1960
Craig Mautner34b73df2014-01-12 21:11:08 -08001961 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001962 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1963 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1964 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001965 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1966 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001967 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001968 }
1969 }
1970
Craig Mautner95da1082014-02-24 17:54:35 -08001971 void deleteActivityContainer(IActivityContainer container) {
1972 ActivityContainer activityContainer = (ActivityContainer)container;
1973 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001974 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1975 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001976 final int stackId = activityContainer.mStackId;
1977 mActivityContainers.remove(stackId);
1978 mWindowManager.removeStack(stackId);
1979 }
1980 }
1981
Jorim Jaggidc249c42015-12-15 14:57:31 -08001982 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001983 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001984 if (stackId == DOCKED_STACK_ID) {
1985 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001986 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001987 return;
1988 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001989 final ActivityStack stack = getStack(stackId);
1990 if (stack == null) {
1991 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1992 return;
1993 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001994
Jorim Jaggidc249c42015-12-15 14:57:31 -08001995 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001996 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1997 // stack size changing so things don't get out of sync.
1998 return;
1999 }
2000
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002001 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002002 mWindowManager.deferSurfaceLayout();
2003 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002004 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002005 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002006 stack.ensureVisibleActivitiesConfigurationLocked(
2007 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002008 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002009 } finally {
2010 mWindowManager.continueSurfaceLayout();
2011 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002012 }
2013 }
2014
Jorim Jaggi192086e2016-03-11 17:17:03 +01002015 void deferUpdateBounds(int stackId) {
2016 final ActivityStack stack = getStack(stackId);
2017 if (stack != null) {
2018 stack.deferUpdateBounds();
2019 }
2020 }
2021
2022 void continueUpdateBounds(int stackId) {
2023 final ActivityStack stack = getStack(stackId);
2024 if (stack != null) {
2025 stack.continueUpdateBounds();
2026 }
2027 }
2028
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002029 void notifyAppTransitionDone() {
2030 continueUpdateBounds(HOME_STACK_ID);
2031 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2032 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002033 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002034 mWindowManager.setTaskDockedResizing(taskId, false);
2035 }
2036 }
2037 mResizingTasksDuringAnimation.clear();
2038 }
2039
Jorim Jaggi192086e2016-03-11 17:17:03 +01002040 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002041 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002042 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002043
Jorim Jaggi192086e2016-03-11 17:17:03 +01002044 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2045 return;
2046 }
2047
Andrii Kulian1e32e022016-09-16 15:29:34 -07002048 stack.updateOverrideConfiguration(bounds, tempTaskBounds, tempTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002049 }
2050
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002051 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2052 final ActivityStack stack = getStack(fromStackId);
2053 if (stack == null) {
2054 return;
2055 }
2056
2057 mWindowManager.deferSurfaceLayout();
2058 try {
2059 if (fromStackId == DOCKED_STACK_ID) {
2060
2061 // We are moving all tasks from the docked stack to the fullscreen stack,
2062 // which is dismissing the docked stack, so resize all other stacks to
2063 // fullscreen here already so we don't end up with resize trashing.
2064 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2065 if (StackId.isResizeableByDockedStack(i)) {
2066 ActivityStack otherStack = getStack(i);
2067 if (otherStack != null) {
2068 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2069 true /* allowResizeInDockedMode */, DEFER_RESUME);
2070 }
2071 }
2072 }
2073
2074 // Also disable docked stack resizing since we have manually adjusted the
2075 // size of other stacks above and we don't want to trigger a docked stack
2076 // resize when we remove task from it below and it is detached from the
2077 // display because it no longer contains any tasks.
2078 mAllowDockedStackResize = false;
2079 }
2080 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2081 final int size = tasks.size();
2082 if (onTop) {
2083 for (int i = 0; i < size; i++) {
2084 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002085 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002086 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002087 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002088
2089 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2090 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002091 } else {
2092 for (int i = size - 1; i >= 0; i--) {
2093 positionTaskInStackLocked(tasks.get(i).taskId,
2094 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2095 }
2096 }
2097 } finally {
2098 mAllowDockedStackResize = true;
2099 mWindowManager.continueSurfaceLayout();
2100 }
2101 }
2102
Jorim Jaggidc249c42015-12-15 14:57:31 -08002103 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002104 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2105 boolean preserveWindows) {
2106 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2107 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2108 false /* deferResume */);
2109 }
2110
2111 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2112 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2113 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002114
2115 if (!mAllowDockedStackResize) {
2116 // Docked stack resize currently disabled.
2117 return;
2118 }
2119
Jorim Jaggidc249c42015-12-15 14:57:31 -08002120 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2121 if (stack == null) {
2122 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2123 return;
2124 }
2125
2126 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2127 mWindowManager.deferSurfaceLayout();
2128 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002129 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2130 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002131 ActivityRecord r = stack.topRunningActivityLocked();
2132 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2133 tempDockedTaskInsetBounds);
2134
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002135 // TODO: Checking for isAttached might not be needed as if the user passes in null
2136 // dockedBounds then they want the docked stack to be dismissed.
2137 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2138 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002139 // In this case we make all other static stacks fullscreen and move all
2140 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002141 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002142
2143 // stack shouldn't contain anymore activities, so nothing to resume.
2144 r = null;
2145 } else {
2146 // Docked stacks occupy a dedicated region on screen so the size of all other
2147 // static stacks need to be adjusted so they don't overlap with the docked stack.
2148 // We get the bounds to use from window manager which has been adjusted for any
2149 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002150 mWindowManager.getStackDockedModeBounds(
2151 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002152 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002153 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2154 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2155 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002156 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002157 }
2158 }
2159 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002160 if (!deferResume) {
2161 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2162 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002163 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002164 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002165 mWindowManager.continueSurfaceLayout();
2166 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2167 }
2168
2169 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2170 tempDockedTaskBounds != null
2171 || tempDockedTaskInsetBounds != null
2172 || tempOtherTaskBounds != null
2173 || tempOtherTaskInsetBounds != null);
2174 }
2175
Robert Carr0d00c2e2016-02-29 17:45:02 -08002176 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2177 final ActivityStack stack = getStack(PINNED_STACK_ID);
2178 if (stack == null) {
2179 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2180 return;
2181 }
2182 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2183 mWindowManager.deferSurfaceLayout();
2184 try {
2185 ActivityRecord r = stack.topRunningActivityLocked();
2186 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2187 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002188 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002189 } finally {
2190 mWindowManager.continueSurfaceLayout();
2191 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2192 }
2193 }
2194
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002195 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2196 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002197 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002198 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002199 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002200 }
2201
Chong Zhang87b21722015-09-21 15:39:51 -07002202 // If this is a forced resize, let it go through even if the bounds is not changing,
2203 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002204 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002205 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002206 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002207 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002208 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002209 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002210
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002211 if (!mWindowManager.isValidTaskId(task.taskId)) {
2212 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002213 // All we can do for now is update the bounds so it can be used when the task is
2214 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002215 task.updateOverrideConfiguration(bounds);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002216 if (task.getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002217 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002218 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002219 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002220 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002221 }
2222
Chong Zhang6de2ae82015-09-30 18:25:21 -07002223 // Do not move the task to another stack here.
2224 // This method assumes that the task is already placed in the right stack.
2225 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002226
Chong Zhang6de2ae82015-09-30 18:25:21 -07002227 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002228
Andrii Kulian8072d112016-09-16 11:11:01 -07002229 final boolean updatedConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002230 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002231 // way and the activity was kept the way it was. If it's false, it means the activity had
2232 // to be relaunched due to configuration change.
2233 boolean kept = true;
Andrii Kulian8072d112016-09-16 11:11:01 -07002234 if (updatedConfig) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002235 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002236 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002237 final ActivityStack stack = r.getStack();
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002238 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002239
2240 if (!deferResume) {
2241
2242 // All other activities must be made visible with their correct configuration.
2243 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2244 if (!kept) {
2245 resumeFocusedStackTopActivityLocked();
2246 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002247 }
2248 }
2249 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002250 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002251
2252 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002253 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002254 }
2255
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002256 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002257 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2258 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002259 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002260 }
2261
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002262 ActivityContainer activityContainer = new ActivityContainer(stackId);
2263 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002264 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002265 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002266 }
2267
2268 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002269 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002270 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2271 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002272 break;
2273 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002274 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002275 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002276 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002277 }
2278
Chong Zhang5dcb2752015-08-18 13:50:26 -07002279 /**
2280 * Restores a recent task to a stack
2281 * @param task The recent task to be restored.
2282 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002283 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002284 * @return true if the task has been restored successfully.
2285 */
2286 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2287 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002288 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002289 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2290 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2291 // Put it in the fullscreen stack.
2292 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002293 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002294
Andrii Kulian02b7a832016-10-06 23:11:56 -07002295 final ActivityStack currentStack = task.getStack();
2296 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002297 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002298 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002299 // Nothing else to do since it is already restored in the right stack.
2300 return true;
2301 }
2302 // Remove current stack association, so we can re-associate the task with the
2303 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002304 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002305 }
2306
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002307 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002308 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002309
2310 if (stack == null) {
2311 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002312 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2313 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002314 return false;
2315 }
2316
Wale Ogunwale5f986092015-12-04 15:35:38 -08002317 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002318 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2319 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002320 final ArrayList<ActivityRecord> activities = task.mActivities;
2321 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002322 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002323 }
2324 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002325 }
2326
Wale Ogunwale040b4702015-08-06 18:10:50 -07002327 /**
2328 * Moves the specified task record to the input stack id.
2329 * WARNING: This method performs an unchecked/raw move of the task and
2330 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002331 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002332 * @param task Task to move.
2333 * @param stackId Id of stack to move task to.
2334 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002335 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002336 * @param reason Reason the task is been moved.
2337 * @return The stack the task was moved to.
2338 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002339 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002340 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002341
2342 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2343 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2344 + "support multi-window task=" + task + " to stackId=" + stackId);
2345 }
2346
Wale Ogunwale06579d62016-04-30 15:29:06 -07002347 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002348 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002349 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002350 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002351 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2352 // Whenever we are moving the top activity from the front stack we want to make sure to move
2353 // the stack to the front.
2354 final boolean wasFront = isFrontStack(prevStack)
2355 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002356
Chong Zhangd545dce2016-02-29 18:09:17 -08002357 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002358 // We don't allow moving a unresizeable task to the docked stack since the docked
2359 // stack is used for split-screen mode and will cause things like the docked divider to
2360 // show up. We instead leave the task in its current stack or move it to the fullscreen
2361 // stack if it isn't currently in a stack.
2362 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002363 Slog.w(TAG, "Can not move unresizeable task=" + task
2364 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2365 }
2366
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002367 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2368 // if a docked stack is created below which will lead to the stack we are moving from and
2369 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002370 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002371 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002372 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002373 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002374 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002375
2376 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002377 // move focus to the new stack by moving the stack to the front.
2378 stack.moveToFrontAndResumeStateIfNeeded(
2379 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002380
Wale Ogunwale040b4702015-08-06 18:10:50 -07002381 return stack;
2382 }
2383
Chong Zhange4fbd322016-03-01 14:44:03 -08002384 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002385 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002386 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2387 false /* deferResume */);
2388 }
2389
2390 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2391 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002392 final TaskRecord task = anyTaskForIdLocked(taskId);
2393 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002394 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002395 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002396 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002397
Andrii Kulian02b7a832016-10-06 23:11:56 -07002398 final ActivityStack currentStack = task.getStack();
2399 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002400 // You are already in the right stack silly...
2401 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002402 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002403 }
2404
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002405 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2406 throw new IllegalArgumentException("moveTaskToStack:"
2407 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2408 }
2409
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002410 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002411 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002412 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002413 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002414 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002415 // We are about to relaunch the activity because its configuration changed due to
2416 // being maximized, i.e. size change. The activity will first remove the old window
2417 // and then add a new one. This call will tell window manager about this, so it can
2418 // preserve the old window until the new one is drawn. This prevents having a gap
2419 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002420 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002421 // Note here we always set the replacing window first, as the flags might be needed
2422 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002423 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002424 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002425
2426 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002427 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002428 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002429 try {
2430 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002431 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002432 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002433
Wale Ogunwale961f4852016-02-01 20:25:54 -08002434 if (!animate) {
2435 stack.mNoAnimActivities.add(topActivity);
2436 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002437
Wale Ogunwale961f4852016-02-01 20:25:54 -08002438 // We might trigger a configuration change. Save the current task bounds for freezing.
2439 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2440
2441 // Make sure the task has the appropriate bounds/size for the stack it is in.
2442 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002443 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2444 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002445 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2446 Rect bounds = task.getLaunchBounds();
2447 if (bounds == null) {
2448 stack.layoutTaskInStack(task, null);
2449 bounds = task.mBounds;
2450 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002451 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2452 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002453 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002454 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2455 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002456 }
2457 } finally {
2458 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002459 }
2460
2461 if (mightReplaceWindow) {
2462 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2463 // window), we need to clear the replace window settings. Otherwise, we schedule a
2464 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002465 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002466 }
2467
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002468 if (!deferResume) {
2469
2470 // The task might have already been running and its visibility needs to be synchronized with
2471 // the visibility of the stack / windows.
2472 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2473 resumeFocusedStackTopActivityLocked();
2474 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002475
Jorim Jaggid53f0922016-04-06 22:16:23 -07002476 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002477
2478 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002479 }
2480
Wale Ogunwale079a0042015-10-24 11:44:07 -07002481 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2482 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2483 if (stack == null) {
2484 throw new IllegalArgumentException(
2485 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2486 }
2487
2488 final ActivityRecord r = stack.topRunningActivityLocked();
2489 if (r == null) {
2490 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2491 + " in stack=" + stack);
2492 return false;
2493 }
2494
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002495 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002496 Slog.w(TAG,
2497 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002498 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002499 return false;
2500 }
2501
Wale Ogunwale480dca02016-02-06 13:58:29 -08002502 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002503 return true;
2504 }
2505
Wale Ogunwale480dca02016-02-06 13:58:29 -08002506 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2507 mWindowManager.deferSurfaceLayout();
2508 try {
2509 final TaskRecord task = r.task;
2510
Andrii Kulian02b7a832016-10-06 23:11:56 -07002511 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002512 // An activity can't be pinned and visible behind at the same time. Go ahead and
2513 // release it from been visible behind before pinning.
2514 requestVisibleBehindLocked(r, false);
2515 }
2516
Wale Ogunwale480dca02016-02-06 13:58:29 -08002517 // Need to make sure the pinned stack exist so we can resize it below...
2518 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2519
2520 // Resize the pinned stack to match the current size of the task the activity we are
2521 // going to be moving is currently contained in. We do this to have the right starting
2522 // animation bounds for the pinned stack to the desired bounds the caller wants.
2523 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2524 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002525 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002526
2527 if (task.mActivities.size() == 1) {
2528 // There is only one activity in the task. So, we can just move the task over to
2529 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002530 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2531 // Move the home stack forward if the task we just moved to the pinned stack
2532 // was launched from home so home should be visible behind it.
2533 moveHomeStackToFront(reason);
2534 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002535 moveTaskToStackLocked(
2536 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2537 } else {
2538 stack.moveActivityToStack(r);
2539 }
2540 } finally {
2541 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002542 }
2543
Wale Ogunwale480dca02016-02-06 13:58:29 -08002544 // The task might have already been running and its visibility needs to be synchronized
2545 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002546 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002547 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002548
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002549 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002550 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002551 }
2552
Chong Zhang6cda19c2016-06-14 19:07:56 -07002553 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2554 if (r == null || !r.isFocusable()) {
2555 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2556 "moveActivityStackToFront: unfocusable r=" + r);
2557 return false;
2558 }
2559
2560 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002561 final ActivityStack stack = r.getStack();
2562 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002563 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2564 + r + " task=" + task);
2565 return false;
2566 }
2567
Chong Zhang6cda19c2016-06-14 19:07:56 -07002568 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2569 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2570 "moveActivityStackToFront: already on top, r=" + r);
2571 return false;
2572 }
2573
2574 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2575 "moveActivityStackToFront: r=" + r);
2576
2577 stack.moveToFront(reason, task);
2578 return true;
2579 }
2580
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002581 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2582 final TaskRecord task = anyTaskForIdLocked(taskId);
2583 if (task == null) {
2584 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2585 return;
2586 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002587 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2588
2589 task.updateOverrideConfigurationForStack(stack);
2590
2591 mWindowManager.positionTaskInStack(
2592 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002593 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002594 // The task might have already been running and its visibility needs to be synchronized with
2595 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002596 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002597 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002598 }
2599
Craig Mautnerac6f8432013-07-17 13:24:59 -07002600 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002601 mTmpFindTaskResult.r = null;
2602 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002603 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002604 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2605 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002606 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2607 final ActivityStack stack = stacks.get(stackNdx);
2608 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002609 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002610 continue;
2611 }
2612 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002613 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2614 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002615 continue;
2616 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002617 stack.findTaskLocked(r, mTmpFindTaskResult);
2618 // It is possible to have task in multiple stacks with the same root affinity.
2619 // If the match we found was based on root affinity we keep on looking to see if
2620 // there is a better match in another stack. We eventually return the match based
2621 // on root affinity if we don't find a better match.
2622 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2623 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002624 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002625 }
2626 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002627 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2628 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002629 }
2630
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002631 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2632 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002633 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2634 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002635 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002636 final ActivityRecord ar = stacks.get(stackNdx)
2637 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002638 if (ar != null) {
2639 return ar;
2640 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002641 }
2642 }
2643 return null;
2644 }
2645
Craig Mautner8d341ef2013-03-26 09:03:27 -07002646 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002647 scheduleSleepTimeout();
2648 if (!mGoingToSleep.isHeld()) {
2649 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002650 if (mLaunchingActivity.isHeld()) {
2651 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2652 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002653 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002654 mLaunchingActivity.release();
2655 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002656 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002657 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002658 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002659 }
2660
2661 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002662 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002663
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002664 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002665 final long endTime = System.currentTimeMillis() + timeout;
2666 while (true) {
2667 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002668 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2669 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002670 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2671 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2672 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002673 }
2674 if (cantShutdown) {
2675 long timeRemaining = endTime - System.currentTimeMillis();
2676 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002677 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002678 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 } catch (InterruptedException e) {
2680 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002681 } else {
2682 Slog.w(TAG, "Activity manager shutdown timed out");
2683 timedout = true;
2684 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002685 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002686 } else {
2687 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002688 }
2689 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002690
2691 // Force checkReadyForSleep to complete.
2692 mSleepTimeout = true;
2693 checkReadyForSleepLocked();
2694
Craig Mautner8d341ef2013-03-26 09:03:27 -07002695 return timedout;
2696 }
2697
2698 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002699 removeSleepTimeouts();
2700 if (mGoingToSleep.isHeld()) {
2701 mGoingToSleep.release();
2702 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2704 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002705 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2706 final ActivityStack stack = stacks.get(stackNdx);
2707 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002708 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002709 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002710 }
Craig Mautner5314a402013-09-26 12:40:16 -07002711 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002712 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002713 mGoingToSleepActivities.clear();
2714 }
2715
2716 void activitySleptLocked(ActivityRecord r) {
2717 mGoingToSleepActivities.remove(r);
2718 checkReadyForSleepLocked();
2719 }
2720
2721 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002722 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002723 // Do not care.
2724 return;
2725 }
2726
2727 if (!mSleepTimeout) {
2728 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002729 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2730 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002731 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2732 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2733 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002734 }
2735
2736 if (mStoppingActivities.size() > 0) {
2737 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002738 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002739 + mStoppingActivities.size() + " activities");
2740 scheduleIdleLocked();
2741 dontSleep = true;
2742 }
2743
2744 if (mGoingToSleepActivities.size() > 0) {
2745 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002746 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002747 + mGoingToSleepActivities.size() + " activities");
2748 dontSleep = true;
2749 }
2750
2751 if (dontSleep) {
2752 return;
2753 }
2754 }
2755
Wei Wang65c7a152016-06-02 18:51:22 -07002756 // Send launch end powerhint before going sleep
2757 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2758
Craig Mautnere0a38842013-12-16 16:14:02 -08002759 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2760 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002761 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2762 stacks.get(stackNdx).goToSleep();
2763 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002764 }
2765
2766 removeSleepTimeouts();
2767
2768 if (mGoingToSleep.isHeld()) {
2769 mGoingToSleep.release();
2770 }
2771 if (mService.mShuttingDown) {
2772 mService.notifyAll();
2773 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002774 }
2775
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002776 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002777 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002778 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002779 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002780 }
2781 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002782 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002783 mWindowManager.executeAppTransition();
2784 return true;
2785 }
2786 return false;
2787 }
2788
Craig Mautner8d341ef2013-03-26 09:03:27 -07002789 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002790 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2791 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002792 int stackNdx = stacks.size() - 1;
2793 while (stackNdx >= 0) {
2794 stacks.get(stackNdx).handleAppCrashLocked(app);
2795 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002796 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002797 }
2798 }
2799
Jose Lima4b6c6692014-08-12 17:41:12 -07002800 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002801 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002802 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002803 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2804 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002805 return false;
2806 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002807
2808 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2809 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2810 + " visible=" + visible + " stackId=" + stack.mStackId
2811 + " can't contain visible behind activities");
2812 return false;
2813 }
2814
Jose Lima4b6c6692014-08-12 17:41:12 -07002815 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002816 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2817 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002818
2819 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002820 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002821 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002822 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002823 return true;
2824 }
2825
2826 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002827 if (visible && top.fullscreen) {
2828 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002829 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2830 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2831 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2832 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002833 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002834 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2835 // Only the activity set as currently visible behind should actively reset its
2836 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002837 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2838 "requestVisibleBehind: returning visible=" + visible
2839 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2840 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002841 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002842 }
2843
Jose Lima4b6c6692014-08-12 17:41:12 -07002844 stack.setVisibleBehindActivity(visible ? r : null);
2845 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002846 // If there is a translucent home activity, we need to force it stop being translucent,
2847 // because we can't depend on the application to necessarily perform that operation.
2848 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002849 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002850 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002851 mService.convertFromTranslucent(next.appToken);
2852 }
2853 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002854 if (top.app != null && top.app.thread != null) {
2855 // Notify the top app of the change.
2856 try {
2857 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2858 } catch (RemoteException e) {
2859 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002860 }
2861 return true;
2862 }
2863
Craig Mautnerbb742462014-07-07 15:28:55 -07002864 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2865 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002866 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002867 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07002868
2869 r.mLaunchTaskBehind = false;
2870 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002871 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002872 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002873 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002874
2875 // When launching tasks behind, update the last active time of the top task after the new
2876 // task has been shown briefly
2877 final ActivityRecord top = stack.topActivity();
2878 if (top != null) {
2879 top.task.touchActiveTime();
2880 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002881 }
2882
2883 void scheduleLaunchTaskBehindComplete(IBinder token) {
2884 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2885 }
2886
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002887 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2888 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002889 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002890 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2891 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002892 final int topStackNdx = stacks.size() - 1;
2893 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2894 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002895 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002896 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002897 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002898 }
2899
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002900 void invalidateTaskLayers() {
2901 mTaskLayersChanged = true;
2902 }
2903
2904 void rankTaskLayersIfNeeded() {
2905 if (!mTaskLayersChanged) {
2906 return;
2907 }
2908 mTaskLayersChanged = false;
2909 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2910 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2911 int baseLayer = 0;
2912 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2913 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2914 }
2915 }
2916 }
2917
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002918 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2919 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2920 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2921 final int topStackNdx = stacks.size() - 1;
2922 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2923 final ActivityStack stack = stacks.get(stackNdx);
2924 stack.clearOtherAppTimeTrackers(except);
2925 }
2926 }
2927 }
2928
Craig Mautner8d341ef2013-03-26 09:03:27 -07002929 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002930 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2931 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002932 final int numStacks = stacks.size();
2933 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2934 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002935 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002936 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002937 }
2938 }
2939
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002940 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2941 // Examine all activities currently running in the process.
2942 TaskRecord firstTask = null;
2943 // Tasks is non-null only if two or more tasks are found.
2944 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002945 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2946 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002947 ActivityRecord r = app.activities.get(i);
2948 // First, if we find an activity that is in the process of being destroyed,
2949 // then we just aren't going to do anything for now; we want things to settle
2950 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002951 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002952 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002953 return;
2954 }
2955 // Don't consider any activies that are currently not in a state where they
2956 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002957 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2958 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002959 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002960 continue;
2961 }
2962 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002963 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002964 + " from " + r);
2965 if (firstTask == null) {
2966 firstTask = r.task;
2967 } else if (firstTask != r.task) {
2968 if (tasks == null) {
2969 tasks = new ArraySet<>();
2970 tasks.add(firstTask);
2971 }
2972 tasks.add(r.task);
2973 }
2974 }
2975 }
2976 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002977 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002978 return;
2979 }
2980 // If we have activities in multiple tasks that are in a position to be destroyed,
2981 // let's iterate through the tasks and release the oldest one.
2982 final int numDisplays = mActivityDisplays.size();
2983 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2984 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2985 // Step through all stacks starting from behind, to hit the oldest things first.
2986 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2987 final ActivityStack stack = stacks.get(stackNdx);
2988 // Try to release activities in this stack; if we manage to, we are done.
2989 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2990 return;
2991 }
2992 }
2993 }
2994 }
2995
Amith Yamasani37a40c22015-06-17 13:25:42 -07002996 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07002997 final int focusStackId = mFocusedStack.getStackId();
2998 // We dismiss the docked stack whenever we switch users.
2999 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3000
3001 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003002 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003003 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003004
Craig Mautner858d8a62013-04-23 17:08:34 -07003005 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003006 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3007 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003008 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003009 final ActivityStack stack = stacks.get(stackNdx);
3010 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003011 TaskRecord task = stack.topTask();
3012 if (task != null) {
3013 mWindowManager.moveTaskToTop(task.taskId);
3014 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003015 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003016 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003017
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003018 ActivityStack stack = getStack(restoreStackId);
3019 if (stack == null) {
3020 stack = mHomeStack;
3021 }
3022 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003023 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003024 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003025 } else {
3026 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003027 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003028 }
Craig Mautner93529a42013-10-04 15:03:13 -07003029 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003030 }
3031
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003032 /** Checks whether the userid is a profile of the current user. */
3033 boolean isCurrentProfileLocked(int userId) {
3034 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003035 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003036 }
3037
Craig Mautnerde4ef022013-04-07 19:01:33 -07003038 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003039 ArrayList<ActivityRecord> stops = null;
3040
3041 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003042 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3043 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003044 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3045 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003046 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003047 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003048 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3049 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003050 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003051 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003052 if (s.finishing) {
3053 // If this activity is finishing, it is sitting on top of
3054 // everyone else but we now know it is no longer needed...
3055 // so get rid of it. Otherwise, we need to go through the
3056 // normal flow and hide it once we determine that it is
3057 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003058 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003059 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003060 }
3061 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003062 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003063 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003064 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003065 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003066 }
3067 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003068 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003069 }
3070 }
3071
3072 return stops;
3073 }
3074
Craig Mautnercf910b02013-04-23 11:23:27 -07003075 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003076 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3077 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3078 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3079 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003080 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003081 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003082 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003083 if (r == null) Slog.e(TAG,
3084 "validateTop...: null top activity, stack=" + stack);
3085 else {
3086 final ActivityRecord pausing = stack.mPausingActivity;
3087 if (pausing != null && pausing == r) Slog.e(TAG,
3088 "validateTop...: top stack has pausing activity r=" + r
3089 + " state=" + state);
3090 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3091 "validateTop...: activity in front not resumed r=" + r
3092 + " state=" + state);
3093 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003094 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003095 final ActivityRecord resumed = stack.mResumedActivity;
3096 if (resumed != null && resumed == r) Slog.e(TAG,
3097 "validateTop...: back stack has resumed activity r=" + r
3098 + " state=" + state);
3099 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3100 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003101 }
3102 }
3103 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003104 }
3105
Craig Mautnere0570202015-05-13 13:06:11 -07003106 private String lockTaskModeToString() {
3107 switch (mLockTaskModeState) {
3108 case LOCK_TASK_MODE_LOCKED:
3109 return "LOCKED";
3110 case LOCK_TASK_MODE_PINNED:
3111 return "PINNED";
3112 case LOCK_TASK_MODE_NONE:
3113 return "NONE";
3114 default: return "unknown=" + mLockTaskModeState;
3115 }
3116 }
3117
Craig Mautner27084302013-03-25 08:05:25 -07003118 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003119 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003120 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003121 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003122 pw.print(prefix);
3123 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003124 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003125 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003126 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3127 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3128 if (packages.size() > 0) {
3129 pw.println(" mLockTaskPackages (userId:packages)=");
3130 for (int i = 0; i < packages.size(); ++i) {
3131 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3132 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3133 }
3134 }
3135 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003136 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003137
Winson43d1f262016-06-14 16:05:55 -07003138 /**
3139 * Dumps the activities matching the given {@param name} in the either the focused stack
3140 * or all visible stacks if {@param dumpVisibleStacks} is true.
3141 */
3142 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3143 if (dumpVisibleStacks) {
3144 ArrayList<ActivityRecord> activities = new ArrayList<>();
3145 int numDisplays = mActivityDisplays.size();
3146 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3147 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3148 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3149 ActivityStack stack = stacks.get(stackNdx);
3150 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3151 activities.addAll(stack.getDumpActivitiesLocked(name));
3152 }
3153 }
3154 }
3155 return activities;
3156 } else {
3157 return mFocusedStack.getDumpActivitiesLocked(name);
3158 }
Craig Mautner20e72272013-04-01 13:45:53 -07003159 }
3160
Dianne Hackborn390517b2013-05-30 15:03:32 -07003161 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3162 boolean needSep, String prefix) {
3163 if (activity != null) {
3164 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3165 if (needSep) {
3166 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003167 }
3168 pw.print(prefix);
3169 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003170 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003171 }
3172 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003173 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003174 }
3175
Craig Mautner8d341ef2013-03-26 09:03:27 -07003176 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3177 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003178 boolean printed = false;
3179 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003180 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3181 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003182 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003183 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003184 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003185 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003186 final ActivityStack stack = stacks.get(stackNdx);
3187 StringBuilder stackHeader = new StringBuilder(128);
3188 stackHeader.append(" Stack #");
3189 stackHeader.append(stack.mStackId);
3190 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003191 stackHeader.append("\n");
3192 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3193 stackHeader.append("\n");
3194 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003195 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3196 needSep, stackHeader.toString());
3197 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3198 !dumpAll, false, dumpPackage, true,
3199 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003200
Craig Mautner4a1cb222013-12-04 16:14:06 -08003201 needSep = printed;
3202 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3203 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003204 if (pr) {
3205 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003206 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003207 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003208 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3209 " mResumedActivity: ");
3210 if (pr) {
3211 printed = true;
3212 needSep = false;
3213 }
3214 if (dumpAll) {
3215 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3216 " mLastPausedActivity: ");
3217 if (pr) {
3218 printed = true;
3219 needSep = true;
3220 }
3221 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3222 needSep, " mLastNoHistoryActivity: ");
3223 }
3224 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003225 }
3226 }
3227
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003228 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3229 false, dumpPackage, true, " Activities waiting to finish:", null);
3230 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3231 false, dumpPackage, true, " Activities waiting to stop:", null);
3232 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3233 false, dumpPackage, true, " Activities waiting for another to become visible:",
3234 null);
3235 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3236 false, dumpPackage, true, " Activities waiting to sleep:", null);
3237 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3238 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003239
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003240 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003241 }
3242
Dianne Hackborn390517b2013-05-30 15:03:32 -07003243 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003244 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003245 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003246 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003247 String innerPrefix = null;
3248 String[] args = null;
3249 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003250 for (int i=list.size()-1; i>=0; i--) {
3251 final ActivityRecord r = list.get(i);
3252 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3253 continue;
3254 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003255 if (innerPrefix == null) {
3256 innerPrefix = prefix + " ";
3257 args = new String[0];
3258 }
3259 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003260 final boolean full = !brief && (complete || !r.isInHistory());
3261 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003262 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003263 needNL = false;
3264 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003265 if (header1 != null) {
3266 pw.println(header1);
3267 header1 = null;
3268 }
3269 if (header2 != null) {
3270 pw.println(header2);
3271 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003272 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003273 if (lastTask != r.task) {
3274 lastTask = r.task;
3275 pw.print(prefix);
3276 pw.print(full ? "* " : " ");
3277 pw.println(lastTask);
3278 if (full) {
3279 lastTask.dump(pw, prefix + " ");
3280 } else if (complete) {
3281 // Complete + brief == give a summary. Isn't that obvious?!?
3282 if (lastTask.intent != null) {
3283 pw.print(prefix); pw.print(" ");
3284 pw.println(lastTask.intent.toInsecureStringWithClip());
3285 }
3286 }
3287 }
3288 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3289 pw.print(" #"); pw.print(i); pw.print(": ");
3290 pw.println(r);
3291 if (full) {
3292 r.dump(pw, innerPrefix);
3293 } else if (complete) {
3294 // Complete + brief == give a summary. Isn't that obvious?!?
3295 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3296 if (r.app != null) {
3297 pw.print(innerPrefix); pw.println(r.app);
3298 }
3299 }
3300 if (client && r.app != null && r.app.thread != null) {
3301 // flush anything that is already in the PrintWriter since the thread is going
3302 // to write to the file descriptor directly
3303 pw.flush();
3304 try {
3305 TransferPipe tp = new TransferPipe();
3306 try {
3307 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3308 r.appToken, innerPrefix, args);
3309 // Short timeout, since blocking here can
3310 // deadlock with the application.
3311 tp.go(fd, 2000);
3312 } finally {
3313 tp.kill();
3314 }
3315 } catch (IOException e) {
3316 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3317 } catch (RemoteException e) {
3318 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3319 }
3320 needNL = true;
3321 }
3322 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003323 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003324 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003325
Craig Mautnerf3333272013-04-22 10:55:53 -07003326 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003327 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3328 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003329 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3330 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003331 }
3332
3333 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003334 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003335 }
3336
3337 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003338 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3339 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003340 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3341 }
3342
Craig Mautner05d29032013-05-03 13:40:13 -07003343 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003344 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3345 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3346 }
Craig Mautner05d29032013-05-03 13:40:13 -07003347 }
3348
Craig Mautner0eea92c2013-05-16 13:35:39 -07003349 void removeSleepTimeouts() {
3350 mSleepTimeout = false;
3351 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3352 }
3353
3354 final void scheduleSleepTimeout() {
3355 removeSleepTimeouts();
3356 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3357 }
3358
Craig Mautner4a1cb222013-12-04 16:14:06 -08003359 @Override
3360 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003361 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003362 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3363 }
3364
3365 @Override
3366 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003367 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003368 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3369 }
3370
3371 @Override
3372 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003373 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003374 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3375 }
3376
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003377 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003378 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003379 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003380 newDisplay = mActivityDisplays.get(displayId) == null;
3381 if (newDisplay) {
3382 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003383 if (activityDisplay.mDisplay == null) {
3384 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3385 return;
3386 }
Craig Mautner4504de52013-12-20 09:06:56 -08003387 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003388 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003389 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003390 }
Craig Mautner4504de52013-12-20 09:06:56 -08003391 if (newDisplay) {
3392 mWindowManager.onDisplayAdded(displayId);
3393 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003394 }
3395
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003396 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003397 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003398 mService.mContext.getResources().getDimensionPixelSize(
3399 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003400 }
3401
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003402 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003403 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003404 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3405 if (activityDisplay != null) {
3406 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003407 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003408 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003409 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003410 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003411 }
3412 }
3413 mWindowManager.onDisplayRemoved(displayId);
3414 }
3415
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003416 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003417 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003418 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3419 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003420 // TODO: Update the bounds.
3421 }
3422 }
3423 mWindowManager.onDisplayChanged(displayId);
3424 }
3425
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003426 private StackInfo getStackInfoLocked(ActivityStack stack) {
Winson529c8e42016-05-17 11:08:40 -07003427 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003428 StackInfo info = new StackInfo();
3429 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3430 info.displayId = Display.DEFAULT_DISPLAY;
3431 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003432 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003433 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003434 info.position = display != null
3435 ? display.mStacks.indexOf(stack)
3436 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003437
3438 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3439 final int numTasks = tasks.size();
3440 int[] taskIds = new int[numTasks];
3441 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003442 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003443 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003444 for (int i = 0; i < numTasks; ++i) {
3445 final TaskRecord task = tasks.get(i);
3446 taskIds[i] = task.taskId;
3447 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3448 : task.realActivity != null ? task.realActivity.flattenToString()
3449 : task.getTopActivity() != null ? task.getTopActivity().packageName
3450 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003451 taskBounds[i] = new Rect();
3452 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003453 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003454 }
3455 info.taskIds = taskIds;
3456 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003457 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003458 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003459
3460 final ActivityRecord top = stack.topRunningActivityLocked();
3461 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003462 return info;
3463 }
3464
3465 StackInfo getStackInfoLocked(int stackId) {
3466 ActivityStack stack = getStack(stackId);
3467 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003468 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003469 }
3470 return null;
3471 }
3472
3473 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003474 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003475 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3476 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003477 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003478 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003479 }
3480 }
3481 return list;
3482 }
3483
Craig Mautner15df08a2015-04-01 12:17:18 -07003484 TaskRecord getLockedTaskLocked() {
3485 final int top = mLockTaskModeTasks.size() - 1;
3486 if (top >= 0) {
3487 return mLockTaskModeTasks.get(top);
3488 }
3489 return null;
3490 }
3491
3492 boolean isLockedTask(TaskRecord task) {
3493 return mLockTaskModeTasks.contains(task);
3494 }
3495
3496 boolean isLastLockedTask(TaskRecord task) {
3497 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3498 }
3499
3500 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003501 if (!mLockTaskModeTasks.remove(task)) {
3502 return;
3503 }
3504 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3505 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003506 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003507 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3508 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003509 final Message lockTaskMsg = Message.obtain();
3510 lockTaskMsg.arg1 = task.userId;
3511 lockTaskMsg.what = LOCK_TASK_END_MSG;
3512 mHandler.sendMessage(lockTaskMsg);
3513 }
3514 }
3515
Andrii Kulianc27916642016-04-12 17:59:27 -07003516 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3517 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3518 false /* forceNonResizable */);
3519 }
3520
Jorim Jaggid53f0922016-04-06 22:16:23 -07003521 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003522 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003523 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3524 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003525 return;
3526 }
3527
Jorim Jaggicd13d332016-04-27 15:40:20 -07003528 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003529 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003530 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003531 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3532
Andrii Kulianc27916642016-04-12 17:59:27 -07003533 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3534 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003535 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003536 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3537 && !topActivity.noDisplay) {
3538 String packageName = topActivity.appInfo.packageName;
Jorim Jaggi2adba072016-03-03 13:43:39 +01003539 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3540 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003541 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003542 }
3543
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003544 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003545 if (mLockTaskNotify != null) {
3546 mLockTaskNotify.showToast(mLockTaskModeState);
3547 }
Jason Monka8f569c2014-07-07 12:15:21 -04003548 }
3549
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003550 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3551 if (mLockTaskModeTasks.contains(task)) {
3552 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3553 }
3554 }
3555
Craig Mautner432f64e2015-05-20 14:59:57 -07003556 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3557 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003558 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003559 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003560 final TaskRecord lockedTask = getLockedTaskLocked();
3561 if (lockedTask != null) {
3562 removeLockedTaskLocked(lockedTask);
3563 if (!mLockTaskModeTasks.isEmpty()) {
3564 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003565 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3566 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003567 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003568 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003569 return;
3570 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003571 }
Craig Mautnere0570202015-05-13 13:06:11 -07003572 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3573 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003574 return;
3575 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003576
3577 // Should have already been checked, but do it again.
3578 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003579 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3580 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003581 return;
3582 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003583 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003584 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003585 return;
3586 }
3587
3588 if (mLockTaskModeTasks.isEmpty()) {
3589 // First locktask.
3590 final Message lockTaskMsg = Message.obtain();
3591 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3592 lockTaskMsg.arg1 = task.userId;
3593 lockTaskMsg.what = LOCK_TASK_START_MSG;
3594 lockTaskMsg.arg2 = lockTaskModeState;
3595 mHandler.sendMessage(lockTaskMsg);
3596 }
3597 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003598 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3599 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003600 mLockTaskModeTasks.remove(task);
3601 mLockTaskModeTasks.add(task);
3602
3603 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003604 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003605 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003606
3607 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003608 findTaskToMoveToFrontLocked(task, 0, null, reason,
3609 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003610 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003611 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003612 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003613 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003614 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003615 }
3616
3617 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003618 return isLockTaskModeViolation(task, false);
3619 }
3620
3621 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3622 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003623 return false;
3624 }
3625 final int lockTaskAuth = task.mLockTaskAuth;
3626 switch (lockTaskAuth) {
3627 case LOCK_TASK_AUTH_DONT_LOCK:
3628 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003629 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003630 case LOCK_TASK_AUTH_LAUNCHABLE:
3631 case LOCK_TASK_AUTH_WHITELISTED:
3632 return false;
3633 case LOCK_TASK_AUTH_PINNABLE:
3634 // Pinnable tasks can't be launched on top of locktask tasks.
3635 return !mLockTaskModeTasks.isEmpty();
3636 default:
3637 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3638 return true;
3639 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003640 }
3641
Craig Mautner15df08a2015-04-01 12:17:18 -07003642 void onLockTaskPackagesUpdatedLocked() {
3643 boolean didSomething = false;
3644 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3645 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003646 final boolean wasWhitelisted =
3647 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3648 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003649 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003650 final boolean isWhitelisted =
3651 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3652 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3653 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003654 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003655 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3656 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003657 removeLockedTaskLocked(lockedTask);
3658 lockedTask.performClearTaskLocked();
3659 didSomething = true;
3660 }
3661 }
3662 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3663 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3664 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3665 final ActivityStack stack = stacks.get(stackNdx);
3666 stack.onLockTaskPackagesUpdatedLocked();
3667 }
3668 }
Craig Mautnere0570202015-05-13 13:06:11 -07003669 final ActivityRecord r = topRunningActivityLocked();
3670 final TaskRecord task = r != null ? r.task : null;
3671 if (mLockTaskModeTasks.isEmpty() && task != null
3672 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3673 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003674 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3675 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3676 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3677 false);
3678 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003679 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003680 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003681 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003682 }
3683 }
3684
Benjamin Franz43261142015-02-11 15:59:44 +00003685 int getLockTaskModeState() {
3686 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003687 }
3688
Jorim Jaggife89d122015-12-22 16:28:44 +01003689 void activityRelaunchedLocked(IBinder token) {
3690 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07003691 if (mService.isSleepingOrShuttingDownLocked()) {
3692 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
3693 if (r != null) {
3694 r.setSleeping(true, true);
3695 }
3696 }
Jorim Jaggife89d122015-12-22 16:28:44 +01003697 }
3698
3699 void activityRelaunchingLocked(ActivityRecord r) {
3700 mWindowManager.notifyAppRelaunching(r.appToken);
3701 }
3702
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003703 void logStackState() {
3704 mActivityMetricsLogger.logWindowState();
3705 }
3706
Andrii Kulian933076d2016-03-29 17:04:42 -07003707 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003708 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3709 final ActivityRecord r = task.mActivities.get(i);
3710 if (r.app != null && r.app.thread != null) {
3711 mMultiWindowModeChangedActivities.add(r);
3712 }
3713 }
3714
3715 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3716 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3717 }
3718 }
3719
Andrii Kulian933076d2016-03-29 17:04:42 -07003720 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003721 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003722 if (prevStack == null || prevStack == stack
3723 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3724 return;
3725 }
3726
3727 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3728 final ActivityRecord r = task.mActivities.get(i);
3729 if (r.app != null && r.app.thread != null) {
3730 mPipModeChangedActivities.add(r);
3731 }
3732 }
3733
3734 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3735 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3736 }
3737 }
3738
Tony Mak853304c2016-04-18 15:17:41 +01003739 void setDockedStackMinimized(boolean minimized) {
3740 mIsDockMinimized = minimized;
3741 if (minimized) {
3742 // Docked stack is not visible, no need to confirm credentials for its top activity.
3743 return;
3744 }
3745 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3746 if (dockedStack == null) {
3747 return;
3748 }
3749 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3750 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3751 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3752 }
3753 }
3754
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003755 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003756
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003757 public ActivityStackSupervisorHandler(Looper looper) {
3758 super(looper);
3759 }
3760
Craig Mautnerf3333272013-04-22 10:55:53 -07003761 void activityIdleInternal(ActivityRecord r) {
3762 synchronized (mService) {
3763 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3764 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003765 }
3766
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003767 @Override
3768 public void handleMessage(Message msg) {
3769 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003770 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3771 synchronized (mService) {
3772 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3773 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003774 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003775 }
3776 }
3777 } break;
3778 case REPORT_PIP_MODE_CHANGED_MSG: {
3779 synchronized (mService) {
3780 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3781 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003782 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003783 }
3784 }
3785 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003786 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003787 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3788 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003789 if (mService.mDidDexOpt) {
3790 mService.mDidDexOpt = false;
3791 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3792 nmsg.obj = msg.obj;
3793 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3794 return;
3795 }
3796 // We don't at this point know if the activity is fullscreen,
3797 // so we need to be conservative and assume it isn't.
3798 activityIdleInternal((ActivityRecord)msg.obj);
3799 } break;
3800 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003801 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003802 activityIdleInternal((ActivityRecord)msg.obj);
3803 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003804 case RESUME_TOP_ACTIVITY_MSG: {
3805 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003806 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003807 }
3808 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003809 case SLEEP_TIMEOUT_MSG: {
3810 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003811 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003812 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3813 mSleepTimeout = true;
3814 checkReadyForSleepLocked();
3815 }
3816 }
3817 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003818 case LAUNCH_TIMEOUT_MSG: {
3819 if (mService.mDidDexOpt) {
3820 mService.mDidDexOpt = false;
3821 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3822 return;
3823 }
3824 synchronized (mService) {
3825 if (mLaunchingActivity.isHeld()) {
3826 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3827 if (VALIDATE_WAKE_LOCK_CALLER
3828 && Binder.getCallingUid() != Process.myUid()) {
3829 throw new IllegalStateException("Calling must be system uid");
3830 }
3831 mLaunchingActivity.release();
3832 }
3833 }
3834 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003835 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003836 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003837 } break;
3838 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003839 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840 } break;
3841 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003842 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003843 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003844 case CONTAINER_CALLBACK_VISIBILITY: {
3845 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003846 final IActivityContainerCallback callback = container.mCallback;
3847 if (callback != null) {
3848 try {
3849 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3850 } catch (RemoteException e) {
3851 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003852 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003853 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003854 case LOCK_TASK_START_MSG: {
3855 // When lock task starts, we disable the status bars.
3856 try {
Jason Monk62515be2014-05-21 16:06:19 -04003857 if (mLockTaskNotify == null) {
3858 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003859 }
Jason Monk62515be2014-05-21 16:06:19 -04003860 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003861 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003862 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003863 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003864 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003865 flags = StatusBarManager.DISABLE_MASK
3866 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003867 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003868 flags = StatusBarManager.DISABLE_MASK
3869 & (~StatusBarManager.DISABLE_BACK)
3870 & (~StatusBarManager.DISABLE_HOME)
3871 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003872 }
3873 getStatusBarService().disable(flags, mToken,
3874 mService.mContext.getPackageName());
3875 }
3876 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003877 if (getDevicePolicyManager() != null) {
3878 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003879 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003880 }
justinzhang5286d3f2014-05-12 17:06:01 -04003881 } catch (RemoteException ex) {
3882 throw new RuntimeException(ex);
3883 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003884 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003885 case LOCK_TASK_END_MSG: {
3886 // When lock task ends, we enable the status bars.
3887 try {
Jason Monk62515be2014-05-21 16:06:19 -04003888 if (getStatusBarService() != null) {
3889 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3890 mService.mContext.getPackageName());
3891 }
3892 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003893 if (getDevicePolicyManager() != null) {
3894 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3895 msg.arg1);
3896 }
Jason Monk62515be2014-05-21 16:06:19 -04003897 if (mLockTaskNotify == null) {
3898 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3899 }
3900 mLockTaskNotify.show(false);
3901 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003902 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003903 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003904 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003905 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003906 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003907 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003908 new LockPatternUtils(mService.mContext)
3909 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003910 }
3911 } catch (SettingNotFoundException e) {
3912 // No setting, don't lock.
3913 }
justinzhang5286d3f2014-05-12 17:06:01 -04003914 } catch (RemoteException ex) {
3915 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003916 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003917 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003918 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003919 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003920 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3921 if (mLockTaskNotify == null) {
3922 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3923 }
3924 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3925 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003926 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3927 final ActivityContainer container = (ActivityContainer) msg.obj;
3928 final IActivityContainerCallback callback = container.mCallback;
3929 if (callback != null) {
3930 try {
3931 callback.onAllActivitiesComplete(container.asBinder());
3932 } catch (RemoteException e) {
3933 }
3934 }
3935 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003936 case LAUNCH_TASK_BEHIND_COMPLETE: {
3937 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003938 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003939 if (r != null) {
3940 handleLaunchTaskBehindCompleteLocked(r);
3941 }
3942 }
3943 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003944
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003945 }
3946 }
3947 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003948
Ying Wangb081a592014-04-22 15:20:16 -07003949 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003950 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3951 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003952 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003953 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003954 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003955 ActivityRecord mParentActivity = null;
3956 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003957
Craig Mautnere3a00d72014-04-16 08:31:19 -07003958 boolean mVisible = true;
3959
Craig Mautner4a1cb222013-12-04 16:14:06 -08003960 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003961 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003962
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003963 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3964 final static int CONTAINER_STATE_NO_SURFACE = 1;
3965 final static int CONTAINER_STATE_FINISHING = 2;
3966 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3967
3968 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003969 synchronized (mService) {
3970 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003971 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003972 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003973 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003974 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003975 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003976
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003977 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003978 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003979 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003980 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003981 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003982 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003983 }
3984
3985 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003986 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003987 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003988 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3989 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003990 return;
3991 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003992 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003993 }
3994 }
3995
3996 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003997 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003998 synchronized (mService) {
3999 if (mActivityDisplay != null) {
4000 return mActivityDisplay.mDisplayId;
4001 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004002 }
4003 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004004 }
4005
Jeff Brownca9bc702014-02-11 14:32:56 -08004006 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004007 public int getStackId() {
4008 synchronized (mService) {
4009 return mStackId;
4010 }
4011 }
4012
4013 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004014 public boolean injectEvent(InputEvent event) {
4015 final long origId = Binder.clearCallingIdentity();
4016 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004017 synchronized (mService) {
4018 if (mActivityDisplay != null) {
4019 return mInputManagerInternal.injectInputEvent(event,
4020 mActivityDisplay.mDisplayId,
4021 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4022 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004023 }
4024 return false;
4025 } finally {
4026 Binder.restoreCallingIdentity(origId);
4027 }
4028 }
4029
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004030 @Override
4031 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004032 synchronized (mService) {
4033 if (mContainerState == CONTAINER_STATE_FINISHING) {
4034 return;
4035 }
4036 mContainerState = CONTAINER_STATE_FINISHING;
4037
Craig Mautnerd163e752014-06-13 17:18:47 -07004038 long origId = Binder.clearCallingIdentity();
4039 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004040 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004041 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004042 } finally {
4043 Binder.restoreCallingIdentity(origId);
4044 }
4045 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004046 }
4047
Craig Mautner60257702014-09-17 15:02:33 -07004048 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004049 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004050 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004051 if (mActivityDisplay != null) {
4052 mActivityDisplay.detachActivitiesLocked(mStack);
4053 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004054 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004055 }
4056 }
4057
4058 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004059 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004060 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004061 }
4062
4063 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004064 public final int startActivityIntentSender(IIntentSender intentSender)
4065 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004066 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4067
4068 if (!(intentSender instanceof PendingIntentRecord)) {
4069 throw new IllegalArgumentException("Bad PendingIntent object");
4070 }
4071
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004072 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004073 Binder.getCallingUid(), mCurrentUser, false,
4074 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004075
4076 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4077 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4078 pendingIntent.key.requestResolvedType);
4079
4080 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4081 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4082 }
4083
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004084 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004085 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004086 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004087 throw new SecurityException(
4088 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4089 }
4090 }
4091
Craig Mautnerdf88d732014-01-27 09:21:32 -08004092 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004093 public IBinder asBinder() {
4094 return this;
4095 }
4096
Craig Mautner4504de52013-12-20 09:06:56 -08004097 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004098 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004099 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004100 }
4101
Craig Mautner4a1cb222013-12-04 16:14:06 -08004102 ActivityStackSupervisor getOuter() {
4103 return ActivityStackSupervisor.this;
4104 }
4105
Craig Mautnerd163e752014-06-13 17:18:47 -07004106 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004107 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004108 }
4109
Craig Mautner6985bad2014-04-21 15:22:06 -07004110 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004111 void setVisible(boolean visible) {
4112 if (mVisible != visible) {
4113 mVisible = visible;
4114 if (mCallback != null) {
4115 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4116 0 /* unused */, this).sendToTarget();
4117 }
4118 }
4119 }
4120
Craig Mautner6985bad2014-04-21 15:22:06 -07004121 void setDrawn() {
4122 }
4123
Craig Mautner1b4bf852014-05-26 15:06:32 -07004124 // You can always start a new task on a regular ActivityStack.
4125 boolean isEligibleForNewTasks() {
4126 return true;
4127 }
4128
Craig Mautnerd163e752014-06-13 17:18:47 -07004129 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004130 detachLocked();
4131 deleteActivityContainer(this);
4132 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004133 }
4134
Craig Mautner34b73df2014-01-12 21:11:08 -08004135 @Override
4136 public String toString() {
4137 return mIdString + (mActivityDisplay == null ? "N" : "A");
4138 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004139 }
4140
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004141 private class VirtualActivityContainer extends ActivityContainer {
4142 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004143 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004144
4145 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4146 super(getNextStackId());
4147 mParentActivity = parent;
4148 mCallback = callback;
4149 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004150 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004151 }
4152
4153 @Override
4154 public void setSurface(Surface surface, int width, int height, int density) {
4155 super.setSurface(surface, width, height, density);
4156
4157 synchronized (mService) {
4158 final long origId = Binder.clearCallingIdentity();
4159 try {
4160 setSurfaceLocked(surface, width, height, density);
4161 } finally {
4162 Binder.restoreCallingIdentity(origId);
4163 }
4164 }
4165 }
4166
4167 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4168 if (mContainerState == CONTAINER_STATE_FINISHING) {
4169 return;
4170 }
4171 VirtualActivityDisplay virtualActivityDisplay =
4172 (VirtualActivityDisplay) mActivityDisplay;
4173 if (virtualActivityDisplay == null) {
4174 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004175 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004176 mActivityDisplay = virtualActivityDisplay;
4177 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004178 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004179 }
4180
4181 if (mSurface != null) {
4182 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004183 }
4184
Craig Mautner6985bad2014-04-21 15:22:06 -07004185 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004186 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004187 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004188 } else {
4189 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004190 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004191 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004192 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004193 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004194 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004195
Craig Mautnerd163e752014-06-13 17:18:47 -07004196 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004197
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004198 if (DEBUG_STACK) Slog.d(TAG_STACK,
4199 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004200 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004201
Craig Mautner6985bad2014-04-21 15:22:06 -07004202 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004203 boolean isAttachedLocked() {
4204 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004205 }
4206
4207 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004208 void setDrawn() {
4209 synchronized (mService) {
4210 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004211 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004212 }
4213 }
4214
Craig Mautner1b4bf852014-05-26 15:06:32 -07004215 // Never start a new task on an ActivityView if it isn't explicitly specified.
4216 @Override
4217 boolean isEligibleForNewTasks() {
4218 return false;
4219 }
4220
Craig Mautnerd163e752014-06-13 17:18:47 -07004221 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004222 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004223 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4224 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4225 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4226 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4227 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004228 }
4229 }
4230
Craig Mautner4a1cb222013-12-04 16:14:06 -08004231 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4232 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004233 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004234 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004235 int mDisplayId;
4236 Display mDisplay;
4237 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004238
Craig Mautner4a1cb222013-12-04 16:14:06 -08004239 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4240 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004241 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004242
Jose Lima4b6c6692014-08-12 17:41:12 -07004243 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004244
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004245 ActivityDisplay() {
4246 }
Craig Mautner4504de52013-12-20 09:06:56 -08004247
Craig Mautner1a70a162014-09-13 12:09:31 -07004248 // After instantiation, check that mDisplay is not null before using this. The alternative
4249 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004250 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004251 final Display display = mDisplayManager.getDisplay(displayId);
4252 if (display == null) {
4253 return;
4254 }
4255 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004256 }
4257
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004258 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004259 mDisplay = display;
4260 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004261 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004262 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004263
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004264 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004265 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004266 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4267 + " onTop=" + onTop);
4268 if (onTop) {
4269 mStacks.add(stack);
4270 } else {
4271 mStacks.add(0, stack);
4272 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004273 }
4274
Craig Mautnere0a38842013-12-16 16:14:02 -08004275 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004276 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004277 + " from displayId=" + mDisplayId);
4278 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004279 }
4280
Jose Lima4b6c6692014-08-12 17:41:12 -07004281 void setVisibleBehindActivity(ActivityRecord r) {
4282 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004283 }
4284
Jose Lima4b6c6692014-08-12 17:41:12 -07004285 boolean hasVisibleBehindActivity() {
4286 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004287 }
4288
Craig Mautner34b73df2014-01-12 21:11:08 -08004289 @Override
4290 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004291 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4292 }
4293 }
4294
4295 class VirtualActivityDisplay extends ActivityDisplay {
4296 VirtualDisplay mVirtualDisplay;
4297
Craig Mautner6985bad2014-04-21 15:22:06 -07004298 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004299 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004300 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4301 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4302 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4303 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004304
4305 init(mVirtualDisplay.getDisplay());
4306
4307 mWindowManager.handleDisplayAdded(mDisplayId);
4308 }
4309
4310 void setSurface(Surface surface) {
4311 if (mVirtualDisplay != null) {
4312 mVirtualDisplay.setSurface(surface);
4313 }
4314 }
4315
4316 @Override
4317 void detachActivitiesLocked(ActivityStack stack) {
4318 super.detachActivitiesLocked(stack);
4319 if (mVirtualDisplay != null) {
4320 mVirtualDisplay.release();
4321 mVirtualDisplay = null;
4322 }
4323 }
4324
4325 @Override
4326 public String toString() {
4327 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004328 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004329 }
Jose Lima58e66d62014-05-27 20:00:27 -07004330
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004331 ActivityStack findStackBehind(ActivityStack stack) {
4332 // TODO(multi-display): We are only looking for stacks on the default display.
4333 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4334 if (display == null) {
4335 return null;
4336 }
4337 final ArrayList<ActivityStack> stacks = display.mStacks;
4338 for (int i = stacks.size() - 1; i >= 0; i--) {
4339 if (stacks.get(i) == stack && i > 0) {
4340 return stacks.get(i - 1);
4341 }
4342 }
4343 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4344 + " in=" + stacks);
4345 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004346
Jorim Jaggic69bd222016-03-15 14:38:37 +01004347 /**
4348 * Puts a task into resizing mode during the next app transition.
4349 *
4350 * @param taskId the id of the task to put into resizing mode
4351 */
4352 private void setResizingDuringAnimation(int taskId) {
4353 mResizingTasksDuringAnimation.add(taskId);
4354 mWindowManager.setTaskDockedResizing(taskId, true);
4355 }
4356
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004357 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4358 final TaskRecord task;
4359 final int callingUid;
4360 final String callingPackage;
4361 final Intent intent;
4362 final int userId;
4363 final ActivityOptions activityOptions = (bOptions != null)
4364 ? new ActivityOptions(bOptions) : null;
4365 final int launchStackId = (activityOptions != null)
4366 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004367 if (launchStackId == HOME_STACK_ID) {
4368 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4369 + taskId + " can't be launch in the home stack.");
4370 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004371
4372 if (launchStackId == DOCKED_STACK_ID) {
4373 mWindowManager.setDockedStackCreateState(
4374 activityOptions.getDockCreateMode(), null /* initialBounds */);
4375
4376 // Defer updating the stack in which recents is until the app transition is done, to
4377 // not run into issues where we still need to draw the task in recents but the
4378 // docked stack is already created.
4379 deferUpdateBounds(HOME_STACK_ID);
4380 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4381 }
4382
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004383 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4384 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004385 continueUpdateBounds(HOME_STACK_ID);
4386 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004387 throw new IllegalArgumentException(
4388 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4389 }
4390
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004391 // Since we don't have an actual source record here, we assume that the currently focused
4392 // activity was the source.
4393 final ActivityStack focusedStack = getFocusedStack();
4394 final ActivityRecord sourceRecord =
4395 focusedStack != null ? focusedStack.topActivity() : null;
4396
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004397 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004398 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004399 moveTaskToStackLocked(
4400 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4401 ANIMATE);
4402 }
4403 }
4404
4405 // If the user must confirm credentials (e.g. when first launching a work app and the
4406 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4407 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4408 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004409 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004410 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004411 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004412 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4413 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004414
4415 // If we are launching the task in the docked stack, put it into resizing mode so
4416 // the window renders full-screen with the background filling the void. Also only
4417 // call this at the end to make sure that tasks exists on the window manager side.
4418 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004419 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004420 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004421
4422 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4423 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004424 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4425 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004426 return ActivityManager.START_TASK_TO_FRONT;
4427 }
4428 callingUid = task.mCallingUid;
4429 callingPackage = task.mCallingPackage;
4430 intent = task.intent;
4431 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4432 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004433 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4434 null, null, 0, 0, bOptions, userId, null, task);
4435 if (launchStackId == DOCKED_STACK_ID) {
4436 setResizingDuringAnimation(task.taskId);
4437 }
4438 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004439 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004440
4441 /**
4442 * @return a list of activities which are the top ones in each visible stack. The first
4443 * entry will be the focused activity.
4444 */
4445 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004446 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4447 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Amith Yamasanie8222e52016-04-08 15:28:47 -07004448 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4449 if (display == null) {
4450 return Collections.EMPTY_LIST;
4451 }
4452 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4453 final ArrayList<ActivityStack> stacks = display.mStacks;
4454 for (int i = stacks.size() - 1; i >= 0; i--) {
4455 ActivityStack stack = stacks.get(i);
4456 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4457 ActivityRecord top = stack.topActivity();
4458 if (top != null) {
4459 if (stack == mFocusedStack) {
4460 topActivityTokens.add(0, top.appToken);
4461 } else {
4462 topActivityTokens.add(top.appToken);
4463 }
4464 }
4465 }
4466 }
4467 return topActivityTokens;
4468 }
Craig Mautner27084302013-03-25 08:05:25 -07004469}