blob: 80d51e50ff57669824c6d3853e50a956dac712ff [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
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700270 // The height/width divide used when fitting a task within a bounds with method
271 // {@link #fitWithinBounds}.
272 // We always want the task to to be visible in the bounds without affecting its size when
273 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
274 // the input bounds right or bottom side minus the width or height divided by this value.
275 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
276
justinzhang5286d3f2014-05-12 17:06:01 -0400277 /** Status Bar Service **/
278 private IBinder mToken = new Binder();
279 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400280 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400281
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700282 // For debugging to make sure the caller when acquiring/releasing our
283 // wake lock is the system process.
284 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800285 /** The number of distinct task ids that can be assigned to the tasks of a single user */
286 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700287
Craig Mautner27084302013-03-25 08:05:25 -0700288 final ActivityManagerService mService;
289
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800290 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800291
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700292 final ActivityStackSupervisorHandler mHandler;
293
294 /** Short cut */
295 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800296 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700297
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700298 /** Counter for next free stack ID to use for dynamic activity stacks. */
299 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700300
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800301 /**
302 * Maps the task identifier that activities are currently being started in to the userId of the
303 * task. Each time a new task is created, the entry for the userId of the task is incremented
304 */
305 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700306
Craig Mautner2420ead2013-04-01 17:13:20 -0700307 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800308 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700309
Craig Mautnere0a38842013-12-16 16:14:02 -0800310 /** The stack containing the launcher app. Assumed to always be attached to
311 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800312 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700313
Craig Mautnere0a38842013-12-16 16:14:02 -0800314 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800315 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700316
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
318 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800319 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321
322 /** List of activities that are waiting for a new activity to become visible before completing
323 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800324 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700325
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700326 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800327 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700328
329 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800330 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700331
Craig Mautnerde4ef022013-04-07 19:01:33 -0700332 /** List of activities that are ready to be stopped, but waiting for the next activity to
333 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800334 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700335
Craig Mautnerf3333272013-04-22 10:55:53 -0700336 /** List of activities that are ready to be finished, but waiting for the previous activity to
337 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800338 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700339
Craig Mautner0eea92c2013-05-16 13:35:39 -0700340 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800341 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700342
Wale Ogunwale22e25262016-02-01 10:32:02 -0800343 /** List of activities whose multi-window mode changed that we need to report to the
344 * application */
345 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
346
347 /** List of activities whose picture-in-picture mode changed that we need to report to the
348 * application */
349 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
350
Craig Mautnerf3333272013-04-22 10:55:53 -0700351 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700352 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700353
Craig Mautnerde4ef022013-04-07 19:01:33 -0700354 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
355 * is being brought in front of us. */
356 boolean mUserLeaving = false;
357
Craig Mautner0eea92c2013-05-16 13:35:39 -0700358 /** Set when we have taken too long waiting to go to sleep. */
359 boolean mSleepTimeout = false;
360
361 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700362 * We don't want to allow the device to go to sleep while in the process
363 * of launching an activity. This is primarily to allow alarm intent
364 * receivers to launch an activity and get that to run before the device
365 * goes back to sleep.
366 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700367 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700368
369 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700370 * Set when the system is going to sleep, until we have
371 * successfully paused the current activity and released our wake lock.
372 * At that point the system is allowed to actually sleep.
373 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700374 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700375
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700376 /** Stack id of the front stack when user switched, indexed by userId. */
377 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700378
Craig Mautner4504de52013-12-20 09:06:56 -0800379 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700381 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800382
383 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700384 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800385
Jeff Brownca9bc702014-02-11 14:32:56 -0800386 InputManagerInternal mInputManagerInternal;
387
Craig Mautner15df08a2015-04-01 12:17:18 -0700388 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
389 * may be finished until there is only one entry left. If this is empty the system is not
390 * in lockTask mode. */
391 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000392 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700393 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
394 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000395 */
396 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400397 /**
398 * Notifies the user when entering/exiting lock-task.
399 */
400 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800401
Wale Ogunwaled046a012015-12-24 13:05:59 -0800402 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800403 boolean inResumeTopActivity;
404
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700405 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700406 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700407 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700408
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700409 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
410 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800411 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700412
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700413 // The default minimal size that will be used if the activity doesn't specify its minimal size.
414 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700415 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700416
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700417 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
418 private boolean mTaskLayersChanged = true;
419
Jorim Jaggi275561a2016-02-23 10:11:02 -0500420 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800421
Jorim Jaggidc249c42015-12-15 14:57:31 -0800422 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
423
Wale Ogunwale39381972015-12-17 17:15:29 -0800424 static class FindTaskResult {
425 ActivityRecord r;
426 boolean matchedByRootAffinity;
427 }
428 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
429
Craig Mautneree36c772014-07-16 14:56:05 -0700430 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100431 * Used to keep track whether app visibilities got changed since the last pause. Useful to
432 * determine whether to invoke the task stack change listener after pausing.
433 */
434 boolean mAppVisibilitiesChangedSinceLastPause;
435
436 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100437 * Set of tasks that are in resizing mode during an app transition to fill the "void".
438 */
439 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
440
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700441
442 /**
443 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
444 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
445 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
446 * like the docked stack going empty.
447 */
448 private boolean mAllowDockedStackResize = true;
449
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100450 /**
Tony Mak853304c2016-04-18 15:17:41 +0100451 * Is dock currently minimized.
452 */
453 boolean mIsDockMinimized;
454
455 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700456 * Description of a request to start a new activity, which has been held
457 * due to app switches being disabled.
458 */
459 static class PendingActivityLaunch {
460 final ActivityRecord r;
461 final ActivityRecord sourceRecord;
462 final int startFlags;
463 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700464 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700465
466 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700467 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700468 r = _r;
469 sourceRecord = _sourceRecord;
470 startFlags = _startFlags;
471 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700472 callerApp = _callerApp;
473 }
474
475 void sendErrorResult(String message) {
476 try {
477 if (callerApp.thread != null) {
478 callerApp.thread.scheduleCrash(message);
479 }
480 } catch (RemoteException e) {
481 Slog.e(TAG, "Exception scheduling crash of failed "
482 + "activity launcher sourceRecord=" + sourceRecord, e);
483 }
Craig Mautneree36c772014-07-16 14:56:05 -0700484 }
485 }
486
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800487 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700488 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700489 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800490 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800491 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700492 }
493
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800494 void setRecentTasks(RecentTasks recentTasks) {
495 mRecentTasks = recentTasks;
496 }
497
Jeff Brown2c43c332014-06-12 22:38:59 -0700498 /**
499 * At the time when the constructor runs, the power manager has not yet been
500 * initialized. So we initialize our wakelocks afterwards.
501 */
502 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800503 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700504 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700505 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700506 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700507 }
508
justinzhang5286d3f2014-05-12 17:06:01 -0400509 // This function returns a IStatusBarService. The value is from ServiceManager.
510 // getService and is cached.
511 private IStatusBarService getStatusBarService() {
512 synchronized (mService) {
513 if (mStatusBarService == null) {
514 mStatusBarService = IStatusBarService.Stub.asInterface(
515 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
516 if (mStatusBarService == null) {
517 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
518 }
519 }
520 return mStatusBarService;
521 }
522 }
523
Jason Monk35c62a42014-06-17 10:24:47 -0400524 private IDevicePolicyManager getDevicePolicyManager() {
525 synchronized (mService) {
526 if (mDevicePolicyManager == null) {
527 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
528 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
529 if (mDevicePolicyManager == null) {
530 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
531 }
532 }
533 return mDevicePolicyManager;
534 }
535 }
536
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700537 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800538 synchronized (mService) {
539 mWindowManager = wm;
540
541 mDisplayManager =
542 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
543 mDisplayManager.registerDisplayListener(this, null);
544
545 Display[] displays = mDisplayManager.getDisplays();
546 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
547 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800548 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700549 if (activityDisplay.mDisplay == null) {
550 throw new IllegalStateException("Default Display does not exist");
551 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800552 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700553 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800554 }
555
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800556 mHomeStack = mFocusedStack = mLastFocusedStack =
557 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800558
559 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800560 }
Craig Mautner27084302013-03-25 08:05:25 -0700561 }
562
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200563 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700564 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200565 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700566 }
567
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700568 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800569 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700570 }
571
Craig Mautnerde4ef022013-04-07 19:01:33 -0700572 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800573 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700574 }
575
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700576 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700577 if (stack == null) {
578 return false;
579 }
580
Craig Mautnerdf88d732014-01-27 09:21:32 -0800581 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
582 if (parent != null) {
583 stack = parent.task.stack;
584 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800585 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700586 }
587
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700588 /** The top most stack. */
589 boolean isFrontStack(ActivityStack stack) {
590 if (stack == null) {
591 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700593
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700594 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
595 if (parent != null) {
596 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800597 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700598 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
599 }
600
Wale Ogunwaled046a012015-12-24 13:05:59 -0800601 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800602 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
603 if (!focusCandidate.isFocusable()) {
604 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
605 focusCandidate = focusCandidate.getNextFocusableStackLocked();
606 }
607
608 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800609 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800610 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800611
Wale Ogunwaled046a012015-12-24 13:05:59 -0800612 EventLogTags.writeAmFocusedStack(
613 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
614 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
615 }
616
617 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800618 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800619 if (r != null && r.idle) {
620 checkFinishBootingLocked();
621 }
622 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700623 }
624
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700625 void moveHomeStackToFront(String reason) {
626 mHomeStack.moveToFront(reason);
627 }
628
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700629 /** Returns true if the focus activity was adjusted to the home stack top activity. */
630 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700631 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700632 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700633 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700634 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700635
Craig Mautner84984fa2014-06-19 11:19:20 -0700636 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700637
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700638 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700639 if (top == null) {
640 return false;
641 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700642 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700643 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700644 }
645
Craig Mautner299f9602015-01-26 09:47:33 -0800646 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700647 if (!mService.mBooting && !mService.mBooted) {
648 // Not ready yet!
649 return false;
650 }
651
Craig Mautner84984fa2014-06-19 11:19:20 -0700652 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700653 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700654 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700655 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700656
Craig Mautner84984fa2014-06-19 11:19:20 -0700657 if (prev != null) {
658 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
659 }
660
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700661 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
662 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800663 final String myReason = reason + " resumeHomeStackTask";
664
Mark Lua56ea122015-10-08 13:31:01 +0800665 // Only resume home activity if isn't finishing.
666 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700667 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800668 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700669 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800670 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700671 }
672
Craig Mautner8d341ef2013-03-26 09:03:27 -0700673 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700674 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700675 }
676
677 /**
678 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
679 * @param id Id of the task we would like returned.
680 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
681 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700682 * @param stackId The stack to restore the task to (default launch stack will be used if
683 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700684 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700685 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800686 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800688 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800689 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
690 ActivityStack stack = stacks.get(stackNdx);
691 TaskRecord task = stack.taskForIdLocked(id);
692 if (task != null) {
693 return task;
694 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700695 }
696 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800697
698 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700699 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800700 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800701 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700702 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800703 return null;
704 }
705
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700706 if (!restoreFromRecents) {
707 return task;
708 }
709
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700710 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700711 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
712 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800713 return null;
714 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700715 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800716 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700717 }
718
Craig Mautner6170f732013-04-02 13:05:23 -0700719 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800720 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800721 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800722 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800723 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
724 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
725 if (r != null) {
726 return r;
727 }
Craig Mautner6170f732013-04-02 13:05:23 -0700728 }
729 }
730 return null;
731 }
732
Tony Mak853304c2016-04-18 15:17:41 +0100733 /**
734 * TODO: Handle freefom mode.
735 * @return true when credential confirmation is needed for the user and there is any
736 * activity started by the user in any visible stack.
737 */
738 boolean isUserLockedProfile(@UserIdInt int userId) {
739 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
740 return false;
741 }
742 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
743 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
744 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
745 for (final ActivityStack activityStack : activityStacks) {
746 if (activityStack == null) {
747 continue;
748 }
749 if (activityStack.topRunningActivityLocked() == null) {
750 continue;
751 }
752 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
753 continue;
754 }
755 if (activityStack.isDockedStack() && mIsDockMinimized) {
756 continue;
757 }
758 final TaskRecord topTask = activityStack.topTask();
759 if (topTask == null) {
760 continue;
761 }
762 // To handle the case that work app is in the task but just is not the top one.
763 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
764 final ActivityRecord activityRecord = topTask.mActivities.get(i);
765 if (activityRecord.userId == userId) {
766 return true;
767 }
768 }
769 }
770 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000771 }
772
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800773 void setNextTaskIdForUserLocked(int taskId, int userId) {
774 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
775 if (taskId > currentTaskId) {
776 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700777 }
778 }
779
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700780 static int nextTaskIdForUser(int taskId, int userId) {
781 int nextTaskId = taskId + 1;
782 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
783 // Wrap around as there will be smaller task ids that are available now.
784 nextTaskId -= MAX_TASK_IDS_PER_USER;
785 }
786 return nextTaskId;
787 }
788
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800789 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800790 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
791 // for a userId u, a taskId can only be in the range
792 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
793 // 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 -0700794 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800795 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
796 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
797 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700798 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800799 if (candidateTaskId == currentTaskId) {
800 // Something wrong!
801 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
802 throw new IllegalStateException("Cannot get an available task id."
803 + " Reached limit of " + MAX_TASK_IDS_PER_USER
804 + " running tasks per user.");
805 }
806 }
807 mCurTaskIdForUser.put(userId, candidateTaskId);
808 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700809 }
810
Chong Zhang6cda19c2016-06-14 19:07:56 -0700811 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800812 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700813 if (stack == null) {
814 return null;
815 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700816 ActivityRecord resumedActivity = stack.mResumedActivity;
817 if (resumedActivity == null || resumedActivity.app == null) {
818 resumedActivity = stack.mPausingActivity;
819 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700820 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700821 }
822 }
823 return resumedActivity;
824 }
825
Dianne Hackbornff072722014-09-24 10:56:28 -0700826 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700827 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800829 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
830 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800831 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
832 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700833 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800834 continue;
835 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700836 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 if (hr != null) {
838 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
839 && processName.equals(hr.processName)) {
840 try {
George Mount2c92c972014-03-20 09:38:23 -0700841 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800842 didSomething = true;
843 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700844 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800845 Slog.w(TAG, "Exception in new application when starting activity "
846 + hr.intent.getComponent().flattenToShortString(), e);
847 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700848 }
Craig Mautner20e72272013-04-01 13:45:53 -0700849 }
Craig Mautner20e72272013-04-01 13:45:53 -0700850 }
851 }
852 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700853 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700854 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700855 }
Craig Mautner20e72272013-04-01 13:45:53 -0700856 return didSomething;
857 }
858
859 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800860 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
861 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800862 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
863 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700864 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800865 continue;
866 }
867 final ActivityRecord resumedActivity = stack.mResumedActivity;
868 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700869 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800870 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800871 return false;
872 }
Craig Mautner20e72272013-04-01 13:45:53 -0700873 }
874 }
Wei Wang65c7a152016-06-02 18:51:22 -0700875 // Send launch end powerhint when idle
876 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700877 return true;
878 }
879
Craig Mautnerde4ef022013-04-07 19:01:33 -0700880 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800881 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
882 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800883 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
884 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700885 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700887 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800888 return false;
889 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700890 }
891 }
892 }
893 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700894 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800895 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
896 mLastFocusedStack + " to=" + mFocusedStack);
897 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700898 return true;
899 }
900
901 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800902 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800903 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
904 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800905 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
906 final ActivityStack stack = stacks.get(stackNdx);
907 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800908 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700909 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800910 return false;
911 }
912 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800913 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700914 }
915 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800916 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700917 }
918
Craig Mautner2acc3892013-09-23 10:28:14 -0700919 /**
920 * Pause all activities in either all of the stacks or just the back stacks.
921 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700922 * @param resuming The resuming activity.
923 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
924 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700925 * @return true if any activity was paused as a result of this call.
926 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700927 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700928 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800929 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
930 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
932 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700933 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700934 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800935 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700936 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
937 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800938 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700939 }
940 }
941 return someActivityPaused;
942 }
943
Craig Mautnerde4ef022013-04-07 19:01:33 -0700944 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700945 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800946 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
947 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800948 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
949 final ActivityStack stack = stacks.get(stackNdx);
950 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700951 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800952 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700953 Slog.d(TAG_STATES,
954 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800955 pausing = false;
956 } else {
957 return false;
958 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700959 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700960 }
961 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700962 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700963 }
964
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700965 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -0700966 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500967 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800968 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
969 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
970 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
971 final ActivityStack stack = stacks.get(stackNdx);
972 if (stack.mResumedActivity != null &&
973 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700974 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800975 }
976 }
977 }
978 }
979
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800980 void cancelInitializingActivities() {
981 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
982 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
983 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
984 stacks.get(stackNdx).cancelInitializingActivities();
985 }
986 }
987 }
988
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700989 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700990 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700991 }
992
993 void sendWaitingVisibleReportLocked(ActivityRecord r) {
994 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700995 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700996 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700997 if (w.who == null) {
998 changed = true;
999 w.timeout = false;
1000 if (r != null) {
1001 w.who = new ComponentName(r.info.packageName, r.info.name);
1002 }
1003 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1004 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001005 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001006 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001007 if (changed) {
1008 mService.notifyAll();
1009 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001010 }
1011
Chong Zhang5022da32016-06-21 16:31:37 -07001012 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1013 boolean changed = false;
1014 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1015 WaitResult w = mWaitingActivityLaunched.remove(i);
1016 if (w.who == null) {
1017 changed = true;
1018 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1019 // the starting activity ends up moving another activity to front, and it should
1020 // wait for this new activity to become visible instead.
1021 // Do not modify other fields.
1022 w.result = START_TASK_TO_FRONT;
1023 }
1024 }
1025 if (changed) {
1026 mService.notifyAll();
1027 }
1028 }
1029
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001030 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1031 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001032 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001033 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001034 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001035 if (w.who == null) {
1036 changed = true;
1037 w.timeout = timeout;
1038 if (r != null) {
1039 w.who = new ComponentName(r.info.packageName, r.info.name);
1040 }
1041 w.thisTime = thisTime;
1042 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001043 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001044 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001045 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001046 if (changed) {
1047 mService.notifyAll();
1048 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001049 }
1050
Craig Mautner29219d92013-04-16 20:19:12 -07001051 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001052 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001053 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001054 if (r != null) {
1055 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001056 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001057
Andrii Kulian7318d632016-07-20 18:59:28 -07001058 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001059 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001060 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1061 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001062 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001063 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001064 if (r != null) {
1065 return r;
1066 }
1067 }
1068 }
1069 return null;
1070 }
1071
Dianne Hackborn09233282014-04-30 11:33:59 -07001072 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001073 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001074 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1075 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001076 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001077 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001078 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001079 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1080 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001081 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001082 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001083 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001084 }
1085 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001086
1087 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1088 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1089 while (maxNum > 0) {
1090 long mostRecentActiveTime = Long.MIN_VALUE;
1091 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001092 final int numTaskLists = runningTaskLists.size();
1093 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1094 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001095 if (!stackTaskList.isEmpty()) {
1096 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1097 if (lastActiveTime > mostRecentActiveTime) {
1098 mostRecentActiveTime = lastActiveTime;
1099 selectedStackList = stackTaskList;
1100 }
1101 }
1102 }
1103 if (selectedStackList != null) {
1104 list.add(selectedStackList.remove(0));
1105 --maxNum;
1106 } else {
1107 break;
1108 }
1109 }
Craig Mautner20e72272013-04-01 13:45:53 -07001110 }
1111
Todd Kennedy7440f172015-12-09 14:31:22 -08001112 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1113 ProfilerInfo profilerInfo) {
1114 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001115 if (aInfo != null) {
1116 // Store the found target back into the intent, because now that
1117 // we have it we never want to do this again. For example, if the
1118 // user navigates back to this point in the history, we should
1119 // always restart the exact same activity.
1120 intent.setComponent(new ComponentName(
1121 aInfo.applicationInfo.packageName, aInfo.name));
1122
1123 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001124 if (!aInfo.processName.equals("system")) {
1125 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001126 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001127 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001128
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001129 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1130 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1131 }
1132
Man Caocfa78b22015-06-11 20:14:34 -07001133 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1134 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1135 }
1136
1137 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001138 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001139 }
1140 }
1141 }
1142 return aInfo;
1143 }
1144
Todd Kennedy7440f172015-12-09 14:31:22 -08001145 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001146 return resolveIntent(intent, resolvedType, userId, 0);
1147 }
1148
1149 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001150 try {
1151 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001152 PackageManager.MATCH_DEFAULT_ONLY | flags
1153 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001154 } catch (RemoteException e) {
1155 }
1156 return null;
1157 }
1158
1159 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1160 ProfilerInfo profilerInfo, int userId) {
1161 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1162 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1163 }
1164
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001165 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1166 boolean andResume, boolean checkConfig) throws RemoteException {
1167
1168 if (!allPausedActivitiesComplete()) {
1169 // While there are activities pausing we skipping starting any new activities until
1170 // pauses are complete. NOTE: that we also do this for activities that are starting in
1171 // the paused state because they will first be resumed then paused on the client side.
1172 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1173 "realStartActivityLocked: Skipping start of r=" + r
1174 + " some activities pausing...");
1175 return false;
1176 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001177
Craig Mautner2568c3a2015-03-26 14:22:34 -07001178 if (andResume) {
1179 r.startFreezingScreenLocked(app, 0);
1180 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001181
Craig Mautner2568c3a2015-03-26 14:22:34 -07001182 // schedule launch ticks to collect information about slow apps.
1183 r.startLaunchTickingLocked();
1184 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001185
1186 // Have the window manager re-evaluate the orientation of
1187 // the screen based on the new activity order. Note that
1188 // as a result of this, it can call back into the activity
1189 // manager with a new orientation. We don't care about that,
1190 // because the activity is not currently running so we are
1191 // just restarting it anyway.
1192 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001193 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Andrii Kulian8072d112016-09-16 11:11:01 -07001194 mService.mGlobalConfiguration,
Craig Mautner2420ead2013-04-01 17:13:20 -07001195 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001196 // Deferring resume here because we're going to launch new activity shortly.
1197 // We don't want to perform a redundant launch of the same record while ensuring
1198 // configurations and trying to resume top activity of focused stack.
1199 mService.updateConfigurationLocked(config, r, false, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -07001200 }
1201
1202 r.app = app;
1203 app.waitingToKill = null;
1204 r.launchCount++;
1205 r.lastLaunchTime = SystemClock.uptimeMillis();
1206
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001207 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001208
1209 int idx = app.activities.indexOf(r);
1210 if (idx < 0) {
1211 app.activities.add(r);
1212 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001213 mService.updateLruProcessLocked(app, true, null);
1214 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001215
Craig Mautner15df08a2015-04-01 12:17:18 -07001216 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001217 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1218 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001219 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001220 }
1221
1222 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001223 try {
1224 if (app.thread == null) {
1225 throw new RemoteException();
1226 }
1227 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001228 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001229 if (andResume) {
1230 results = r.results;
1231 newIntents = r.newIntents;
1232 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001233 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1234 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1235 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001236 if (andResume) {
1237 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1238 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001239 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001241 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001242 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001243 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001244 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001245 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1246 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 r.sleeping = false;
1248 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001249 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001250 mService.showAskCompatModeDialogLocked(r);
1251 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001252 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1254 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1255 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001256 final String profileFile = mService.mProfileFile;
1257 if (profileFile != null) {
1258 ParcelFileDescriptor profileFd = mService.mProfileFd;
1259 if (profileFd != null) {
1260 try {
1261 profileFd = profileFd.dup();
1262 } catch (IOException e) {
1263 if (profileFd != null) {
1264 try {
1265 profileFd.close();
1266 } catch (IOException o) {
1267 }
1268 profileFd = null;
1269 }
1270 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001271 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001272
1273 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1274 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 }
1276 }
1277 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001278
Craig Mautner2568c3a2015-03-26 14:22:34 -07001279 if (andResume) {
1280 app.hasShownUi = true;
1281 app.pendingUiClean = true;
1282 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001283 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001285 System.identityHashCode(r), r.info,
1286 new Configuration(mService.mGlobalConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001287 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001288 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001289 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001290
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001291 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001292 // This may be a heavy-weight process! Note that the package
1293 // manager will ensure that only activity can run in the main
1294 // process of the .apk, which is the only thing that will be
1295 // considered heavy-weight.
1296 if (app.processName.equals(app.info.packageName)) {
1297 if (mService.mHeavyWeightProcess != null
1298 && mService.mHeavyWeightProcess != app) {
1299 Slog.w(TAG, "Starting new heavy weight process " + app
1300 + " when already running "
1301 + mService.mHeavyWeightProcess);
1302 }
1303 mService.mHeavyWeightProcess = app;
1304 Message msg = mService.mHandler.obtainMessage(
1305 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1306 msg.obj = r;
1307 mService.mHandler.sendMessage(msg);
1308 }
1309 }
1310
1311 } catch (RemoteException e) {
1312 if (r.launchFailed) {
1313 // This is the second time we failed -- finish activity
1314 // and give up.
1315 Slog.e(TAG, "Second failure launching "
1316 + r.intent.getComponent().flattenToShortString()
1317 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001318 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001319 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1320 "2nd-crash", false);
1321 return false;
1322 }
1323
1324 // This is the first time we failed -- restart process and
1325 // retry.
1326 app.activities.remove(r);
1327 throw e;
1328 }
1329
1330 r.launchFailed = false;
1331 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001332 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001333 }
1334
1335 if (andResume) {
1336 // As part of the process of launching, ActivityThread also performs
1337 // a resume.
1338 stack.minimalResumeActivityLocked(r);
1339 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001340 // This activity is not starting in the resumed state... which should look like we asked
1341 // it to pause+stop (but remain visible), and it has done so and reported back the
1342 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001343 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001344 "Moving to PAUSED: " + r + " (starting in paused state)");
1345 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001346 }
1347
1348 // Launch the new version setup screen if needed. We do this -after-
1349 // launching the initial activity (that is, home), so that it can have
1350 // a chance to initialize itself while in the background, making the
1351 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001352 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001353 mService.startSetupActivityLocked();
1354 }
1355
Dianne Hackborn465fa392014-09-14 14:21:18 -07001356 // Update any services we are bound to that might care about whether
1357 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001358 if (r.app != null) {
1359 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1360 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001361
Craig Mautner2420ead2013-04-01 17:13:20 -07001362 return true;
1363 }
1364
Craig Mautnere79d42682013-04-01 19:01:53 -07001365 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001366 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001367 // Is this activity's application already running?
1368 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001369 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001370
1371 r.task.stack.setLaunchTime(r);
1372
1373 if (app != null && app.thread != null) {
1374 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001375 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1376 || !"android".equals(r.info.packageName)) {
1377 // Don't add this if it is a platform component that is marked
1378 // to run in multiple processes, because this is actually
1379 // part of the framework so doesn't make sense to track as a
1380 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001381 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1382 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001383 }
George Mount2c92c972014-03-20 09:38:23 -07001384 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001385 return;
1386 } catch (RemoteException e) {
1387 Slog.w(TAG, "Exception when starting activity "
1388 + r.intent.getComponent().flattenToShortString(), e);
1389 }
1390
1391 // If a dead object exception was thrown -- fall through to
1392 // restart the application.
1393 }
1394
1395 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001396 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001397 }
1398
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001399 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001400 String resultWho, int requestCode, int callingPid, int callingUid,
1401 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001402 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001403 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1404 callingUid);
1405 if (startAnyPerm == PERMISSION_GRANTED) {
1406 return true;
1407 }
1408 final int componentRestriction = getComponentRestrictionForCallingPackage(
1409 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1410 final int actionRestriction = getActionRestrictionForCallingPackage(
1411 intent.getAction(), callingPackage, callingPid, callingUid);
1412 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1413 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1414 if (resultRecord != null) {
1415 resultStack.sendActivityResultLocked(-1,
1416 resultRecord, resultWho, requestCode,
1417 Activity.RESULT_CANCELED, null);
1418 }
1419 final String msg;
1420 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1421 msg = "Permission Denial: starting " + intent.toString()
1422 + " from " + callerApp + " (pid=" + callingPid
1423 + ", uid=" + callingUid + ")" + " with revoked permission "
1424 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1425 } else if (!aInfo.exported) {
1426 msg = "Permission Denial: starting " + intent.toString()
1427 + " from " + callerApp + " (pid=" + callingPid
1428 + ", uid=" + callingUid + ")"
1429 + " not exported from uid " + aInfo.applicationInfo.uid;
1430 } else {
1431 msg = "Permission Denial: starting " + intent.toString()
1432 + " from " + callerApp + " (pid=" + callingPid
1433 + ", uid=" + callingUid + ")"
1434 + " requires " + aInfo.permission;
1435 }
1436 Slog.w(TAG, msg);
1437 throw new SecurityException(msg);
1438 }
1439
1440 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1441 final String message = "Appop Denial: starting " + intent.toString()
1442 + " from " + callerApp + " (pid=" + callingPid
1443 + ", uid=" + callingUid + ")"
1444 + " requires " + AppOpsManager.permissionToOp(
1445 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1446 Slog.w(TAG, message);
1447 return false;
1448 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1449 final String message = "Appop Denial: starting " + intent.toString()
1450 + " from " + callerApp + " (pid=" + callingPid
1451 + ", uid=" + callingUid + ")"
1452 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1453 Slog.w(TAG, message);
1454 return false;
1455 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001456 if (options != null && options.getLaunchTaskId() != -1) {
1457 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1458 callingPid, callingUid);
1459 if (startInTaskPerm != PERMISSION_GRANTED) {
1460 final String msg = "Permission Denial: starting " + intent.toString()
1461 + " from " + callerApp + " (pid=" + callingPid
1462 + ", uid=" + callingUid + ") with launchTaskId="
1463 + options.getLaunchTaskId();
1464 Slog.w(TAG, msg);
1465 throw new SecurityException(msg);
1466 }
1467 }
1468
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001469 return true;
1470 }
1471
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001472 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001473 final long identity = Binder.clearCallingIdentity();
1474 try {
1475 return UserManager.get(mService.mContext).getUserInfo(userId);
1476 } finally {
1477 Binder.restoreCallingIdentity(identity);
1478 }
1479 }
1480
Svet Ganov99b60432015-06-27 13:15:22 -07001481 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001482 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001483 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1484 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001485 == PackageManager.PERMISSION_DENIED) {
1486 return ACTIVITY_RESTRICTION_PERMISSION;
1487 }
1488
Christopher Tateff7add02015-08-17 10:23:22 -07001489 if (activityInfo.permission == null) {
1490 return ACTIVITY_RESTRICTION_NONE;
1491 }
1492
Svet Ganov99b60432015-06-27 13:15:22 -07001493 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1494 if (opCode == AppOpsManager.OP_NONE) {
1495 return ACTIVITY_RESTRICTION_NONE;
1496 }
1497
1498 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1499 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001500 if (!ignoreTargetSecurity) {
1501 return ACTIVITY_RESTRICTION_APPOP;
1502 }
Svet Ganov99b60432015-06-27 13:15:22 -07001503 }
1504
1505 return ACTIVITY_RESTRICTION_NONE;
1506 }
1507
Svetoslav7008b512015-06-24 18:47:07 -07001508 private int getActionRestrictionForCallingPackage(String action,
1509 String callingPackage, int callingPid, int callingUid) {
1510 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001511 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001512 }
1513
1514 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1515 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001516 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001517 }
1518
1519 final PackageInfo packageInfo;
1520 try {
1521 packageInfo = mService.mContext.getPackageManager()
1522 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1523 } catch (PackageManager.NameNotFoundException e) {
1524 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001525 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001526 }
1527
1528 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001529 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001530 }
1531
1532 if (mService.checkPermission(permission, callingPid, callingUid) ==
1533 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001534 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001535 }
1536
1537 final int opCode = AppOpsManager.permissionToOpCode(permission);
1538 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001539 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001540 }
1541
1542 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1543 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001544 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001545 }
1546
Svet Ganov99b60432015-06-27 13:15:22 -07001547 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001548 }
1549
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001550 void setLaunchSource(int uid) {
1551 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1552 }
1553
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001554 void acquireLaunchWakelock() {
1555 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1556 throw new IllegalStateException("Calling must be system uid");
1557 }
1558 mLaunchingActivity.acquire();
1559 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1560 // To be safe, don't allow the wake lock to be held for too long.
1561 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1562 }
1563 }
1564
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001565 /**
1566 * Called when the frontmost task is idle.
1567 * @return the state of mService.mBooting before this was called.
1568 */
1569 private boolean checkFinishBootingLocked() {
1570 final boolean booting = mService.mBooting;
1571 boolean enableScreen = false;
1572 mService.mBooting = false;
1573 if (!mService.mBooted) {
1574 mService.mBooted = true;
1575 enableScreen = true;
1576 }
1577 if (booting || enableScreen) {
1578 mService.postFinishBooting(booting, enableScreen);
1579 }
1580 return booting;
1581 }
1582
Craig Mautnerf3333272013-04-22 10:55:53 -07001583 // Checked.
1584 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1585 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001586 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001587
Craig Mautnerf3333272013-04-22 10:55:53 -07001588 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001589 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001590 int NS = 0;
1591 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001592 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001593 boolean activityRemoved = false;
1594
Wale Ogunwale7d701172015-03-11 15:36:30 -07001595 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001596 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001597 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1598 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001599 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1600 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001601 if (fromTimeout) {
1602 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001603 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001604
1605 // This is a hack to semi-deal with a race condition
1606 // in the client where it can be constructed with a
1607 // newer configuration from when we asked it to launch.
1608 // We'll update with whatever configuration it now says
1609 // it used to launch.
1610 if (config != null) {
1611 r.configuration = config;
1612 }
1613
1614 // We are now idle. If someone is waiting for a thumbnail from
1615 // us, we can now deliver.
1616 r.idle = true;
1617
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001618 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001619 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001620 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001621 }
1622 }
1623
1624 if (allResumedActivitiesIdle()) {
1625 if (r != null) {
1626 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001627 }
1628
1629 if (mLaunchingActivity.isHeld()) {
1630 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1631 if (VALIDATE_WAKE_LOCK_CALLER &&
1632 Binder.getCallingUid() != Process.myUid()) {
1633 throw new IllegalStateException("Calling must be system uid");
1634 }
1635 mLaunchingActivity.release();
1636 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001637 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001638 }
1639
1640 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001641 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001642 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001643 if ((NF = mFinishingActivities.size()) > 0) {
1644 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001645 mFinishingActivities.clear();
1646 }
1647
Craig Mautnerf3333272013-04-22 10:55:53 -07001648 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001649 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001650 mStartingUsers.clear();
1651 }
1652
Craig Mautnerf3333272013-04-22 10:55:53 -07001653 // Stop any activities that are scheduled to do so but have been
1654 // waiting for the next one to start.
1655 for (int i = 0; i < NS; i++) {
1656 r = stops.get(i);
1657 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001658 if (stack != null) {
1659 if (r.finishing) {
1660 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1661 } else {
1662 stack.stopActivityLocked(r);
1663 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001664 }
1665 }
1666
1667 // Finish any activities that are scheduled to do so but have been
1668 // waiting for the next one to start.
1669 for (int i = 0; i < NF; i++) {
1670 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001671 final ActivityStack stack = r.task.stack;
1672 if (stack != null) {
1673 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1674 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001675 }
1676
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001677 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001678 // Complete user switch
1679 if (startingUsers != null) {
1680 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001681 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001682 }
1683 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001684 }
1685
1686 mService.trimApplications();
1687 //dump();
1688 //mWindowManager.dump();
1689
Craig Mautnerf3333272013-04-22 10:55:53 -07001690 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001691 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001692 }
1693
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001694 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001695 }
1696
Craig Mautner8e569572013-10-11 17:36:59 -07001697 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001698 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001699 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1700 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001701 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1702 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1703 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001704 }
Craig Mautner19091252013-10-05 00:03:53 -07001705 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001706 }
1707
1708 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001709 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1710 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001711 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1712 stacks.get(stackNdx).closeSystemDialogsLocked();
1713 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001714 }
1715 }
1716
Craig Mautner93529a42013-10-04 15:03:13 -07001717 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001718 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001719 }
1720
Craig Mautner8d341ef2013-03-26 09:03:27 -07001721 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001722 * Update the last used stack id for non-current user (current user's last
1723 * used stack is the focused stack)
1724 */
1725 void updateUserStackLocked(int userId, ActivityStack stack) {
1726 if (userId != mCurrentUser) {
1727 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1728 }
1729 }
1730
1731 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001732 * @return true if some activity was finished (or would have finished if doit were true).
1733 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001734 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1735 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001736 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1738 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001739 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001740 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001741 if (stack.finishDisabledPackageActivitiesLocked(
1742 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001743 didSomething = true;
1744 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001745 }
1746 }
1747 return didSomething;
1748 }
1749
Dianne Hackborna413dc02013-07-12 12:02:55 -07001750 void updatePreviousProcessLocked(ActivityRecord r) {
1751 // Now that this process has stopped, we may want to consider
1752 // it to be the previous app to try to keep around in case
1753 // the user wants to return to it.
1754
1755 // First, found out what is currently the foreground app, so that
1756 // we don't blow away the previous app if this activity is being
1757 // hosted by the process that is actually still the foreground.
1758 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001759 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1760 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001761 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1762 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001763 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001764 if (stack.mResumedActivity != null) {
1765 fgApp = stack.mResumedActivity.app;
1766 } else if (stack.mPausingActivity != null) {
1767 fgApp = stack.mPausingActivity.app;
1768 }
1769 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001770 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001771 }
1772 }
1773
1774 // Now set this one as the previous process, only if that really
1775 // makes sense to.
1776 if (r.app != null && fgApp != null && r.app != fgApp
1777 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001778 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001779 mService.mPreviousProcess = r.app;
1780 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1781 }
1782 }
1783
Wale Ogunwaled046a012015-12-24 13:05:59 -08001784 boolean resumeFocusedStackTopActivityLocked() {
1785 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001786 }
1787
Wale Ogunwaled046a012015-12-24 13:05:59 -08001788 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001789 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001790 if (targetStack != null && isFocusedStack(targetStack)) {
1791 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001792 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001793 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1794 if (r == null || r.state != RESUMED) {
1795 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1796 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001797 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001798 }
1799
Todd Kennedy39bfee52016-02-24 10:28:21 -08001800 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1801 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1802 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1803 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1804 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1805 }
1806 }
1807 }
1808
Adrian Roos20d7df32016-01-12 18:59:43 +01001809 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1810 TaskRecord finishedTask = null;
1811 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001812 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1813 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001814 final int numStacks = stacks.size();
1815 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1816 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001817 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1818 if (stack == focusedStack || finishedTask == null) {
1819 finishedTask = t;
1820 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001821 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001822 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001823 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001824 }
1825
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001826 void finishVoiceTask(IVoiceInteractionSession session) {
1827 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1828 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1829 final int numStacks = stacks.size();
1830 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1831 final ActivityStack stack = stacks.get(stackNdx);
1832 stack.finishVoiceTask(session);
1833 }
1834 }
1835 }
1836
Andrii Kulianc27916642016-04-12 17:59:27 -07001837 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1838 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001839 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1840 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001841 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001842 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1843 // Caller wants the home activity moved with it. To accomplish this,
1844 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001845 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001846 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001847 if (task.stack == null) {
1848 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1849 + task + " to front. Stack is null");
1850 return;
1851 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001852
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001853 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001854 int stackId = options.getLaunchStackId();
1855 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001856 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001857 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001858 if (stackId == INVALID_STACK_ID) {
1859 stackId = task.getLaunchStackId();
1860 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001861 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001862 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1863 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1864 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001865 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1866 // still need moveTaskToFrontLocked() below for any transition settings.
1867 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001868 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1869 resizeStackLocked(stackId, bounds,
1870 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001871 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001872 } else {
1873 // WM resizeTask must be done after the task is moved to the correct stack,
1874 // because Task's setBounds() also updates dim layer's bounds, but that has
1875 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001876 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001877 false /* relayout */, false /* forced */);
1878 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001879 }
1880 }
1881
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001882 final ActivityRecord r = task.getTopActivity();
1883 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1884 r == null ? null : r.appTimeTracker, reason);
1885
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001886 if (DEBUG_STACK) Slog.d(TAG_STACK,
1887 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001888
Andrii Kulianc27916642016-04-12 17:59:27 -07001889 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1890 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001891 }
1892
Wale Ogunwale854809c2015-12-27 16:18:19 -08001893 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001894 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001895 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001896 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001897 return false;
1898 }
1899 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1900 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001901 }
1902
Craig Mautner967212c2013-04-13 21:10:58 -07001903 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001904 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001905 }
1906
1907 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001908 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1909 if (activityContainer != null) {
1910 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001911 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001912 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001913 return null;
1914 }
1915 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001916 }
1917
Craig Mautner967212c2013-04-13 21:10:58 -07001918 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001919 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001920 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1921 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001922 }
1923 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001924 }
1925
Craig Mautner4a1cb222013-12-04 16:14:06 -08001926 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001927 ActivityRecord homeActivity = getHomeActivity();
1928 if (homeActivity != null) {
1929 return homeActivity.appToken;
1930 }
1931 return null;
1932 }
1933
1934 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001935 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001936 }
1937
1938 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001939 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1940 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1941 final TaskRecord task = tasks.get(taskNdx);
1942 if (task.isHomeTask()) {
1943 final ArrayList<ActivityRecord> activities = task.mActivities;
1944 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1945 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001946 if (r.isHomeActivity()
1947 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001948 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001949 }
1950 }
1951 }
1952 }
1953 return null;
1954 }
1955
Chong Zhangb15758a2015-11-17 12:12:03 -08001956 /**
1957 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1958 * the docked stack itself, or if it's side-by-side to the docked stack.
1959 */
1960 boolean isStackDockedInEffect(int stackId) {
1961 return stackId == DOCKED_STACK_ID ||
1962 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1963 }
1964
Todd Kennedyca4d8422015-01-15 15:19:22 -08001965 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001966 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001967 ActivityContainer activityContainer =
1968 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001969 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001970 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1971 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001972 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001973 return activityContainer;
1974 }
1975
Craig Mautner34b73df2014-01-12 21:11:08 -08001976 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001977 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1978 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1979 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001980 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1981 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001982 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001983 }
1984 }
1985
Craig Mautner95da1082014-02-24 17:54:35 -08001986 void deleteActivityContainer(IActivityContainer container) {
1987 ActivityContainer activityContainer = (ActivityContainer)container;
1988 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001989 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1990 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001991 final int stackId = activityContainer.mStackId;
1992 mActivityContainers.remove(stackId);
1993 mWindowManager.removeStack(stackId);
1994 }
1995 }
1996
Jorim Jaggidc249c42015-12-15 14:57:31 -08001997 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001998 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001999 if (stackId == DOCKED_STACK_ID) {
2000 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002001 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002002 return;
2003 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002004 final ActivityStack stack = getStack(stackId);
2005 if (stack == null) {
2006 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2007 return;
2008 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002009
Jorim Jaggidc249c42015-12-15 14:57:31 -08002010 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002011 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2012 // stack size changing so things don't get out of sync.
2013 return;
2014 }
2015
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002016 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002017 mWindowManager.deferSurfaceLayout();
2018 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002019 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002020 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002021 stack.ensureVisibleActivitiesConfigurationLocked(
2022 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002023 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002024 } finally {
2025 mWindowManager.continueSurfaceLayout();
2026 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002027 }
2028 }
2029
Jorim Jaggi192086e2016-03-11 17:17:03 +01002030 void deferUpdateBounds(int stackId) {
2031 final ActivityStack stack = getStack(stackId);
2032 if (stack != null) {
2033 stack.deferUpdateBounds();
2034 }
2035 }
2036
2037 void continueUpdateBounds(int stackId) {
2038 final ActivityStack stack = getStack(stackId);
2039 if (stack != null) {
2040 stack.continueUpdateBounds();
2041 }
2042 }
2043
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002044 void notifyAppTransitionDone() {
2045 continueUpdateBounds(HOME_STACK_ID);
2046 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2047 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002048 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002049 mWindowManager.setTaskDockedResizing(taskId, false);
2050 }
2051 }
2052 mResizingTasksDuringAnimation.clear();
2053 }
2054
Jorim Jaggi192086e2016-03-11 17:17:03 +01002055 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002056 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002057 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002058
Jorim Jaggi192086e2016-03-11 17:17:03 +01002059 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2060 return;
2061 }
2062
Jorim Jaggidc249c42015-12-15 14:57:31 -08002063 mTmpBounds.clear();
2064 mTmpConfigs.clear();
2065 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002066 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002067 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2068 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002069 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002070 final TaskRecord task = tasks.get(i);
2071 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002072 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2073 // For freeform stack we don't adjust the size of the tasks to match that
2074 // of the stack, but we do try to make sure the tasks are still contained
2075 // with the bounds of the stack.
2076 tempRect2.set(task.mBounds);
2077 fitWithinBounds(tempRect2, bounds);
2078 task.updateOverrideConfiguration(tempRect2);
2079 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002080 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002081 }
2082 }
2083
2084 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2085 mTmpBounds.put(task.taskId, task.mBounds);
2086 if (tempTaskInsetBounds != null) {
2087 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2088 }
2089 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002090
2091 // We might trigger a configuration change. Save the current task bounds for freezing.
2092 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002093 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2094 mTmpBounds, mTmpInsetBounds);
2095 stack.setBounds(bounds);
2096 }
2097
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002098 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2099 final ActivityStack stack = getStack(fromStackId);
2100 if (stack == null) {
2101 return;
2102 }
2103
2104 mWindowManager.deferSurfaceLayout();
2105 try {
2106 if (fromStackId == DOCKED_STACK_ID) {
2107
2108 // We are moving all tasks from the docked stack to the fullscreen stack,
2109 // which is dismissing the docked stack, so resize all other stacks to
2110 // fullscreen here already so we don't end up with resize trashing.
2111 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2112 if (StackId.isResizeableByDockedStack(i)) {
2113 ActivityStack otherStack = getStack(i);
2114 if (otherStack != null) {
2115 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2116 true /* allowResizeInDockedMode */, DEFER_RESUME);
2117 }
2118 }
2119 }
2120
2121 // Also disable docked stack resizing since we have manually adjusted the
2122 // size of other stacks above and we don't want to trigger a docked stack
2123 // resize when we remove task from it below and it is detached from the
2124 // display because it no longer contains any tasks.
2125 mAllowDockedStackResize = false;
2126 }
2127 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2128 final int size = tasks.size();
2129 if (onTop) {
2130 for (int i = 0; i < size; i++) {
2131 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002132 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002133 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002134 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002135
2136 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2137 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002138 } else {
2139 for (int i = size - 1; i >= 0; i--) {
2140 positionTaskInStackLocked(tasks.get(i).taskId,
2141 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2142 }
2143 }
2144 } finally {
2145 mAllowDockedStackResize = true;
2146 mWindowManager.continueSurfaceLayout();
2147 }
2148 }
2149
Jorim Jaggidc249c42015-12-15 14:57:31 -08002150 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002151 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2152 boolean preserveWindows) {
2153 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2154 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2155 false /* deferResume */);
2156 }
2157
2158 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2159 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2160 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002161
2162 if (!mAllowDockedStackResize) {
2163 // Docked stack resize currently disabled.
2164 return;
2165 }
2166
Jorim Jaggidc249c42015-12-15 14:57:31 -08002167 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2168 if (stack == null) {
2169 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2170 return;
2171 }
2172
2173 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2174 mWindowManager.deferSurfaceLayout();
2175 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002176 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2177 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002178 ActivityRecord r = stack.topRunningActivityLocked();
2179 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2180 tempDockedTaskInsetBounds);
2181
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002182 // TODO: Checking for isAttached might not be needed as if the user passes in null
2183 // dockedBounds then they want the docked stack to be dismissed.
2184 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2185 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002186 // In this case we make all other static stacks fullscreen and move all
2187 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002188 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002189
2190 // stack shouldn't contain anymore activities, so nothing to resume.
2191 r = null;
2192 } else {
2193 // Docked stacks occupy a dedicated region on screen so the size of all other
2194 // static stacks need to be adjusted so they don't overlap with the docked stack.
2195 // We get the bounds to use from window manager which has been adjusted for any
2196 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002197 mWindowManager.getStackDockedModeBounds(
2198 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002199 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002200 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2201 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2202 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002203 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002204 }
2205 }
2206 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002207 if (!deferResume) {
2208 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2209 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002210 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002211 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002212 mWindowManager.continueSurfaceLayout();
2213 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2214 }
2215
2216 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2217 tempDockedTaskBounds != null
2218 || tempDockedTaskInsetBounds != null
2219 || tempOtherTaskBounds != null
2220 || tempOtherTaskInsetBounds != null);
2221 }
2222
Robert Carr0d00c2e2016-02-29 17:45:02 -08002223 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2224 final ActivityStack stack = getStack(PINNED_STACK_ID);
2225 if (stack == null) {
2226 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2227 return;
2228 }
2229 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2230 mWindowManager.deferSurfaceLayout();
2231 try {
2232 ActivityRecord r = stack.topRunningActivityLocked();
2233 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2234 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002235 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002236 } finally {
2237 mWindowManager.continueSurfaceLayout();
2238 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2239 }
2240 }
2241
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002242 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2243 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002244 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002245 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002246 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002247 }
2248
Chong Zhang87b21722015-09-21 15:39:51 -07002249 // If this is a forced resize, let it go through even if the bounds is not changing,
2250 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002251 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002252 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002253 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002254 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002255 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002256 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002257
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002258 if (!mWindowManager.isValidTaskId(task.taskId)) {
2259 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002260 // All we can do for now is update the bounds so it can be used when the task is
2261 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002262 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002263 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2264 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002265 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002266 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002267 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002268 }
2269
Chong Zhang6de2ae82015-09-30 18:25:21 -07002270 // Do not move the task to another stack here.
2271 // This method assumes that the task is already placed in the right stack.
2272 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002273
Chong Zhang6de2ae82015-09-30 18:25:21 -07002274 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002275
Andrii Kulian8072d112016-09-16 11:11:01 -07002276 final boolean updatedConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002277 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002278 // way and the activity was kept the way it was. If it's false, it means the activity had
2279 // to be relaunched due to configuration change.
2280 boolean kept = true;
Andrii Kulian8072d112016-09-16 11:11:01 -07002281 if (updatedConfig) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002282 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002283 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002284 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002285 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002286
2287 if (!deferResume) {
2288
2289 // All other activities must be made visible with their correct configuration.
2290 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2291 if (!kept) {
2292 resumeFocusedStackTopActivityLocked();
2293 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002294 }
2295 }
2296 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002297 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002298
2299 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002300 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002301 }
2302
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002303 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002304 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2305 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002306 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002307 }
2308
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002309 ActivityContainer activityContainer = new ActivityContainer(stackId);
2310 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002311 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002312 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002313 }
2314
2315 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002316 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002317 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2318 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002319 break;
2320 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002321 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002322 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002323 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002324 }
2325
Chong Zhang5dcb2752015-08-18 13:50:26 -07002326 /**
2327 * Restores a recent task to a stack
2328 * @param task The recent task to be restored.
2329 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002330 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002331 * @return true if the task has been restored successfully.
2332 */
2333 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2334 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002335 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002336 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2337 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2338 // Put it in the fullscreen stack.
2339 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002340 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002341
Wale Ogunwale706ed792015-08-02 10:29:44 -07002342 if (task.stack != null) {
2343 // Task has already been restored once. See if we need to do anything more
2344 if (task.stack.mStackId == stackId) {
2345 // Nothing else to do since it is already restored in the right stack.
2346 return true;
2347 }
2348 // Remove current stack association, so we can re-associate the task with the
2349 // right stack below.
Wale Ogunwale06579d62016-04-30 15:29:06 -07002350 task.stack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002351 }
2352
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002353 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002354 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002355
2356 if (stack == null) {
2357 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002358 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2359 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002360 return false;
2361 }
2362
Wale Ogunwale5f986092015-12-04 15:35:38 -08002363 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002364 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2365 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002366 final ArrayList<ActivityRecord> activities = task.mActivities;
2367 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002368 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002369 }
2370 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002371 }
2372
Wale Ogunwale040b4702015-08-06 18:10:50 -07002373 /**
2374 * Moves the specified task record to the input stack id.
2375 * WARNING: This method performs an unchecked/raw move of the task and
2376 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002377 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002378 * @param task Task to move.
2379 * @param stackId Id of stack to move task to.
2380 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002381 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002382 * @param reason Reason the task is been moved.
2383 * @return The stack the task was moved to.
2384 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002385 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002386 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002387
2388 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2389 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2390 + "support multi-window task=" + task + " to stackId=" + stackId);
2391 }
2392
Wale Ogunwale06579d62016-04-30 15:29:06 -07002393 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002394 final ActivityStack prevStack = task.stack;
2395 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002396 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002397 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2398 // Whenever we are moving the top activity from the front stack we want to make sure to move
2399 // the stack to the front.
2400 final boolean wasFront = isFrontStack(prevStack)
2401 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002402
Chong Zhangd545dce2016-02-29 18:09:17 -08002403 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002404 // We don't allow moving a unresizeable task to the docked stack since the docked
2405 // stack is used for split-screen mode and will cause things like the docked divider to
2406 // show up. We instead leave the task in its current stack or move it to the fullscreen
2407 // stack if it isn't currently in a stack.
2408 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002409 Slog.w(TAG, "Can not move unresizeable task=" + task
2410 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2411 }
2412
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002413 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2414 // if a docked stack is created below which will lead to the stack we are moving from and
2415 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002416 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002417 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002418 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002419 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002420 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002421
2422 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002423 // move focus to the new stack by moving the stack to the front.
2424 stack.moveToFrontAndResumeStateIfNeeded(
2425 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002426
Wale Ogunwale040b4702015-08-06 18:10:50 -07002427 return stack;
2428 }
2429
Chong Zhange4fbd322016-03-01 14:44:03 -08002430 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002431 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002432 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2433 false /* deferResume */);
2434 }
2435
2436 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2437 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002438 final TaskRecord task = anyTaskForIdLocked(taskId);
2439 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002440 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002441 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002442 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002443
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002444 if (task.stack != null && task.stack.mStackId == stackId) {
2445 // You are already in the right stack silly...
2446 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002447 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002448 }
2449
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002450 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2451 throw new IllegalArgumentException("moveTaskToStack:"
2452 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2453 }
2454
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002455 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002456 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002457 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002458 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002459 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002460 // We are about to relaunch the activity because its configuration changed due to
2461 // being maximized, i.e. size change. The activity will first remove the old window
2462 // and then add a new one. This call will tell window manager about this, so it can
2463 // preserve the old window until the new one is drawn. This prevents having a gap
2464 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002465 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002466 // Note here we always set the replacing window first, as the flags might be needed
2467 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002468 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002469 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002470
2471 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002472 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002473 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002474 try {
2475 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002476 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002477 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002478
Wale Ogunwale961f4852016-02-01 20:25:54 -08002479 if (!animate) {
2480 stack.mNoAnimActivities.add(topActivity);
2481 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002482
Wale Ogunwale961f4852016-02-01 20:25:54 -08002483 // We might trigger a configuration change. Save the current task bounds for freezing.
2484 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2485
2486 // Make sure the task has the appropriate bounds/size for the stack it is in.
2487 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002488 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2489 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002490 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2491 Rect bounds = task.getLaunchBounds();
2492 if (bounds == null) {
2493 stack.layoutTaskInStack(task, null);
2494 bounds = task.mBounds;
2495 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002496 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2497 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002498 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002499 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2500 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002501 }
2502 } finally {
2503 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002504 }
2505
2506 if (mightReplaceWindow) {
2507 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2508 // window), we need to clear the replace window settings. Otherwise, we schedule a
2509 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002510 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002511 }
2512
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002513 if (!deferResume) {
2514
2515 // The task might have already been running and its visibility needs to be synchronized with
2516 // the visibility of the stack / windows.
2517 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2518 resumeFocusedStackTopActivityLocked();
2519 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002520
Jorim Jaggid53f0922016-04-06 22:16:23 -07002521 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002522
2523 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002524 }
2525
Wale Ogunwale079a0042015-10-24 11:44:07 -07002526 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2527 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2528 if (stack == null) {
2529 throw new IllegalArgumentException(
2530 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2531 }
2532
2533 final ActivityRecord r = stack.topRunningActivityLocked();
2534 if (r == null) {
2535 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2536 + " in stack=" + stack);
2537 return false;
2538 }
2539
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002540 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002541 Slog.w(TAG,
2542 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002543 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002544 return false;
2545 }
2546
Wale Ogunwale480dca02016-02-06 13:58:29 -08002547 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002548 return true;
2549 }
2550
Wale Ogunwale480dca02016-02-06 13:58:29 -08002551 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2552 mWindowManager.deferSurfaceLayout();
2553 try {
2554 final TaskRecord task = r.task;
2555
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002556 if (r == task.stack.getVisibleBehindActivity()) {
2557 // An activity can't be pinned and visible behind at the same time. Go ahead and
2558 // release it from been visible behind before pinning.
2559 requestVisibleBehindLocked(r, false);
2560 }
2561
Wale Ogunwale480dca02016-02-06 13:58:29 -08002562 // Need to make sure the pinned stack exist so we can resize it below...
2563 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2564
2565 // Resize the pinned stack to match the current size of the task the activity we are
2566 // going to be moving is currently contained in. We do this to have the right starting
2567 // animation bounds for the pinned stack to the desired bounds the caller wants.
2568 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2569 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002570 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002571
2572 if (task.mActivities.size() == 1) {
2573 // There is only one activity in the task. So, we can just move the task over to
2574 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002575 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2576 // Move the home stack forward if the task we just moved to the pinned stack
2577 // was launched from home so home should be visible behind it.
2578 moveHomeStackToFront(reason);
2579 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002580 moveTaskToStackLocked(
2581 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2582 } else {
2583 stack.moveActivityToStack(r);
2584 }
2585 } finally {
2586 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002587 }
2588
Wale Ogunwale480dca02016-02-06 13:58:29 -08002589 // The task might have already been running and its visibility needs to be synchronized
2590 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002591 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002592 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002593
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002594 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002595 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002596 }
2597
Chong Zhang6cda19c2016-06-14 19:07:56 -07002598 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2599 if (r == null || !r.isFocusable()) {
2600 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2601 "moveActivityStackToFront: unfocusable r=" + r);
2602 return false;
2603 }
2604
2605 final TaskRecord task = r.task;
2606 if (task == null || task.stack == null) {
2607 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2608 + r + " task=" + task);
2609 return false;
2610 }
2611
2612 final ActivityStack stack = task.stack;
2613 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2614 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2615 "moveActivityStackToFront: already on top, r=" + r);
2616 return false;
2617 }
2618
2619 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2620 "moveActivityStackToFront: r=" + r);
2621
2622 stack.moveToFront(reason, task);
2623 return true;
2624 }
2625
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002626 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2627 final TaskRecord task = anyTaskForIdLocked(taskId);
2628 if (task == null) {
2629 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2630 return;
2631 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002632 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2633
2634 task.updateOverrideConfigurationForStack(stack);
2635
2636 mWindowManager.positionTaskInStack(
2637 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002638 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002639 // The task might have already been running and its visibility needs to be synchronized with
2640 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002641 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002642 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002643 }
2644
Craig Mautnerac6f8432013-07-17 13:24:59 -07002645 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002646 mTmpFindTaskResult.r = null;
2647 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002648 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002649 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2650 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002651 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2652 final ActivityStack stack = stacks.get(stackNdx);
2653 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002654 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002655 continue;
2656 }
2657 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002658 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2659 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002660 continue;
2661 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002662 stack.findTaskLocked(r, mTmpFindTaskResult);
2663 // It is possible to have task in multiple stacks with the same root affinity.
2664 // If the match we found was based on root affinity we keep on looking to see if
2665 // there is a better match in another stack. We eventually return the match based
2666 // on root affinity if we don't find a better match.
2667 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2668 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002669 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002670 }
2671 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002672 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2673 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002674 }
2675
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002676 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2677 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002678 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2679 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002680 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002681 final ActivityRecord ar = stacks.get(stackNdx)
2682 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002683 if (ar != null) {
2684 return ar;
2685 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002686 }
2687 }
2688 return null;
2689 }
2690
Craig Mautner8d341ef2013-03-26 09:03:27 -07002691 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002692 scheduleSleepTimeout();
2693 if (!mGoingToSleep.isHeld()) {
2694 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002695 if (mLaunchingActivity.isHeld()) {
2696 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2697 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002698 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002699 mLaunchingActivity.release();
2700 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002701 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002702 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002703 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002704 }
2705
2706 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002707 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002708
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002709 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002710 final long endTime = System.currentTimeMillis() + timeout;
2711 while (true) {
2712 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002713 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2714 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002715 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2716 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2717 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002718 }
2719 if (cantShutdown) {
2720 long timeRemaining = endTime - System.currentTimeMillis();
2721 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002722 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002723 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002724 } catch (InterruptedException e) {
2725 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002726 } else {
2727 Slog.w(TAG, "Activity manager shutdown timed out");
2728 timedout = true;
2729 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002730 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002731 } else {
2732 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002733 }
2734 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002735
2736 // Force checkReadyForSleep to complete.
2737 mSleepTimeout = true;
2738 checkReadyForSleepLocked();
2739
Craig Mautner8d341ef2013-03-26 09:03:27 -07002740 return timedout;
2741 }
2742
2743 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002744 removeSleepTimeouts();
2745 if (mGoingToSleep.isHeld()) {
2746 mGoingToSleep.release();
2747 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002748 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2749 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002750 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2751 final ActivityStack stack = stacks.get(stackNdx);
2752 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002753 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002754 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002755 }
Craig Mautner5314a402013-09-26 12:40:16 -07002756 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002757 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002758 mGoingToSleepActivities.clear();
2759 }
2760
2761 void activitySleptLocked(ActivityRecord r) {
2762 mGoingToSleepActivities.remove(r);
2763 checkReadyForSleepLocked();
2764 }
2765
2766 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002767 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002768 // Do not care.
2769 return;
2770 }
2771
2772 if (!mSleepTimeout) {
2773 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002774 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2775 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002776 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2777 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2778 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002779 }
2780
2781 if (mStoppingActivities.size() > 0) {
2782 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002783 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002784 + mStoppingActivities.size() + " activities");
2785 scheduleIdleLocked();
2786 dontSleep = true;
2787 }
2788
2789 if (mGoingToSleepActivities.size() > 0) {
2790 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002791 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002792 + mGoingToSleepActivities.size() + " activities");
2793 dontSleep = true;
2794 }
2795
2796 if (dontSleep) {
2797 return;
2798 }
2799 }
2800
Wei Wang65c7a152016-06-02 18:51:22 -07002801 // Send launch end powerhint before going sleep
2802 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2803
Craig Mautnere0a38842013-12-16 16:14:02 -08002804 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2805 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002806 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2807 stacks.get(stackNdx).goToSleep();
2808 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002809 }
2810
2811 removeSleepTimeouts();
2812
2813 if (mGoingToSleep.isHeld()) {
2814 mGoingToSleep.release();
2815 }
2816 if (mService.mShuttingDown) {
2817 mService.notifyAll();
2818 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002819 }
2820
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002821 boolean reportResumedActivityLocked(ActivityRecord r) {
2822 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002823 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002824 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002825 }
2826 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002827 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002828 mWindowManager.executeAppTransition();
2829 return true;
2830 }
2831 return false;
2832 }
2833
Craig Mautner8d341ef2013-03-26 09:03:27 -07002834 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002835 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2836 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002837 int stackNdx = stacks.size() - 1;
2838 while (stackNdx >= 0) {
2839 stacks.get(stackNdx).handleAppCrashLocked(app);
2840 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002841 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002842 }
2843 }
2844
Jose Lima4b6c6692014-08-12 17:41:12 -07002845 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002846 final ActivityStack stack = r.task.stack;
2847 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002848 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2849 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002850 return false;
2851 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002852
2853 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2854 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2855 + " visible=" + visible + " stackId=" + stack.mStackId
2856 + " can't contain visible behind activities");
2857 return false;
2858 }
2859
Jose Lima4b6c6692014-08-12 17:41:12 -07002860 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002861 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2862 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002863
2864 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002865 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002866 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002867 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002868 return true;
2869 }
2870
2871 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002872 if (visible && top.fullscreen) {
2873 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002874 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2875 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2876 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2877 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002878 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002879 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2880 // Only the activity set as currently visible behind should actively reset its
2881 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002882 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2883 "requestVisibleBehind: returning visible=" + visible
2884 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2885 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002886 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002887 }
2888
Jose Lima4b6c6692014-08-12 17:41:12 -07002889 stack.setVisibleBehindActivity(visible ? r : null);
2890 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002891 // If there is a translucent home activity, we need to force it stop being translucent,
2892 // because we can't depend on the application to necessarily perform that operation.
2893 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002894 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002895 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002896 mService.convertFromTranslucent(next.appToken);
2897 }
2898 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002899 if (top.app != null && top.app.thread != null) {
2900 // Notify the top app of the change.
2901 try {
2902 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2903 } catch (RemoteException e) {
2904 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002905 }
2906 return true;
2907 }
2908
Craig Mautnerbb742462014-07-07 15:28:55 -07002909 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2910 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002911 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002912 final ActivityStack stack = task.stack;
2913
2914 r.mLaunchTaskBehind = false;
2915 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002916 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002917 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002918 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002919
2920 // When launching tasks behind, update the last active time of the top task after the new
2921 // task has been shown briefly
2922 final ActivityRecord top = stack.topActivity();
2923 if (top != null) {
2924 top.task.touchActiveTime();
2925 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002926 }
2927
2928 void scheduleLaunchTaskBehindComplete(IBinder token) {
2929 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2930 }
2931
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002932 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2933 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002934 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002935 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2936 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002937 final int topStackNdx = stacks.size() - 1;
2938 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2939 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002940 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002941 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002942 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002943 }
2944
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002945 void invalidateTaskLayers() {
2946 mTaskLayersChanged = true;
2947 }
2948
2949 void rankTaskLayersIfNeeded() {
2950 if (!mTaskLayersChanged) {
2951 return;
2952 }
2953 mTaskLayersChanged = false;
2954 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2955 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2956 int baseLayer = 0;
2957 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2958 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2959 }
2960 }
2961 }
2962
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002963 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2964 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2965 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2966 final int topStackNdx = stacks.size() - 1;
2967 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2968 final ActivityStack stack = stacks.get(stackNdx);
2969 stack.clearOtherAppTimeTrackers(except);
2970 }
2971 }
2972 }
2973
Craig Mautner8d341ef2013-03-26 09:03:27 -07002974 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2976 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002977 final int numStacks = stacks.size();
2978 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2979 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002980 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002981 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002982 }
2983 }
2984
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002985 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2986 // Examine all activities currently running in the process.
2987 TaskRecord firstTask = null;
2988 // Tasks is non-null only if two or more tasks are found.
2989 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002990 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2991 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002992 ActivityRecord r = app.activities.get(i);
2993 // First, if we find an activity that is in the process of being destroyed,
2994 // then we just aren't going to do anything for now; we want things to settle
2995 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002996 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002997 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002998 return;
2999 }
3000 // Don't consider any activies that are currently not in a state where they
3001 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003002 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3003 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003004 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003005 continue;
3006 }
3007 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003008 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003009 + " from " + r);
3010 if (firstTask == null) {
3011 firstTask = r.task;
3012 } else if (firstTask != r.task) {
3013 if (tasks == null) {
3014 tasks = new ArraySet<>();
3015 tasks.add(firstTask);
3016 }
3017 tasks.add(r.task);
3018 }
3019 }
3020 }
3021 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003022 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003023 return;
3024 }
3025 // If we have activities in multiple tasks that are in a position to be destroyed,
3026 // let's iterate through the tasks and release the oldest one.
3027 final int numDisplays = mActivityDisplays.size();
3028 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3029 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3030 // Step through all stacks starting from behind, to hit the oldest things first.
3031 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3032 final ActivityStack stack = stacks.get(stackNdx);
3033 // Try to release activities in this stack; if we manage to, we are done.
3034 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3035 return;
3036 }
3037 }
3038 }
3039 }
3040
Amith Yamasani37a40c22015-06-17 13:25:42 -07003041 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003042 final int focusStackId = mFocusedStack.getStackId();
3043 // We dismiss the docked stack whenever we switch users.
3044 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3045
3046 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003047 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003048 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003049
Craig Mautner858d8a62013-04-23 17:08:34 -07003050 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003051 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3052 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003053 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003054 final ActivityStack stack = stacks.get(stackNdx);
3055 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003056 TaskRecord task = stack.topTask();
3057 if (task != null) {
3058 mWindowManager.moveTaskToTop(task.taskId);
3059 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003060 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003061 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003062
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003063 ActivityStack stack = getStack(restoreStackId);
3064 if (stack == null) {
3065 stack = mHomeStack;
3066 }
3067 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003068 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003069 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003070 } else {
3071 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003072 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003073 }
Craig Mautner93529a42013-10-04 15:03:13 -07003074 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003075 }
3076
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003077 /** Checks whether the userid is a profile of the current user. */
3078 boolean isCurrentProfileLocked(int userId) {
3079 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003080 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003081 }
3082
Craig Mautnerde4ef022013-04-07 19:01:33 -07003083 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003084 ArrayList<ActivityRecord> stops = null;
3085
3086 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003087 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3088 ActivityRecord s = mStoppingActivities.get(activityNdx);
Wale Ogunwale3c519302016-07-14 09:17:59 -07003089 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003090 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003091 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3092 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003093 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003094 if (s.finishing) {
3095 // If this activity is finishing, it is sitting on top of
3096 // everyone else but we now know it is no longer needed...
3097 // so get rid of it. Otherwise, we need to go through the
3098 // normal flow and hide it once we determine that it is
3099 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003100 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003101 mWindowManager.setAppVisibility(s.appToken, false);
Wale Ogunwale3c519302016-07-14 09:17:59 -07003102 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003103 }
3104 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003105 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003106 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003107 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003108 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003109 }
3110 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003111 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003112 }
3113 }
3114
3115 return stops;
3116 }
3117
Craig Mautnercf910b02013-04-23 11:23:27 -07003118 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003119 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3120 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3121 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3122 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003123 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003124 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003125 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003126 if (r == null) Slog.e(TAG,
3127 "validateTop...: null top activity, stack=" + stack);
3128 else {
3129 final ActivityRecord pausing = stack.mPausingActivity;
3130 if (pausing != null && pausing == r) Slog.e(TAG,
3131 "validateTop...: top stack has pausing activity r=" + r
3132 + " state=" + state);
3133 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3134 "validateTop...: activity in front not resumed r=" + r
3135 + " state=" + state);
3136 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003137 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003138 final ActivityRecord resumed = stack.mResumedActivity;
3139 if (resumed != null && resumed == r) Slog.e(TAG,
3140 "validateTop...: back stack has resumed activity r=" + r
3141 + " state=" + state);
3142 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3143 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003144 }
3145 }
3146 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003147 }
3148
Craig Mautnere0570202015-05-13 13:06:11 -07003149 private String lockTaskModeToString() {
3150 switch (mLockTaskModeState) {
3151 case LOCK_TASK_MODE_LOCKED:
3152 return "LOCKED";
3153 case LOCK_TASK_MODE_PINNED:
3154 return "PINNED";
3155 case LOCK_TASK_MODE_NONE:
3156 return "NONE";
3157 default: return "unknown=" + mLockTaskModeState;
3158 }
3159 }
3160
Craig Mautner27084302013-03-25 08:05:25 -07003161 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003162 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003163 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003164 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003165 pw.print(prefix);
3166 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003167 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003168 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003169 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3170 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3171 if (packages.size() > 0) {
3172 pw.println(" mLockTaskPackages (userId:packages)=");
3173 for (int i = 0; i < packages.size(); ++i) {
3174 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3175 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3176 }
3177 }
3178 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003179 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003180
Winson43d1f262016-06-14 16:05:55 -07003181 /**
3182 * Dumps the activities matching the given {@param name} in the either the focused stack
3183 * or all visible stacks if {@param dumpVisibleStacks} is true.
3184 */
3185 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3186 if (dumpVisibleStacks) {
3187 ArrayList<ActivityRecord> activities = new ArrayList<>();
3188 int numDisplays = mActivityDisplays.size();
3189 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3190 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3191 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3192 ActivityStack stack = stacks.get(stackNdx);
3193 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3194 activities.addAll(stack.getDumpActivitiesLocked(name));
3195 }
3196 }
3197 }
3198 return activities;
3199 } else {
3200 return mFocusedStack.getDumpActivitiesLocked(name);
3201 }
Craig Mautner20e72272013-04-01 13:45:53 -07003202 }
3203
Dianne Hackborn390517b2013-05-30 15:03:32 -07003204 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3205 boolean needSep, String prefix) {
3206 if (activity != null) {
3207 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3208 if (needSep) {
3209 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003210 }
3211 pw.print(prefix);
3212 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003213 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003214 }
3215 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003216 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003217 }
3218
Craig Mautner8d341ef2013-03-26 09:03:27 -07003219 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3220 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003221 boolean printed = false;
3222 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003223 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3224 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003225 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003226 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003227 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003228 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003229 final ActivityStack stack = stacks.get(stackNdx);
3230 StringBuilder stackHeader = new StringBuilder(128);
3231 stackHeader.append(" Stack #");
3232 stackHeader.append(stack.mStackId);
3233 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003234 stackHeader.append("\n");
3235 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3236 stackHeader.append("\n");
3237 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003238 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3239 needSep, stackHeader.toString());
3240 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3241 !dumpAll, false, dumpPackage, true,
3242 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003243
Craig Mautner4a1cb222013-12-04 16:14:06 -08003244 needSep = printed;
3245 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3246 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003247 if (pr) {
3248 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003249 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003250 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003251 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3252 " mResumedActivity: ");
3253 if (pr) {
3254 printed = true;
3255 needSep = false;
3256 }
3257 if (dumpAll) {
3258 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3259 " mLastPausedActivity: ");
3260 if (pr) {
3261 printed = true;
3262 needSep = true;
3263 }
3264 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3265 needSep, " mLastNoHistoryActivity: ");
3266 }
3267 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003268 }
3269 }
3270
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003271 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3272 false, dumpPackage, true, " Activities waiting to finish:", null);
3273 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3274 false, dumpPackage, true, " Activities waiting to stop:", null);
3275 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3276 false, dumpPackage, true, " Activities waiting for another to become visible:",
3277 null);
3278 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3279 false, dumpPackage, true, " Activities waiting to sleep:", null);
3280 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3281 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003282
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003283 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003284 }
3285
Dianne Hackborn390517b2013-05-30 15:03:32 -07003286 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003287 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003288 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003289 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003290 String innerPrefix = null;
3291 String[] args = null;
3292 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003293 for (int i=list.size()-1; i>=0; i--) {
3294 final ActivityRecord r = list.get(i);
3295 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3296 continue;
3297 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003298 if (innerPrefix == null) {
3299 innerPrefix = prefix + " ";
3300 args = new String[0];
3301 }
3302 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003303 final boolean full = !brief && (complete || !r.isInHistory());
3304 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003305 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003306 needNL = false;
3307 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003308 if (header1 != null) {
3309 pw.println(header1);
3310 header1 = null;
3311 }
3312 if (header2 != null) {
3313 pw.println(header2);
3314 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003315 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003316 if (lastTask != r.task) {
3317 lastTask = r.task;
3318 pw.print(prefix);
3319 pw.print(full ? "* " : " ");
3320 pw.println(lastTask);
3321 if (full) {
3322 lastTask.dump(pw, prefix + " ");
3323 } else if (complete) {
3324 // Complete + brief == give a summary. Isn't that obvious?!?
3325 if (lastTask.intent != null) {
3326 pw.print(prefix); pw.print(" ");
3327 pw.println(lastTask.intent.toInsecureStringWithClip());
3328 }
3329 }
3330 }
3331 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3332 pw.print(" #"); pw.print(i); pw.print(": ");
3333 pw.println(r);
3334 if (full) {
3335 r.dump(pw, innerPrefix);
3336 } else if (complete) {
3337 // Complete + brief == give a summary. Isn't that obvious?!?
3338 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3339 if (r.app != null) {
3340 pw.print(innerPrefix); pw.println(r.app);
3341 }
3342 }
3343 if (client && r.app != null && r.app.thread != null) {
3344 // flush anything that is already in the PrintWriter since the thread is going
3345 // to write to the file descriptor directly
3346 pw.flush();
3347 try {
3348 TransferPipe tp = new TransferPipe();
3349 try {
3350 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3351 r.appToken, innerPrefix, args);
3352 // Short timeout, since blocking here can
3353 // deadlock with the application.
3354 tp.go(fd, 2000);
3355 } finally {
3356 tp.kill();
3357 }
3358 } catch (IOException e) {
3359 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3360 } catch (RemoteException e) {
3361 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3362 }
3363 needNL = true;
3364 }
3365 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003366 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003367 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003368
Craig Mautnerf3333272013-04-22 10:55:53 -07003369 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003370 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3371 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003372 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3373 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003374 }
3375
3376 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003377 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003378 }
3379
3380 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003381 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3382 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003383 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3384 }
3385
Craig Mautner05d29032013-05-03 13:40:13 -07003386 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003387 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3388 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3389 }
Craig Mautner05d29032013-05-03 13:40:13 -07003390 }
3391
Craig Mautner0eea92c2013-05-16 13:35:39 -07003392 void removeSleepTimeouts() {
3393 mSleepTimeout = false;
3394 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3395 }
3396
3397 final void scheduleSleepTimeout() {
3398 removeSleepTimeouts();
3399 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3400 }
3401
Craig Mautner4a1cb222013-12-04 16:14:06 -08003402 @Override
3403 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003404 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003405 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3406 }
3407
3408 @Override
3409 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003410 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003411 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3412 }
3413
3414 @Override
3415 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003416 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003417 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3418 }
3419
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003420 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003421 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003422 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003423 newDisplay = mActivityDisplays.get(displayId) == null;
3424 if (newDisplay) {
3425 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003426 if (activityDisplay.mDisplay == null) {
3427 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3428 return;
3429 }
Craig Mautner4504de52013-12-20 09:06:56 -08003430 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003431 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003432 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003433 }
Craig Mautner4504de52013-12-20 09:06:56 -08003434 if (newDisplay) {
3435 mWindowManager.onDisplayAdded(displayId);
3436 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003437 }
3438
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003439 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003440 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003441 mService.mContext.getResources().getDimensionPixelSize(
3442 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003443 }
3444
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003445 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003446 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003447 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3448 if (activityDisplay != null) {
3449 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003450 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003451 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003452 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003453 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003454 }
3455 }
3456 mWindowManager.onDisplayRemoved(displayId);
3457 }
3458
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003459 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003460 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003461 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3462 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003463 // TODO: Update the bounds.
3464 }
3465 }
3466 mWindowManager.onDisplayChanged(displayId);
3467 }
3468
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003469 private StackInfo getStackInfoLocked(ActivityStack stack) {
Winson529c8e42016-05-17 11:08:40 -07003470 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003471 StackInfo info = new StackInfo();
3472 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3473 info.displayId = Display.DEFAULT_DISPLAY;
3474 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003475 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003476 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003477 info.position = display != null
3478 ? display.mStacks.indexOf(stack)
3479 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003480
3481 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3482 final int numTasks = tasks.size();
3483 int[] taskIds = new int[numTasks];
3484 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003485 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003486 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003487 for (int i = 0; i < numTasks; ++i) {
3488 final TaskRecord task = tasks.get(i);
3489 taskIds[i] = task.taskId;
3490 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3491 : task.realActivity != null ? task.realActivity.flattenToString()
3492 : task.getTopActivity() != null ? task.getTopActivity().packageName
3493 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003494 taskBounds[i] = new Rect();
3495 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003496 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003497 }
3498 info.taskIds = taskIds;
3499 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003500 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003501 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003502
3503 final ActivityRecord top = stack.topRunningActivityLocked();
3504 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003505 return info;
3506 }
3507
3508 StackInfo getStackInfoLocked(int stackId) {
3509 ActivityStack stack = getStack(stackId);
3510 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003511 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003512 }
3513 return null;
3514 }
3515
3516 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003517 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003518 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3519 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003520 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003521 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003522 }
3523 }
3524 return list;
3525 }
3526
Craig Mautner15df08a2015-04-01 12:17:18 -07003527 TaskRecord getLockedTaskLocked() {
3528 final int top = mLockTaskModeTasks.size() - 1;
3529 if (top >= 0) {
3530 return mLockTaskModeTasks.get(top);
3531 }
3532 return null;
3533 }
3534
3535 boolean isLockedTask(TaskRecord task) {
3536 return mLockTaskModeTasks.contains(task);
3537 }
3538
3539 boolean isLastLockedTask(TaskRecord task) {
3540 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3541 }
3542
3543 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003544 if (!mLockTaskModeTasks.remove(task)) {
3545 return;
3546 }
3547 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3548 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003549 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003550 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3551 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003552 final Message lockTaskMsg = Message.obtain();
3553 lockTaskMsg.arg1 = task.userId;
3554 lockTaskMsg.what = LOCK_TASK_END_MSG;
3555 mHandler.sendMessage(lockTaskMsg);
3556 }
3557 }
3558
Andrii Kulianc27916642016-04-12 17:59:27 -07003559 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3560 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3561 false /* forceNonResizable */);
3562 }
3563
Jorim Jaggid53f0922016-04-06 22:16:23 -07003564 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003565 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003566 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3567 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003568 return;
3569 }
3570
Jorim Jaggicd13d332016-04-27 15:40:20 -07003571 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003572 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003573 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003574 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3575
Andrii Kulianc27916642016-04-12 17:59:27 -07003576 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3577 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003578 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003579 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3580 && !topActivity.noDisplay) {
3581 String packageName = topActivity.appInfo.packageName;
Jorim Jaggi2adba072016-03-03 13:43:39 +01003582 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3583 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003584 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003585 }
3586
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003587 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003588 if (mLockTaskNotify != null) {
3589 mLockTaskNotify.showToast(mLockTaskModeState);
3590 }
Jason Monka8f569c2014-07-07 12:15:21 -04003591 }
3592
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003593 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3594 if (mLockTaskModeTasks.contains(task)) {
3595 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3596 }
3597 }
3598
Craig Mautner432f64e2015-05-20 14:59:57 -07003599 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3600 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003601 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003602 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003603 final TaskRecord lockedTask = getLockedTaskLocked();
3604 if (lockedTask != null) {
3605 removeLockedTaskLocked(lockedTask);
3606 if (!mLockTaskModeTasks.isEmpty()) {
3607 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003608 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3609 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003610 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003611 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003612 return;
3613 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003614 }
Craig Mautnere0570202015-05-13 13:06:11 -07003615 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3616 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003617 return;
3618 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003619
3620 // Should have already been checked, but do it again.
3621 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003622 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3623 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003624 return;
3625 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003626 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003627 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003628 return;
3629 }
3630
3631 if (mLockTaskModeTasks.isEmpty()) {
3632 // First locktask.
3633 final Message lockTaskMsg = Message.obtain();
3634 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3635 lockTaskMsg.arg1 = task.userId;
3636 lockTaskMsg.what = LOCK_TASK_START_MSG;
3637 lockTaskMsg.arg2 = lockTaskModeState;
3638 mHandler.sendMessage(lockTaskMsg);
3639 }
3640 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003641 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3642 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003643 mLockTaskModeTasks.remove(task);
3644 mLockTaskModeTasks.add(task);
3645
3646 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003647 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003648 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003649
3650 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003651 findTaskToMoveToFrontLocked(task, 0, null, reason,
3652 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003653 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003654 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3655 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3656 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003657 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003658 }
3659
3660 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003661 return isLockTaskModeViolation(task, false);
3662 }
3663
3664 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3665 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003666 return false;
3667 }
3668 final int lockTaskAuth = task.mLockTaskAuth;
3669 switch (lockTaskAuth) {
3670 case LOCK_TASK_AUTH_DONT_LOCK:
3671 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003672 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003673 case LOCK_TASK_AUTH_LAUNCHABLE:
3674 case LOCK_TASK_AUTH_WHITELISTED:
3675 return false;
3676 case LOCK_TASK_AUTH_PINNABLE:
3677 // Pinnable tasks can't be launched on top of locktask tasks.
3678 return !mLockTaskModeTasks.isEmpty();
3679 default:
3680 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3681 return true;
3682 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003683 }
3684
Craig Mautner15df08a2015-04-01 12:17:18 -07003685 void onLockTaskPackagesUpdatedLocked() {
3686 boolean didSomething = false;
3687 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3688 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003689 final boolean wasWhitelisted =
3690 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3691 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003692 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003693 final boolean isWhitelisted =
3694 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3695 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3696 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003697 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003698 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3699 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003700 removeLockedTaskLocked(lockedTask);
3701 lockedTask.performClearTaskLocked();
3702 didSomething = true;
3703 }
3704 }
3705 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3706 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3707 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3708 final ActivityStack stack = stacks.get(stackNdx);
3709 stack.onLockTaskPackagesUpdatedLocked();
3710 }
3711 }
Craig Mautnere0570202015-05-13 13:06:11 -07003712 final ActivityRecord r = topRunningActivityLocked();
3713 final TaskRecord task = r != null ? r.task : null;
3714 if (mLockTaskModeTasks.isEmpty() && task != null
3715 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3716 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003717 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3718 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3719 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3720 false);
3721 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003722 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003723 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003724 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003725 }
3726 }
3727
Benjamin Franz43261142015-02-11 15:59:44 +00003728 int getLockTaskModeState() {
3729 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003730 }
3731
Jorim Jaggife89d122015-12-22 16:28:44 +01003732 void activityRelaunchedLocked(IBinder token) {
3733 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07003734 if (mService.isSleepingOrShuttingDownLocked()) {
3735 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
3736 if (r != null) {
3737 r.setSleeping(true, true);
3738 }
3739 }
Jorim Jaggife89d122015-12-22 16:28:44 +01003740 }
3741
3742 void activityRelaunchingLocked(ActivityRecord r) {
3743 mWindowManager.notifyAppRelaunching(r.appToken);
3744 }
3745
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003746 void logStackState() {
3747 mActivityMetricsLogger.logWindowState();
3748 }
3749
Andrii Kulian933076d2016-03-29 17:04:42 -07003750 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003751 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3752 final ActivityRecord r = task.mActivities.get(i);
3753 if (r.app != null && r.app.thread != null) {
3754 mMultiWindowModeChangedActivities.add(r);
3755 }
3756 }
3757
3758 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3759 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3760 }
3761 }
3762
Andrii Kulian933076d2016-03-29 17:04:42 -07003763 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003764 final ActivityStack stack = task.stack;
3765 if (prevStack == null || prevStack == stack
3766 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3767 return;
3768 }
3769
3770 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3771 final ActivityRecord r = task.mActivities.get(i);
3772 if (r.app != null && r.app.thread != null) {
3773 mPipModeChangedActivities.add(r);
3774 }
3775 }
3776
3777 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3778 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3779 }
3780 }
3781
Tony Mak853304c2016-04-18 15:17:41 +01003782 void setDockedStackMinimized(boolean minimized) {
3783 mIsDockMinimized = minimized;
3784 if (minimized) {
3785 // Docked stack is not visible, no need to confirm credentials for its top activity.
3786 return;
3787 }
3788 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3789 if (dockedStack == null) {
3790 return;
3791 }
3792 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3793 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3794 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3795 }
3796 }
3797
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003798 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003799
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003800 public ActivityStackSupervisorHandler(Looper looper) {
3801 super(looper);
3802 }
3803
Craig Mautnerf3333272013-04-22 10:55:53 -07003804 void activityIdleInternal(ActivityRecord r) {
3805 synchronized (mService) {
3806 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3807 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003808 }
3809
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003810 @Override
3811 public void handleMessage(Message msg) {
3812 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003813 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3814 synchronized (mService) {
3815 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3816 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003817 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003818 }
3819 }
3820 } break;
3821 case REPORT_PIP_MODE_CHANGED_MSG: {
3822 synchronized (mService) {
3823 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3824 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003825 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003826 }
3827 }
3828 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003829 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003830 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3831 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003832 if (mService.mDidDexOpt) {
3833 mService.mDidDexOpt = false;
3834 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3835 nmsg.obj = msg.obj;
3836 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3837 return;
3838 }
3839 // We don't at this point know if the activity is fullscreen,
3840 // so we need to be conservative and assume it isn't.
3841 activityIdleInternal((ActivityRecord)msg.obj);
3842 } break;
3843 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003844 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003845 activityIdleInternal((ActivityRecord)msg.obj);
3846 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003847 case RESUME_TOP_ACTIVITY_MSG: {
3848 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003849 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003850 }
3851 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003852 case SLEEP_TIMEOUT_MSG: {
3853 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003854 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003855 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3856 mSleepTimeout = true;
3857 checkReadyForSleepLocked();
3858 }
3859 }
3860 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003861 case LAUNCH_TIMEOUT_MSG: {
3862 if (mService.mDidDexOpt) {
3863 mService.mDidDexOpt = false;
3864 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3865 return;
3866 }
3867 synchronized (mService) {
3868 if (mLaunchingActivity.isHeld()) {
3869 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3870 if (VALIDATE_WAKE_LOCK_CALLER
3871 && Binder.getCallingUid() != Process.myUid()) {
3872 throw new IllegalStateException("Calling must be system uid");
3873 }
3874 mLaunchingActivity.release();
3875 }
3876 }
3877 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003878 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003879 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003880 } break;
3881 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003882 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003883 } break;
3884 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003885 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003886 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003887 case CONTAINER_CALLBACK_VISIBILITY: {
3888 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003889 final IActivityContainerCallback callback = container.mCallback;
3890 if (callback != null) {
3891 try {
3892 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3893 } catch (RemoteException e) {
3894 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003895 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003896 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003897 case LOCK_TASK_START_MSG: {
3898 // When lock task starts, we disable the status bars.
3899 try {
Jason Monk62515be2014-05-21 16:06:19 -04003900 if (mLockTaskNotify == null) {
3901 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003902 }
Jason Monk62515be2014-05-21 16:06:19 -04003903 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003904 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003905 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003906 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003907 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003908 flags = StatusBarManager.DISABLE_MASK
3909 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003910 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003911 flags = StatusBarManager.DISABLE_MASK
3912 & (~StatusBarManager.DISABLE_BACK)
3913 & (~StatusBarManager.DISABLE_HOME)
3914 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003915 }
3916 getStatusBarService().disable(flags, mToken,
3917 mService.mContext.getPackageName());
3918 }
3919 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003920 if (getDevicePolicyManager() != null) {
3921 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003922 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003923 }
justinzhang5286d3f2014-05-12 17:06:01 -04003924 } catch (RemoteException ex) {
3925 throw new RuntimeException(ex);
3926 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003927 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003928 case LOCK_TASK_END_MSG: {
3929 // When lock task ends, we enable the status bars.
3930 try {
Jason Monk62515be2014-05-21 16:06:19 -04003931 if (getStatusBarService() != null) {
3932 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3933 mService.mContext.getPackageName());
3934 }
3935 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003936 if (getDevicePolicyManager() != null) {
3937 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3938 msg.arg1);
3939 }
Jason Monk62515be2014-05-21 16:06:19 -04003940 if (mLockTaskNotify == null) {
3941 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3942 }
3943 mLockTaskNotify.show(false);
3944 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003945 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003946 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003947 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003948 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003949 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003950 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003951 new LockPatternUtils(mService.mContext)
3952 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003953 }
3954 } catch (SettingNotFoundException e) {
3955 // No setting, don't lock.
3956 }
justinzhang5286d3f2014-05-12 17:06:01 -04003957 } catch (RemoteException ex) {
3958 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003959 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003960 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003961 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003962 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003963 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3964 if (mLockTaskNotify == null) {
3965 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3966 }
3967 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3968 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003969 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3970 final ActivityContainer container = (ActivityContainer) msg.obj;
3971 final IActivityContainerCallback callback = container.mCallback;
3972 if (callback != null) {
3973 try {
3974 callback.onAllActivitiesComplete(container.asBinder());
3975 } catch (RemoteException e) {
3976 }
3977 }
3978 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003979 case LAUNCH_TASK_BEHIND_COMPLETE: {
3980 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003981 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003982 if (r != null) {
3983 handleLaunchTaskBehindCompleteLocked(r);
3984 }
3985 }
3986 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003987
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003988 }
3989 }
3990 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003991
Ying Wangb081a592014-04-22 15:20:16 -07003992 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003993 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3994 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003995 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003996 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003997 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003998 ActivityRecord mParentActivity = null;
3999 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004000
Craig Mautnere3a00d72014-04-16 08:31:19 -07004001 boolean mVisible = true;
4002
Craig Mautner4a1cb222013-12-04 16:14:06 -08004003 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004004 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004005
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004006 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4007 final static int CONTAINER_STATE_NO_SURFACE = 1;
4008 final static int CONTAINER_STATE_FINISHING = 2;
4009 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4010
4011 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012 synchronized (mService) {
4013 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004014 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004015 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004016 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004017 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004018 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004019
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004020 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004021 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004022 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004023 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004024 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004025 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004026 }
4027
4028 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004029 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004030 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004031 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4032 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004033 return;
4034 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004035 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004036 }
4037 }
4038
4039 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004040 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004041 synchronized (mService) {
4042 if (mActivityDisplay != null) {
4043 return mActivityDisplay.mDisplayId;
4044 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004045 }
4046 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004047 }
4048
Jeff Brownca9bc702014-02-11 14:32:56 -08004049 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004050 public int getStackId() {
4051 synchronized (mService) {
4052 return mStackId;
4053 }
4054 }
4055
4056 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004057 public boolean injectEvent(InputEvent event) {
4058 final long origId = Binder.clearCallingIdentity();
4059 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004060 synchronized (mService) {
4061 if (mActivityDisplay != null) {
4062 return mInputManagerInternal.injectInputEvent(event,
4063 mActivityDisplay.mDisplayId,
4064 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4065 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004066 }
4067 return false;
4068 } finally {
4069 Binder.restoreCallingIdentity(origId);
4070 }
4071 }
4072
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004073 @Override
4074 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004075 synchronized (mService) {
4076 if (mContainerState == CONTAINER_STATE_FINISHING) {
4077 return;
4078 }
4079 mContainerState = CONTAINER_STATE_FINISHING;
4080
Craig Mautnerd163e752014-06-13 17:18:47 -07004081 long origId = Binder.clearCallingIdentity();
4082 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004083 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004084 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004085 } finally {
4086 Binder.restoreCallingIdentity(origId);
4087 }
4088 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004089 }
4090
Craig Mautner60257702014-09-17 15:02:33 -07004091 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004092 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004093 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004094 if (mActivityDisplay != null) {
4095 mActivityDisplay.detachActivitiesLocked(mStack);
4096 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004097 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004098 }
4099 }
4100
4101 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004102 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004103 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004104 }
4105
4106 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004107 public final int startActivityIntentSender(IIntentSender intentSender)
4108 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004109 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4110
4111 if (!(intentSender instanceof PendingIntentRecord)) {
4112 throw new IllegalArgumentException("Bad PendingIntent object");
4113 }
4114
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004115 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004116 Binder.getCallingUid(), mCurrentUser, false,
4117 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004118
4119 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4120 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4121 pendingIntent.key.requestResolvedType);
4122
4123 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4124 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4125 }
4126
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004127 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004128 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004129 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004130 throw new SecurityException(
4131 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4132 }
4133 }
4134
Craig Mautnerdf88d732014-01-27 09:21:32 -08004135 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004136 public IBinder asBinder() {
4137 return this;
4138 }
4139
Craig Mautner4504de52013-12-20 09:06:56 -08004140 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004141 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004142 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004143 }
4144
Craig Mautner4a1cb222013-12-04 16:14:06 -08004145 ActivityStackSupervisor getOuter() {
4146 return ActivityStackSupervisor.this;
4147 }
4148
Craig Mautnerd163e752014-06-13 17:18:47 -07004149 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004150 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004151 }
4152
Craig Mautner6985bad2014-04-21 15:22:06 -07004153 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004154 void setVisible(boolean visible) {
4155 if (mVisible != visible) {
4156 mVisible = visible;
4157 if (mCallback != null) {
4158 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4159 0 /* unused */, this).sendToTarget();
4160 }
4161 }
4162 }
4163
Craig Mautner6985bad2014-04-21 15:22:06 -07004164 void setDrawn() {
4165 }
4166
Craig Mautner1b4bf852014-05-26 15:06:32 -07004167 // You can always start a new task on a regular ActivityStack.
4168 boolean isEligibleForNewTasks() {
4169 return true;
4170 }
4171
Craig Mautnerd163e752014-06-13 17:18:47 -07004172 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004173 detachLocked();
4174 deleteActivityContainer(this);
4175 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004176 }
4177
Craig Mautner34b73df2014-01-12 21:11:08 -08004178 @Override
4179 public String toString() {
4180 return mIdString + (mActivityDisplay == null ? "N" : "A");
4181 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004182 }
4183
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004184 private class VirtualActivityContainer extends ActivityContainer {
4185 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004186 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004187
4188 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4189 super(getNextStackId());
4190 mParentActivity = parent;
4191 mCallback = callback;
4192 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004193 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004194 }
4195
4196 @Override
4197 public void setSurface(Surface surface, int width, int height, int density) {
4198 super.setSurface(surface, width, height, density);
4199
4200 synchronized (mService) {
4201 final long origId = Binder.clearCallingIdentity();
4202 try {
4203 setSurfaceLocked(surface, width, height, density);
4204 } finally {
4205 Binder.restoreCallingIdentity(origId);
4206 }
4207 }
4208 }
4209
4210 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4211 if (mContainerState == CONTAINER_STATE_FINISHING) {
4212 return;
4213 }
4214 VirtualActivityDisplay virtualActivityDisplay =
4215 (VirtualActivityDisplay) mActivityDisplay;
4216 if (virtualActivityDisplay == null) {
4217 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004218 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004219 mActivityDisplay = virtualActivityDisplay;
4220 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004221 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004222 }
4223
4224 if (mSurface != null) {
4225 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004226 }
4227
Craig Mautner6985bad2014-04-21 15:22:06 -07004228 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004229 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004230 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004231 } else {
4232 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004233 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004234 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004235 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004236 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004237 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004238
Craig Mautnerd163e752014-06-13 17:18:47 -07004239 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004240
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004241 if (DEBUG_STACK) Slog.d(TAG_STACK,
4242 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004243 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004244
Craig Mautner6985bad2014-04-21 15:22:06 -07004245 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004246 boolean isAttachedLocked() {
4247 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004248 }
4249
4250 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004251 void setDrawn() {
4252 synchronized (mService) {
4253 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004254 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004255 }
4256 }
4257
Craig Mautner1b4bf852014-05-26 15:06:32 -07004258 // Never start a new task on an ActivityView if it isn't explicitly specified.
4259 @Override
4260 boolean isEligibleForNewTasks() {
4261 return false;
4262 }
4263
Craig Mautnerd163e752014-06-13 17:18:47 -07004264 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004265 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004266 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4267 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4268 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4269 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4270 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004271 }
4272 }
4273
Craig Mautner4a1cb222013-12-04 16:14:06 -08004274 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4275 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004276 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004277 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004278 int mDisplayId;
4279 Display mDisplay;
4280 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004281
Craig Mautner4a1cb222013-12-04 16:14:06 -08004282 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4283 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004284 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004285
Jose Lima4b6c6692014-08-12 17:41:12 -07004286 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004287
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004288 ActivityDisplay() {
4289 }
Craig Mautner4504de52013-12-20 09:06:56 -08004290
Craig Mautner1a70a162014-09-13 12:09:31 -07004291 // After instantiation, check that mDisplay is not null before using this. The alternative
4292 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004293 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004294 final Display display = mDisplayManager.getDisplay(displayId);
4295 if (display == null) {
4296 return;
4297 }
4298 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004299 }
4300
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004301 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004302 mDisplay = display;
4303 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004304 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004305 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004306
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004307 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004308 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004309 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4310 + " onTop=" + onTop);
4311 if (onTop) {
4312 mStacks.add(stack);
4313 } else {
4314 mStacks.add(0, stack);
4315 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004316 }
4317
Craig Mautnere0a38842013-12-16 16:14:02 -08004318 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004319 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004320 + " from displayId=" + mDisplayId);
4321 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004322 }
4323
Jose Lima4b6c6692014-08-12 17:41:12 -07004324 void setVisibleBehindActivity(ActivityRecord r) {
4325 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004326 }
4327
Jose Lima4b6c6692014-08-12 17:41:12 -07004328 boolean hasVisibleBehindActivity() {
4329 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004330 }
4331
Craig Mautner34b73df2014-01-12 21:11:08 -08004332 @Override
4333 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004334 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4335 }
4336 }
4337
4338 class VirtualActivityDisplay extends ActivityDisplay {
4339 VirtualDisplay mVirtualDisplay;
4340
Craig Mautner6985bad2014-04-21 15:22:06 -07004341 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004342 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004343 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4344 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4345 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4346 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004347
4348 init(mVirtualDisplay.getDisplay());
4349
4350 mWindowManager.handleDisplayAdded(mDisplayId);
4351 }
4352
4353 void setSurface(Surface surface) {
4354 if (mVirtualDisplay != null) {
4355 mVirtualDisplay.setSurface(surface);
4356 }
4357 }
4358
4359 @Override
4360 void detachActivitiesLocked(ActivityStack stack) {
4361 super.detachActivitiesLocked(stack);
4362 if (mVirtualDisplay != null) {
4363 mVirtualDisplay.release();
4364 mVirtualDisplay = null;
4365 }
4366 }
4367
4368 @Override
4369 public String toString() {
4370 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004371 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004372 }
Jose Lima58e66d62014-05-27 20:00:27 -07004373
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004374 /**
4375 * Adjust bounds to stay within stack bounds.
4376 *
4377 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4378 * that keep them unchanged, but be contained within the stack bounds.
4379 *
4380 * @param bounds Bounds to be adjusted.
4381 * @param stackBounds Bounds within which the other bounds should remain.
4382 */
4383 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4384 if (stackBounds == null || stackBounds.contains(bounds)) {
4385 return;
4386 }
4387
4388 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4389 final int maxRight = stackBounds.right
4390 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4391 int horizontalDiff = stackBounds.left - bounds.left;
4392 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4393 || (bounds.left + horizontalDiff >= maxRight)) {
4394 horizontalDiff = maxRight - bounds.left;
4395 }
4396 bounds.left += horizontalDiff;
4397 bounds.right += horizontalDiff;
4398 }
4399
4400 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4401 final int maxBottom = stackBounds.bottom
4402 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4403 int verticalDiff = stackBounds.top - bounds.top;
4404 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4405 || (bounds.top + verticalDiff >= maxBottom)) {
4406 verticalDiff = maxBottom - bounds.top;
4407 }
4408 bounds.top += verticalDiff;
4409 bounds.bottom += verticalDiff;
4410 }
4411 }
4412
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004413 ActivityStack findStackBehind(ActivityStack stack) {
4414 // TODO(multi-display): We are only looking for stacks on the default display.
4415 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4416 if (display == null) {
4417 return null;
4418 }
4419 final ArrayList<ActivityStack> stacks = display.mStacks;
4420 for (int i = stacks.size() - 1; i >= 0; i--) {
4421 if (stacks.get(i) == stack && i > 0) {
4422 return stacks.get(i - 1);
4423 }
4424 }
4425 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4426 + " in=" + stacks);
4427 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004428
Jorim Jaggic69bd222016-03-15 14:38:37 +01004429 /**
4430 * Puts a task into resizing mode during the next app transition.
4431 *
4432 * @param taskId the id of the task to put into resizing mode
4433 */
4434 private void setResizingDuringAnimation(int taskId) {
4435 mResizingTasksDuringAnimation.add(taskId);
4436 mWindowManager.setTaskDockedResizing(taskId, true);
4437 }
4438
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004439 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4440 final TaskRecord task;
4441 final int callingUid;
4442 final String callingPackage;
4443 final Intent intent;
4444 final int userId;
4445 final ActivityOptions activityOptions = (bOptions != null)
4446 ? new ActivityOptions(bOptions) : null;
4447 final int launchStackId = (activityOptions != null)
4448 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004449 if (launchStackId == HOME_STACK_ID) {
4450 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4451 + taskId + " can't be launch in the home stack.");
4452 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004453
4454 if (launchStackId == DOCKED_STACK_ID) {
4455 mWindowManager.setDockedStackCreateState(
4456 activityOptions.getDockCreateMode(), null /* initialBounds */);
4457
4458 // Defer updating the stack in which recents is until the app transition is done, to
4459 // not run into issues where we still need to draw the task in recents but the
4460 // docked stack is already created.
4461 deferUpdateBounds(HOME_STACK_ID);
4462 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4463 }
4464
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004465 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4466 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004467 continueUpdateBounds(HOME_STACK_ID);
4468 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004469 throw new IllegalArgumentException(
4470 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4471 }
4472
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004473 // Since we don't have an actual source record here, we assume that the currently focused
4474 // activity was the source.
4475 final ActivityStack focusedStack = getFocusedStack();
4476 final ActivityRecord sourceRecord =
4477 focusedStack != null ? focusedStack.topActivity() : null;
4478
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004479 if (launchStackId != INVALID_STACK_ID) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004480 if (task.stack.mStackId != launchStackId) {
4481 moveTaskToStackLocked(
4482 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4483 ANIMATE);
4484 }
4485 }
4486
4487 // If the user must confirm credentials (e.g. when first launching a work app and the
4488 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4489 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4490 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004491 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004492 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004493 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004494 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4495 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004496
4497 // If we are launching the task in the docked stack, put it into resizing mode so
4498 // the window renders full-screen with the background filling the void. Also only
4499 // call this at the end to make sure that tasks exists on the window manager side.
4500 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004501 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004502 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004503
4504 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4505 ActivityManager.START_TASK_TO_FRONT,
4506 sourceRecord != null ? sourceRecord.task.stack.mStackId : INVALID_STACK_ID,
4507 sourceRecord, task.stack);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004508 return ActivityManager.START_TASK_TO_FRONT;
4509 }
4510 callingUid = task.mCallingUid;
4511 callingPackage = task.mCallingPackage;
4512 intent = task.intent;
4513 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4514 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004515 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4516 null, null, 0, 0, bOptions, userId, null, task);
4517 if (launchStackId == DOCKED_STACK_ID) {
4518 setResizingDuringAnimation(task.taskId);
4519 }
4520 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004521 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004522
4523 /**
4524 * @return a list of activities which are the top ones in each visible stack. The first
4525 * entry will be the focused activity.
4526 */
4527 public List<IBinder> getTopVisibleActivities() {
4528 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4529 if (display == null) {
4530 return Collections.EMPTY_LIST;
4531 }
4532 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4533 final ArrayList<ActivityStack> stacks = display.mStacks;
4534 for (int i = stacks.size() - 1; i >= 0; i--) {
4535 ActivityStack stack = stacks.get(i);
4536 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4537 ActivityRecord top = stack.topActivity();
4538 if (top != null) {
4539 if (stack == mFocusedStack) {
4540 topActivityTokens.add(0, top.appToken);
4541 } else {
4542 topActivityTokens.add(top.appToken);
4543 }
4544 }
4545 }
4546 }
4547 return topActivityTokens;
4548 }
Craig Mautner27084302013-03-25 08:05:25 -07004549}