blob: a4fc251b70d87ea4b36eb3e7d7621469539e42e0 [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);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001192 // Deferring resume here because we're going to launch new activity shortly.
1193 // We don't want to perform a redundant launch of the same record while ensuring
1194 // configurations and trying to resume top activity of focused stack.
1195 mService.updateConfigurationLocked(config, r, false, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -07001196 }
1197
1198 r.app = app;
1199 app.waitingToKill = null;
1200 r.launchCount++;
1201 r.lastLaunchTime = SystemClock.uptimeMillis();
1202
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001203 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001204
1205 int idx = app.activities.indexOf(r);
1206 if (idx < 0) {
1207 app.activities.add(r);
1208 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001209 mService.updateLruProcessLocked(app, true, null);
1210 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001211
Craig Mautner15df08a2015-04-01 12:17:18 -07001212 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001213 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1214 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001215 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001216 }
1217
1218 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001219 try {
1220 if (app.thread == null) {
1221 throw new RemoteException();
1222 }
1223 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001224 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001225 if (andResume) {
1226 results = r.results;
1227 newIntents = r.newIntents;
1228 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001229 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1230 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1231 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001232 if (andResume) {
1233 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1234 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001235 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001236 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001237 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001238 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001239 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001241 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1242 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001243 r.sleeping = false;
1244 r.forceNewConfig = false;
Alan Viverette5f31b982016-06-21 14:46:14 -04001245 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001246 mService.showAskCompatModeDialogLocked(r);
1247 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001248 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1250 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1251 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001252 final String profileFile = mService.mProfileFile;
1253 if (profileFile != null) {
1254 ParcelFileDescriptor profileFd = mService.mProfileFd;
1255 if (profileFd != null) {
1256 try {
1257 profileFd = profileFd.dup();
1258 } catch (IOException e) {
1259 if (profileFd != null) {
1260 try {
1261 profileFd.close();
1262 } catch (IOException o) {
1263 }
1264 profileFd = null;
1265 }
1266 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001268
1269 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1270 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001271 }
1272 }
1273 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001274
Craig Mautner2568c3a2015-03-26 14:22:34 -07001275 if (andResume) {
1276 app.hasShownUi = true;
1277 app.pendingUiClean = true;
1278 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001279 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001281 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001282 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001283 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001284 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001285
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001286 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001287 // This may be a heavy-weight process! Note that the package
1288 // manager will ensure that only activity can run in the main
1289 // process of the .apk, which is the only thing that will be
1290 // considered heavy-weight.
1291 if (app.processName.equals(app.info.packageName)) {
1292 if (mService.mHeavyWeightProcess != null
1293 && mService.mHeavyWeightProcess != app) {
1294 Slog.w(TAG, "Starting new heavy weight process " + app
1295 + " when already running "
1296 + mService.mHeavyWeightProcess);
1297 }
1298 mService.mHeavyWeightProcess = app;
1299 Message msg = mService.mHandler.obtainMessage(
1300 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1301 msg.obj = r;
1302 mService.mHandler.sendMessage(msg);
1303 }
1304 }
1305
1306 } catch (RemoteException e) {
1307 if (r.launchFailed) {
1308 // This is the second time we failed -- finish activity
1309 // and give up.
1310 Slog.e(TAG, "Second failure launching "
1311 + r.intent.getComponent().flattenToShortString()
1312 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001313 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001314 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1315 "2nd-crash", false);
1316 return false;
1317 }
1318
1319 // This is the first time we failed -- restart process and
1320 // retry.
1321 app.activities.remove(r);
1322 throw e;
1323 }
1324
1325 r.launchFailed = false;
1326 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001327 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001328 }
1329
1330 if (andResume) {
1331 // As part of the process of launching, ActivityThread also performs
1332 // a resume.
1333 stack.minimalResumeActivityLocked(r);
1334 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001335 // This activity is not starting in the resumed state... which should look like we asked
1336 // it to pause+stop (but remain visible), and it has done so and reported back the
1337 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001338 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001339 "Moving to PAUSED: " + r + " (starting in paused state)");
1340 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001341 }
1342
1343 // Launch the new version setup screen if needed. We do this -after-
1344 // launching the initial activity (that is, home), so that it can have
1345 // a chance to initialize itself while in the background, making the
1346 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001347 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001348 mService.startSetupActivityLocked();
1349 }
1350
Dianne Hackborn465fa392014-09-14 14:21:18 -07001351 // Update any services we are bound to that might care about whether
1352 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001353 if (r.app != null) {
1354 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1355 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001356
Craig Mautner2420ead2013-04-01 17:13:20 -07001357 return true;
1358 }
1359
Craig Mautnere79d42682013-04-01 19:01:53 -07001360 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001361 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001362 // Is this activity's application already running?
1363 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001364 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001365
1366 r.task.stack.setLaunchTime(r);
1367
1368 if (app != null && app.thread != null) {
1369 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001370 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1371 || !"android".equals(r.info.packageName)) {
1372 // Don't add this if it is a platform component that is marked
1373 // to run in multiple processes, because this is actually
1374 // part of the framework so doesn't make sense to track as a
1375 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001376 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1377 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001378 }
George Mount2c92c972014-03-20 09:38:23 -07001379 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001380 return;
1381 } catch (RemoteException e) {
1382 Slog.w(TAG, "Exception when starting activity "
1383 + r.intent.getComponent().flattenToShortString(), e);
1384 }
1385
1386 // If a dead object exception was thrown -- fall through to
1387 // restart the application.
1388 }
1389
1390 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001391 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001392 }
1393
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001394 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001395 String resultWho, int requestCode, int callingPid, int callingUid,
1396 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001397 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001398 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1399 callingUid);
1400 if (startAnyPerm == PERMISSION_GRANTED) {
1401 return true;
1402 }
1403 final int componentRestriction = getComponentRestrictionForCallingPackage(
1404 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1405 final int actionRestriction = getActionRestrictionForCallingPackage(
1406 intent.getAction(), callingPackage, callingPid, callingUid);
1407 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1408 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1409 if (resultRecord != null) {
1410 resultStack.sendActivityResultLocked(-1,
1411 resultRecord, resultWho, requestCode,
1412 Activity.RESULT_CANCELED, null);
1413 }
1414 final String msg;
1415 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1416 msg = "Permission Denial: starting " + intent.toString()
1417 + " from " + callerApp + " (pid=" + callingPid
1418 + ", uid=" + callingUid + ")" + " with revoked permission "
1419 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1420 } else if (!aInfo.exported) {
1421 msg = "Permission Denial: starting " + intent.toString()
1422 + " from " + callerApp + " (pid=" + callingPid
1423 + ", uid=" + callingUid + ")"
1424 + " not exported from uid " + aInfo.applicationInfo.uid;
1425 } else {
1426 msg = "Permission Denial: starting " + intent.toString()
1427 + " from " + callerApp + " (pid=" + callingPid
1428 + ", uid=" + callingUid + ")"
1429 + " requires " + aInfo.permission;
1430 }
1431 Slog.w(TAG, msg);
1432 throw new SecurityException(msg);
1433 }
1434
1435 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1436 final String message = "Appop Denial: starting " + intent.toString()
1437 + " from " + callerApp + " (pid=" + callingPid
1438 + ", uid=" + callingUid + ")"
1439 + " requires " + AppOpsManager.permissionToOp(
1440 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1441 Slog.w(TAG, message);
1442 return false;
1443 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1444 final String message = "Appop Denial: starting " + intent.toString()
1445 + " from " + callerApp + " (pid=" + callingPid
1446 + ", uid=" + callingUid + ")"
1447 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1448 Slog.w(TAG, message);
1449 return false;
1450 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001451 if (options != null && options.getLaunchTaskId() != -1) {
1452 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1453 callingPid, callingUid);
1454 if (startInTaskPerm != PERMISSION_GRANTED) {
1455 final String msg = "Permission Denial: starting " + intent.toString()
1456 + " from " + callerApp + " (pid=" + callingPid
1457 + ", uid=" + callingUid + ") with launchTaskId="
1458 + options.getLaunchTaskId();
1459 Slog.w(TAG, msg);
1460 throw new SecurityException(msg);
1461 }
1462 }
1463
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001464 return true;
1465 }
1466
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001467 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001468 final long identity = Binder.clearCallingIdentity();
1469 try {
1470 return UserManager.get(mService.mContext).getUserInfo(userId);
1471 } finally {
1472 Binder.restoreCallingIdentity(identity);
1473 }
1474 }
1475
Svet Ganov99b60432015-06-27 13:15:22 -07001476 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001477 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001478 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1479 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001480 == PackageManager.PERMISSION_DENIED) {
1481 return ACTIVITY_RESTRICTION_PERMISSION;
1482 }
1483
Christopher Tateff7add02015-08-17 10:23:22 -07001484 if (activityInfo.permission == null) {
1485 return ACTIVITY_RESTRICTION_NONE;
1486 }
1487
Svet Ganov99b60432015-06-27 13:15:22 -07001488 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1489 if (opCode == AppOpsManager.OP_NONE) {
1490 return ACTIVITY_RESTRICTION_NONE;
1491 }
1492
1493 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1494 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001495 if (!ignoreTargetSecurity) {
1496 return ACTIVITY_RESTRICTION_APPOP;
1497 }
Svet Ganov99b60432015-06-27 13:15:22 -07001498 }
1499
1500 return ACTIVITY_RESTRICTION_NONE;
1501 }
1502
Svetoslav7008b512015-06-24 18:47:07 -07001503 private int getActionRestrictionForCallingPackage(String action,
1504 String callingPackage, int callingPid, int callingUid) {
1505 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001506 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001507 }
1508
1509 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1510 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001511 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001512 }
1513
1514 final PackageInfo packageInfo;
1515 try {
1516 packageInfo = mService.mContext.getPackageManager()
1517 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1518 } catch (PackageManager.NameNotFoundException e) {
1519 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001520 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001521 }
1522
1523 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001524 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001525 }
1526
1527 if (mService.checkPermission(permission, callingPid, callingUid) ==
1528 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001529 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001530 }
1531
1532 final int opCode = AppOpsManager.permissionToOpCode(permission);
1533 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001534 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001535 }
1536
1537 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1538 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001539 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001540 }
1541
Svet Ganov99b60432015-06-27 13:15:22 -07001542 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001543 }
1544
Wale Ogunwaled046a012015-12-24 13:05:59 -08001545 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001546 if (r == null) {
1547 // Not sure what you are trying to do, but it is not going to work...
1548 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001549 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001550 final TaskRecord task = r.task;
1551 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001552 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001553 return false;
1554 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001555 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001556 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001557 }
1558
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001559 void setLaunchSource(int uid) {
1560 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1561 }
1562
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001563 void acquireLaunchWakelock() {
1564 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1565 throw new IllegalStateException("Calling must be system uid");
1566 }
1567 mLaunchingActivity.acquire();
1568 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1569 // To be safe, don't allow the wake lock to be held for too long.
1570 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1571 }
1572 }
1573
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001574 /**
1575 * Called when the frontmost task is idle.
1576 * @return the state of mService.mBooting before this was called.
1577 */
1578 private boolean checkFinishBootingLocked() {
1579 final boolean booting = mService.mBooting;
1580 boolean enableScreen = false;
1581 mService.mBooting = false;
1582 if (!mService.mBooted) {
1583 mService.mBooted = true;
1584 enableScreen = true;
1585 }
1586 if (booting || enableScreen) {
1587 mService.postFinishBooting(booting, enableScreen);
1588 }
1589 return booting;
1590 }
1591
Craig Mautnerf3333272013-04-22 10:55:53 -07001592 // Checked.
1593 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1594 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001595 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001596
Craig Mautnerf3333272013-04-22 10:55:53 -07001597 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001598 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001599 int NS = 0;
1600 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001601 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001602 boolean activityRemoved = false;
1603
Wale Ogunwale7d701172015-03-11 15:36:30 -07001604 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001605 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001606 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1607 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001608 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1609 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001610 if (fromTimeout) {
1611 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001612 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001613
1614 // This is a hack to semi-deal with a race condition
1615 // in the client where it can be constructed with a
1616 // newer configuration from when we asked it to launch.
1617 // We'll update with whatever configuration it now says
1618 // it used to launch.
1619 if (config != null) {
1620 r.configuration = config;
1621 }
1622
1623 // We are now idle. If someone is waiting for a thumbnail from
1624 // us, we can now deliver.
1625 r.idle = true;
1626
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001627 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001628 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001629 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001630 }
1631 }
1632
1633 if (allResumedActivitiesIdle()) {
1634 if (r != null) {
1635 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001636 }
1637
1638 if (mLaunchingActivity.isHeld()) {
1639 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1640 if (VALIDATE_WAKE_LOCK_CALLER &&
1641 Binder.getCallingUid() != Process.myUid()) {
1642 throw new IllegalStateException("Calling must be system uid");
1643 }
1644 mLaunchingActivity.release();
1645 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001646 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001647 }
1648
1649 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001650 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001651 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001652 if ((NF = mFinishingActivities.size()) > 0) {
1653 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001654 mFinishingActivities.clear();
1655 }
1656
Craig Mautnerf3333272013-04-22 10:55:53 -07001657 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001658 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001659 mStartingUsers.clear();
1660 }
1661
Craig Mautnerf3333272013-04-22 10:55:53 -07001662 // Stop any activities that are scheduled to do so but have been
1663 // waiting for the next one to start.
1664 for (int i = 0; i < NS; i++) {
1665 r = stops.get(i);
1666 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001667 if (stack != null) {
1668 if (r.finishing) {
1669 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1670 } else {
1671 stack.stopActivityLocked(r);
1672 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001673 }
1674 }
1675
1676 // Finish any activities that are scheduled to do so but have been
1677 // waiting for the next one to start.
1678 for (int i = 0; i < NF; i++) {
1679 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001680 final ActivityStack stack = r.task.stack;
1681 if (stack != null) {
1682 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1683 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001684 }
1685
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001686 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001687 // Complete user switch
1688 if (startingUsers != null) {
1689 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001690 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001691 }
1692 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001693 }
1694
1695 mService.trimApplications();
1696 //dump();
1697 //mWindowManager.dump();
1698
Craig Mautnerf3333272013-04-22 10:55:53 -07001699 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001700 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001701 }
1702
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001703 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001704 }
1705
Craig Mautner8e569572013-10-11 17:36:59 -07001706 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001707 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001708 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1709 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001710 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1711 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1712 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001713 }
Craig Mautner19091252013-10-05 00:03:53 -07001714 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001715 }
1716
1717 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001718 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1719 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001720 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1721 stacks.get(stackNdx).closeSystemDialogsLocked();
1722 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001723 }
1724 }
1725
Craig Mautner93529a42013-10-04 15:03:13 -07001726 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001727 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001728 }
1729
Craig Mautner8d341ef2013-03-26 09:03:27 -07001730 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001731 * Update the last used stack id for non-current user (current user's last
1732 * used stack is the focused stack)
1733 */
1734 void updateUserStackLocked(int userId, ActivityStack stack) {
1735 if (userId != mCurrentUser) {
1736 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1737 }
1738 }
1739
1740 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001741 * @return true if some activity was finished (or would have finished if doit were true).
1742 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001743 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1744 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001745 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001746 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1747 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001748 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001749 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001750 if (stack.finishDisabledPackageActivitiesLocked(
1751 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001752 didSomething = true;
1753 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001754 }
1755 }
1756 return didSomething;
1757 }
1758
Dianne Hackborna413dc02013-07-12 12:02:55 -07001759 void updatePreviousProcessLocked(ActivityRecord r) {
1760 // Now that this process has stopped, we may want to consider
1761 // it to be the previous app to try to keep around in case
1762 // the user wants to return to it.
1763
1764 // First, found out what is currently the foreground app, so that
1765 // we don't blow away the previous app if this activity is being
1766 // hosted by the process that is actually still the foreground.
1767 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001768 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1769 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001770 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1771 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001772 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001773 if (stack.mResumedActivity != null) {
1774 fgApp = stack.mResumedActivity.app;
1775 } else if (stack.mPausingActivity != null) {
1776 fgApp = stack.mPausingActivity.app;
1777 }
1778 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001779 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001780 }
1781 }
1782
1783 // Now set this one as the previous process, only if that really
1784 // makes sense to.
1785 if (r.app != null && fgApp != null && r.app != fgApp
1786 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001787 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001788 mService.mPreviousProcess = r.app;
1789 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1790 }
1791 }
1792
Wale Ogunwaled046a012015-12-24 13:05:59 -08001793 boolean resumeFocusedStackTopActivityLocked() {
1794 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001795 }
1796
Wale Ogunwaled046a012015-12-24 13:05:59 -08001797 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001798 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001799 if (targetStack != null && isFocusedStack(targetStack)) {
1800 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001801 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001802 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1803 if (r == null || r.state != RESUMED) {
1804 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1805 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001806 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001807 }
1808
Todd Kennedy39bfee52016-02-24 10:28:21 -08001809 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1810 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1811 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1812 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1813 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1814 }
1815 }
1816 }
1817
Adrian Roos20d7df32016-01-12 18:59:43 +01001818 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1819 TaskRecord finishedTask = null;
1820 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001821 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1822 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001823 final int numStacks = stacks.size();
1824 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1825 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001826 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1827 if (stack == focusedStack || finishedTask == null) {
1828 finishedTask = t;
1829 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001830 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001831 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001832 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001833 }
1834
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001835 void finishVoiceTask(IVoiceInteractionSession session) {
1836 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1837 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1838 final int numStacks = stacks.size();
1839 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1840 final ActivityStack stack = stacks.get(stackNdx);
1841 stack.finishVoiceTask(session);
1842 }
1843 }
1844 }
1845
Andrii Kulianc27916642016-04-12 17:59:27 -07001846 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1847 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001848 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1849 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001850 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001851 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1852 // Caller wants the home activity moved with it. To accomplish this,
1853 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001854 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001855 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001856 if (task.stack == null) {
1857 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1858 + task + " to front. Stack is null");
1859 return;
1860 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001861
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001862 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001863 int stackId = options.getLaunchStackId();
1864 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001865 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001866 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001867 if (stackId == INVALID_STACK_ID) {
1868 stackId = task.getLaunchStackId();
1869 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001870 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001871 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1872 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1873 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001874 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1875 // still need moveTaskToFrontLocked() below for any transition settings.
1876 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001877 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1878 resizeStackLocked(stackId, bounds,
1879 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001880 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001881 } else {
1882 // WM resizeTask must be done after the task is moved to the correct stack,
1883 // because Task's setBounds() also updates dim layer's bounds, but that has
1884 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001885 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001886 false /* relayout */, false /* forced */);
1887 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001888 }
1889 }
1890
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001891 final ActivityRecord r = task.getTopActivity();
1892 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1893 r == null ? null : r.appTimeTracker, reason);
1894
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001895 if (DEBUG_STACK) Slog.d(TAG_STACK,
1896 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001897
Andrii Kulianc27916642016-04-12 17:59:27 -07001898 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1899 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001900 }
1901
Wale Ogunwale854809c2015-12-27 16:18:19 -08001902 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001903 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001904 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001905 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001906 return false;
1907 }
1908 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1909 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001910 }
1911
Craig Mautner967212c2013-04-13 21:10:58 -07001912 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001913 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001914 }
1915
1916 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001917 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1918 if (activityContainer != null) {
1919 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001920 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001921 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001922 return null;
1923 }
1924 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001925 }
1926
Craig Mautner967212c2013-04-13 21:10:58 -07001927 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001928 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001929 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1930 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001931 }
1932 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001933 }
1934
Craig Mautner4a1cb222013-12-04 16:14:06 -08001935 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001936 ActivityRecord homeActivity = getHomeActivity();
1937 if (homeActivity != null) {
1938 return homeActivity.appToken;
1939 }
1940 return null;
1941 }
1942
1943 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001944 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001945 }
1946
1947 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001948 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1949 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1950 final TaskRecord task = tasks.get(taskNdx);
1951 if (task.isHomeTask()) {
1952 final ArrayList<ActivityRecord> activities = task.mActivities;
1953 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1954 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001955 if (r.isHomeActivity()
1956 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001957 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001958 }
1959 }
1960 }
1961 }
1962 return null;
1963 }
1964
Chong Zhangb15758a2015-11-17 12:12:03 -08001965 /**
1966 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1967 * the docked stack itself, or if it's side-by-side to the docked stack.
1968 */
1969 boolean isStackDockedInEffect(int stackId) {
1970 return stackId == DOCKED_STACK_ID ||
1971 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1972 }
1973
Todd Kennedyca4d8422015-01-15 15:19:22 -08001974 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001975 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001976 ActivityContainer activityContainer =
1977 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001978 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001979 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1980 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001981 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001982 return activityContainer;
1983 }
1984
Craig Mautner34b73df2014-01-12 21:11:08 -08001985 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001986 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1987 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1988 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001989 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1990 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001991 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001992 }
1993 }
1994
Craig Mautner95da1082014-02-24 17:54:35 -08001995 void deleteActivityContainer(IActivityContainer container) {
1996 ActivityContainer activityContainer = (ActivityContainer)container;
1997 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001998 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1999 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002000 final int stackId = activityContainer.mStackId;
2001 mActivityContainers.remove(stackId);
2002 mWindowManager.removeStack(stackId);
2003 }
2004 }
2005
Jorim Jaggidc249c42015-12-15 14:57:31 -08002006 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002007 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002008 if (stackId == DOCKED_STACK_ID) {
2009 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002010 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002011 return;
2012 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002013 final ActivityStack stack = getStack(stackId);
2014 if (stack == null) {
2015 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2016 return;
2017 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002018
Jorim Jaggidc249c42015-12-15 14:57:31 -08002019 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002020 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2021 // stack size changing so things don't get out of sync.
2022 return;
2023 }
2024
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002025 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002026 mWindowManager.deferSurfaceLayout();
2027 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002028 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002029 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002030 stack.ensureVisibleActivitiesConfigurationLocked(
2031 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002032 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002033 } finally {
2034 mWindowManager.continueSurfaceLayout();
2035 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002036 }
2037 }
2038
Jorim Jaggi192086e2016-03-11 17:17:03 +01002039 void deferUpdateBounds(int stackId) {
2040 final ActivityStack stack = getStack(stackId);
2041 if (stack != null) {
2042 stack.deferUpdateBounds();
2043 }
2044 }
2045
2046 void continueUpdateBounds(int stackId) {
2047 final ActivityStack stack = getStack(stackId);
2048 if (stack != null) {
2049 stack.continueUpdateBounds();
2050 }
2051 }
2052
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002053 void notifyAppTransitionDone() {
2054 continueUpdateBounds(HOME_STACK_ID);
2055 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2056 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002057 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002058 mWindowManager.setTaskDockedResizing(taskId, false);
2059 }
2060 }
2061 mResizingTasksDuringAnimation.clear();
2062 }
2063
Jorim Jaggi192086e2016-03-11 17:17:03 +01002064 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002065 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002066 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002067
Jorim Jaggi192086e2016-03-11 17:17:03 +01002068 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2069 return;
2070 }
2071
Jorim Jaggidc249c42015-12-15 14:57:31 -08002072 mTmpBounds.clear();
2073 mTmpConfigs.clear();
2074 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002075 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002076 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2077 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002078 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002079 final TaskRecord task = tasks.get(i);
2080 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002081 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2082 // For freeform stack we don't adjust the size of the tasks to match that
2083 // of the stack, but we do try to make sure the tasks are still contained
2084 // with the bounds of the stack.
2085 tempRect2.set(task.mBounds);
2086 fitWithinBounds(tempRect2, bounds);
2087 task.updateOverrideConfiguration(tempRect2);
2088 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002089 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002090 }
2091 }
2092
2093 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2094 mTmpBounds.put(task.taskId, task.mBounds);
2095 if (tempTaskInsetBounds != null) {
2096 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2097 }
2098 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002099
2100 // We might trigger a configuration change. Save the current task bounds for freezing.
2101 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002102 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2103 mTmpBounds, mTmpInsetBounds);
2104 stack.setBounds(bounds);
2105 }
2106
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002107 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2108 final ActivityStack stack = getStack(fromStackId);
2109 if (stack == null) {
2110 return;
2111 }
2112
2113 mWindowManager.deferSurfaceLayout();
2114 try {
2115 if (fromStackId == DOCKED_STACK_ID) {
2116
2117 // We are moving all tasks from the docked stack to the fullscreen stack,
2118 // which is dismissing the docked stack, so resize all other stacks to
2119 // fullscreen here already so we don't end up with resize trashing.
2120 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2121 if (StackId.isResizeableByDockedStack(i)) {
2122 ActivityStack otherStack = getStack(i);
2123 if (otherStack != null) {
2124 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2125 true /* allowResizeInDockedMode */, DEFER_RESUME);
2126 }
2127 }
2128 }
2129
2130 // Also disable docked stack resizing since we have manually adjusted the
2131 // size of other stacks above and we don't want to trigger a docked stack
2132 // resize when we remove task from it below and it is detached from the
2133 // display because it no longer contains any tasks.
2134 mAllowDockedStackResize = false;
2135 }
2136 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2137 final int size = tasks.size();
2138 if (onTop) {
2139 for (int i = 0; i < size; i++) {
2140 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002141 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002142 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002143 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002144
2145 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2146 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002147 } else {
2148 for (int i = size - 1; i >= 0; i--) {
2149 positionTaskInStackLocked(tasks.get(i).taskId,
2150 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2151 }
2152 }
2153 } finally {
2154 mAllowDockedStackResize = true;
2155 mWindowManager.continueSurfaceLayout();
2156 }
2157 }
2158
Jorim Jaggidc249c42015-12-15 14:57:31 -08002159 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002160 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2161 boolean preserveWindows) {
2162 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2163 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2164 false /* deferResume */);
2165 }
2166
2167 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2168 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2169 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002170
2171 if (!mAllowDockedStackResize) {
2172 // Docked stack resize currently disabled.
2173 return;
2174 }
2175
Jorim Jaggidc249c42015-12-15 14:57:31 -08002176 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2177 if (stack == null) {
2178 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2179 return;
2180 }
2181
2182 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2183 mWindowManager.deferSurfaceLayout();
2184 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002185 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2186 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002187 ActivityRecord r = stack.topRunningActivityLocked();
2188 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2189 tempDockedTaskInsetBounds);
2190
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002191 // TODO: Checking for isAttached might not be needed as if the user passes in null
2192 // dockedBounds then they want the docked stack to be dismissed.
2193 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2194 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002195 // In this case we make all other static stacks fullscreen and move all
2196 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002197 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002198
2199 // stack shouldn't contain anymore activities, so nothing to resume.
2200 r = null;
2201 } else {
2202 // Docked stacks occupy a dedicated region on screen so the size of all other
2203 // static stacks need to be adjusted so they don't overlap with the docked stack.
2204 // We get the bounds to use from window manager which has been adjusted for any
2205 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002206 mWindowManager.getStackDockedModeBounds(
2207 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002208 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002209 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2210 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2211 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002212 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002213 }
2214 }
2215 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002216 if (!deferResume) {
2217 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2218 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002219 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002220 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002221 mWindowManager.continueSurfaceLayout();
2222 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2223 }
2224
2225 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2226 tempDockedTaskBounds != null
2227 || tempDockedTaskInsetBounds != null
2228 || tempOtherTaskBounds != null
2229 || tempOtherTaskInsetBounds != null);
2230 }
2231
Robert Carr0d00c2e2016-02-29 17:45:02 -08002232 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2233 final ActivityStack stack = getStack(PINNED_STACK_ID);
2234 if (stack == null) {
2235 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2236 return;
2237 }
2238 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2239 mWindowManager.deferSurfaceLayout();
2240 try {
2241 ActivityRecord r = stack.topRunningActivityLocked();
2242 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2243 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002244 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002245 } finally {
2246 mWindowManager.continueSurfaceLayout();
2247 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2248 }
2249 }
2250
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002251 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2252 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002253 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002254 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002255 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002256 }
2257
Chong Zhang87b21722015-09-21 15:39:51 -07002258 // If this is a forced resize, let it go through even if the bounds is not changing,
2259 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002260 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002261 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002262 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002263 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002264 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002265 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002266
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002267 if (!mWindowManager.isValidTaskId(task.taskId)) {
2268 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002269 // All we can do for now is update the bounds so it can be used when the task is
2270 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002271 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002272 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2273 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002274 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002275 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002276 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002277 }
2278
Chong Zhang6de2ae82015-09-30 18:25:21 -07002279 // Do not move the task to another stack here.
2280 // This method assumes that the task is already placed in the right stack.
2281 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002282
Chong Zhang6de2ae82015-09-30 18:25:21 -07002283 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002284
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002285 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002286 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002287 // way and the activity was kept the way it was. If it's false, it means the activity had
2288 // to be relaunched due to configuration change.
2289 boolean kept = true;
2290 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002291 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002292 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002293 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002294 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002295
2296 if (!deferResume) {
2297
2298 // All other activities must be made visible with their correct configuration.
2299 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2300 if (!kept) {
2301 resumeFocusedStackTopActivityLocked();
2302 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002303 }
2304 }
2305 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002306 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002307
2308 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002309 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002310 }
2311
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002312 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002313 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2314 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002315 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002316 }
2317
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002318 ActivityContainer activityContainer = new ActivityContainer(stackId);
2319 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002320 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002321 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002322 }
2323
2324 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002325 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002326 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2327 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002328 break;
2329 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002330 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002331 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002332 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002333 }
2334
Chong Zhang5dcb2752015-08-18 13:50:26 -07002335 /**
2336 * Restores a recent task to a stack
2337 * @param task The recent task to be restored.
2338 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002339 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002340 * @return true if the task has been restored successfully.
2341 */
2342 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2343 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002344 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002345 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2346 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2347 // Put it in the fullscreen stack.
2348 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002349 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002350
Wale Ogunwale706ed792015-08-02 10:29:44 -07002351 if (task.stack != null) {
2352 // Task has already been restored once. See if we need to do anything more
2353 if (task.stack.mStackId == stackId) {
2354 // Nothing else to do since it is already restored in the right stack.
2355 return true;
2356 }
2357 // Remove current stack association, so we can re-associate the task with the
2358 // right stack below.
Wale Ogunwale06579d62016-04-30 15:29:06 -07002359 task.stack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002360 }
2361
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002362 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002363 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002364
2365 if (stack == null) {
2366 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002367 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2368 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002369 return false;
2370 }
2371
Wale Ogunwale5f986092015-12-04 15:35:38 -08002372 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002373 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2374 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002375 final ArrayList<ActivityRecord> activities = task.mActivities;
2376 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002377 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002378 }
2379 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002380 }
2381
Wale Ogunwale040b4702015-08-06 18:10:50 -07002382 /**
2383 * Moves the specified task record to the input stack id.
2384 * WARNING: This method performs an unchecked/raw move of the task and
2385 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002386 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002387 * @param task Task to move.
2388 * @param stackId Id of stack to move task to.
2389 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002390 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002391 * @param reason Reason the task is been moved.
2392 * @return The stack the task was moved to.
2393 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002394 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002395 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002396
2397 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2398 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2399 + "support multi-window task=" + task + " to stackId=" + stackId);
2400 }
2401
Wale Ogunwale06579d62016-04-30 15:29:06 -07002402 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002403 final ActivityStack prevStack = task.stack;
2404 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002405 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002406 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2407 // Whenever we are moving the top activity from the front stack we want to make sure to move
2408 // the stack to the front.
2409 final boolean wasFront = isFrontStack(prevStack)
2410 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002411
Chong Zhangd545dce2016-02-29 18:09:17 -08002412 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002413 // We don't allow moving a unresizeable task to the docked stack since the docked
2414 // stack is used for split-screen mode and will cause things like the docked divider to
2415 // show up. We instead leave the task in its current stack or move it to the fullscreen
2416 // stack if it isn't currently in a stack.
2417 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002418 Slog.w(TAG, "Can not move unresizeable task=" + task
2419 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2420 }
2421
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002422 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2423 // if a docked stack is created below which will lead to the stack we are moving from and
2424 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002425 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002426 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002427 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002428 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002429 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002430
2431 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002432 // move focus to the new stack by moving the stack to the front.
2433 stack.moveToFrontAndResumeStateIfNeeded(
2434 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002435
Wale Ogunwale040b4702015-08-06 18:10:50 -07002436 return stack;
2437 }
2438
Chong Zhange4fbd322016-03-01 14:44:03 -08002439 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002440 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002441 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2442 false /* deferResume */);
2443 }
2444
2445 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2446 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002447 final TaskRecord task = anyTaskForIdLocked(taskId);
2448 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002449 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002450 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002451 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002452
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002453 if (task.stack != null && task.stack.mStackId == stackId) {
2454 // You are already in the right stack silly...
2455 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002456 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002457 }
2458
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002459 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2460 throw new IllegalArgumentException("moveTaskToStack:"
2461 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2462 }
2463
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002464 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002465 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002466 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002467 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002468 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002469 // We are about to relaunch the activity because its configuration changed due to
2470 // being maximized, i.e. size change. The activity will first remove the old window
2471 // and then add a new one. This call will tell window manager about this, so it can
2472 // preserve the old window until the new one is drawn. This prevents having a gap
2473 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002474 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002475 // Note here we always set the replacing window first, as the flags might be needed
2476 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002477 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002478 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002479
2480 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002481 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002482 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002483 try {
2484 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002485 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002486 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002487
Wale Ogunwale961f4852016-02-01 20:25:54 -08002488 if (!animate) {
2489 stack.mNoAnimActivities.add(topActivity);
2490 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002491
Wale Ogunwale961f4852016-02-01 20:25:54 -08002492 // We might trigger a configuration change. Save the current task bounds for freezing.
2493 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2494
2495 // Make sure the task has the appropriate bounds/size for the stack it is in.
2496 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002497 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2498 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002499 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2500 Rect bounds = task.getLaunchBounds();
2501 if (bounds == null) {
2502 stack.layoutTaskInStack(task, null);
2503 bounds = task.mBounds;
2504 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002505 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2506 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002507 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002508 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2509 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002510 }
2511 } finally {
2512 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002513 }
2514
2515 if (mightReplaceWindow) {
2516 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2517 // window), we need to clear the replace window settings. Otherwise, we schedule a
2518 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002519 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002520 }
2521
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002522 if (!deferResume) {
2523
2524 // The task might have already been running and its visibility needs to be synchronized with
2525 // the visibility of the stack / windows.
2526 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2527 resumeFocusedStackTopActivityLocked();
2528 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002529
Jorim Jaggid53f0922016-04-06 22:16:23 -07002530 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002531
2532 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002533 }
2534
Wale Ogunwale079a0042015-10-24 11:44:07 -07002535 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2536 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2537 if (stack == null) {
2538 throw new IllegalArgumentException(
2539 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2540 }
2541
2542 final ActivityRecord r = stack.topRunningActivityLocked();
2543 if (r == null) {
2544 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2545 + " in stack=" + stack);
2546 return false;
2547 }
2548
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002549 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002550 Slog.w(TAG,
2551 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002552 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002553 return false;
2554 }
2555
Wale Ogunwale480dca02016-02-06 13:58:29 -08002556 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002557 return true;
2558 }
2559
Wale Ogunwale480dca02016-02-06 13:58:29 -08002560 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2561 mWindowManager.deferSurfaceLayout();
2562 try {
2563 final TaskRecord task = r.task;
2564
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002565 if (r == task.stack.getVisibleBehindActivity()) {
2566 // An activity can't be pinned and visible behind at the same time. Go ahead and
2567 // release it from been visible behind before pinning.
2568 requestVisibleBehindLocked(r, false);
2569 }
2570
Wale Ogunwale480dca02016-02-06 13:58:29 -08002571 // Need to make sure the pinned stack exist so we can resize it below...
2572 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2573
2574 // Resize the pinned stack to match the current size of the task the activity we are
2575 // going to be moving is currently contained in. We do this to have the right starting
2576 // animation bounds for the pinned stack to the desired bounds the caller wants.
2577 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2578 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002579 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002580
2581 if (task.mActivities.size() == 1) {
2582 // There is only one activity in the task. So, we can just move the task over to
2583 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002584 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2585 // Move the home stack forward if the task we just moved to the pinned stack
2586 // was launched from home so home should be visible behind it.
2587 moveHomeStackToFront(reason);
2588 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002589 moveTaskToStackLocked(
2590 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2591 } else {
2592 stack.moveActivityToStack(r);
2593 }
2594 } finally {
2595 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002596 }
2597
Wale Ogunwale480dca02016-02-06 13:58:29 -08002598 // The task might have already been running and its visibility needs to be synchronized
2599 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002600 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002601 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002602
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002603 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002604 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002605 }
2606
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002607 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2608 final TaskRecord task = anyTaskForIdLocked(taskId);
2609 if (task == null) {
2610 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2611 return;
2612 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002613 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2614
2615 task.updateOverrideConfigurationForStack(stack);
2616
2617 mWindowManager.positionTaskInStack(
2618 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002619 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002620 // The task might have already been running and its visibility needs to be synchronized with
2621 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002622 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002623 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002624 }
2625
Craig Mautnerac6f8432013-07-17 13:24:59 -07002626 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002627 mTmpFindTaskResult.r = null;
2628 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002629 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002630 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2631 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002632 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2633 final ActivityStack stack = stacks.get(stackNdx);
2634 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002635 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002636 continue;
2637 }
2638 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002639 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2640 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002641 continue;
2642 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002643 stack.findTaskLocked(r, mTmpFindTaskResult);
2644 // It is possible to have task in multiple stacks with the same root affinity.
2645 // If the match we found was based on root affinity we keep on looking to see if
2646 // there is a better match in another stack. We eventually return the match based
2647 // on root affinity if we don't find a better match.
2648 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2649 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002650 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002651 }
2652 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002653 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2654 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002655 }
2656
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002657 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2658 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002659 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2660 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002661 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002662 final ActivityRecord ar = stacks.get(stackNdx)
2663 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002664 if (ar != null) {
2665 return ar;
2666 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002667 }
2668 }
2669 return null;
2670 }
2671
Craig Mautner8d341ef2013-03-26 09:03:27 -07002672 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002673 scheduleSleepTimeout();
2674 if (!mGoingToSleep.isHeld()) {
2675 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002676 if (mLaunchingActivity.isHeld()) {
2677 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2678 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002679 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002680 mLaunchingActivity.release();
2681 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002682 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002683 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002684 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002685 }
2686
2687 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002688 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002689
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002690 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002691 final long endTime = System.currentTimeMillis() + timeout;
2692 while (true) {
2693 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002694 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2695 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002696 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2697 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2698 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002699 }
2700 if (cantShutdown) {
2701 long timeRemaining = endTime - System.currentTimeMillis();
2702 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002703 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002704 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002705 } catch (InterruptedException e) {
2706 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002707 } else {
2708 Slog.w(TAG, "Activity manager shutdown timed out");
2709 timedout = true;
2710 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002711 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002712 } else {
2713 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002714 }
2715 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002716
2717 // Force checkReadyForSleep to complete.
2718 mSleepTimeout = true;
2719 checkReadyForSleepLocked();
2720
Craig Mautner8d341ef2013-03-26 09:03:27 -07002721 return timedout;
2722 }
2723
2724 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002725 removeSleepTimeouts();
2726 if (mGoingToSleep.isHeld()) {
2727 mGoingToSleep.release();
2728 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002729 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2730 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002731 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2732 final ActivityStack stack = stacks.get(stackNdx);
2733 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002734 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002735 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002736 }
Craig Mautner5314a402013-09-26 12:40:16 -07002737 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002738 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002739 mGoingToSleepActivities.clear();
2740 }
2741
2742 void activitySleptLocked(ActivityRecord r) {
2743 mGoingToSleepActivities.remove(r);
2744 checkReadyForSleepLocked();
2745 }
2746
2747 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002748 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002749 // Do not care.
2750 return;
2751 }
2752
2753 if (!mSleepTimeout) {
2754 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002755 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2756 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002757 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2758 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2759 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002760 }
2761
2762 if (mStoppingActivities.size() > 0) {
2763 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002764 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002765 + mStoppingActivities.size() + " activities");
2766 scheduleIdleLocked();
2767 dontSleep = true;
2768 }
2769
2770 if (mGoingToSleepActivities.size() > 0) {
2771 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002772 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002773 + mGoingToSleepActivities.size() + " activities");
2774 dontSleep = true;
2775 }
2776
2777 if (dontSleep) {
2778 return;
2779 }
2780 }
2781
Wei Wang65c7a152016-06-02 18:51:22 -07002782 // Send launch end powerhint before going sleep
2783 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2784
Craig Mautnere0a38842013-12-16 16:14:02 -08002785 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2786 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002787 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2788 stacks.get(stackNdx).goToSleep();
2789 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002790 }
2791
2792 removeSleepTimeouts();
2793
2794 if (mGoingToSleep.isHeld()) {
2795 mGoingToSleep.release();
2796 }
2797 if (mService.mShuttingDown) {
2798 mService.notifyAll();
2799 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002800 }
2801
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002802 boolean reportResumedActivityLocked(ActivityRecord r) {
2803 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002804 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002805 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002806 }
2807 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002808 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002809 mWindowManager.executeAppTransition();
2810 return true;
2811 }
2812 return false;
2813 }
2814
Craig Mautner8d341ef2013-03-26 09:03:27 -07002815 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002816 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2817 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002818 int stackNdx = stacks.size() - 1;
2819 while (stackNdx >= 0) {
2820 stacks.get(stackNdx).handleAppCrashLocked(app);
2821 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002822 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002823 }
2824 }
2825
Jose Lima4b6c6692014-08-12 17:41:12 -07002826 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002827 final ActivityStack stack = r.task.stack;
2828 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002829 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2830 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002831 return false;
2832 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002833
2834 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2835 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2836 + " visible=" + visible + " stackId=" + stack.mStackId
2837 + " can't contain visible behind activities");
2838 return false;
2839 }
2840
Jose Lima4b6c6692014-08-12 17:41:12 -07002841 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002842 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2843 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002844
2845 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002846 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002847 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002848 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002849 return true;
2850 }
2851
2852 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002853 if (visible && top.fullscreen) {
2854 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002855 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2856 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2857 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2858 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002859 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002860 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2861 // Only the activity set as currently visible behind should actively reset its
2862 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002863 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2864 "requestVisibleBehind: returning visible=" + visible
2865 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2866 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002867 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002868 }
2869
Jose Lima4b6c6692014-08-12 17:41:12 -07002870 stack.setVisibleBehindActivity(visible ? r : null);
2871 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002872 // If there is a translucent home activity, we need to force it stop being translucent,
2873 // because we can't depend on the application to necessarily perform that operation.
2874 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002875 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002876 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002877 mService.convertFromTranslucent(next.appToken);
2878 }
2879 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002880 if (top.app != null && top.app.thread != null) {
2881 // Notify the top app of the change.
2882 try {
2883 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2884 } catch (RemoteException e) {
2885 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002886 }
2887 return true;
2888 }
2889
Craig Mautnerbb742462014-07-07 15:28:55 -07002890 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2891 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002892 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002893 final ActivityStack stack = task.stack;
2894
2895 r.mLaunchTaskBehind = false;
2896 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002897 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002898 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002899 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002900
2901 // When launching tasks behind, update the last active time of the top task after the new
2902 // task has been shown briefly
2903 final ActivityRecord top = stack.topActivity();
2904 if (top != null) {
2905 top.task.touchActiveTime();
2906 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002907 }
2908
2909 void scheduleLaunchTaskBehindComplete(IBinder token) {
2910 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2911 }
2912
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002913 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2914 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002915 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002916 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2917 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002918 final int topStackNdx = stacks.size() - 1;
2919 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2920 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002921 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002922 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002923 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002924 }
2925
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002926 void invalidateTaskLayers() {
2927 mTaskLayersChanged = true;
2928 }
2929
2930 void rankTaskLayersIfNeeded() {
2931 if (!mTaskLayersChanged) {
2932 return;
2933 }
2934 mTaskLayersChanged = false;
2935 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2936 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2937 int baseLayer = 0;
2938 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2939 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2940 }
2941 }
2942 }
2943
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002944 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2945 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2946 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2947 final int topStackNdx = stacks.size() - 1;
2948 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2949 final ActivityStack stack = stacks.get(stackNdx);
2950 stack.clearOtherAppTimeTrackers(except);
2951 }
2952 }
2953 }
2954
Craig Mautner8d341ef2013-03-26 09:03:27 -07002955 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002956 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2957 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002958 final int numStacks = stacks.size();
2959 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2960 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002961 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002962 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002963 }
2964 }
2965
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002966 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2967 // Examine all activities currently running in the process.
2968 TaskRecord firstTask = null;
2969 // Tasks is non-null only if two or more tasks are found.
2970 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002971 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2972 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002973 ActivityRecord r = app.activities.get(i);
2974 // First, if we find an activity that is in the process of being destroyed,
2975 // then we just aren't going to do anything for now; we want things to settle
2976 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002977 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002978 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002979 return;
2980 }
2981 // Don't consider any activies that are currently not in a state where they
2982 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002983 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2984 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002985 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002986 continue;
2987 }
2988 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002989 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002990 + " from " + r);
2991 if (firstTask == null) {
2992 firstTask = r.task;
2993 } else if (firstTask != r.task) {
2994 if (tasks == null) {
2995 tasks = new ArraySet<>();
2996 tasks.add(firstTask);
2997 }
2998 tasks.add(r.task);
2999 }
3000 }
3001 }
3002 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003003 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003004 return;
3005 }
3006 // If we have activities in multiple tasks that are in a position to be destroyed,
3007 // let's iterate through the tasks and release the oldest one.
3008 final int numDisplays = mActivityDisplays.size();
3009 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3010 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3011 // Step through all stacks starting from behind, to hit the oldest things first.
3012 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3013 final ActivityStack stack = stacks.get(stackNdx);
3014 // Try to release activities in this stack; if we manage to, we are done.
3015 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3016 return;
3017 }
3018 }
3019 }
3020 }
3021
Amith Yamasani37a40c22015-06-17 13:25:42 -07003022 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003023 final int focusStackId = mFocusedStack.getStackId();
3024 // We dismiss the docked stack whenever we switch users.
3025 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3026
3027 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003028 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003029 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003030
Craig Mautner858d8a62013-04-23 17:08:34 -07003031 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003032 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3033 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003034 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003035 final ActivityStack stack = stacks.get(stackNdx);
3036 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003037 TaskRecord task = stack.topTask();
3038 if (task != null) {
3039 mWindowManager.moveTaskToTop(task.taskId);
3040 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003041 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003042 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003043
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003044 ActivityStack stack = getStack(restoreStackId);
3045 if (stack == null) {
3046 stack = mHomeStack;
3047 }
3048 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003049 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003050 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003051 } else {
3052 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003053 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003054 }
Craig Mautner93529a42013-10-04 15:03:13 -07003055 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003056 }
3057
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003058 /** Checks whether the userid is a profile of the current user. */
3059 boolean isCurrentProfileLocked(int userId) {
3060 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003061 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003062 }
3063
Chong Zhang45c25ce2015-08-10 22:18:26 -07003064 /** Checks whether the activity should be shown for current user. */
3065 boolean okToShowLocked(ActivityRecord r) {
Nicolas Prevot1219c922016-06-20 17:25:12 +01003066 return r != null && ((r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
3067 || (isCurrentProfileLocked(r.userId)
3068 && !mService.mUserController.isUserStoppingOrShuttingDownLocked(r.userId)));
Chong Zhang45c25ce2015-08-10 22:18:26 -07003069 }
3070
Craig Mautnerde4ef022013-04-07 19:01:33 -07003071 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003072 ArrayList<ActivityRecord> stops = null;
3073
3074 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003075 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3076 ActivityRecord s = mStoppingActivities.get(activityNdx);
3077 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003078 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003079 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3080 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003081 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003082 if (s.finishing) {
3083 // If this activity is finishing, it is sitting on top of
3084 // everyone else but we now know it is no longer needed...
3085 // so get rid of it. Otherwise, we need to go through the
3086 // normal flow and hide it once we determine that it is
3087 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003088 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003089 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003090 }
3091 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003092 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003093 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003094 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003095 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003096 }
3097 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003098 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003099 }
3100 }
3101
3102 return stops;
3103 }
3104
Craig Mautnercf910b02013-04-23 11:23:27 -07003105 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003106 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3107 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3108 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3109 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003110 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003111 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003112 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003113 if (r == null) Slog.e(TAG,
3114 "validateTop...: null top activity, stack=" + stack);
3115 else {
3116 final ActivityRecord pausing = stack.mPausingActivity;
3117 if (pausing != null && pausing == r) Slog.e(TAG,
3118 "validateTop...: top stack has pausing activity r=" + r
3119 + " state=" + state);
3120 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3121 "validateTop...: activity in front not resumed r=" + r
3122 + " state=" + state);
3123 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003124 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003125 final ActivityRecord resumed = stack.mResumedActivity;
3126 if (resumed != null && resumed == r) Slog.e(TAG,
3127 "validateTop...: back stack has resumed activity r=" + r
3128 + " state=" + state);
3129 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3130 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003131 }
3132 }
3133 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003134 }
3135
Craig Mautnere0570202015-05-13 13:06:11 -07003136 private String lockTaskModeToString() {
3137 switch (mLockTaskModeState) {
3138 case LOCK_TASK_MODE_LOCKED:
3139 return "LOCKED";
3140 case LOCK_TASK_MODE_PINNED:
3141 return "PINNED";
3142 case LOCK_TASK_MODE_NONE:
3143 return "NONE";
3144 default: return "unknown=" + mLockTaskModeState;
3145 }
3146 }
3147
Craig Mautner27084302013-03-25 08:05:25 -07003148 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003149 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003150 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003151 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003152 pw.print(prefix);
3153 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003154 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003155 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003156 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3157 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3158 if (packages.size() > 0) {
3159 pw.println(" mLockTaskPackages (userId:packages)=");
3160 for (int i = 0; i < packages.size(); ++i) {
3161 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3162 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3163 }
3164 }
3165 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003166 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003167
Craig Mautner20e72272013-04-01 13:45:53 -07003168 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003169 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003170 }
3171
Dianne Hackborn390517b2013-05-30 15:03:32 -07003172 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3173 boolean needSep, String prefix) {
3174 if (activity != null) {
3175 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3176 if (needSep) {
3177 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003178 }
3179 pw.print(prefix);
3180 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003181 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003182 }
3183 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003184 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003185 }
3186
Craig Mautner8d341ef2013-03-26 09:03:27 -07003187 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3188 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003189 boolean printed = false;
3190 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003191 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3192 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003193 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003194 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003195 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003196 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003197 final ActivityStack stack = stacks.get(stackNdx);
3198 StringBuilder stackHeader = new StringBuilder(128);
3199 stackHeader.append(" Stack #");
3200 stackHeader.append(stack.mStackId);
3201 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003202 stackHeader.append("\n");
3203 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3204 stackHeader.append("\n");
3205 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003206 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3207 needSep, stackHeader.toString());
3208 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3209 !dumpAll, false, dumpPackage, true,
3210 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003211
Craig Mautner4a1cb222013-12-04 16:14:06 -08003212 needSep = printed;
3213 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3214 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003215 if (pr) {
3216 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003217 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003218 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003219 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3220 " mResumedActivity: ");
3221 if (pr) {
3222 printed = true;
3223 needSep = false;
3224 }
3225 if (dumpAll) {
3226 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3227 " mLastPausedActivity: ");
3228 if (pr) {
3229 printed = true;
3230 needSep = true;
3231 }
3232 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3233 needSep, " mLastNoHistoryActivity: ");
3234 }
3235 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003236 }
3237 }
3238
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003239 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3240 false, dumpPackage, true, " Activities waiting to finish:", null);
3241 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3242 false, dumpPackage, true, " Activities waiting to stop:", null);
3243 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3244 false, dumpPackage, true, " Activities waiting for another to become visible:",
3245 null);
3246 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3247 false, dumpPackage, true, " Activities waiting to sleep:", null);
3248 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3249 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003250
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003251 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003252 }
3253
Dianne Hackborn390517b2013-05-30 15:03:32 -07003254 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003255 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003256 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003257 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003258 String innerPrefix = null;
3259 String[] args = null;
3260 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003261 for (int i=list.size()-1; i>=0; i--) {
3262 final ActivityRecord r = list.get(i);
3263 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3264 continue;
3265 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003266 if (innerPrefix == null) {
3267 innerPrefix = prefix + " ";
3268 args = new String[0];
3269 }
3270 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003271 final boolean full = !brief && (complete || !r.isInHistory());
3272 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003273 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003274 needNL = false;
3275 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003276 if (header1 != null) {
3277 pw.println(header1);
3278 header1 = null;
3279 }
3280 if (header2 != null) {
3281 pw.println(header2);
3282 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003283 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003284 if (lastTask != r.task) {
3285 lastTask = r.task;
3286 pw.print(prefix);
3287 pw.print(full ? "* " : " ");
3288 pw.println(lastTask);
3289 if (full) {
3290 lastTask.dump(pw, prefix + " ");
3291 } else if (complete) {
3292 // Complete + brief == give a summary. Isn't that obvious?!?
3293 if (lastTask.intent != null) {
3294 pw.print(prefix); pw.print(" ");
3295 pw.println(lastTask.intent.toInsecureStringWithClip());
3296 }
3297 }
3298 }
3299 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3300 pw.print(" #"); pw.print(i); pw.print(": ");
3301 pw.println(r);
3302 if (full) {
3303 r.dump(pw, innerPrefix);
3304 } else if (complete) {
3305 // Complete + brief == give a summary. Isn't that obvious?!?
3306 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3307 if (r.app != null) {
3308 pw.print(innerPrefix); pw.println(r.app);
3309 }
3310 }
3311 if (client && r.app != null && r.app.thread != null) {
3312 // flush anything that is already in the PrintWriter since the thread is going
3313 // to write to the file descriptor directly
3314 pw.flush();
3315 try {
3316 TransferPipe tp = new TransferPipe();
3317 try {
3318 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3319 r.appToken, innerPrefix, args);
3320 // Short timeout, since blocking here can
3321 // deadlock with the application.
3322 tp.go(fd, 2000);
3323 } finally {
3324 tp.kill();
3325 }
3326 } catch (IOException e) {
3327 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3328 } catch (RemoteException e) {
3329 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3330 }
3331 needNL = true;
3332 }
3333 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003334 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003335 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003336
Craig Mautnerf3333272013-04-22 10:55:53 -07003337 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003338 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3339 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003340 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3341 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003342 }
3343
3344 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003345 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003346 }
3347
3348 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003349 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3350 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003351 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3352 }
3353
Craig Mautner05d29032013-05-03 13:40:13 -07003354 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003355 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3356 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3357 }
Craig Mautner05d29032013-05-03 13:40:13 -07003358 }
3359
Craig Mautner0eea92c2013-05-16 13:35:39 -07003360 void removeSleepTimeouts() {
3361 mSleepTimeout = false;
3362 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3363 }
3364
3365 final void scheduleSleepTimeout() {
3366 removeSleepTimeouts();
3367 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3368 }
3369
Craig Mautner4a1cb222013-12-04 16:14:06 -08003370 @Override
3371 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003372 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003373 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3374 }
3375
3376 @Override
3377 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003378 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003379 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3380 }
3381
3382 @Override
3383 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003384 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003385 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3386 }
3387
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003388 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003389 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003390 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003391 newDisplay = mActivityDisplays.get(displayId) == null;
3392 if (newDisplay) {
3393 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003394 if (activityDisplay.mDisplay == null) {
3395 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3396 return;
3397 }
Craig Mautner4504de52013-12-20 09:06:56 -08003398 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003399 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003400 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003401 }
Craig Mautner4504de52013-12-20 09:06:56 -08003402 if (newDisplay) {
3403 mWindowManager.onDisplayAdded(displayId);
3404 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003405 }
3406
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003407 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003408 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003409 mService.mContext.getResources().getDimensionPixelSize(
3410 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003411 }
3412
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003413 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003414 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003415 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3416 if (activityDisplay != null) {
3417 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003418 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003419 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003420 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003421 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003422 }
3423 }
3424 mWindowManager.onDisplayRemoved(displayId);
3425 }
3426
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003427 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003428 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003429 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3430 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003431 // TODO: Update the bounds.
3432 }
3433 }
3434 mWindowManager.onDisplayChanged(displayId);
3435 }
3436
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003437 private StackInfo getStackInfoLocked(ActivityStack stack) {
Winson529c8e42016-05-17 11:08:40 -07003438 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003439 StackInfo info = new StackInfo();
3440 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3441 info.displayId = Display.DEFAULT_DISPLAY;
3442 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003443 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003444 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003445 info.position = display != null
3446 ? display.mStacks.indexOf(stack)
3447 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003448
3449 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3450 final int numTasks = tasks.size();
3451 int[] taskIds = new int[numTasks];
3452 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003453 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003454 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003455 for (int i = 0; i < numTasks; ++i) {
3456 final TaskRecord task = tasks.get(i);
3457 taskIds[i] = task.taskId;
3458 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3459 : task.realActivity != null ? task.realActivity.flattenToString()
3460 : task.getTopActivity() != null ? task.getTopActivity().packageName
3461 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003462 taskBounds[i] = new Rect();
3463 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003464 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003465 }
3466 info.taskIds = taskIds;
3467 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003468 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003469 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003470
3471 final ActivityRecord top = stack.topRunningActivityLocked();
3472 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003473 return info;
3474 }
3475
3476 StackInfo getStackInfoLocked(int stackId) {
3477 ActivityStack stack = getStack(stackId);
3478 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003479 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003480 }
3481 return null;
3482 }
3483
3484 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003485 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003486 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3487 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003488 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003489 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003490 }
3491 }
3492 return list;
3493 }
3494
Craig Mautner15df08a2015-04-01 12:17:18 -07003495 TaskRecord getLockedTaskLocked() {
3496 final int top = mLockTaskModeTasks.size() - 1;
3497 if (top >= 0) {
3498 return mLockTaskModeTasks.get(top);
3499 }
3500 return null;
3501 }
3502
3503 boolean isLockedTask(TaskRecord task) {
3504 return mLockTaskModeTasks.contains(task);
3505 }
3506
3507 boolean isLastLockedTask(TaskRecord task) {
3508 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3509 }
3510
3511 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003512 if (!mLockTaskModeTasks.remove(task)) {
3513 return;
3514 }
3515 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3516 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003517 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003518 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3519 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003520 final Message lockTaskMsg = Message.obtain();
3521 lockTaskMsg.arg1 = task.userId;
3522 lockTaskMsg.what = LOCK_TASK_END_MSG;
3523 mHandler.sendMessage(lockTaskMsg);
3524 }
3525 }
3526
Andrii Kulianc27916642016-04-12 17:59:27 -07003527 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3528 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3529 false /* forceNonResizable */);
3530 }
3531
Jorim Jaggid53f0922016-04-06 22:16:23 -07003532 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003533 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003534 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3535 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003536 return;
3537 }
3538
Jorim Jaggicd13d332016-04-27 15:40:20 -07003539 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003540 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003541 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003542 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3543
Andrii Kulianc27916642016-04-12 17:59:27 -07003544 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3545 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003546 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003547 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3548 && !topActivity.noDisplay) {
3549 String packageName = topActivity.appInfo.packageName;
Jorim Jaggi2adba072016-03-03 13:43:39 +01003550 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3551 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003552 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003553 }
3554
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003555 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003556 if (mLockTaskNotify != null) {
3557 mLockTaskNotify.showToast(mLockTaskModeState);
3558 }
Jason Monka8f569c2014-07-07 12:15:21 -04003559 }
3560
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003561 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3562 if (mLockTaskModeTasks.contains(task)) {
3563 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3564 }
3565 }
3566
Craig Mautner432f64e2015-05-20 14:59:57 -07003567 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3568 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003569 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003570 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003571 final TaskRecord lockedTask = getLockedTaskLocked();
3572 if (lockedTask != null) {
3573 removeLockedTaskLocked(lockedTask);
3574 if (!mLockTaskModeTasks.isEmpty()) {
3575 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003576 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3577 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003578 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003579 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003580 return;
3581 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003582 }
Craig Mautnere0570202015-05-13 13:06:11 -07003583 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3584 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003585 return;
3586 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003587
3588 // Should have already been checked, but do it again.
3589 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003590 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3591 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003592 return;
3593 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003594 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003595 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003596 return;
3597 }
3598
3599 if (mLockTaskModeTasks.isEmpty()) {
3600 // First locktask.
3601 final Message lockTaskMsg = Message.obtain();
3602 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3603 lockTaskMsg.arg1 = task.userId;
3604 lockTaskMsg.what = LOCK_TASK_START_MSG;
3605 lockTaskMsg.arg2 = lockTaskModeState;
3606 mHandler.sendMessage(lockTaskMsg);
3607 }
3608 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003609 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3610 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003611 mLockTaskModeTasks.remove(task);
3612 mLockTaskModeTasks.add(task);
3613
3614 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003615 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003616 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003617
3618 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003619 findTaskToMoveToFrontLocked(task, 0, null, reason,
3620 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003621 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003622 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3623 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3624 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003625 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003626 }
3627
3628 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003629 return isLockTaskModeViolation(task, false);
3630 }
3631
3632 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3633 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003634 return false;
3635 }
3636 final int lockTaskAuth = task.mLockTaskAuth;
3637 switch (lockTaskAuth) {
3638 case LOCK_TASK_AUTH_DONT_LOCK:
3639 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003640 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003641 case LOCK_TASK_AUTH_LAUNCHABLE:
3642 case LOCK_TASK_AUTH_WHITELISTED:
3643 return false;
3644 case LOCK_TASK_AUTH_PINNABLE:
3645 // Pinnable tasks can't be launched on top of locktask tasks.
3646 return !mLockTaskModeTasks.isEmpty();
3647 default:
3648 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3649 return true;
3650 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003651 }
3652
Craig Mautner15df08a2015-04-01 12:17:18 -07003653 void onLockTaskPackagesUpdatedLocked() {
3654 boolean didSomething = false;
3655 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3656 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003657 final boolean wasWhitelisted =
3658 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3659 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003660 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003661 final boolean isWhitelisted =
3662 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3663 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3664 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003665 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003666 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3667 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003668 removeLockedTaskLocked(lockedTask);
3669 lockedTask.performClearTaskLocked();
3670 didSomething = true;
3671 }
3672 }
3673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3674 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3675 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3676 final ActivityStack stack = stacks.get(stackNdx);
3677 stack.onLockTaskPackagesUpdatedLocked();
3678 }
3679 }
Craig Mautnere0570202015-05-13 13:06:11 -07003680 final ActivityRecord r = topRunningActivityLocked();
3681 final TaskRecord task = r != null ? r.task : null;
3682 if (mLockTaskModeTasks.isEmpty() && task != null
3683 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3684 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003685 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3686 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3687 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3688 false);
3689 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003690 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003691 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003692 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003693 }
3694 }
3695
Benjamin Franz43261142015-02-11 15:59:44 +00003696 int getLockTaskModeState() {
3697 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003698 }
3699
Jorim Jaggife89d122015-12-22 16:28:44 +01003700 void activityRelaunchedLocked(IBinder token) {
3701 mWindowManager.notifyAppRelaunchingFinished(token);
3702 }
3703
3704 void activityRelaunchingLocked(ActivityRecord r) {
3705 mWindowManager.notifyAppRelaunching(r.appToken);
3706 }
3707
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003708 void logStackState() {
3709 mActivityMetricsLogger.logWindowState();
3710 }
3711
Andrii Kulian933076d2016-03-29 17:04:42 -07003712 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003713 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3714 final ActivityRecord r = task.mActivities.get(i);
3715 if (r.app != null && r.app.thread != null) {
3716 mMultiWindowModeChangedActivities.add(r);
3717 }
3718 }
3719
3720 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3721 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3722 }
3723 }
3724
Andrii Kulian933076d2016-03-29 17:04:42 -07003725 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003726 final ActivityStack stack = task.stack;
3727 if (prevStack == null || prevStack == stack
3728 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3729 return;
3730 }
3731
3732 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3733 final ActivityRecord r = task.mActivities.get(i);
3734 if (r.app != null && r.app.thread != null) {
3735 mPipModeChangedActivities.add(r);
3736 }
3737 }
3738
3739 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3740 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3741 }
3742 }
3743
Tony Mak853304c2016-04-18 15:17:41 +01003744 void setDockedStackMinimized(boolean minimized) {
3745 mIsDockMinimized = minimized;
3746 if (minimized) {
3747 // Docked stack is not visible, no need to confirm credentials for its top activity.
3748 return;
3749 }
3750 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3751 if (dockedStack == null) {
3752 return;
3753 }
3754 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3755 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3756 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3757 }
3758 }
3759
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003760 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003761
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003762 public ActivityStackSupervisorHandler(Looper looper) {
3763 super(looper);
3764 }
3765
Craig Mautnerf3333272013-04-22 10:55:53 -07003766 void activityIdleInternal(ActivityRecord r) {
3767 synchronized (mService) {
3768 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3769 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003770 }
3771
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003772 @Override
3773 public void handleMessage(Message msg) {
3774 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003775 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3776 synchronized (mService) {
3777 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3778 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003779 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003780 }
3781 }
3782 } break;
3783 case REPORT_PIP_MODE_CHANGED_MSG: {
3784 synchronized (mService) {
3785 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3786 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003787 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003788 }
3789 }
3790 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003791 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003792 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3793 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003794 if (mService.mDidDexOpt) {
3795 mService.mDidDexOpt = false;
3796 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3797 nmsg.obj = msg.obj;
3798 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3799 return;
3800 }
3801 // We don't at this point know if the activity is fullscreen,
3802 // so we need to be conservative and assume it isn't.
3803 activityIdleInternal((ActivityRecord)msg.obj);
3804 } break;
3805 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003806 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003807 activityIdleInternal((ActivityRecord)msg.obj);
3808 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003809 case RESUME_TOP_ACTIVITY_MSG: {
3810 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003811 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003812 }
3813 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003814 case SLEEP_TIMEOUT_MSG: {
3815 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003816 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003817 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3818 mSleepTimeout = true;
3819 checkReadyForSleepLocked();
3820 }
3821 }
3822 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003823 case LAUNCH_TIMEOUT_MSG: {
3824 if (mService.mDidDexOpt) {
3825 mService.mDidDexOpt = false;
3826 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3827 return;
3828 }
3829 synchronized (mService) {
3830 if (mLaunchingActivity.isHeld()) {
3831 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3832 if (VALIDATE_WAKE_LOCK_CALLER
3833 && Binder.getCallingUid() != Process.myUid()) {
3834 throw new IllegalStateException("Calling must be system uid");
3835 }
3836 mLaunchingActivity.release();
3837 }
3838 }
3839 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003841 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003842 } break;
3843 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003844 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003845 } break;
3846 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003847 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003849 case CONTAINER_CALLBACK_VISIBILITY: {
3850 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003851 final IActivityContainerCallback callback = container.mCallback;
3852 if (callback != null) {
3853 try {
3854 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3855 } catch (RemoteException e) {
3856 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003857 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003858 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003859 case LOCK_TASK_START_MSG: {
3860 // When lock task starts, we disable the status bars.
3861 try {
Jason Monk62515be2014-05-21 16:06:19 -04003862 if (mLockTaskNotify == null) {
3863 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003864 }
Jason Monk62515be2014-05-21 16:06:19 -04003865 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003866 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003867 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003868 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003869 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003870 flags = StatusBarManager.DISABLE_MASK
3871 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003872 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003873 flags = StatusBarManager.DISABLE_MASK
3874 & (~StatusBarManager.DISABLE_BACK)
3875 & (~StatusBarManager.DISABLE_HOME)
3876 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003877 }
3878 getStatusBarService().disable(flags, mToken,
3879 mService.mContext.getPackageName());
3880 }
3881 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003882 if (getDevicePolicyManager() != null) {
3883 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003884 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003885 }
justinzhang5286d3f2014-05-12 17:06:01 -04003886 } catch (RemoteException ex) {
3887 throw new RuntimeException(ex);
3888 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003889 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003890 case LOCK_TASK_END_MSG: {
3891 // When lock task ends, we enable the status bars.
3892 try {
Jason Monk62515be2014-05-21 16:06:19 -04003893 if (getStatusBarService() != null) {
3894 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3895 mService.mContext.getPackageName());
3896 }
3897 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003898 if (getDevicePolicyManager() != null) {
3899 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3900 msg.arg1);
3901 }
Jason Monk62515be2014-05-21 16:06:19 -04003902 if (mLockTaskNotify == null) {
3903 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3904 }
3905 mLockTaskNotify.show(false);
3906 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003907 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003908 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003909 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003910 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003911 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003912 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003913 new LockPatternUtils(mService.mContext)
3914 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003915 }
3916 } catch (SettingNotFoundException e) {
3917 // No setting, don't lock.
3918 }
justinzhang5286d3f2014-05-12 17:06:01 -04003919 } catch (RemoteException ex) {
3920 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003921 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003922 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003923 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003924 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003925 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3926 if (mLockTaskNotify == null) {
3927 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3928 }
3929 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3930 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003931 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3932 final ActivityContainer container = (ActivityContainer) msg.obj;
3933 final IActivityContainerCallback callback = container.mCallback;
3934 if (callback != null) {
3935 try {
3936 callback.onAllActivitiesComplete(container.asBinder());
3937 } catch (RemoteException e) {
3938 }
3939 }
3940 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003941 case LAUNCH_TASK_BEHIND_COMPLETE: {
3942 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003943 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003944 if (r != null) {
3945 handleLaunchTaskBehindCompleteLocked(r);
3946 }
3947 }
3948 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003949
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003950 }
3951 }
3952 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003953
Ying Wangb081a592014-04-22 15:20:16 -07003954 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003955 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3956 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003957 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003958 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003959 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003960 ActivityRecord mParentActivity = null;
3961 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003962
Craig Mautnere3a00d72014-04-16 08:31:19 -07003963 boolean mVisible = true;
3964
Craig Mautner4a1cb222013-12-04 16:14:06 -08003965 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003966 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003967
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003968 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3969 final static int CONTAINER_STATE_NO_SURFACE = 1;
3970 final static int CONTAINER_STATE_FINISHING = 2;
3971 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3972
3973 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003974 synchronized (mService) {
3975 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003976 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003977 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003978 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003979 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003980 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003981
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003982 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003983 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003984 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003985 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003986 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003987 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003988 }
3989
3990 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003991 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003992 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003993 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3994 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003995 return;
3996 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003997 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003998 }
3999 }
4000
4001 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004002 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004003 synchronized (mService) {
4004 if (mActivityDisplay != null) {
4005 return mActivityDisplay.mDisplayId;
4006 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004007 }
4008 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 }
4010
Jeff Brownca9bc702014-02-11 14:32:56 -08004011 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004012 public int getStackId() {
4013 synchronized (mService) {
4014 return mStackId;
4015 }
4016 }
4017
4018 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004019 public boolean injectEvent(InputEvent event) {
4020 final long origId = Binder.clearCallingIdentity();
4021 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004022 synchronized (mService) {
4023 if (mActivityDisplay != null) {
4024 return mInputManagerInternal.injectInputEvent(event,
4025 mActivityDisplay.mDisplayId,
4026 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4027 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004028 }
4029 return false;
4030 } finally {
4031 Binder.restoreCallingIdentity(origId);
4032 }
4033 }
4034
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004035 @Override
4036 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004037 synchronized (mService) {
4038 if (mContainerState == CONTAINER_STATE_FINISHING) {
4039 return;
4040 }
4041 mContainerState = CONTAINER_STATE_FINISHING;
4042
Craig Mautnerd163e752014-06-13 17:18:47 -07004043 long origId = Binder.clearCallingIdentity();
4044 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004045 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004046 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004047 } finally {
4048 Binder.restoreCallingIdentity(origId);
4049 }
4050 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004051 }
4052
Craig Mautner60257702014-09-17 15:02:33 -07004053 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004054 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004055 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004056 if (mActivityDisplay != null) {
4057 mActivityDisplay.detachActivitiesLocked(mStack);
4058 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004059 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004060 }
4061 }
4062
4063 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004064 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004065 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004066 }
4067
4068 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004069 public final int startActivityIntentSender(IIntentSender intentSender)
4070 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004071 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4072
4073 if (!(intentSender instanceof PendingIntentRecord)) {
4074 throw new IllegalArgumentException("Bad PendingIntent object");
4075 }
4076
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004077 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004078 Binder.getCallingUid(), mCurrentUser, false,
4079 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004080
4081 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4082 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4083 pendingIntent.key.requestResolvedType);
4084
4085 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4086 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4087 }
4088
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004089 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004090 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004091 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004092 throw new SecurityException(
4093 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4094 }
4095 }
4096
Craig Mautnerdf88d732014-01-27 09:21:32 -08004097 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004098 public IBinder asBinder() {
4099 return this;
4100 }
4101
Craig Mautner4504de52013-12-20 09:06:56 -08004102 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004103 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004104 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004105 }
4106
Craig Mautner4a1cb222013-12-04 16:14:06 -08004107 ActivityStackSupervisor getOuter() {
4108 return ActivityStackSupervisor.this;
4109 }
4110
Craig Mautnerd163e752014-06-13 17:18:47 -07004111 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004112 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004113 }
4114
Craig Mautner6985bad2014-04-21 15:22:06 -07004115 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004116 void setVisible(boolean visible) {
4117 if (mVisible != visible) {
4118 mVisible = visible;
4119 if (mCallback != null) {
4120 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4121 0 /* unused */, this).sendToTarget();
4122 }
4123 }
4124 }
4125
Craig Mautner6985bad2014-04-21 15:22:06 -07004126 void setDrawn() {
4127 }
4128
Craig Mautner1b4bf852014-05-26 15:06:32 -07004129 // You can always start a new task on a regular ActivityStack.
4130 boolean isEligibleForNewTasks() {
4131 return true;
4132 }
4133
Craig Mautnerd163e752014-06-13 17:18:47 -07004134 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004135 detachLocked();
4136 deleteActivityContainer(this);
4137 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004138 }
4139
Craig Mautner34b73df2014-01-12 21:11:08 -08004140 @Override
4141 public String toString() {
4142 return mIdString + (mActivityDisplay == null ? "N" : "A");
4143 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004144 }
4145
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004146 private class VirtualActivityContainer extends ActivityContainer {
4147 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004148 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004149
4150 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4151 super(getNextStackId());
4152 mParentActivity = parent;
4153 mCallback = callback;
4154 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004155 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004156 }
4157
4158 @Override
4159 public void setSurface(Surface surface, int width, int height, int density) {
4160 super.setSurface(surface, width, height, density);
4161
4162 synchronized (mService) {
4163 final long origId = Binder.clearCallingIdentity();
4164 try {
4165 setSurfaceLocked(surface, width, height, density);
4166 } finally {
4167 Binder.restoreCallingIdentity(origId);
4168 }
4169 }
4170 }
4171
4172 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4173 if (mContainerState == CONTAINER_STATE_FINISHING) {
4174 return;
4175 }
4176 VirtualActivityDisplay virtualActivityDisplay =
4177 (VirtualActivityDisplay) mActivityDisplay;
4178 if (virtualActivityDisplay == null) {
4179 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004180 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004181 mActivityDisplay = virtualActivityDisplay;
4182 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004183 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004184 }
4185
4186 if (mSurface != null) {
4187 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004188 }
4189
Craig Mautner6985bad2014-04-21 15:22:06 -07004190 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004191 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004192 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004193 } else {
4194 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004195 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004196 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004197 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004198 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004199 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004200
Craig Mautnerd163e752014-06-13 17:18:47 -07004201 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004202
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004203 if (DEBUG_STACK) Slog.d(TAG_STACK,
4204 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004205 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004206
Craig Mautner6985bad2014-04-21 15:22:06 -07004207 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004208 boolean isAttachedLocked() {
4209 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004210 }
4211
4212 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004213 void setDrawn() {
4214 synchronized (mService) {
4215 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004216 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004217 }
4218 }
4219
Craig Mautner1b4bf852014-05-26 15:06:32 -07004220 // Never start a new task on an ActivityView if it isn't explicitly specified.
4221 @Override
4222 boolean isEligibleForNewTasks() {
4223 return false;
4224 }
4225
Craig Mautnerd163e752014-06-13 17:18:47 -07004226 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004227 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004228 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4229 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4230 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4231 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4232 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004233 }
4234 }
4235
Craig Mautner4a1cb222013-12-04 16:14:06 -08004236 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4237 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004238 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004239 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004240 int mDisplayId;
4241 Display mDisplay;
4242 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004243
Craig Mautner4a1cb222013-12-04 16:14:06 -08004244 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4245 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004246 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004247
Jose Lima4b6c6692014-08-12 17:41:12 -07004248 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004249
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004250 ActivityDisplay() {
4251 }
Craig Mautner4504de52013-12-20 09:06:56 -08004252
Craig Mautner1a70a162014-09-13 12:09:31 -07004253 // After instantiation, check that mDisplay is not null before using this. The alternative
4254 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004255 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004256 final Display display = mDisplayManager.getDisplay(displayId);
4257 if (display == null) {
4258 return;
4259 }
4260 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004261 }
4262
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004263 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004264 mDisplay = display;
4265 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004266 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004267 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004268
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004269 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004270 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004271 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4272 + " onTop=" + onTop);
4273 if (onTop) {
4274 mStacks.add(stack);
4275 } else {
4276 mStacks.add(0, stack);
4277 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004278 }
4279
Craig Mautnere0a38842013-12-16 16:14:02 -08004280 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004281 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004282 + " from displayId=" + mDisplayId);
4283 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004284 }
4285
Jose Lima4b6c6692014-08-12 17:41:12 -07004286 void setVisibleBehindActivity(ActivityRecord r) {
4287 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004288 }
4289
Jose Lima4b6c6692014-08-12 17:41:12 -07004290 boolean hasVisibleBehindActivity() {
4291 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004292 }
4293
Craig Mautner34b73df2014-01-12 21:11:08 -08004294 @Override
4295 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004296 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4297 }
4298 }
4299
4300 class VirtualActivityDisplay extends ActivityDisplay {
4301 VirtualDisplay mVirtualDisplay;
4302
Craig Mautner6985bad2014-04-21 15:22:06 -07004303 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004304 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004305 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4306 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4307 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4308 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004309
4310 init(mVirtualDisplay.getDisplay());
4311
4312 mWindowManager.handleDisplayAdded(mDisplayId);
4313 }
4314
4315 void setSurface(Surface surface) {
4316 if (mVirtualDisplay != null) {
4317 mVirtualDisplay.setSurface(surface);
4318 }
4319 }
4320
4321 @Override
4322 void detachActivitiesLocked(ActivityStack stack) {
4323 super.detachActivitiesLocked(stack);
4324 if (mVirtualDisplay != null) {
4325 mVirtualDisplay.release();
4326 mVirtualDisplay = null;
4327 }
4328 }
4329
4330 @Override
4331 public String toString() {
4332 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004333 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004334 }
Jose Lima58e66d62014-05-27 20:00:27 -07004335
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004336 /**
4337 * Adjust bounds to stay within stack bounds.
4338 *
4339 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4340 * that keep them unchanged, but be contained within the stack bounds.
4341 *
4342 * @param bounds Bounds to be adjusted.
4343 * @param stackBounds Bounds within which the other bounds should remain.
4344 */
4345 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4346 if (stackBounds == null || stackBounds.contains(bounds)) {
4347 return;
4348 }
4349
4350 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4351 final int maxRight = stackBounds.right
4352 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4353 int horizontalDiff = stackBounds.left - bounds.left;
4354 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4355 || (bounds.left + horizontalDiff >= maxRight)) {
4356 horizontalDiff = maxRight - bounds.left;
4357 }
4358 bounds.left += horizontalDiff;
4359 bounds.right += horizontalDiff;
4360 }
4361
4362 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4363 final int maxBottom = stackBounds.bottom
4364 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4365 int verticalDiff = stackBounds.top - bounds.top;
4366 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4367 || (bounds.top + verticalDiff >= maxBottom)) {
4368 verticalDiff = maxBottom - bounds.top;
4369 }
4370 bounds.top += verticalDiff;
4371 bounds.bottom += verticalDiff;
4372 }
4373 }
4374
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004375 ActivityStack findStackBehind(ActivityStack stack) {
4376 // TODO(multi-display): We are only looking for stacks on the default display.
4377 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4378 if (display == null) {
4379 return null;
4380 }
4381 final ArrayList<ActivityStack> stacks = display.mStacks;
4382 for (int i = stacks.size() - 1; i >= 0; i--) {
4383 if (stacks.get(i) == stack && i > 0) {
4384 return stacks.get(i - 1);
4385 }
4386 }
4387 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4388 + " in=" + stacks);
4389 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004390
Jorim Jaggic69bd222016-03-15 14:38:37 +01004391 /**
4392 * Puts a task into resizing mode during the next app transition.
4393 *
4394 * @param taskId the id of the task to put into resizing mode
4395 */
4396 private void setResizingDuringAnimation(int taskId) {
4397 mResizingTasksDuringAnimation.add(taskId);
4398 mWindowManager.setTaskDockedResizing(taskId, true);
4399 }
4400
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004401 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4402 final TaskRecord task;
4403 final int callingUid;
4404 final String callingPackage;
4405 final Intent intent;
4406 final int userId;
4407 final ActivityOptions activityOptions = (bOptions != null)
4408 ? new ActivityOptions(bOptions) : null;
4409 final int launchStackId = (activityOptions != null)
4410 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004411 if (launchStackId == HOME_STACK_ID) {
4412 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4413 + taskId + " can't be launch in the home stack.");
4414 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004415
4416 if (launchStackId == DOCKED_STACK_ID) {
4417 mWindowManager.setDockedStackCreateState(
4418 activityOptions.getDockCreateMode(), null /* initialBounds */);
4419
4420 // Defer updating the stack in which recents is until the app transition is done, to
4421 // not run into issues where we still need to draw the task in recents but the
4422 // docked stack is already created.
4423 deferUpdateBounds(HOME_STACK_ID);
4424 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4425 }
4426
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004427 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4428 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004429 continueUpdateBounds(HOME_STACK_ID);
4430 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004431 throw new IllegalArgumentException(
4432 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4433 }
4434
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004435 // Since we don't have an actual source record here, we assume that the currently focused
4436 // activity was the source.
4437 final ActivityStack focusedStack = getFocusedStack();
4438 final ActivityRecord sourceRecord =
4439 focusedStack != null ? focusedStack.topActivity() : null;
4440
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004441 if (launchStackId != INVALID_STACK_ID) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004442 if (task.stack.mStackId != launchStackId) {
4443 moveTaskToStackLocked(
4444 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4445 ANIMATE);
4446 }
4447 }
4448
4449 // If the user must confirm credentials (e.g. when first launching a work app and the
4450 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4451 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4452 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004453 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004454 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004455 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004456 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4457 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004458
4459 // If we are launching the task in the docked stack, put it into resizing mode so
4460 // the window renders full-screen with the background filling the void. Also only
4461 // call this at the end to make sure that tasks exists on the window manager side.
4462 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004463 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004464 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004465
4466 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4467 ActivityManager.START_TASK_TO_FRONT,
4468 sourceRecord != null ? sourceRecord.task.stack.mStackId : INVALID_STACK_ID,
4469 sourceRecord, task.stack);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004470 return ActivityManager.START_TASK_TO_FRONT;
4471 }
4472 callingUid = task.mCallingUid;
4473 callingPackage = task.mCallingPackage;
4474 intent = task.intent;
4475 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4476 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004477 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4478 null, null, 0, 0, bOptions, userId, null, task);
4479 if (launchStackId == DOCKED_STACK_ID) {
4480 setResizingDuringAnimation(task.taskId);
4481 }
4482 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004483 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004484
4485 /**
4486 * @return a list of activities which are the top ones in each visible stack. The first
4487 * entry will be the focused activity.
4488 */
4489 public List<IBinder> getTopVisibleActivities() {
4490 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4491 if (display == null) {
4492 return Collections.EMPTY_LIST;
4493 }
4494 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4495 final ArrayList<ActivityStack> stacks = display.mStacks;
4496 for (int i = stacks.size() - 1; i >= 0; i--) {
4497 ActivityStack stack = stacks.get(i);
4498 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4499 ActivityRecord top = stack.topActivity();
4500 if (top != null) {
4501 if (stack == mFocusedStack) {
4502 topActivityTokens.add(0, top.appToken);
4503 } else {
4504 topActivityTokens.add(top.appToken);
4505 }
4506 }
4507 }
4508 }
4509 return topActivityTokens;
4510 }
Craig Mautner27084302013-03-25 08:05:25 -07004511}