blob: fc56b5c8d0d809b35107e9522e4bda33e1a6d860 [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;
126import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
127import static android.content.pm.PackageManager.PERMISSION_GRANTED;
128import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
131import 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;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
151import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
152import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
153import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
154import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800155import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100156import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggid53f0922016-04-06 22:16:23 -0700157import static com.android.server.am.ActivityManagerService.NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100158import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100159import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
160import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
161import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
162import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
163import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
164import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
165import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
166import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
167import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
168import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
169import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700170import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Tony Mak853304c2016-04-18 15:17:41 +0100171import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Winsond46b7272016-04-20 11:54:27 -0700172import static com.android.server.am.ActivityStack.STACK_VISIBLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100173import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
174import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
175import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
176import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
177import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100178import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100179
Craig Mautner4a1cb222013-12-04 16:14:06 -0800180public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800181 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700184 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700185 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700186 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700188 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800191 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800193
Craig Mautnerf3333272013-04-22 10:55:53 -0700194 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700196
Craig Mautner0eea92c2013-05-16 13:35:39 -0700197 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700199
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700200 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700202
Craig Mautner05d29032013-05-03 13:40:13 -0700203 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
204 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
205 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700206 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700207 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800208 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
209 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
210 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700211 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400212 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
213 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700214 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700215 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700216 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800217 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
218 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800219
Wale Ogunwale040b4702015-08-06 18:10:50 -0700220 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700221
Jason Monk62515be2014-05-21 16:06:19 -0400222 private static final String LOCK_TASK_TAG = "Lock-to-App";
223
Wale Ogunwale040b4702015-08-06 18:10:50 -0700224 // Used to indicate if an object (e.g. stack) that we are trying to get
225 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800226 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700227
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700228 // Used to indicate that windows of activities should be preserved during the resize.
229 static final boolean PRESERVE_WINDOWS = true;
230
Wale Ogunwale040b4702015-08-06 18:10:50 -0700231 // Used to indicate if an object (e.g. task) should be moved/created
232 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700233 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700234
235 // Used to indicate that an objects (e.g. task) removal from its container
236 // (e.g. stack) is due to it moving to another container.
237 static final boolean MOVING = true;
238
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700239 // Force the focus to change to the stack we are moving a task to..
240 static final boolean FORCE_FOCUS = true;
241
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700242 // Restore task from the saved recents if it can't be found in any live stack.
243 static final boolean RESTORE_FROM_RECENTS = true;
244
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700245 // Don't execute any calls to resume.
246 static final boolean DEFER_RESUME = true;
247
Svetoslav7008b512015-06-24 18:47:07 -0700248 // Activity actions an app cannot start if it uses a permission which is not granted.
249 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
250 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700251
Svetoslav7008b512015-06-24 18:47:07 -0700252 static {
253 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
254 Manifest.permission.CAMERA);
255 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
256 Manifest.permission.CAMERA);
257 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
258 Manifest.permission.CALL_PHONE);
259 }
260
Svet Ganov99b60432015-06-27 13:15:22 -0700261 /** Action restriction: launching the activity is not restricted. */
262 private static final int ACTIVITY_RESTRICTION_NONE = 0;
263 /** Action restriction: launching the activity is restricted by a permission. */
264 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
265 /** Action restriction: launching the activity is restricted by an app op. */
266 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700267
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700268 // The height/width divide used when fitting a task within a bounds with method
269 // {@link #fitWithinBounds}.
270 // We always want the task to to be visible in the bounds without affecting its size when
271 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
272 // the input bounds right or bottom side minus the width or height divided by this value.
273 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
274
justinzhang5286d3f2014-05-12 17:06:01 -0400275 /** Status Bar Service **/
276 private IBinder mToken = new Binder();
277 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400278 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400279
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700280 // For debugging to make sure the caller when acquiring/releasing our
281 // wake lock is the system process.
282 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800283 /** The number of distinct task ids that can be assigned to the tasks of a single user */
284 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700285
Craig Mautner27084302013-03-25 08:05:25 -0700286 final ActivityManagerService mService;
287
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800288 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800289
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700290 final ActivityStackSupervisorHandler mHandler;
291
292 /** Short cut */
293 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800294 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700295
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700296 /** Counter for next free stack ID to use for dynamic activity stacks. */
297 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700298
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800299 /**
300 * Maps the task identifier that activities are currently being started in to the userId of the
301 * task. Each time a new task is created, the entry for the userId of the task is incremented
302 */
303 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700304
Craig Mautner2420ead2013-04-01 17:13:20 -0700305 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800306 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700307
Craig Mautnere0a38842013-12-16 16:14:02 -0800308 /** The stack containing the launcher app. Assumed to always be attached to
309 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800310 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700311
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800313 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700314
Craig Mautner4a1cb222013-12-04 16:14:06 -0800315 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
316 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800317 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800318 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700319
320 /** List of activities that are waiting for a new activity to become visible before completing
321 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800322 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800325 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700326
327 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800328 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329
Craig Mautnerde4ef022013-04-07 19:01:33 -0700330 /** List of activities that are ready to be stopped, but waiting for the next activity to
331 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800332 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700333
Craig Mautnerf3333272013-04-22 10:55:53 -0700334 /** List of activities that are ready to be finished, but waiting for the previous activity to
335 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800336 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700337
Craig Mautner0eea92c2013-05-16 13:35:39 -0700338 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800339 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700340
Wale Ogunwale22e25262016-02-01 10:32:02 -0800341 /** List of activities whose multi-window mode changed that we need to report to the
342 * application */
343 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
344
345 /** List of activities whose picture-in-picture mode changed that we need to report to the
346 * application */
347 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
348
Craig Mautnerf3333272013-04-22 10:55:53 -0700349 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700350 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700351
Craig Mautnerde4ef022013-04-07 19:01:33 -0700352 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
353 * is being brought in front of us. */
354 boolean mUserLeaving = false;
355
Craig Mautner0eea92c2013-05-16 13:35:39 -0700356 /** Set when we have taken too long waiting to go to sleep. */
357 boolean mSleepTimeout = false;
358
359 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700360 * We don't want to allow the device to go to sleep while in the process
361 * of launching an activity. This is primarily to allow alarm intent
362 * receivers to launch an activity and get that to run before the device
363 * goes back to sleep.
364 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700365 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700366
367 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368 * Set when the system is going to sleep, until we have
369 * successfully paused the current activity and released our wake lock.
370 * At that point the system is allowed to actually sleep.
371 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700372 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700373
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700374 /** Stack id of the front stack when user switched, indexed by userId. */
375 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700376
Craig Mautner4504de52013-12-20 09:06:56 -0800377 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700379 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380
381 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700382 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383
Jeff Brownca9bc702014-02-11 14:32:56 -0800384 InputManagerInternal mInputManagerInternal;
385
Craig Mautner15df08a2015-04-01 12:17:18 -0700386 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
387 * may be finished until there is only one entry left. If this is empty the system is not
388 * in lockTask mode. */
389 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000390 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700391 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
392 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000393 */
394 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400395 /**
396 * Notifies the user when entering/exiting lock-task.
397 */
398 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800399
Wale Ogunwaled046a012015-12-24 13:05:59 -0800400 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800401 boolean inResumeTopActivity;
402
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700403 // 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 -0700404 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700405 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700406
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700407 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
408 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800409 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700410
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700411 // The default minimal size that will be used if the activity doesn't specify its minimal size.
412 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700413 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700414
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700415 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
416 private boolean mTaskLayersChanged = true;
417
Jorim Jaggi275561a2016-02-23 10:11:02 -0500418 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800419
Jorim Jaggidc249c42015-12-15 14:57:31 -0800420 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
421
Wale Ogunwale39381972015-12-17 17:15:29 -0800422 static class FindTaskResult {
423 ActivityRecord r;
424 boolean matchedByRootAffinity;
425 }
426 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
427
Craig Mautneree36c772014-07-16 14:56:05 -0700428 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100429 * Used to keep track whether app visibilities got changed since the last pause. Useful to
430 * determine whether to invoke the task stack change listener after pausing.
431 */
432 boolean mAppVisibilitiesChangedSinceLastPause;
433
434 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100435 * Set of tasks that are in resizing mode during an app transition to fill the "void".
436 */
437 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
438
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700439
440 /**
441 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
442 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
443 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
444 * like the docked stack going empty.
445 */
446 private boolean mAllowDockedStackResize = true;
447
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100448 /**
Tony Mak853304c2016-04-18 15:17:41 +0100449 * Is dock currently minimized.
450 */
451 boolean mIsDockMinimized;
452
453 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700454 * Description of a request to start a new activity, which has been held
455 * due to app switches being disabled.
456 */
457 static class PendingActivityLaunch {
458 final ActivityRecord r;
459 final ActivityRecord sourceRecord;
460 final int startFlags;
461 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700462 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700463
464 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700465 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700466 r = _r;
467 sourceRecord = _sourceRecord;
468 startFlags = _startFlags;
469 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700470 callerApp = _callerApp;
471 }
472
473 void sendErrorResult(String message) {
474 try {
475 if (callerApp.thread != null) {
476 callerApp.thread.scheduleCrash(message);
477 }
478 } catch (RemoteException e) {
479 Slog.e(TAG, "Exception scheduling crash of failed "
480 + "activity launcher sourceRecord=" + sourceRecord, e);
481 }
Craig Mautneree36c772014-07-16 14:56:05 -0700482 }
483 }
484
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800485 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700486 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700487 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800488 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800489 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700490 }
491
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800492 void setRecentTasks(RecentTasks recentTasks) {
493 mRecentTasks = recentTasks;
494 }
495
Jeff Brown2c43c332014-06-12 22:38:59 -0700496 /**
497 * At the time when the constructor runs, the power manager has not yet been
498 * initialized. So we initialize our wakelocks afterwards.
499 */
500 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800501 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700502 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700503 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700504 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700505 }
506
justinzhang5286d3f2014-05-12 17:06:01 -0400507 // This function returns a IStatusBarService. The value is from ServiceManager.
508 // getService and is cached.
509 private IStatusBarService getStatusBarService() {
510 synchronized (mService) {
511 if (mStatusBarService == null) {
512 mStatusBarService = IStatusBarService.Stub.asInterface(
513 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
514 if (mStatusBarService == null) {
515 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
516 }
517 }
518 return mStatusBarService;
519 }
520 }
521
Jason Monk35c62a42014-06-17 10:24:47 -0400522 private IDevicePolicyManager getDevicePolicyManager() {
523 synchronized (mService) {
524 if (mDevicePolicyManager == null) {
525 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
526 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
527 if (mDevicePolicyManager == null) {
528 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
529 }
530 }
531 return mDevicePolicyManager;
532 }
533 }
534
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700535 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800536 synchronized (mService) {
537 mWindowManager = wm;
538
539 mDisplayManager =
540 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
541 mDisplayManager.registerDisplayListener(this, null);
542
543 Display[] displays = mDisplayManager.getDisplays();
544 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
545 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800546 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700547 if (activityDisplay.mDisplay == null) {
548 throw new IllegalStateException("Default Display does not exist");
549 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800550 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700551 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800552 }
553
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800554 mHomeStack = mFocusedStack = mLastFocusedStack =
555 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800556
557 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800558 }
Craig Mautner27084302013-03-25 08:05:25 -0700559 }
560
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200561 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700562 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200563 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700564 }
565
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700566 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800567 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700568 }
569
Craig Mautnerde4ef022013-04-07 19:01:33 -0700570 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800571 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700572 }
573
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700574 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700575 if (stack == null) {
576 return false;
577 }
578
Craig Mautnerdf88d732014-01-27 09:21:32 -0800579 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
580 if (parent != null) {
581 stack = parent.task.stack;
582 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800583 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700584 }
585
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700586 /** The top most stack. */
587 boolean isFrontStack(ActivityStack stack) {
588 if (stack == null) {
589 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700591
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700592 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
593 if (parent != null) {
594 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800595 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700596 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
597 }
598
Wale Ogunwaled046a012015-12-24 13:05:59 -0800599 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800600 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
601 if (!focusCandidate.isFocusable()) {
602 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
603 focusCandidate = focusCandidate.getNextFocusableStackLocked();
604 }
605
606 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800607 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800608 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800609
Wale Ogunwaled046a012015-12-24 13:05:59 -0800610 EventLogTags.writeAmFocusedStack(
611 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
612 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
613 }
614
615 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800616 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800617 // The focus activity should always be the top activity in the focused stack.
618 // There will be chaos and anarchy if it isn't...
619 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
620 }
Craig Mautnerde313752015-01-22 14:28:03 -0800621
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800622 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800623 if (r != null && r.idle) {
624 checkFinishBootingLocked();
625 }
626 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700627 }
628
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700629 void moveHomeStackToFront(String reason) {
630 mHomeStack.moveToFront(reason);
631 }
632
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700633 /** Returns true if the focus activity was adjusted to the home stack top activity. */
634 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700635 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700636 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700637 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700638 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700639
Craig Mautner84984fa2014-06-19 11:19:20 -0700640 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700641
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700642 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700643 if (top == null) {
644 return false;
645 }
646 mService.setFocusedActivityLocked(top, reason);
647 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700648 }
649
Craig Mautner299f9602015-01-26 09:47:33 -0800650 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700651 if (!mService.mBooting && !mService.mBooted) {
652 // Not ready yet!
653 return false;
654 }
655
Craig Mautner84984fa2014-06-19 11:19:20 -0700656 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700657 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700658 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700659 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700660
Craig Mautner84984fa2014-06-19 11:19:20 -0700661 if (prev != null) {
662 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
663 }
664
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700665 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
666 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800667 final String myReason = reason + " resumeHomeStackTask";
668
Mark Lua56ea122015-10-08 13:31:01 +0800669 // Only resume home activity if isn't finishing.
670 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800671 mService.setFocusedActivityLocked(r, myReason);
672 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700673 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800674 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700675 }
676
Craig Mautner8d341ef2013-03-26 09:03:27 -0700677 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700678 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700679 }
680
681 /**
682 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
683 * @param id Id of the task we would like returned.
684 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
685 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700686 * @param stackId The stack to restore the task to (default launch stack will be used if
687 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700688 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700689 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800690 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800691 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800692 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800693 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
694 ActivityStack stack = stacks.get(stackNdx);
695 TaskRecord task = stack.taskForIdLocked(id);
696 if (task != null) {
697 return task;
698 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700699 }
700 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800701
702 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700703 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800704 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800705 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700706 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800707 return null;
708 }
709
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700710 if (!restoreFromRecents) {
711 return task;
712 }
713
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700714 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700715 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
716 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800717 return null;
718 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700719 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800720 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700721 }
722
Craig Mautner6170f732013-04-02 13:05:23 -0700723 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800724 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800725 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800726 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
728 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
729 if (r != null) {
730 return r;
731 }
Craig Mautner6170f732013-04-02 13:05:23 -0700732 }
733 }
734 return null;
735 }
736
Tony Mak853304c2016-04-18 15:17:41 +0100737 /**
738 * TODO: Handle freefom mode.
739 * @return true when credential confirmation is needed for the user and there is any
740 * activity started by the user in any visible stack.
741 */
742 boolean isUserLockedProfile(@UserIdInt int userId) {
743 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
744 return false;
745 }
746 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
747 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
748 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
749 for (final ActivityStack activityStack : activityStacks) {
750 if (activityStack == null) {
751 continue;
752 }
753 if (activityStack.topRunningActivityLocked() == null) {
754 continue;
755 }
756 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
757 continue;
758 }
759 if (activityStack.isDockedStack() && mIsDockMinimized) {
760 continue;
761 }
762 final TaskRecord topTask = activityStack.topTask();
763 if (topTask == null) {
764 continue;
765 }
766 // To handle the case that work app is in the task but just is not the top one.
767 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
768 final ActivityRecord activityRecord = topTask.mActivities.get(i);
769 if (activityRecord.userId == userId) {
770 return true;
771 }
772 }
773 }
774 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000775 }
776
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800777 void setNextTaskIdForUserLocked(int taskId, int userId) {
778 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
779 if (taskId > currentTaskId) {
780 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700781 }
782 }
783
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800784 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800785 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
786 // for a userId u, a taskId can only be in the range
787 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
788 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
789 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800790 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
791 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
792 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800793 candidateTaskId++;
794 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
795 // Wrap around as there will be smaller task ids that are available now.
796 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700797 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800798 if (candidateTaskId == currentTaskId) {
799 // Something wrong!
800 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
801 throw new IllegalStateException("Cannot get an available task id."
802 + " Reached limit of " + MAX_TASK_IDS_PER_USER
803 + " running tasks per user.");
804 }
805 }
806 mCurTaskIdForUser.put(userId, candidateTaskId);
807 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700808 }
809
Craig Mautnerde4ef022013-04-07 19:01:33 -0700810 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800811 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700812 if (stack == null) {
813 return null;
814 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700815 ActivityRecord resumedActivity = stack.mResumedActivity;
816 if (resumedActivity == null || resumedActivity.app == null) {
817 resumedActivity = stack.mPausingActivity;
818 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700819 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700820 }
821 }
822 return resumedActivity;
823 }
824
Dianne Hackbornff072722014-09-24 10:56:28 -0700825 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700826 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800827 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800828 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
829 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
831 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700832 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 continue;
834 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700835 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800836 if (hr != null) {
837 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
838 && processName.equals(hr.processName)) {
839 try {
George Mount2c92c972014-03-20 09:38:23 -0700840 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800841 didSomething = true;
842 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700843 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 Slog.w(TAG, "Exception in new application when starting activity "
845 + hr.intent.getComponent().flattenToShortString(), e);
846 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700847 }
Craig Mautner20e72272013-04-01 13:45:53 -0700848 }
Craig Mautner20e72272013-04-01 13:45:53 -0700849 }
850 }
851 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700852 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700853 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700854 }
Craig Mautner20e72272013-04-01 13:45:53 -0700855 return didSomething;
856 }
857
858 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800859 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
860 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
862 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700863 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800864 continue;
865 }
866 final ActivityRecord resumedActivity = stack.mResumedActivity;
867 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700868 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800869 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 return false;
871 }
Craig Mautner20e72272013-04-01 13:45:53 -0700872 }
873 }
Wei Wang65c7a152016-06-02 18:51:22 -0700874 // Send launch end powerhint when idle
875 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700876 return true;
877 }
878
Craig Mautnerde4ef022013-04-07 19:01:33 -0700879 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800880 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
881 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800882 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
883 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700884 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700886 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 return false;
888 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700889 }
890 }
891 }
892 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700893 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800894 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
895 mLastFocusedStack + " to=" + mFocusedStack);
896 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700897 return true;
898 }
899
900 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800901 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800902 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
903 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800904 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
905 final ActivityStack stack = stacks.get(stackNdx);
906 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800907 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700908 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800909 return false;
910 }
911 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800912 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700913 }
914 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800915 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700916 }
917
Craig Mautner2acc3892013-09-23 10:28:14 -0700918 /**
919 * Pause all activities in either all of the stacks or just the back stacks.
920 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700921 * @return true if any activity was paused as a result of this call.
922 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700923 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700924 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800925 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
926 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
928 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700929 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700930 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700932 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
933 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800934 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700935 }
936 }
937 return someActivityPaused;
938 }
939
Craig Mautnerde4ef022013-04-07 19:01:33 -0700940 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700941 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800942 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
943 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800944 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
945 final ActivityStack stack = stacks.get(stackNdx);
946 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700947 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800948 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700949 Slog.d(TAG_STATES,
950 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800951 pausing = false;
952 } else {
953 return false;
954 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700955 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700956 }
957 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700958 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700959 }
960
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700961 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
962 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500963 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800964 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
965 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
966 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
967 final ActivityStack stack = stacks.get(stackNdx);
968 if (stack.mResumedActivity != null &&
969 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700970 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800971 }
972 }
973 }
974 }
975
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800976 void cancelInitializingActivities() {
977 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
978 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
979 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
980 stacks.get(stackNdx).cancelInitializingActivities();
981 }
982 }
983 }
984
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700985 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700986 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700987 }
988
989 void sendWaitingVisibleReportLocked(ActivityRecord r) {
990 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700991 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700992 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700993 if (w.who == null) {
994 changed = true;
995 w.timeout = false;
996 if (r != null) {
997 w.who = new ComponentName(r.info.packageName, r.info.name);
998 }
999 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1000 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001001 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001002 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001003 if (changed) {
1004 mService.notifyAll();
1005 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001006 }
1007
Chong Zhang5022da32016-06-21 16:31:37 -07001008 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1009 boolean changed = false;
1010 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1011 WaitResult w = mWaitingActivityLaunched.remove(i);
1012 if (w.who == null) {
1013 changed = true;
1014 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1015 // the starting activity ends up moving another activity to front, and it should
1016 // wait for this new activity to become visible instead.
1017 // Do not modify other fields.
1018 w.result = START_TASK_TO_FRONT;
1019 }
1020 }
1021 if (changed) {
1022 mService.notifyAll();
1023 }
1024 }
1025
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001026 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1027 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001028 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001029 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001030 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001031 if (w.who == null) {
1032 changed = true;
1033 w.timeout = timeout;
1034 if (r != null) {
1035 w.who = new ComponentName(r.info.packageName, r.info.name);
1036 }
1037 w.thisTime = thisTime;
1038 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001039 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001041 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001042 if (changed) {
1043 mService.notifyAll();
1044 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001045 }
1046
Craig Mautner29219d92013-04-16 20:19:12 -07001047 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001048 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001049 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001050 if (r != null) {
1051 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001052 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001053
Craig Mautner4a1cb222013-12-04 16:14:06 -08001054 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001055 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001056 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1057 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001058 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001059 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001060 if (r != null) {
1061 return r;
1062 }
1063 }
1064 }
1065 return null;
1066 }
1067
Dianne Hackborn09233282014-04-30 11:33:59 -07001068 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001069 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001070 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1071 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001072 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001073 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001074 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001075 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1076 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001077 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001078 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001079 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001080 }
1081 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001082
1083 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1084 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1085 while (maxNum > 0) {
1086 long mostRecentActiveTime = Long.MIN_VALUE;
1087 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001088 final int numTaskLists = runningTaskLists.size();
1089 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1090 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001091 if (!stackTaskList.isEmpty()) {
1092 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1093 if (lastActiveTime > mostRecentActiveTime) {
1094 mostRecentActiveTime = lastActiveTime;
1095 selectedStackList = stackTaskList;
1096 }
1097 }
1098 }
1099 if (selectedStackList != null) {
1100 list.add(selectedStackList.remove(0));
1101 --maxNum;
1102 } else {
1103 break;
1104 }
1105 }
Craig Mautner20e72272013-04-01 13:45:53 -07001106 }
1107
Todd Kennedy7440f172015-12-09 14:31:22 -08001108 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1109 ProfilerInfo profilerInfo) {
1110 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001111 if (aInfo != null) {
1112 // Store the found target back into the intent, because now that
1113 // we have it we never want to do this again. For example, if the
1114 // user navigates back to this point in the history, we should
1115 // always restart the exact same activity.
1116 intent.setComponent(new ComponentName(
1117 aInfo.applicationInfo.packageName, aInfo.name));
1118
1119 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001120 if (!aInfo.processName.equals("system")) {
1121 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001122 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001123 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001124
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001125 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1126 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1127 }
1128
Man Caocfa78b22015-06-11 20:14:34 -07001129 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1130 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1131 }
1132
1133 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001134 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001135 }
1136 }
1137 }
1138 return aInfo;
1139 }
1140
Todd Kennedy7440f172015-12-09 14:31:22 -08001141 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001142 return resolveIntent(intent, resolvedType, userId, 0);
1143 }
1144
1145 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001146 try {
1147 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001148 PackageManager.MATCH_DEFAULT_ONLY | flags
1149 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001150 } catch (RemoteException e) {
1151 }
1152 return null;
1153 }
1154
1155 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1156 ProfilerInfo profilerInfo, int userId) {
1157 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1158 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1159 }
1160
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001161 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1162 boolean andResume, boolean checkConfig) throws RemoteException {
1163
1164 if (!allPausedActivitiesComplete()) {
1165 // While there are activities pausing we skipping starting any new activities until
1166 // pauses are complete. NOTE: that we also do this for activities that are starting in
1167 // the paused state because they will first be resumed then paused on the client side.
1168 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1169 "realStartActivityLocked: Skipping start of r=" + r
1170 + " some activities pausing...");
1171 return false;
1172 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001173
Craig Mautner2568c3a2015-03-26 14:22:34 -07001174 if (andResume) {
1175 r.startFreezingScreenLocked(app, 0);
1176 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001177
Craig Mautner2568c3a2015-03-26 14:22:34 -07001178 // schedule launch ticks to collect information about slow apps.
1179 r.startLaunchTickingLocked();
1180 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001181
1182 // Have the window manager re-evaluate the orientation of
1183 // the screen based on the new activity order. Note that
1184 // as a result of this, it can call back into the activity
1185 // manager with a new orientation. We don't care about that,
1186 // because the activity is not currently running so we are
1187 // just restarting it anyway.
1188 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001189 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001190 mService.mConfiguration,
1191 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001192 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001193 }
1194
1195 r.app = app;
1196 app.waitingToKill = null;
1197 r.launchCount++;
1198 r.lastLaunchTime = SystemClock.uptimeMillis();
1199
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001200 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001201
1202 int idx = app.activities.indexOf(r);
1203 if (idx < 0) {
1204 app.activities.add(r);
1205 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001206 mService.updateLruProcessLocked(app, true, null);
1207 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001208
Craig Mautner15df08a2015-04-01 12:17:18 -07001209 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001210 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1211 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001212 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001213 }
1214
1215 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001216 try {
1217 if (app.thread == null) {
1218 throw new RemoteException();
1219 }
1220 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001221 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001222 if (andResume) {
1223 results = r.results;
1224 newIntents = r.newIntents;
1225 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001226 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1227 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1228 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001229 if (andResume) {
1230 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1231 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001232 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001234 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001235 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001236 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001237 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001238 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1239 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 r.sleeping = false;
1241 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001242 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001243 mService.showAskCompatModeDialogLocked(r);
1244 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001245 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001246 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1247 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1248 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001249 final String profileFile = mService.mProfileFile;
1250 if (profileFile != null) {
1251 ParcelFileDescriptor profileFd = mService.mProfileFd;
1252 if (profileFd != null) {
1253 try {
1254 profileFd = profileFd.dup();
1255 } catch (IOException e) {
1256 if (profileFd != null) {
1257 try {
1258 profileFd.close();
1259 } catch (IOException o) {
1260 }
1261 profileFd = null;
1262 }
1263 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001264 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001265
1266 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1267 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 }
1269 }
1270 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001271
Craig Mautner2568c3a2015-03-26 14:22:34 -07001272 if (andResume) {
1273 app.hasShownUi = true;
1274 app.pendingUiClean = true;
1275 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001276 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001277 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001278 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001279 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001280 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001281 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001282
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001283 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 // This may be a heavy-weight process! Note that the package
1285 // manager will ensure that only activity can run in the main
1286 // process of the .apk, which is the only thing that will be
1287 // considered heavy-weight.
1288 if (app.processName.equals(app.info.packageName)) {
1289 if (mService.mHeavyWeightProcess != null
1290 && mService.mHeavyWeightProcess != app) {
1291 Slog.w(TAG, "Starting new heavy weight process " + app
1292 + " when already running "
1293 + mService.mHeavyWeightProcess);
1294 }
1295 mService.mHeavyWeightProcess = app;
1296 Message msg = mService.mHandler.obtainMessage(
1297 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1298 msg.obj = r;
1299 mService.mHandler.sendMessage(msg);
1300 }
1301 }
1302
1303 } catch (RemoteException e) {
1304 if (r.launchFailed) {
1305 // This is the second time we failed -- finish activity
1306 // and give up.
1307 Slog.e(TAG, "Second failure launching "
1308 + r.intent.getComponent().flattenToShortString()
1309 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001310 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001311 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1312 "2nd-crash", false);
1313 return false;
1314 }
1315
1316 // This is the first time we failed -- restart process and
1317 // retry.
1318 app.activities.remove(r);
1319 throw e;
1320 }
1321
1322 r.launchFailed = false;
1323 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001324 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001325 }
1326
1327 if (andResume) {
1328 // As part of the process of launching, ActivityThread also performs
1329 // a resume.
1330 stack.minimalResumeActivityLocked(r);
1331 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001332 // This activity is not starting in the resumed state... which should look like we asked
1333 // it to pause+stop (but remain visible), and it has done so and reported back the
1334 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001335 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001336 "Moving to PAUSED: " + r + " (starting in paused state)");
1337 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001338 }
1339
1340 // Launch the new version setup screen if needed. We do this -after-
1341 // launching the initial activity (that is, home), so that it can have
1342 // a chance to initialize itself while in the background, making the
1343 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001344 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001345 mService.startSetupActivityLocked();
1346 }
1347
Dianne Hackborn465fa392014-09-14 14:21:18 -07001348 // Update any services we are bound to that might care about whether
1349 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001350 if (r.app != null) {
1351 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1352 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001353
Craig Mautner2420ead2013-04-01 17:13:20 -07001354 return true;
1355 }
1356
Craig Mautnere79d42682013-04-01 19:01:53 -07001357 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001358 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001359 // Is this activity's application already running?
1360 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001361 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001362
1363 r.task.stack.setLaunchTime(r);
1364
1365 if (app != null && app.thread != null) {
1366 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001367 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1368 || !"android".equals(r.info.packageName)) {
1369 // Don't add this if it is a platform component that is marked
1370 // to run in multiple processes, because this is actually
1371 // part of the framework so doesn't make sense to track as a
1372 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001373 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1374 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001375 }
George Mount2c92c972014-03-20 09:38:23 -07001376 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001377 return;
1378 } catch (RemoteException e) {
1379 Slog.w(TAG, "Exception when starting activity "
1380 + r.intent.getComponent().flattenToShortString(), e);
1381 }
1382
1383 // If a dead object exception was thrown -- fall through to
1384 // restart the application.
1385 }
1386
1387 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001388 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001389 }
1390
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001391 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001392 String resultWho, int requestCode, int callingPid, int callingUid,
1393 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001394 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001395 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1396 callingUid);
1397 if (startAnyPerm == PERMISSION_GRANTED) {
1398 return true;
1399 }
1400 final int componentRestriction = getComponentRestrictionForCallingPackage(
1401 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1402 final int actionRestriction = getActionRestrictionForCallingPackage(
1403 intent.getAction(), callingPackage, callingPid, callingUid);
1404 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1405 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1406 if (resultRecord != null) {
1407 resultStack.sendActivityResultLocked(-1,
1408 resultRecord, resultWho, requestCode,
1409 Activity.RESULT_CANCELED, null);
1410 }
1411 final String msg;
1412 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1413 msg = "Permission Denial: starting " + intent.toString()
1414 + " from " + callerApp + " (pid=" + callingPid
1415 + ", uid=" + callingUid + ")" + " with revoked permission "
1416 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1417 } else if (!aInfo.exported) {
1418 msg = "Permission Denial: starting " + intent.toString()
1419 + " from " + callerApp + " (pid=" + callingPid
1420 + ", uid=" + callingUid + ")"
1421 + " not exported from uid " + aInfo.applicationInfo.uid;
1422 } else {
1423 msg = "Permission Denial: starting " + intent.toString()
1424 + " from " + callerApp + " (pid=" + callingPid
1425 + ", uid=" + callingUid + ")"
1426 + " requires " + aInfo.permission;
1427 }
1428 Slog.w(TAG, msg);
1429 throw new SecurityException(msg);
1430 }
1431
1432 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1433 final String message = "Appop Denial: starting " + intent.toString()
1434 + " from " + callerApp + " (pid=" + callingPid
1435 + ", uid=" + callingUid + ")"
1436 + " requires " + AppOpsManager.permissionToOp(
1437 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1438 Slog.w(TAG, message);
1439 return false;
1440 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1441 final String message = "Appop Denial: starting " + intent.toString()
1442 + " from " + callerApp + " (pid=" + callingPid
1443 + ", uid=" + callingUid + ")"
1444 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1445 Slog.w(TAG, message);
1446 return false;
1447 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001448 if (options != null && options.getLaunchTaskId() != -1) {
1449 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1450 callingPid, callingUid);
1451 if (startInTaskPerm != PERMISSION_GRANTED) {
1452 final String msg = "Permission Denial: starting " + intent.toString()
1453 + " from " + callerApp + " (pid=" + callingPid
1454 + ", uid=" + callingUid + ") with launchTaskId="
1455 + options.getLaunchTaskId();
1456 Slog.w(TAG, msg);
1457 throw new SecurityException(msg);
1458 }
1459 }
1460
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001461 return true;
1462 }
1463
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001464 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001465 final long identity = Binder.clearCallingIdentity();
1466 try {
1467 return UserManager.get(mService.mContext).getUserInfo(userId);
1468 } finally {
1469 Binder.restoreCallingIdentity(identity);
1470 }
1471 }
1472
Svet Ganov99b60432015-06-27 13:15:22 -07001473 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001474 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001475 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1476 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001477 == PackageManager.PERMISSION_DENIED) {
1478 return ACTIVITY_RESTRICTION_PERMISSION;
1479 }
1480
Christopher Tateff7add02015-08-17 10:23:22 -07001481 if (activityInfo.permission == null) {
1482 return ACTIVITY_RESTRICTION_NONE;
1483 }
1484
Svet Ganov99b60432015-06-27 13:15:22 -07001485 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1486 if (opCode == AppOpsManager.OP_NONE) {
1487 return ACTIVITY_RESTRICTION_NONE;
1488 }
1489
1490 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1491 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001492 if (!ignoreTargetSecurity) {
1493 return ACTIVITY_RESTRICTION_APPOP;
1494 }
Svet Ganov99b60432015-06-27 13:15:22 -07001495 }
1496
1497 return ACTIVITY_RESTRICTION_NONE;
1498 }
1499
Svetoslav7008b512015-06-24 18:47:07 -07001500 private int getActionRestrictionForCallingPackage(String action,
1501 String callingPackage, int callingPid, int callingUid) {
1502 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001503 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001504 }
1505
1506 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1507 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001508 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001509 }
1510
1511 final PackageInfo packageInfo;
1512 try {
1513 packageInfo = mService.mContext.getPackageManager()
1514 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1515 } catch (PackageManager.NameNotFoundException e) {
1516 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001517 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001518 }
1519
1520 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001521 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001522 }
1523
1524 if (mService.checkPermission(permission, callingPid, callingUid) ==
1525 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001526 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001527 }
1528
1529 final int opCode = AppOpsManager.permissionToOpCode(permission);
1530 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001531 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001532 }
1533
1534 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1535 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001536 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001537 }
1538
Svet Ganov99b60432015-06-27 13:15:22 -07001539 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001540 }
1541
Wale Ogunwaled046a012015-12-24 13:05:59 -08001542 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001543 if (r == null) {
1544 // Not sure what you are trying to do, but it is not going to work...
1545 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001546 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001547 final TaskRecord task = r.task;
1548 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001549 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001550 return false;
1551 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001552 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001553 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001554 }
1555
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001556 void setLaunchSource(int uid) {
1557 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1558 }
1559
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001560 void acquireLaunchWakelock() {
1561 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1562 throw new IllegalStateException("Calling must be system uid");
1563 }
1564 mLaunchingActivity.acquire();
1565 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1566 // To be safe, don't allow the wake lock to be held for too long.
1567 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1568 }
1569 }
1570
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001571 /**
1572 * Called when the frontmost task is idle.
1573 * @return the state of mService.mBooting before this was called.
1574 */
1575 private boolean checkFinishBootingLocked() {
1576 final boolean booting = mService.mBooting;
1577 boolean enableScreen = false;
1578 mService.mBooting = false;
1579 if (!mService.mBooted) {
1580 mService.mBooted = true;
1581 enableScreen = true;
1582 }
1583 if (booting || enableScreen) {
1584 mService.postFinishBooting(booting, enableScreen);
1585 }
1586 return booting;
1587 }
1588
Craig Mautnerf3333272013-04-22 10:55:53 -07001589 // Checked.
1590 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1591 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001592 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001593
Craig Mautnerf3333272013-04-22 10:55:53 -07001594 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001595 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001596 int NS = 0;
1597 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001598 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001599 boolean activityRemoved = false;
1600
Wale Ogunwale7d701172015-03-11 15:36:30 -07001601 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001602 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001603 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1604 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001605 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1606 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001607 if (fromTimeout) {
1608 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001609 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001610
1611 // This is a hack to semi-deal with a race condition
1612 // in the client where it can be constructed with a
1613 // newer configuration from when we asked it to launch.
1614 // We'll update with whatever configuration it now says
1615 // it used to launch.
1616 if (config != null) {
1617 r.configuration = config;
1618 }
1619
1620 // We are now idle. If someone is waiting for a thumbnail from
1621 // us, we can now deliver.
1622 r.idle = true;
1623
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001624 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001625 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001626 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001627 }
1628 }
1629
1630 if (allResumedActivitiesIdle()) {
1631 if (r != null) {
1632 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001633 }
1634
1635 if (mLaunchingActivity.isHeld()) {
1636 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1637 if (VALIDATE_WAKE_LOCK_CALLER &&
1638 Binder.getCallingUid() != Process.myUid()) {
1639 throw new IllegalStateException("Calling must be system uid");
1640 }
1641 mLaunchingActivity.release();
1642 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001643 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001644 }
1645
1646 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001647 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001648 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001649 if ((NF = mFinishingActivities.size()) > 0) {
1650 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001651 mFinishingActivities.clear();
1652 }
1653
Craig Mautnerf3333272013-04-22 10:55:53 -07001654 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001655 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001656 mStartingUsers.clear();
1657 }
1658
Craig Mautnerf3333272013-04-22 10:55:53 -07001659 // Stop any activities that are scheduled to do so but have been
1660 // waiting for the next one to start.
1661 for (int i = 0; i < NS; i++) {
1662 r = stops.get(i);
1663 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001664 if (stack != null) {
1665 if (r.finishing) {
1666 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1667 } else {
1668 stack.stopActivityLocked(r);
1669 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001670 }
1671 }
1672
1673 // Finish any activities that are scheduled to do so but have been
1674 // waiting for the next one to start.
1675 for (int i = 0; i < NF; i++) {
1676 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001677 final ActivityStack stack = r.task.stack;
1678 if (stack != null) {
1679 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1680 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001681 }
1682
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001683 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001684 // Complete user switch
1685 if (startingUsers != null) {
1686 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001687 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001688 }
1689 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001690 }
1691
1692 mService.trimApplications();
1693 //dump();
1694 //mWindowManager.dump();
1695
Craig Mautnerf3333272013-04-22 10:55:53 -07001696 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001697 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001698 }
1699
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001700 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001701 }
1702
Craig Mautner8e569572013-10-11 17:36:59 -07001703 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001704 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001705 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1706 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001707 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1708 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1709 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001710 }
Craig Mautner19091252013-10-05 00:03:53 -07001711 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001712 }
1713
1714 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001715 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1716 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001717 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1718 stacks.get(stackNdx).closeSystemDialogsLocked();
1719 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001720 }
1721 }
1722
Craig Mautner93529a42013-10-04 15:03:13 -07001723 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001724 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001725 }
1726
Craig Mautner8d341ef2013-03-26 09:03:27 -07001727 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001728 * Update the last used stack id for non-current user (current user's last
1729 * used stack is the focused stack)
1730 */
1731 void updateUserStackLocked(int userId, ActivityStack stack) {
1732 if (userId != mCurrentUser) {
1733 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1734 }
1735 }
1736
1737 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001738 * @return true if some activity was finished (or would have finished if doit were true).
1739 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001740 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1741 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001742 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001743 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1744 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001745 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001746 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001747 if (stack.finishDisabledPackageActivitiesLocked(
1748 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001749 didSomething = true;
1750 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001751 }
1752 }
1753 return didSomething;
1754 }
1755
Dianne Hackborna413dc02013-07-12 12:02:55 -07001756 void updatePreviousProcessLocked(ActivityRecord r) {
1757 // Now that this process has stopped, we may want to consider
1758 // it to be the previous app to try to keep around in case
1759 // the user wants to return to it.
1760
1761 // First, found out what is currently the foreground app, so that
1762 // we don't blow away the previous app if this activity is being
1763 // hosted by the process that is actually still the foreground.
1764 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001765 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1766 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001767 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1768 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001769 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001770 if (stack.mResumedActivity != null) {
1771 fgApp = stack.mResumedActivity.app;
1772 } else if (stack.mPausingActivity != null) {
1773 fgApp = stack.mPausingActivity.app;
1774 }
1775 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001776 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001777 }
1778 }
1779
1780 // Now set this one as the previous process, only if that really
1781 // makes sense to.
1782 if (r.app != null && fgApp != null && r.app != fgApp
1783 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001784 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001785 mService.mPreviousProcess = r.app;
1786 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1787 }
1788 }
1789
Wale Ogunwaled046a012015-12-24 13:05:59 -08001790 boolean resumeFocusedStackTopActivityLocked() {
1791 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001792 }
1793
Wale Ogunwaled046a012015-12-24 13:05:59 -08001794 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001795 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001796 if (targetStack != null && isFocusedStack(targetStack)) {
1797 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001798 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001799 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1800 if (r == null || r.state != RESUMED) {
1801 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1802 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001803 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001804 }
1805
Todd Kennedy39bfee52016-02-24 10:28:21 -08001806 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1808 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1809 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1810 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1811 }
1812 }
1813 }
1814
Adrian Roos20d7df32016-01-12 18:59:43 +01001815 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1816 TaskRecord finishedTask = null;
1817 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001818 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1819 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001820 final int numStacks = stacks.size();
1821 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1822 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001823 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1824 if (stack == focusedStack || finishedTask == null) {
1825 finishedTask = t;
1826 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001827 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001828 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001829 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001830 }
1831
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001832 void finishVoiceTask(IVoiceInteractionSession session) {
1833 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1834 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1835 final int numStacks = stacks.size();
1836 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1837 final ActivityStack stack = stacks.get(stackNdx);
1838 stack.finishVoiceTask(session);
1839 }
1840 }
1841 }
1842
Andrii Kulianc27916642016-04-12 17:59:27 -07001843 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1844 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001845 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1846 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001847 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001848 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1849 // Caller wants the home activity moved with it. To accomplish this,
1850 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001851 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001852 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001853 if (task.stack == null) {
1854 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1855 + task + " to front. Stack is null");
1856 return;
1857 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001858
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001859 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001860 int stackId = options.getLaunchStackId();
1861 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001862 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001863 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001864 if (stackId == INVALID_STACK_ID) {
1865 stackId = task.getLaunchStackId();
1866 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001867 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001868 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1869 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1870 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001871 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1872 // still need moveTaskToFrontLocked() below for any transition settings.
1873 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001874 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1875 resizeStackLocked(stackId, bounds,
1876 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001877 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001878 } else {
1879 // WM resizeTask must be done after the task is moved to the correct stack,
1880 // because Task's setBounds() also updates dim layer's bounds, but that has
1881 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001882 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001883 false /* relayout */, false /* forced */);
1884 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001885 }
1886 }
1887
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001888 final ActivityRecord r = task.getTopActivity();
1889 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1890 r == null ? null : r.appTimeTracker, reason);
1891
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001892 if (DEBUG_STACK) Slog.d(TAG_STACK,
1893 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001894
Andrii Kulianc27916642016-04-12 17:59:27 -07001895 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1896 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001897 }
1898
Wale Ogunwale854809c2015-12-27 16:18:19 -08001899 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001900 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001901 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001902 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001903 return false;
1904 }
1905 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1906 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001907 }
1908
Craig Mautner967212c2013-04-13 21:10:58 -07001909 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001910 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001911 }
1912
1913 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001914 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1915 if (activityContainer != null) {
1916 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001917 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001918 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001919 return null;
1920 }
1921 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001922 }
1923
Craig Mautner967212c2013-04-13 21:10:58 -07001924 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001925 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001926 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1927 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001928 }
1929 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001930 }
1931
Craig Mautner4a1cb222013-12-04 16:14:06 -08001932 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001933 ActivityRecord homeActivity = getHomeActivity();
1934 if (homeActivity != null) {
1935 return homeActivity.appToken;
1936 }
1937 return null;
1938 }
1939
1940 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001941 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001942 }
1943
1944 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001945 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1946 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1947 final TaskRecord task = tasks.get(taskNdx);
1948 if (task.isHomeTask()) {
1949 final ArrayList<ActivityRecord> activities = task.mActivities;
1950 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1951 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001952 if (r.isHomeActivity()
1953 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001954 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001955 }
1956 }
1957 }
1958 }
1959 return null;
1960 }
1961
Chong Zhangb15758a2015-11-17 12:12:03 -08001962 /**
1963 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1964 * the docked stack itself, or if it's side-by-side to the docked stack.
1965 */
1966 boolean isStackDockedInEffect(int stackId) {
1967 return stackId == DOCKED_STACK_ID ||
1968 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1969 }
1970
Todd Kennedyca4d8422015-01-15 15:19:22 -08001971 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001972 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001973 ActivityContainer activityContainer =
1974 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001975 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001976 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1977 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001978 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001979 return activityContainer;
1980 }
1981
Craig Mautner34b73df2014-01-12 21:11:08 -08001982 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001983 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1984 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1985 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001986 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1987 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001988 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001989 }
1990 }
1991
Craig Mautner95da1082014-02-24 17:54:35 -08001992 void deleteActivityContainer(IActivityContainer container) {
1993 ActivityContainer activityContainer = (ActivityContainer)container;
1994 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001995 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1996 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001997 final int stackId = activityContainer.mStackId;
1998 mActivityContainers.remove(stackId);
1999 mWindowManager.removeStack(stackId);
2000 }
2001 }
2002
Jorim Jaggidc249c42015-12-15 14:57:31 -08002003 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002004 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002005 if (stackId == DOCKED_STACK_ID) {
2006 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
2007 preserveWindows);
2008 return;
2009 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002010 final ActivityStack stack = getStack(stackId);
2011 if (stack == null) {
2012 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2013 return;
2014 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002015
Jorim Jaggidc249c42015-12-15 14:57:31 -08002016 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002017 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2018 // stack size changing so things don't get out of sync.
2019 return;
2020 }
2021
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002022 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002023 mWindowManager.deferSurfaceLayout();
2024 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002025 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002026 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002027 stack.ensureVisibleActivitiesConfigurationLocked(
2028 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002029 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002030 } finally {
2031 mWindowManager.continueSurfaceLayout();
2032 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002033 }
2034 }
2035
Jorim Jaggi192086e2016-03-11 17:17:03 +01002036 void deferUpdateBounds(int stackId) {
2037 final ActivityStack stack = getStack(stackId);
2038 if (stack != null) {
2039 stack.deferUpdateBounds();
2040 }
2041 }
2042
2043 void continueUpdateBounds(int stackId) {
2044 final ActivityStack stack = getStack(stackId);
2045 if (stack != null) {
2046 stack.continueUpdateBounds();
2047 }
2048 }
2049
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002050 void notifyAppTransitionDone() {
2051 continueUpdateBounds(HOME_STACK_ID);
2052 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2053 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002054 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002055 mWindowManager.setTaskDockedResizing(taskId, false);
2056 }
2057 }
2058 mResizingTasksDuringAnimation.clear();
2059 }
2060
Jorim Jaggi192086e2016-03-11 17:17:03 +01002061 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002062 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002063 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002064
Jorim Jaggi192086e2016-03-11 17:17:03 +01002065 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2066 return;
2067 }
2068
Jorim Jaggidc249c42015-12-15 14:57:31 -08002069 mTmpBounds.clear();
2070 mTmpConfigs.clear();
2071 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002072 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002073 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2074 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002075 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002076 final TaskRecord task = tasks.get(i);
2077 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002078 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2079 // For freeform stack we don't adjust the size of the tasks to match that
2080 // of the stack, but we do try to make sure the tasks are still contained
2081 // with the bounds of the stack.
2082 tempRect2.set(task.mBounds);
2083 fitWithinBounds(tempRect2, bounds);
2084 task.updateOverrideConfiguration(tempRect2);
2085 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002086 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002087 }
2088 }
2089
2090 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2091 mTmpBounds.put(task.taskId, task.mBounds);
2092 if (tempTaskInsetBounds != null) {
2093 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2094 }
2095 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002096
2097 // We might trigger a configuration change. Save the current task bounds for freezing.
2098 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002099 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2100 mTmpBounds, mTmpInsetBounds);
2101 stack.setBounds(bounds);
2102 }
2103
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002104 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2105 final ActivityStack stack = getStack(fromStackId);
2106 if (stack == null) {
2107 return;
2108 }
2109
2110 mWindowManager.deferSurfaceLayout();
2111 try {
2112 if (fromStackId == DOCKED_STACK_ID) {
2113
2114 // We are moving all tasks from the docked stack to the fullscreen stack,
2115 // which is dismissing the docked stack, so resize all other stacks to
2116 // fullscreen here already so we don't end up with resize trashing.
2117 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2118 if (StackId.isResizeableByDockedStack(i)) {
2119 ActivityStack otherStack = getStack(i);
2120 if (otherStack != null) {
2121 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2122 true /* allowResizeInDockedMode */, DEFER_RESUME);
2123 }
2124 }
2125 }
2126
2127 // Also disable docked stack resizing since we have manually adjusted the
2128 // size of other stacks above and we don't want to trigger a docked stack
2129 // resize when we remove task from it below and it is detached from the
2130 // display because it no longer contains any tasks.
2131 mAllowDockedStackResize = false;
2132 }
2133 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2134 final int size = tasks.size();
2135 if (onTop) {
2136 for (int i = 0; i < size; i++) {
2137 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002138 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002139 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002140 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002141
2142 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2143 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002144 } else {
2145 for (int i = size - 1; i >= 0; i--) {
2146 positionTaskInStackLocked(tasks.get(i).taskId,
2147 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2148 }
2149 }
2150 } finally {
2151 mAllowDockedStackResize = true;
2152 mWindowManager.continueSurfaceLayout();
2153 }
2154 }
2155
Jorim Jaggidc249c42015-12-15 14:57:31 -08002156 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2157 Rect tempDockedTaskInsetBounds,
2158 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002159
2160 if (!mAllowDockedStackResize) {
2161 // Docked stack resize currently disabled.
2162 return;
2163 }
2164
Jorim Jaggidc249c42015-12-15 14:57:31 -08002165 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2166 if (stack == null) {
2167 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2168 return;
2169 }
2170
2171 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2172 mWindowManager.deferSurfaceLayout();
2173 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002174 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2175 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002176 ActivityRecord r = stack.topRunningActivityLocked();
2177 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2178 tempDockedTaskInsetBounds);
2179
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002180 // TODO: Checking for isAttached might not be needed as if the user passes in null
2181 // dockedBounds then they want the docked stack to be dismissed.
2182 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2183 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002184 // In this case we make all other static stacks fullscreen and move all
2185 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002186 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002187
2188 // stack shouldn't contain anymore activities, so nothing to resume.
2189 r = null;
2190 } else {
2191 // Docked stacks occupy a dedicated region on screen so the size of all other
2192 // static stacks need to be adjusted so they don't overlap with the docked stack.
2193 // We get the bounds to use from window manager which has been adjusted for any
2194 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002195 mWindowManager.getStackDockedModeBounds(
2196 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002197 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002198 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2199 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2200 tempOtherTaskInsetBounds, preserveWindows,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002201 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002202 }
2203 }
2204 }
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002205 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002206 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002207 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002208 mWindowManager.continueSurfaceLayout();
2209 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2210 }
2211
2212 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2213 tempDockedTaskBounds != null
2214 || tempDockedTaskInsetBounds != null
2215 || tempOtherTaskBounds != null
2216 || tempOtherTaskInsetBounds != null);
2217 }
2218
Robert Carr0d00c2e2016-02-29 17:45:02 -08002219 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2220 final ActivityStack stack = getStack(PINNED_STACK_ID);
2221 if (stack == null) {
2222 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2223 return;
2224 }
2225 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2226 mWindowManager.deferSurfaceLayout();
2227 try {
2228 ActivityRecord r = stack.topRunningActivityLocked();
2229 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2230 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002231 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002232 } finally {
2233 mWindowManager.continueSurfaceLayout();
2234 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2235 }
2236 }
2237
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002238 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2239 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002240 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002241 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002242 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002243 }
2244
Chong Zhang87b21722015-09-21 15:39:51 -07002245 // If this is a forced resize, let it go through even if the bounds is not changing,
2246 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002247 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002248 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002249 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002250 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002251 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002252 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002253
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002254 if (!mWindowManager.isValidTaskId(task.taskId)) {
2255 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002256 // All we can do for now is update the bounds so it can be used when the task is
2257 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002258 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002259 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2260 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002261 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002262 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002263 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002264 }
2265
Chong Zhang6de2ae82015-09-30 18:25:21 -07002266 // Do not move the task to another stack here.
2267 // This method assumes that the task is already placed in the right stack.
2268 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002269
Chong Zhang6de2ae82015-09-30 18:25:21 -07002270 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002271
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002272 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002273 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002274 // way and the activity was kept the way it was. If it's false, it means the activity had
2275 // to be relaunched due to configuration change.
2276 boolean kept = true;
2277 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002278 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002279 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002280 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002281 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002282
2283 if (!deferResume) {
2284
2285 // All other activities must be made visible with their correct configuration.
2286 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2287 if (!kept) {
2288 resumeFocusedStackTopActivityLocked();
2289 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002290 }
2291 }
2292 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002293 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002294
2295 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002296 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002297 }
2298
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002299 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002300 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2301 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002302 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002303 }
2304
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002305 ActivityContainer activityContainer = new ActivityContainer(stackId);
2306 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002307 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002308 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002309 }
2310
2311 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002312 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002313 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2314 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002315 break;
2316 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002317 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002318 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002319 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002320 }
2321
Chong Zhang5dcb2752015-08-18 13:50:26 -07002322 /**
2323 * Restores a recent task to a stack
2324 * @param task The recent task to be restored.
2325 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002326 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002327 * @return true if the task has been restored successfully.
2328 */
2329 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2330 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002331 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002332 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2333 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2334 // Put it in the fullscreen stack.
2335 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002336 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002337
Wale Ogunwale706ed792015-08-02 10:29:44 -07002338 if (task.stack != null) {
2339 // Task has already been restored once. See if we need to do anything more
2340 if (task.stack.mStackId == stackId) {
2341 // Nothing else to do since it is already restored in the right stack.
2342 return true;
2343 }
2344 // Remove current stack association, so we can re-associate the task with the
2345 // right stack below.
Wale Ogunwale06579d62016-04-30 15:29:06 -07002346 task.stack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002347 }
2348
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002349 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002350 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002351
2352 if (stack == null) {
2353 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002354 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2355 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002356 return false;
2357 }
2358
Wale Ogunwale5f986092015-12-04 15:35:38 -08002359 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002360 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2361 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002362 final ArrayList<ActivityRecord> activities = task.mActivities;
2363 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002364 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002365 }
2366 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002367 }
2368
Wale Ogunwale040b4702015-08-06 18:10:50 -07002369 /**
2370 * Moves the specified task record to the input stack id.
2371 * WARNING: This method performs an unchecked/raw move of the task and
2372 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002373 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002374 * @param task Task to move.
2375 * @param stackId Id of stack to move task to.
2376 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002377 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002378 * @param reason Reason the task is been moved.
2379 * @return The stack the task was moved to.
2380 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002381 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002382 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002383
2384 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2385 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2386 + "support multi-window task=" + task + " to stackId=" + stackId);
2387 }
2388
Wale Ogunwale06579d62016-04-30 15:29:06 -07002389 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002390 final ActivityStack prevStack = task.stack;
2391 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002392 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002393 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2394 // Whenever we are moving the top activity from the front stack we want to make sure to move
2395 // the stack to the front.
2396 final boolean wasFront = isFrontStack(prevStack)
2397 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002398
Chong Zhangd545dce2016-02-29 18:09:17 -08002399 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002400 // We don't allow moving a unresizeable task to the docked stack since the docked
2401 // stack is used for split-screen mode and will cause things like the docked divider to
2402 // show up. We instead leave the task in its current stack or move it to the fullscreen
2403 // stack if it isn't currently in a stack.
2404 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002405 Slog.w(TAG, "Can not move unresizeable task=" + task
2406 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2407 }
2408
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002409 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2410 // if a docked stack is created below which will lead to the stack we are moving from and
2411 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002412 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002413 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002414 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002415 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002416 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002417
2418 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002419 // move focus to the new stack by moving the stack to the front.
2420 stack.moveToFrontAndResumeStateIfNeeded(
2421 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002422
Wale Ogunwale040b4702015-08-06 18:10:50 -07002423 return stack;
2424 }
2425
Chong Zhange4fbd322016-03-01 14:44:03 -08002426 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002427 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002428 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2429 false /* deferResume */);
2430 }
2431
2432 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2433 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002434 final TaskRecord task = anyTaskForIdLocked(taskId);
2435 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002436 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002437 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002438 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002439
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002440 if (task.stack != null && task.stack.mStackId == stackId) {
2441 // You are already in the right stack silly...
2442 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002443 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002444 }
2445
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002446 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2447 throw new IllegalArgumentException("moveTaskToStack:"
2448 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2449 }
2450
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002451 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002452 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002453 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002454 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002455 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002456 // We are about to relaunch the activity because its configuration changed due to
2457 // being maximized, i.e. size change. The activity will first remove the old window
2458 // and then add a new one. This call will tell window manager about this, so it can
2459 // preserve the old window until the new one is drawn. This prevents having a gap
2460 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002461 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002462 // Note here we always set the replacing window first, as the flags might be needed
2463 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002464 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002465 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002466
2467 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002468 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002469 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002470 try {
2471 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002472 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002473 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002474
Wale Ogunwale961f4852016-02-01 20:25:54 -08002475 if (!animate) {
2476 stack.mNoAnimActivities.add(topActivity);
2477 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002478
Wale Ogunwale961f4852016-02-01 20:25:54 -08002479 // We might trigger a configuration change. Save the current task bounds for freezing.
2480 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2481
2482 // Make sure the task has the appropriate bounds/size for the stack it is in.
2483 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002484 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2485 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002486 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2487 Rect bounds = task.getLaunchBounds();
2488 if (bounds == null) {
2489 stack.layoutTaskInStack(task, null);
2490 bounds = task.mBounds;
2491 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002492 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2493 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002494 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002495 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2496 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002497 }
2498 } finally {
2499 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002500 }
2501
2502 if (mightReplaceWindow) {
2503 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2504 // window), we need to clear the replace window settings. Otherwise, we schedule a
2505 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002506 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002507 }
2508
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002509 if (!deferResume) {
2510
2511 // The task might have already been running and its visibility needs to be synchronized with
2512 // the visibility of the stack / windows.
2513 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2514 resumeFocusedStackTopActivityLocked();
2515 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002516
Jorim Jaggid53f0922016-04-06 22:16:23 -07002517 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002518
2519 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002520 }
2521
Wale Ogunwale079a0042015-10-24 11:44:07 -07002522 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2523 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2524 if (stack == null) {
2525 throw new IllegalArgumentException(
2526 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2527 }
2528
2529 final ActivityRecord r = stack.topRunningActivityLocked();
2530 if (r == null) {
2531 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2532 + " in stack=" + stack);
2533 return false;
2534 }
2535
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002536 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002537 Slog.w(TAG,
2538 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002539 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002540 return false;
2541 }
2542
Wale Ogunwale480dca02016-02-06 13:58:29 -08002543 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002544 return true;
2545 }
2546
Wale Ogunwale480dca02016-02-06 13:58:29 -08002547 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2548 mWindowManager.deferSurfaceLayout();
2549 try {
2550 final TaskRecord task = r.task;
2551
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002552 if (r == task.stack.getVisibleBehindActivity()) {
2553 // An activity can't be pinned and visible behind at the same time. Go ahead and
2554 // release it from been visible behind before pinning.
2555 requestVisibleBehindLocked(r, false);
2556 }
2557
Wale Ogunwale480dca02016-02-06 13:58:29 -08002558 // Need to make sure the pinned stack exist so we can resize it below...
2559 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2560
2561 // Resize the pinned stack to match the current size of the task the activity we are
2562 // going to be moving is currently contained in. We do this to have the right starting
2563 // animation bounds for the pinned stack to the desired bounds the caller wants.
2564 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2565 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002566 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002567
2568 if (task.mActivities.size() == 1) {
2569 // There is only one activity in the task. So, we can just move the task over to
2570 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002571 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2572 // Move the home stack forward if the task we just moved to the pinned stack
2573 // was launched from home so home should be visible behind it.
2574 moveHomeStackToFront(reason);
2575 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002576 moveTaskToStackLocked(
2577 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2578 } else {
2579 stack.moveActivityToStack(r);
2580 }
2581 } finally {
2582 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002583 }
2584
Wale Ogunwale480dca02016-02-06 13:58:29 -08002585 // The task might have already been running and its visibility needs to be synchronized
2586 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002587 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002588 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002589
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002590 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002591 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002592 }
2593
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002594 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2595 final TaskRecord task = anyTaskForIdLocked(taskId);
2596 if (task == null) {
2597 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2598 return;
2599 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002600 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2601
2602 task.updateOverrideConfigurationForStack(stack);
2603
2604 mWindowManager.positionTaskInStack(
2605 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002606 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002607 // The task might have already been running and its visibility needs to be synchronized with
2608 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002609 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002610 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002611 }
2612
Craig Mautnerac6f8432013-07-17 13:24:59 -07002613 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002614 mTmpFindTaskResult.r = null;
2615 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002616 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002617 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2618 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002619 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2620 final ActivityStack stack = stacks.get(stackNdx);
2621 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002622 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002623 continue;
2624 }
2625 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002626 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2627 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002628 continue;
2629 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002630 stack.findTaskLocked(r, mTmpFindTaskResult);
2631 // It is possible to have task in multiple stacks with the same root affinity.
2632 // If the match we found was based on root affinity we keep on looking to see if
2633 // there is a better match in another stack. We eventually return the match based
2634 // on root affinity if we don't find a better match.
2635 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2636 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002637 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002638 }
2639 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002640 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2641 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002642 }
2643
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002644 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2645 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002646 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2647 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002648 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002649 final ActivityRecord ar = stacks.get(stackNdx)
2650 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002651 if (ar != null) {
2652 return ar;
2653 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002654 }
2655 }
2656 return null;
2657 }
2658
Craig Mautner8d341ef2013-03-26 09:03:27 -07002659 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002660 scheduleSleepTimeout();
2661 if (!mGoingToSleep.isHeld()) {
2662 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002663 if (mLaunchingActivity.isHeld()) {
2664 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2665 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002666 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002667 mLaunchingActivity.release();
2668 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002669 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002670 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002671 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002672 }
2673
2674 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002675 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002676
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002677 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002678 final long endTime = System.currentTimeMillis() + timeout;
2679 while (true) {
2680 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002681 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2682 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002683 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2684 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2685 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002686 }
2687 if (cantShutdown) {
2688 long timeRemaining = endTime - System.currentTimeMillis();
2689 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002690 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002691 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002692 } catch (InterruptedException e) {
2693 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002694 } else {
2695 Slog.w(TAG, "Activity manager shutdown timed out");
2696 timedout = true;
2697 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002698 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002699 } else {
2700 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002701 }
2702 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002703
2704 // Force checkReadyForSleep to complete.
2705 mSleepTimeout = true;
2706 checkReadyForSleepLocked();
2707
Craig Mautner8d341ef2013-03-26 09:03:27 -07002708 return timedout;
2709 }
2710
2711 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002712 removeSleepTimeouts();
2713 if (mGoingToSleep.isHeld()) {
2714 mGoingToSleep.release();
2715 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002716 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2717 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002718 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2719 final ActivityStack stack = stacks.get(stackNdx);
2720 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002721 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002722 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002723 }
Craig Mautner5314a402013-09-26 12:40:16 -07002724 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002725 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002726 mGoingToSleepActivities.clear();
2727 }
2728
2729 void activitySleptLocked(ActivityRecord r) {
2730 mGoingToSleepActivities.remove(r);
2731 checkReadyForSleepLocked();
2732 }
2733
2734 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002735 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002736 // Do not care.
2737 return;
2738 }
2739
2740 if (!mSleepTimeout) {
2741 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002742 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2743 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002744 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2745 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2746 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002747 }
2748
2749 if (mStoppingActivities.size() > 0) {
2750 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002751 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002752 + mStoppingActivities.size() + " activities");
2753 scheduleIdleLocked();
2754 dontSleep = true;
2755 }
2756
2757 if (mGoingToSleepActivities.size() > 0) {
2758 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002759 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002760 + mGoingToSleepActivities.size() + " activities");
2761 dontSleep = true;
2762 }
2763
2764 if (dontSleep) {
2765 return;
2766 }
2767 }
2768
Wei Wang65c7a152016-06-02 18:51:22 -07002769 // Send launch end powerhint before going sleep
2770 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2771
Craig Mautnere0a38842013-12-16 16:14:02 -08002772 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2773 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002774 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2775 stacks.get(stackNdx).goToSleep();
2776 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002777 }
2778
2779 removeSleepTimeouts();
2780
2781 if (mGoingToSleep.isHeld()) {
2782 mGoingToSleep.release();
2783 }
2784 if (mService.mShuttingDown) {
2785 mService.notifyAll();
2786 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002787 }
2788
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002789 boolean reportResumedActivityLocked(ActivityRecord r) {
2790 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002791 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002792 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002793 }
2794 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002795 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002796 mWindowManager.executeAppTransition();
2797 return true;
2798 }
2799 return false;
2800 }
2801
Craig Mautner8d341ef2013-03-26 09:03:27 -07002802 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002803 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2804 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002805 int stackNdx = stacks.size() - 1;
2806 while (stackNdx >= 0) {
2807 stacks.get(stackNdx).handleAppCrashLocked(app);
2808 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002809 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002810 }
2811 }
2812
Jose Lima4b6c6692014-08-12 17:41:12 -07002813 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002814 final ActivityStack stack = r.task.stack;
2815 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002816 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2817 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002818 return false;
2819 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002820
2821 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2822 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2823 + " visible=" + visible + " stackId=" + stack.mStackId
2824 + " can't contain visible behind activities");
2825 return false;
2826 }
2827
Jose Lima4b6c6692014-08-12 17:41:12 -07002828 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002829 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2830 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002831
2832 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002833 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002834 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002835 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002836 return true;
2837 }
2838
2839 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002840 if (visible && top.fullscreen) {
2841 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002842 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2843 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2844 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2845 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002846 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002847 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2848 // Only the activity set as currently visible behind should actively reset its
2849 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002850 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2851 "requestVisibleBehind: returning visible=" + visible
2852 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2853 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002854 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002855 }
2856
Jose Lima4b6c6692014-08-12 17:41:12 -07002857 stack.setVisibleBehindActivity(visible ? r : null);
2858 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002859 // If there is a translucent home activity, we need to force it stop being translucent,
2860 // because we can't depend on the application to necessarily perform that operation.
2861 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002862 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002863 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002864 mService.convertFromTranslucent(next.appToken);
2865 }
2866 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002867 if (top.app != null && top.app.thread != null) {
2868 // Notify the top app of the change.
2869 try {
2870 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2871 } catch (RemoteException e) {
2872 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002873 }
2874 return true;
2875 }
2876
Craig Mautnerbb742462014-07-07 15:28:55 -07002877 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2878 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002879 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002880 final ActivityStack stack = task.stack;
2881
2882 r.mLaunchTaskBehind = false;
2883 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002884 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002885 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002886 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002887
2888 // When launching tasks behind, update the last active time of the top task after the new
2889 // task has been shown briefly
2890 final ActivityRecord top = stack.topActivity();
2891 if (top != null) {
2892 top.task.touchActiveTime();
2893 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002894 }
2895
2896 void scheduleLaunchTaskBehindComplete(IBinder token) {
2897 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2898 }
2899
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002900 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2901 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002902 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002903 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2904 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002905 final int topStackNdx = stacks.size() - 1;
2906 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2907 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002908 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002909 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002910 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002911 }
2912
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002913 void invalidateTaskLayers() {
2914 mTaskLayersChanged = true;
2915 }
2916
2917 void rankTaskLayersIfNeeded() {
2918 if (!mTaskLayersChanged) {
2919 return;
2920 }
2921 mTaskLayersChanged = false;
2922 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2923 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2924 int baseLayer = 0;
2925 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2926 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2927 }
2928 }
2929 }
2930
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002931 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2932 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2933 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2934 final int topStackNdx = stacks.size() - 1;
2935 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2936 final ActivityStack stack = stacks.get(stackNdx);
2937 stack.clearOtherAppTimeTrackers(except);
2938 }
2939 }
2940 }
2941
Craig Mautner8d341ef2013-03-26 09:03:27 -07002942 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002943 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2944 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002945 final int numStacks = stacks.size();
2946 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2947 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002948 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002949 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002950 }
2951 }
2952
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002953 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2954 // Examine all activities currently running in the process.
2955 TaskRecord firstTask = null;
2956 // Tasks is non-null only if two or more tasks are found.
2957 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002958 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2959 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002960 ActivityRecord r = app.activities.get(i);
2961 // First, if we find an activity that is in the process of being destroyed,
2962 // then we just aren't going to do anything for now; we want things to settle
2963 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002964 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002965 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002966 return;
2967 }
2968 // Don't consider any activies that are currently not in a state where they
2969 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002970 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2971 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002972 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002973 continue;
2974 }
2975 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002976 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002977 + " from " + r);
2978 if (firstTask == null) {
2979 firstTask = r.task;
2980 } else if (firstTask != r.task) {
2981 if (tasks == null) {
2982 tasks = new ArraySet<>();
2983 tasks.add(firstTask);
2984 }
2985 tasks.add(r.task);
2986 }
2987 }
2988 }
2989 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002990 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002991 return;
2992 }
2993 // If we have activities in multiple tasks that are in a position to be destroyed,
2994 // let's iterate through the tasks and release the oldest one.
2995 final int numDisplays = mActivityDisplays.size();
2996 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2997 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2998 // Step through all stacks starting from behind, to hit the oldest things first.
2999 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3000 final ActivityStack stack = stacks.get(stackNdx);
3001 // Try to release activities in this stack; if we manage to, we are done.
3002 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3003 return;
3004 }
3005 }
3006 }
3007 }
3008
Amith Yamasani37a40c22015-06-17 13:25:42 -07003009 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003010 final int focusStackId = mFocusedStack.getStackId();
3011 // We dismiss the docked stack whenever we switch users.
3012 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3013
3014 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003015 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003016 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003017
Craig Mautner858d8a62013-04-23 17:08:34 -07003018 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003019 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3020 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003021 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003022 final ActivityStack stack = stacks.get(stackNdx);
3023 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003024 TaskRecord task = stack.topTask();
3025 if (task != null) {
3026 mWindowManager.moveTaskToTop(task.taskId);
3027 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003028 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003029 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003030
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003031 ActivityStack stack = getStack(restoreStackId);
3032 if (stack == null) {
3033 stack = mHomeStack;
3034 }
3035 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003036 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003037 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003038 } else {
3039 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003040 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003041 }
Craig Mautner93529a42013-10-04 15:03:13 -07003042 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003043 }
3044
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003045 /** Checks whether the userid is a profile of the current user. */
3046 boolean isCurrentProfileLocked(int userId) {
3047 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003048 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003049 }
3050
Chong Zhang45c25ce2015-08-10 22:18:26 -07003051 /** Checks whether the activity should be shown for current user. */
3052 boolean okToShowLocked(ActivityRecord r) {
Nicolas Prevot1219c922016-06-20 17:25:12 +01003053 return r != null && ((r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
3054 || (isCurrentProfileLocked(r.userId)
3055 && !mService.mUserController.isUserStoppingOrShuttingDownLocked(r.userId)));
Chong Zhang45c25ce2015-08-10 22:18:26 -07003056 }
3057
Craig Mautnerde4ef022013-04-07 19:01:33 -07003058 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003059 ArrayList<ActivityRecord> stops = null;
3060
3061 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003062 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3063 ActivityRecord s = mStoppingActivities.get(activityNdx);
3064 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003065 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003066 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3067 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003068 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003069 if (s.finishing) {
3070 // If this activity is finishing, it is sitting on top of
3071 // everyone else but we now know it is no longer needed...
3072 // so get rid of it. Otherwise, we need to go through the
3073 // normal flow and hide it once we determine that it is
3074 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003075 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003076 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003077 }
3078 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003079 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003080 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003081 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003082 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003083 }
3084 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003085 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003086 }
3087 }
3088
3089 return stops;
3090 }
3091
Craig Mautnercf910b02013-04-23 11:23:27 -07003092 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003093 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3094 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3095 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3096 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003097 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003098 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003099 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003100 if (r == null) Slog.e(TAG,
3101 "validateTop...: null top activity, stack=" + stack);
3102 else {
3103 final ActivityRecord pausing = stack.mPausingActivity;
3104 if (pausing != null && pausing == r) Slog.e(TAG,
3105 "validateTop...: top stack has pausing activity r=" + r
3106 + " state=" + state);
3107 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3108 "validateTop...: activity in front not resumed r=" + r
3109 + " state=" + state);
3110 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003111 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003112 final ActivityRecord resumed = stack.mResumedActivity;
3113 if (resumed != null && resumed == r) Slog.e(TAG,
3114 "validateTop...: back stack has resumed activity r=" + r
3115 + " state=" + state);
3116 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3117 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003118 }
3119 }
3120 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003121 }
3122
Craig Mautnere0570202015-05-13 13:06:11 -07003123 private String lockTaskModeToString() {
3124 switch (mLockTaskModeState) {
3125 case LOCK_TASK_MODE_LOCKED:
3126 return "LOCKED";
3127 case LOCK_TASK_MODE_PINNED:
3128 return "PINNED";
3129 case LOCK_TASK_MODE_NONE:
3130 return "NONE";
3131 default: return "unknown=" + mLockTaskModeState;
3132 }
3133 }
3134
Craig Mautner27084302013-03-25 08:05:25 -07003135 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003136 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003137 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003138 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003139 pw.print(prefix);
3140 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003141 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003142 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003143 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3144 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3145 if (packages.size() > 0) {
3146 pw.println(" mLockTaskPackages (userId:packages)=");
3147 for (int i = 0; i < packages.size(); ++i) {
3148 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3149 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3150 }
3151 }
3152 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003153 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003154
Winson43d1f262016-06-14 16:05:55 -07003155 /**
3156 * Dumps the activities matching the given {@param name} in the either the focused stack
3157 * or all visible stacks if {@param dumpVisibleStacks} is true.
3158 */
3159 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3160 if (dumpVisibleStacks) {
3161 ArrayList<ActivityRecord> activities = new ArrayList<>();
3162 int numDisplays = mActivityDisplays.size();
3163 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3164 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3165 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3166 ActivityStack stack = stacks.get(stackNdx);
3167 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3168 activities.addAll(stack.getDumpActivitiesLocked(name));
3169 }
3170 }
3171 }
3172 return activities;
3173 } else {
3174 return mFocusedStack.getDumpActivitiesLocked(name);
3175 }
Craig Mautner20e72272013-04-01 13:45:53 -07003176 }
3177
Dianne Hackborn390517b2013-05-30 15:03:32 -07003178 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3179 boolean needSep, String prefix) {
3180 if (activity != null) {
3181 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3182 if (needSep) {
3183 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003184 }
3185 pw.print(prefix);
3186 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003187 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003188 }
3189 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003190 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003191 }
3192
Craig Mautner8d341ef2013-03-26 09:03:27 -07003193 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3194 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003195 boolean printed = false;
3196 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003197 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3198 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003199 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003200 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003201 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003202 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003203 final ActivityStack stack = stacks.get(stackNdx);
3204 StringBuilder stackHeader = new StringBuilder(128);
3205 stackHeader.append(" Stack #");
3206 stackHeader.append(stack.mStackId);
3207 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003208 stackHeader.append("\n");
3209 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3210 stackHeader.append("\n");
3211 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003212 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3213 needSep, stackHeader.toString());
3214 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3215 !dumpAll, false, dumpPackage, true,
3216 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003217
Craig Mautner4a1cb222013-12-04 16:14:06 -08003218 needSep = printed;
3219 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3220 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003221 if (pr) {
3222 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003223 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003224 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003225 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3226 " mResumedActivity: ");
3227 if (pr) {
3228 printed = true;
3229 needSep = false;
3230 }
3231 if (dumpAll) {
3232 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3233 " mLastPausedActivity: ");
3234 if (pr) {
3235 printed = true;
3236 needSep = true;
3237 }
3238 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3239 needSep, " mLastNoHistoryActivity: ");
3240 }
3241 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003242 }
3243 }
3244
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003245 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3246 false, dumpPackage, true, " Activities waiting to finish:", null);
3247 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3248 false, dumpPackage, true, " Activities waiting to stop:", null);
3249 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3250 false, dumpPackage, true, " Activities waiting for another to become visible:",
3251 null);
3252 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3253 false, dumpPackage, true, " Activities waiting to sleep:", null);
3254 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3255 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003256
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003257 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003258 }
3259
Dianne Hackborn390517b2013-05-30 15:03:32 -07003260 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003261 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003262 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003263 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003264 String innerPrefix = null;
3265 String[] args = null;
3266 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003267 for (int i=list.size()-1; i>=0; i--) {
3268 final ActivityRecord r = list.get(i);
3269 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3270 continue;
3271 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003272 if (innerPrefix == null) {
3273 innerPrefix = prefix + " ";
3274 args = new String[0];
3275 }
3276 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003277 final boolean full = !brief && (complete || !r.isInHistory());
3278 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003279 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003280 needNL = false;
3281 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003282 if (header1 != null) {
3283 pw.println(header1);
3284 header1 = null;
3285 }
3286 if (header2 != null) {
3287 pw.println(header2);
3288 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003289 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003290 if (lastTask != r.task) {
3291 lastTask = r.task;
3292 pw.print(prefix);
3293 pw.print(full ? "* " : " ");
3294 pw.println(lastTask);
3295 if (full) {
3296 lastTask.dump(pw, prefix + " ");
3297 } else if (complete) {
3298 // Complete + brief == give a summary. Isn't that obvious?!?
3299 if (lastTask.intent != null) {
3300 pw.print(prefix); pw.print(" ");
3301 pw.println(lastTask.intent.toInsecureStringWithClip());
3302 }
3303 }
3304 }
3305 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3306 pw.print(" #"); pw.print(i); pw.print(": ");
3307 pw.println(r);
3308 if (full) {
3309 r.dump(pw, innerPrefix);
3310 } else if (complete) {
3311 // Complete + brief == give a summary. Isn't that obvious?!?
3312 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3313 if (r.app != null) {
3314 pw.print(innerPrefix); pw.println(r.app);
3315 }
3316 }
3317 if (client && r.app != null && r.app.thread != null) {
3318 // flush anything that is already in the PrintWriter since the thread is going
3319 // to write to the file descriptor directly
3320 pw.flush();
3321 try {
3322 TransferPipe tp = new TransferPipe();
3323 try {
3324 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3325 r.appToken, innerPrefix, args);
3326 // Short timeout, since blocking here can
3327 // deadlock with the application.
3328 tp.go(fd, 2000);
3329 } finally {
3330 tp.kill();
3331 }
3332 } catch (IOException e) {
3333 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3334 } catch (RemoteException e) {
3335 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3336 }
3337 needNL = true;
3338 }
3339 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003340 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003341 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003342
Craig Mautnerf3333272013-04-22 10:55:53 -07003343 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003344 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3345 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003346 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3347 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003348 }
3349
3350 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003351 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003352 }
3353
3354 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003355 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3356 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003357 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3358 }
3359
Craig Mautner05d29032013-05-03 13:40:13 -07003360 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003361 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3362 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3363 }
Craig Mautner05d29032013-05-03 13:40:13 -07003364 }
3365
Craig Mautner0eea92c2013-05-16 13:35:39 -07003366 void removeSleepTimeouts() {
3367 mSleepTimeout = false;
3368 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3369 }
3370
3371 final void scheduleSleepTimeout() {
3372 removeSleepTimeouts();
3373 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3374 }
3375
Craig Mautner4a1cb222013-12-04 16:14:06 -08003376 @Override
3377 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003378 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003379 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3380 }
3381
3382 @Override
3383 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003384 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003385 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3386 }
3387
3388 @Override
3389 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003390 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003391 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3392 }
3393
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003394 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003395 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003396 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003397 newDisplay = mActivityDisplays.get(displayId) == null;
3398 if (newDisplay) {
3399 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003400 if (activityDisplay.mDisplay == null) {
3401 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3402 return;
3403 }
Craig Mautner4504de52013-12-20 09:06:56 -08003404 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003405 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003406 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003407 }
Craig Mautner4504de52013-12-20 09:06:56 -08003408 if (newDisplay) {
3409 mWindowManager.onDisplayAdded(displayId);
3410 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003411 }
3412
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003413 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003414 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003415 mService.mContext.getResources().getDimensionPixelSize(
3416 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003417 }
3418
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003419 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003420 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003421 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3422 if (activityDisplay != null) {
3423 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003424 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003425 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003426 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003427 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003428 }
3429 }
3430 mWindowManager.onDisplayRemoved(displayId);
3431 }
3432
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003433 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003434 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003435 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3436 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003437 // TODO: Update the bounds.
3438 }
3439 }
3440 mWindowManager.onDisplayChanged(displayId);
3441 }
3442
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003443 private StackInfo getStackInfoLocked(ActivityStack stack) {
Winson529c8e42016-05-17 11:08:40 -07003444 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003445 StackInfo info = new StackInfo();
3446 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3447 info.displayId = Display.DEFAULT_DISPLAY;
3448 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003449 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003450 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003451 info.position = display != null
3452 ? display.mStacks.indexOf(stack)
3453 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003454
3455 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3456 final int numTasks = tasks.size();
3457 int[] taskIds = new int[numTasks];
3458 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003459 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003460 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003461 for (int i = 0; i < numTasks; ++i) {
3462 final TaskRecord task = tasks.get(i);
3463 taskIds[i] = task.taskId;
3464 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3465 : task.realActivity != null ? task.realActivity.flattenToString()
3466 : task.getTopActivity() != null ? task.getTopActivity().packageName
3467 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003468 taskBounds[i] = new Rect();
3469 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003470 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003471 }
3472 info.taskIds = taskIds;
3473 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003474 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003475 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003476
3477 final ActivityRecord top = stack.topRunningActivityLocked();
3478 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003479 return info;
3480 }
3481
3482 StackInfo getStackInfoLocked(int stackId) {
3483 ActivityStack stack = getStack(stackId);
3484 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003485 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003486 }
3487 return null;
3488 }
3489
3490 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003491 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003492 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3493 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003494 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003495 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003496 }
3497 }
3498 return list;
3499 }
3500
Craig Mautner15df08a2015-04-01 12:17:18 -07003501 TaskRecord getLockedTaskLocked() {
3502 final int top = mLockTaskModeTasks.size() - 1;
3503 if (top >= 0) {
3504 return mLockTaskModeTasks.get(top);
3505 }
3506 return null;
3507 }
3508
3509 boolean isLockedTask(TaskRecord task) {
3510 return mLockTaskModeTasks.contains(task);
3511 }
3512
3513 boolean isLastLockedTask(TaskRecord task) {
3514 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3515 }
3516
3517 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003518 if (!mLockTaskModeTasks.remove(task)) {
3519 return;
3520 }
3521 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3522 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003523 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003524 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3525 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003526 final Message lockTaskMsg = Message.obtain();
3527 lockTaskMsg.arg1 = task.userId;
3528 lockTaskMsg.what = LOCK_TASK_END_MSG;
3529 mHandler.sendMessage(lockTaskMsg);
3530 }
3531 }
3532
Andrii Kulianc27916642016-04-12 17:59:27 -07003533 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3534 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3535 false /* forceNonResizable */);
3536 }
3537
Jorim Jaggid53f0922016-04-06 22:16:23 -07003538 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003539 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003540 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3541 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003542 return;
3543 }
3544
Jorim Jaggicd13d332016-04-27 15:40:20 -07003545 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003546 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003547 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003548 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3549
Andrii Kulianc27916642016-04-12 17:59:27 -07003550 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3551 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003552 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003553 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3554 && !topActivity.noDisplay) {
3555 String packageName = topActivity.appInfo.packageName;
Jorim Jaggi2adba072016-03-03 13:43:39 +01003556 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3557 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003558 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003559 }
3560
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003561 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003562 if (mLockTaskNotify != null) {
3563 mLockTaskNotify.showToast(mLockTaskModeState);
3564 }
Jason Monka8f569c2014-07-07 12:15:21 -04003565 }
3566
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003567 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3568 if (mLockTaskModeTasks.contains(task)) {
3569 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3570 }
3571 }
3572
Craig Mautner432f64e2015-05-20 14:59:57 -07003573 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3574 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003575 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003576 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003577 final TaskRecord lockedTask = getLockedTaskLocked();
3578 if (lockedTask != null) {
3579 removeLockedTaskLocked(lockedTask);
3580 if (!mLockTaskModeTasks.isEmpty()) {
3581 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003582 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3583 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003584 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003585 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003586 return;
3587 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003588 }
Craig Mautnere0570202015-05-13 13:06:11 -07003589 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3590 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003591 return;
3592 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003593
3594 // Should have already been checked, but do it again.
3595 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003596 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3597 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003598 return;
3599 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003600 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003601 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003602 return;
3603 }
3604
3605 if (mLockTaskModeTasks.isEmpty()) {
3606 // First locktask.
3607 final Message lockTaskMsg = Message.obtain();
3608 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3609 lockTaskMsg.arg1 = task.userId;
3610 lockTaskMsg.what = LOCK_TASK_START_MSG;
3611 lockTaskMsg.arg2 = lockTaskModeState;
3612 mHandler.sendMessage(lockTaskMsg);
3613 }
3614 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003615 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3616 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003617 mLockTaskModeTasks.remove(task);
3618 mLockTaskModeTasks.add(task);
3619
3620 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003621 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003622 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003623
3624 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003625 findTaskToMoveToFrontLocked(task, 0, null, reason,
3626 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003627 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003628 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3629 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3630 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003631 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003632 }
3633
3634 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003635 return isLockTaskModeViolation(task, false);
3636 }
3637
3638 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3639 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003640 return false;
3641 }
3642 final int lockTaskAuth = task.mLockTaskAuth;
3643 switch (lockTaskAuth) {
3644 case LOCK_TASK_AUTH_DONT_LOCK:
3645 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003646 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003647 case LOCK_TASK_AUTH_LAUNCHABLE:
3648 case LOCK_TASK_AUTH_WHITELISTED:
3649 return false;
3650 case LOCK_TASK_AUTH_PINNABLE:
3651 // Pinnable tasks can't be launched on top of locktask tasks.
3652 return !mLockTaskModeTasks.isEmpty();
3653 default:
3654 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3655 return true;
3656 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003657 }
3658
Craig Mautner15df08a2015-04-01 12:17:18 -07003659 void onLockTaskPackagesUpdatedLocked() {
3660 boolean didSomething = false;
3661 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3662 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003663 final boolean wasWhitelisted =
3664 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3665 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003666 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003667 final boolean isWhitelisted =
3668 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3669 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3670 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003671 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003672 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3673 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003674 removeLockedTaskLocked(lockedTask);
3675 lockedTask.performClearTaskLocked();
3676 didSomething = true;
3677 }
3678 }
3679 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3680 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3681 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3682 final ActivityStack stack = stacks.get(stackNdx);
3683 stack.onLockTaskPackagesUpdatedLocked();
3684 }
3685 }
Craig Mautnere0570202015-05-13 13:06:11 -07003686 final ActivityRecord r = topRunningActivityLocked();
3687 final TaskRecord task = r != null ? r.task : null;
3688 if (mLockTaskModeTasks.isEmpty() && task != null
3689 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3690 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003691 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3692 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3693 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3694 false);
3695 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003696 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003697 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003698 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003699 }
3700 }
3701
Benjamin Franz43261142015-02-11 15:59:44 +00003702 int getLockTaskModeState() {
3703 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003704 }
3705
Jorim Jaggife89d122015-12-22 16:28:44 +01003706 void activityRelaunchedLocked(IBinder token) {
3707 mWindowManager.notifyAppRelaunchingFinished(token);
3708 }
3709
3710 void activityRelaunchingLocked(ActivityRecord r) {
3711 mWindowManager.notifyAppRelaunching(r.appToken);
3712 }
3713
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003714 void logStackState() {
3715 mActivityMetricsLogger.logWindowState();
3716 }
3717
Andrii Kulian933076d2016-03-29 17:04:42 -07003718 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003719 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3720 final ActivityRecord r = task.mActivities.get(i);
3721 if (r.app != null && r.app.thread != null) {
3722 mMultiWindowModeChangedActivities.add(r);
3723 }
3724 }
3725
3726 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3727 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3728 }
3729 }
3730
Andrii Kulian933076d2016-03-29 17:04:42 -07003731 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003732 final ActivityStack stack = task.stack;
3733 if (prevStack == null || prevStack == stack
3734 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3735 return;
3736 }
3737
3738 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3739 final ActivityRecord r = task.mActivities.get(i);
3740 if (r.app != null && r.app.thread != null) {
3741 mPipModeChangedActivities.add(r);
3742 }
3743 }
3744
3745 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3746 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3747 }
3748 }
3749
Tony Mak853304c2016-04-18 15:17:41 +01003750 void setDockedStackMinimized(boolean minimized) {
3751 mIsDockMinimized = minimized;
3752 if (minimized) {
3753 // Docked stack is not visible, no need to confirm credentials for its top activity.
3754 return;
3755 }
3756 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3757 if (dockedStack == null) {
3758 return;
3759 }
3760 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3761 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3762 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3763 }
3764 }
3765
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003766 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003767
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003768 public ActivityStackSupervisorHandler(Looper looper) {
3769 super(looper);
3770 }
3771
Craig Mautnerf3333272013-04-22 10:55:53 -07003772 void activityIdleInternal(ActivityRecord r) {
3773 synchronized (mService) {
3774 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3775 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003776 }
3777
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003778 @Override
3779 public void handleMessage(Message msg) {
3780 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003781 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3782 synchronized (mService) {
3783 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3784 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003785 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003786 }
3787 }
3788 } break;
3789 case REPORT_PIP_MODE_CHANGED_MSG: {
3790 synchronized (mService) {
3791 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3792 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003793 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003794 }
3795 }
3796 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003797 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003798 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3799 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003800 if (mService.mDidDexOpt) {
3801 mService.mDidDexOpt = false;
3802 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3803 nmsg.obj = msg.obj;
3804 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3805 return;
3806 }
3807 // We don't at this point know if the activity is fullscreen,
3808 // so we need to be conservative and assume it isn't.
3809 activityIdleInternal((ActivityRecord)msg.obj);
3810 } break;
3811 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003812 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003813 activityIdleInternal((ActivityRecord)msg.obj);
3814 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003815 case RESUME_TOP_ACTIVITY_MSG: {
3816 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003817 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003818 }
3819 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003820 case SLEEP_TIMEOUT_MSG: {
3821 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003822 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003823 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3824 mSleepTimeout = true;
3825 checkReadyForSleepLocked();
3826 }
3827 }
3828 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003829 case LAUNCH_TIMEOUT_MSG: {
3830 if (mService.mDidDexOpt) {
3831 mService.mDidDexOpt = false;
3832 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3833 return;
3834 }
3835 synchronized (mService) {
3836 if (mLaunchingActivity.isHeld()) {
3837 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3838 if (VALIDATE_WAKE_LOCK_CALLER
3839 && Binder.getCallingUid() != Process.myUid()) {
3840 throw new IllegalStateException("Calling must be system uid");
3841 }
3842 mLaunchingActivity.release();
3843 }
3844 }
3845 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003846 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003847 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 } break;
3849 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003850 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003851 } break;
3852 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003853 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003854 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003855 case CONTAINER_CALLBACK_VISIBILITY: {
3856 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003857 final IActivityContainerCallback callback = container.mCallback;
3858 if (callback != null) {
3859 try {
3860 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3861 } catch (RemoteException e) {
3862 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003863 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003864 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003865 case LOCK_TASK_START_MSG: {
3866 // When lock task starts, we disable the status bars.
3867 try {
Jason Monk62515be2014-05-21 16:06:19 -04003868 if (mLockTaskNotify == null) {
3869 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003870 }
Jason Monk62515be2014-05-21 16:06:19 -04003871 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003872 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003873 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003874 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003875 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003876 flags = StatusBarManager.DISABLE_MASK
3877 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003878 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003879 flags = StatusBarManager.DISABLE_MASK
3880 & (~StatusBarManager.DISABLE_BACK)
3881 & (~StatusBarManager.DISABLE_HOME)
3882 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003883 }
3884 getStatusBarService().disable(flags, mToken,
3885 mService.mContext.getPackageName());
3886 }
3887 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003888 if (getDevicePolicyManager() != null) {
3889 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003890 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003891 }
justinzhang5286d3f2014-05-12 17:06:01 -04003892 } catch (RemoteException ex) {
3893 throw new RuntimeException(ex);
3894 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003895 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003896 case LOCK_TASK_END_MSG: {
3897 // When lock task ends, we enable the status bars.
3898 try {
Jason Monk62515be2014-05-21 16:06:19 -04003899 if (getStatusBarService() != null) {
3900 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3901 mService.mContext.getPackageName());
3902 }
3903 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003904 if (getDevicePolicyManager() != null) {
3905 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3906 msg.arg1);
3907 }
Jason Monk62515be2014-05-21 16:06:19 -04003908 if (mLockTaskNotify == null) {
3909 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3910 }
3911 mLockTaskNotify.show(false);
3912 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003913 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003914 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003915 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003916 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003917 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003918 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003919 new LockPatternUtils(mService.mContext)
3920 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003921 }
3922 } catch (SettingNotFoundException e) {
3923 // No setting, don't lock.
3924 }
justinzhang5286d3f2014-05-12 17:06:01 -04003925 } catch (RemoteException ex) {
3926 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003927 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003928 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003929 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003930 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003931 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3932 if (mLockTaskNotify == null) {
3933 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3934 }
3935 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3936 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003937 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3938 final ActivityContainer container = (ActivityContainer) msg.obj;
3939 final IActivityContainerCallback callback = container.mCallback;
3940 if (callback != null) {
3941 try {
3942 callback.onAllActivitiesComplete(container.asBinder());
3943 } catch (RemoteException e) {
3944 }
3945 }
3946 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003947 case LAUNCH_TASK_BEHIND_COMPLETE: {
3948 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003949 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003950 if (r != null) {
3951 handleLaunchTaskBehindCompleteLocked(r);
3952 }
3953 }
3954 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003955
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003956 }
3957 }
3958 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003959
Ying Wangb081a592014-04-22 15:20:16 -07003960 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003961 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3962 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003963 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003964 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003965 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003966 ActivityRecord mParentActivity = null;
3967 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003968
Craig Mautnere3a00d72014-04-16 08:31:19 -07003969 boolean mVisible = true;
3970
Craig Mautner4a1cb222013-12-04 16:14:06 -08003971 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003972 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003973
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003974 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3975 final static int CONTAINER_STATE_NO_SURFACE = 1;
3976 final static int CONTAINER_STATE_FINISHING = 2;
3977 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3978
3979 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003980 synchronized (mService) {
3981 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003982 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003983 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003984 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003985 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003986 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003987
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003988 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003989 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003990 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003991 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003992 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003993 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003994 }
3995
3996 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003997 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003998 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003999 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4000 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004001 return;
4002 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004003 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004004 }
4005 }
4006
4007 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004008 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004009 synchronized (mService) {
4010 if (mActivityDisplay != null) {
4011 return mActivityDisplay.mDisplayId;
4012 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004013 }
4014 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004015 }
4016
Jeff Brownca9bc702014-02-11 14:32:56 -08004017 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004018 public int getStackId() {
4019 synchronized (mService) {
4020 return mStackId;
4021 }
4022 }
4023
4024 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004025 public boolean injectEvent(InputEvent event) {
4026 final long origId = Binder.clearCallingIdentity();
4027 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004028 synchronized (mService) {
4029 if (mActivityDisplay != null) {
4030 return mInputManagerInternal.injectInputEvent(event,
4031 mActivityDisplay.mDisplayId,
4032 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4033 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004034 }
4035 return false;
4036 } finally {
4037 Binder.restoreCallingIdentity(origId);
4038 }
4039 }
4040
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004041 @Override
4042 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004043 synchronized (mService) {
4044 if (mContainerState == CONTAINER_STATE_FINISHING) {
4045 return;
4046 }
4047 mContainerState = CONTAINER_STATE_FINISHING;
4048
Craig Mautnerd163e752014-06-13 17:18:47 -07004049 long origId = Binder.clearCallingIdentity();
4050 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004051 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004052 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004053 } finally {
4054 Binder.restoreCallingIdentity(origId);
4055 }
4056 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004057 }
4058
Craig Mautner60257702014-09-17 15:02:33 -07004059 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004060 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004061 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004062 if (mActivityDisplay != null) {
4063 mActivityDisplay.detachActivitiesLocked(mStack);
4064 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004065 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004066 }
4067 }
4068
4069 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004070 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004071 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004072 }
4073
4074 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004075 public final int startActivityIntentSender(IIntentSender intentSender)
4076 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004077 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4078
4079 if (!(intentSender instanceof PendingIntentRecord)) {
4080 throw new IllegalArgumentException("Bad PendingIntent object");
4081 }
4082
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004083 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004084 Binder.getCallingUid(), mCurrentUser, false,
4085 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004086
4087 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4088 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4089 pendingIntent.key.requestResolvedType);
4090
4091 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4092 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4093 }
4094
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004095 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004096 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004097 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004098 throw new SecurityException(
4099 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4100 }
4101 }
4102
Craig Mautnerdf88d732014-01-27 09:21:32 -08004103 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004104 public IBinder asBinder() {
4105 return this;
4106 }
4107
Craig Mautner4504de52013-12-20 09:06:56 -08004108 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004109 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004110 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004111 }
4112
Craig Mautner4a1cb222013-12-04 16:14:06 -08004113 ActivityStackSupervisor getOuter() {
4114 return ActivityStackSupervisor.this;
4115 }
4116
Craig Mautnerd163e752014-06-13 17:18:47 -07004117 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004118 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004119 }
4120
Craig Mautner6985bad2014-04-21 15:22:06 -07004121 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004122 void setVisible(boolean visible) {
4123 if (mVisible != visible) {
4124 mVisible = visible;
4125 if (mCallback != null) {
4126 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4127 0 /* unused */, this).sendToTarget();
4128 }
4129 }
4130 }
4131
Craig Mautner6985bad2014-04-21 15:22:06 -07004132 void setDrawn() {
4133 }
4134
Craig Mautner1b4bf852014-05-26 15:06:32 -07004135 // You can always start a new task on a regular ActivityStack.
4136 boolean isEligibleForNewTasks() {
4137 return true;
4138 }
4139
Craig Mautnerd163e752014-06-13 17:18:47 -07004140 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004141 detachLocked();
4142 deleteActivityContainer(this);
4143 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004144 }
4145
Craig Mautner34b73df2014-01-12 21:11:08 -08004146 @Override
4147 public String toString() {
4148 return mIdString + (mActivityDisplay == null ? "N" : "A");
4149 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004150 }
4151
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004152 private class VirtualActivityContainer extends ActivityContainer {
4153 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004154 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004155
4156 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4157 super(getNextStackId());
4158 mParentActivity = parent;
4159 mCallback = callback;
4160 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004161 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004162 }
4163
4164 @Override
4165 public void setSurface(Surface surface, int width, int height, int density) {
4166 super.setSurface(surface, width, height, density);
4167
4168 synchronized (mService) {
4169 final long origId = Binder.clearCallingIdentity();
4170 try {
4171 setSurfaceLocked(surface, width, height, density);
4172 } finally {
4173 Binder.restoreCallingIdentity(origId);
4174 }
4175 }
4176 }
4177
4178 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4179 if (mContainerState == CONTAINER_STATE_FINISHING) {
4180 return;
4181 }
4182 VirtualActivityDisplay virtualActivityDisplay =
4183 (VirtualActivityDisplay) mActivityDisplay;
4184 if (virtualActivityDisplay == null) {
4185 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004186 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004187 mActivityDisplay = virtualActivityDisplay;
4188 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004189 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004190 }
4191
4192 if (mSurface != null) {
4193 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004194 }
4195
Craig Mautner6985bad2014-04-21 15:22:06 -07004196 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004197 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004198 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004199 } else {
4200 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004201 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004202 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004203 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004204 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004205 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004206
Craig Mautnerd163e752014-06-13 17:18:47 -07004207 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004208
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004209 if (DEBUG_STACK) Slog.d(TAG_STACK,
4210 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004211 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004212
Craig Mautner6985bad2014-04-21 15:22:06 -07004213 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004214 boolean isAttachedLocked() {
4215 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004216 }
4217
4218 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004219 void setDrawn() {
4220 synchronized (mService) {
4221 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004222 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004223 }
4224 }
4225
Craig Mautner1b4bf852014-05-26 15:06:32 -07004226 // Never start a new task on an ActivityView if it isn't explicitly specified.
4227 @Override
4228 boolean isEligibleForNewTasks() {
4229 return false;
4230 }
4231
Craig Mautnerd163e752014-06-13 17:18:47 -07004232 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004233 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004234 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4235 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4236 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4237 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4238 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004239 }
4240 }
4241
Craig Mautner4a1cb222013-12-04 16:14:06 -08004242 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4243 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004244 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004245 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004246 int mDisplayId;
4247 Display mDisplay;
4248 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004249
Craig Mautner4a1cb222013-12-04 16:14:06 -08004250 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4251 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004252 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004253
Jose Lima4b6c6692014-08-12 17:41:12 -07004254 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004255
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004256 ActivityDisplay() {
4257 }
Craig Mautner4504de52013-12-20 09:06:56 -08004258
Craig Mautner1a70a162014-09-13 12:09:31 -07004259 // After instantiation, check that mDisplay is not null before using this. The alternative
4260 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004261 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004262 final Display display = mDisplayManager.getDisplay(displayId);
4263 if (display == null) {
4264 return;
4265 }
4266 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004267 }
4268
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004269 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004270 mDisplay = display;
4271 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004272 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004273 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004274
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004275 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004276 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004277 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4278 + " onTop=" + onTop);
4279 if (onTop) {
4280 mStacks.add(stack);
4281 } else {
4282 mStacks.add(0, stack);
4283 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004284 }
4285
Craig Mautnere0a38842013-12-16 16:14:02 -08004286 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004287 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004288 + " from displayId=" + mDisplayId);
4289 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004290 }
4291
Jose Lima4b6c6692014-08-12 17:41:12 -07004292 void setVisibleBehindActivity(ActivityRecord r) {
4293 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004294 }
4295
Jose Lima4b6c6692014-08-12 17:41:12 -07004296 boolean hasVisibleBehindActivity() {
4297 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004298 }
4299
Craig Mautner34b73df2014-01-12 21:11:08 -08004300 @Override
4301 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004302 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4303 }
4304 }
4305
4306 class VirtualActivityDisplay extends ActivityDisplay {
4307 VirtualDisplay mVirtualDisplay;
4308
Craig Mautner6985bad2014-04-21 15:22:06 -07004309 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004310 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004311 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4312 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4313 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4314 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004315
4316 init(mVirtualDisplay.getDisplay());
4317
4318 mWindowManager.handleDisplayAdded(mDisplayId);
4319 }
4320
4321 void setSurface(Surface surface) {
4322 if (mVirtualDisplay != null) {
4323 mVirtualDisplay.setSurface(surface);
4324 }
4325 }
4326
4327 @Override
4328 void detachActivitiesLocked(ActivityStack stack) {
4329 super.detachActivitiesLocked(stack);
4330 if (mVirtualDisplay != null) {
4331 mVirtualDisplay.release();
4332 mVirtualDisplay = null;
4333 }
4334 }
4335
4336 @Override
4337 public String toString() {
4338 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004339 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004340 }
Jose Lima58e66d62014-05-27 20:00:27 -07004341
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004342 /**
4343 * Adjust bounds to stay within stack bounds.
4344 *
4345 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4346 * that keep them unchanged, but be contained within the stack bounds.
4347 *
4348 * @param bounds Bounds to be adjusted.
4349 * @param stackBounds Bounds within which the other bounds should remain.
4350 */
4351 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4352 if (stackBounds == null || stackBounds.contains(bounds)) {
4353 return;
4354 }
4355
4356 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4357 final int maxRight = stackBounds.right
4358 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4359 int horizontalDiff = stackBounds.left - bounds.left;
4360 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4361 || (bounds.left + horizontalDiff >= maxRight)) {
4362 horizontalDiff = maxRight - bounds.left;
4363 }
4364 bounds.left += horizontalDiff;
4365 bounds.right += horizontalDiff;
4366 }
4367
4368 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4369 final int maxBottom = stackBounds.bottom
4370 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4371 int verticalDiff = stackBounds.top - bounds.top;
4372 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4373 || (bounds.top + verticalDiff >= maxBottom)) {
4374 verticalDiff = maxBottom - bounds.top;
4375 }
4376 bounds.top += verticalDiff;
4377 bounds.bottom += verticalDiff;
4378 }
4379 }
4380
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004381 ActivityStack findStackBehind(ActivityStack stack) {
4382 // TODO(multi-display): We are only looking for stacks on the default display.
4383 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4384 if (display == null) {
4385 return null;
4386 }
4387 final ArrayList<ActivityStack> stacks = display.mStacks;
4388 for (int i = stacks.size() - 1; i >= 0; i--) {
4389 if (stacks.get(i) == stack && i > 0) {
4390 return stacks.get(i - 1);
4391 }
4392 }
4393 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4394 + " in=" + stacks);
4395 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004396
Jorim Jaggic69bd222016-03-15 14:38:37 +01004397 /**
4398 * Puts a task into resizing mode during the next app transition.
4399 *
4400 * @param taskId the id of the task to put into resizing mode
4401 */
4402 private void setResizingDuringAnimation(int taskId) {
4403 mResizingTasksDuringAnimation.add(taskId);
4404 mWindowManager.setTaskDockedResizing(taskId, true);
4405 }
4406
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004407 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4408 final TaskRecord task;
4409 final int callingUid;
4410 final String callingPackage;
4411 final Intent intent;
4412 final int userId;
4413 final ActivityOptions activityOptions = (bOptions != null)
4414 ? new ActivityOptions(bOptions) : null;
4415 final int launchStackId = (activityOptions != null)
4416 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004417 if (launchStackId == HOME_STACK_ID) {
4418 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4419 + taskId + " can't be launch in the home stack.");
4420 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004421
4422 if (launchStackId == DOCKED_STACK_ID) {
4423 mWindowManager.setDockedStackCreateState(
4424 activityOptions.getDockCreateMode(), null /* initialBounds */);
4425
4426 // Defer updating the stack in which recents is until the app transition is done, to
4427 // not run into issues where we still need to draw the task in recents but the
4428 // docked stack is already created.
4429 deferUpdateBounds(HOME_STACK_ID);
4430 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4431 }
4432
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004433 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4434 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004435 continueUpdateBounds(HOME_STACK_ID);
4436 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004437 throw new IllegalArgumentException(
4438 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4439 }
4440
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004441 // Since we don't have an actual source record here, we assume that the currently focused
4442 // activity was the source.
4443 final ActivityStack focusedStack = getFocusedStack();
4444 final ActivityRecord sourceRecord =
4445 focusedStack != null ? focusedStack.topActivity() : null;
4446
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004447 if (launchStackId != INVALID_STACK_ID) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004448 if (task.stack.mStackId != launchStackId) {
4449 moveTaskToStackLocked(
4450 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4451 ANIMATE);
4452 }
4453 }
4454
4455 // If the user must confirm credentials (e.g. when first launching a work app and the
4456 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4457 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4458 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004459 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004460 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004461 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004462 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4463 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004464
4465 // If we are launching the task in the docked stack, put it into resizing mode so
4466 // the window renders full-screen with the background filling the void. Also only
4467 // call this at the end to make sure that tasks exists on the window manager side.
4468 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004469 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004470 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004471
4472 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4473 ActivityManager.START_TASK_TO_FRONT,
4474 sourceRecord != null ? sourceRecord.task.stack.mStackId : INVALID_STACK_ID,
4475 sourceRecord, task.stack);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004476 return ActivityManager.START_TASK_TO_FRONT;
4477 }
4478 callingUid = task.mCallingUid;
4479 callingPackage = task.mCallingPackage;
4480 intent = task.intent;
4481 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4482 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004483 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4484 null, null, 0, 0, bOptions, userId, null, task);
4485 if (launchStackId == DOCKED_STACK_ID) {
4486 setResizingDuringAnimation(task.taskId);
4487 }
4488 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004489 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004490
4491 /**
4492 * @return a list of activities which are the top ones in each visible stack. The first
4493 * entry will be the focused activity.
4494 */
4495 public List<IBinder> getTopVisibleActivities() {
4496 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4497 if (display == null) {
4498 return Collections.EMPTY_LIST;
4499 }
4500 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4501 final ArrayList<ActivityStack> stacks = display.mStacks;
4502 for (int i = stacks.size() - 1; i >= 0; i--) {
4503 ActivityStack stack = stacks.get(i);
4504 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4505 ActivityRecord top = stack.topActivity();
4506 if (top != null) {
4507 if (stack == mFocusedStack) {
4508 topActivityTokens.add(0, top.appToken);
4509 } else {
4510 topActivityTokens.add(top.appToken);
4511 }
4512 }
4513 }
4514 }
4515 return topActivityTokens;
4516 }
Craig Mautner27084302013-03-25 08:05:25 -07004517}