blob: e91bb5c9866ab65d05339a0527660d8804178567 [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;
Craig Mautner2420ead2013-04-01 17:13:20 -070020import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070021import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080022import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070023import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080024import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070025import android.app.ActivityOptions;
26import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070027import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080028import android.app.IActivityContainer;
29import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070030import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080031import android.app.IActivityManager.WaitResult;
Jeff Hao1b012d32014-08-20 10:35:34 -070032import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070033import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040034import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040035import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070036import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070037import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070038import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070039import android.content.Intent;
40import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070042import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.content.pm.PackageManager;
44import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010045import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080047import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080048import android.hardware.display.DisplayManager;
49import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080050import android.hardware.display.DisplayManagerGlobal;
51import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080052import android.hardware.input.InputManager;
53import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010055import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070056import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070057import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070060import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070062import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070063import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070064import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040065import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070066import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070067import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070068import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070069import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +010070import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070071import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070072import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040073import android.provider.Settings;
74import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070075import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070076import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070077import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070078import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070079import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080080import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -080081import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080082import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080083import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080084import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080085import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -080086
Dianne Hackborn85d558c2014-11-04 10:31:54 -080087import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070088import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040089import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -070090import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -040091import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -080092import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -070093import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070094import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070095
Craig Mautner8d341ef2013-03-26 09:03:27 -070096import java.io.FileDescriptor;
97import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070098import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -070099import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700100import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700101import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700102import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800103import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700104import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700105
Jorim Jaggife89d122015-12-22 16:28:44 +0100106import static android.Manifest.permission.START_ANY_ACTIVITY;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100107import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100108import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
109import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
110import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
111import static android.app.ActivityManager.RESIZE_MODE_FORCED;
112import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
113import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
114import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
115import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
116import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
117import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
118import static android.app.ActivityManager.StackId.HOME_STACK_ID;
119import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
120import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
121import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
122import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
123import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
124import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggi51605272016-02-24 18:06:49 -0500125import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100126import static android.content.pm.PackageManager.PERMISSION_GRANTED;
127import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
140import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
151import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
152import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
153import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800154import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100155import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggid53f0922016-04-06 22:16:23 -0700156import static com.android.server.am.ActivityManagerService.NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100157import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100158import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
159import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
160import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
161import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
162import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
163import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
164import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
165import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
166import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
167import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
168import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
169import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
170import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
171import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
172import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
173import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100174import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100175
Craig Mautner4a1cb222013-12-04 16:14:06 -0800176public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800177 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700178 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700179 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700180 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700181 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700182 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700184 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700186 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800187 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800189
Craig Mautnerf3333272013-04-22 10:55:53 -0700190 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700192
Craig Mautner0eea92c2013-05-16 13:35:39 -0700193 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700195
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700196 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700198
Craig Mautner05d29032013-05-03 13:40:13 -0700199 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
200 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
201 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700202 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700203 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800204 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
205 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
206 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700207 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400208 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
209 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700210 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700211 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700212 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800213 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
214 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800215
Wale Ogunwale040b4702015-08-06 18:10:50 -0700216 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700217
Jason Monk62515be2014-05-21 16:06:19 -0400218 private static final String LOCK_TASK_TAG = "Lock-to-App";
219
Wale Ogunwale040b4702015-08-06 18:10:50 -0700220 // Used to indicate if an object (e.g. stack) that we are trying to get
221 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800222 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700223
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700224 // Used to indicate that windows of activities should be preserved during the resize.
225 static final boolean PRESERVE_WINDOWS = true;
226
Wale Ogunwale040b4702015-08-06 18:10:50 -0700227 // Used to indicate if an object (e.g. task) should be moved/created
228 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700229 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700230
231 // Used to indicate that an objects (e.g. task) removal from its container
232 // (e.g. stack) is due to it moving to another container.
233 static final boolean MOVING = true;
234
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700235 // Force the focus to change to the stack we are moving a task to..
236 static final boolean FORCE_FOCUS = true;
237
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700238 // Restore task from the saved recents if it can't be found in any live stack.
239 static final boolean RESTORE_FROM_RECENTS = true;
240
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700241 // Don't execute any calls to resume.
242 static final boolean DEFER_RESUME = true;
243
Svetoslav7008b512015-06-24 18:47:07 -0700244 // Activity actions an app cannot start if it uses a permission which is not granted.
245 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
246 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700247
Svetoslav7008b512015-06-24 18:47:07 -0700248 static {
249 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
250 Manifest.permission.CAMERA);
251 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
252 Manifest.permission.CAMERA);
253 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
254 Manifest.permission.CALL_PHONE);
255 }
256
Svet Ganov99b60432015-06-27 13:15:22 -0700257 /** Action restriction: launching the activity is not restricted. */
258 private static final int ACTIVITY_RESTRICTION_NONE = 0;
259 /** Action restriction: launching the activity is restricted by a permission. */
260 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
261 /** Action restriction: launching the activity is restricted by an app op. */
262 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700263
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700264 // The height/width divide used when fitting a task within a bounds with method
265 // {@link #fitWithinBounds}.
266 // We always want the task to to be visible in the bounds without affecting its size when
267 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
268 // the input bounds right or bottom side minus the width or height divided by this value.
269 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
270
justinzhang5286d3f2014-05-12 17:06:01 -0400271 /** Status Bar Service **/
272 private IBinder mToken = new Binder();
273 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400274 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400275
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700276 // For debugging to make sure the caller when acquiring/releasing our
277 // wake lock is the system process.
278 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800279 /** The number of distinct task ids that can be assigned to the tasks of a single user */
280 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700281
Craig Mautner27084302013-03-25 08:05:25 -0700282 final ActivityManagerService mService;
283
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800284 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800285
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700286 final ActivityStackSupervisorHandler mHandler;
287
288 /** Short cut */
289 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800290 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700291
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700292 /** Counter for next free stack ID to use for dynamic activity stacks. */
293 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700294
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800295 /**
296 * Maps the task identifier that activities are currently being started in to the userId of the
297 * task. Each time a new task is created, the entry for the userId of the task is incremented
298 */
299 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700300
Craig Mautner2420ead2013-04-01 17:13:20 -0700301 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800302 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700303
Craig Mautnere0a38842013-12-16 16:14:02 -0800304 /** The stack containing the launcher app. Assumed to always be attached to
305 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800306 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700307
Craig Mautnere0a38842013-12-16 16:14:02 -0800308 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800309 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700310
Craig Mautner4a1cb222013-12-04 16:14:06 -0800311 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
312 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800313 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800314 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700315
316 /** List of activities that are waiting for a new activity to become visible before completing
317 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800318 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700319
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700320 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800321 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322
323 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800324 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700325
Craig Mautnerde4ef022013-04-07 19:01:33 -0700326 /** List of activities that are ready to be stopped, but waiting for the next activity to
327 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800328 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700329
Craig Mautnerf3333272013-04-22 10:55:53 -0700330 /** List of activities that are ready to be finished, but waiting for the previous activity to
331 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800332 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700333
Craig Mautner0eea92c2013-05-16 13:35:39 -0700334 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800335 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700336
Wale Ogunwale22e25262016-02-01 10:32:02 -0800337 /** List of activities whose multi-window mode changed that we need to report to the
338 * application */
339 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
340
341 /** List of activities whose picture-in-picture mode changed that we need to report to the
342 * application */
343 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
344
Craig Mautnerf3333272013-04-22 10:55:53 -0700345 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700346 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700347
Craig Mautnerde4ef022013-04-07 19:01:33 -0700348 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
349 * is being brought in front of us. */
350 boolean mUserLeaving = false;
351
Craig Mautner0eea92c2013-05-16 13:35:39 -0700352 /** Set when we have taken too long waiting to go to sleep. */
353 boolean mSleepTimeout = false;
354
355 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700356 * We don't want to allow the device to go to sleep while in the process
357 * of launching an activity. This is primarily to allow alarm intent
358 * receivers to launch an activity and get that to run before the device
359 * goes back to sleep.
360 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700361 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700362
363 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700364 * Set when the system is going to sleep, until we have
365 * successfully paused the current activity and released our wake lock.
366 * At that point the system is allowed to actually sleep.
367 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700368 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700369
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700370 /** Stack id of the front stack when user switched, indexed by userId. */
371 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700372
Craig Mautner4504de52013-12-20 09:06:56 -0800373 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800374 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700375 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800376
377 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700378 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379
Jeff Brownca9bc702014-02-11 14:32:56 -0800380 InputManagerInternal mInputManagerInternal;
381
Craig Mautner15df08a2015-04-01 12:17:18 -0700382 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
383 * may be finished until there is only one entry left. If this is empty the system is not
384 * in lockTask mode. */
385 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000386 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700387 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
388 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000389 */
390 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400391 /**
392 * Notifies the user when entering/exiting lock-task.
393 */
394 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800395
Wale Ogunwaled046a012015-12-24 13:05:59 -0800396 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800397 boolean inResumeTopActivity;
398
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700399 // 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 -0700400 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700401 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700402
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700403 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
404 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800405 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700406
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700407 // The default minimal size that will be used if the activity doesn't specify its minimal size.
408 // It will be calculated when the default display gets added.
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800409 int mDefaultMinimalSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700410
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700411 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
412 private boolean mTaskLayersChanged = true;
413
Jorim Jaggi275561a2016-02-23 10:11:02 -0500414 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800415
Jorim Jaggidc249c42015-12-15 14:57:31 -0800416 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
417
Wale Ogunwale39381972015-12-17 17:15:29 -0800418 static class FindTaskResult {
419 ActivityRecord r;
420 boolean matchedByRootAffinity;
421 }
422 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
423
Craig Mautneree36c772014-07-16 14:56:05 -0700424 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100425 * Used to keep track whether app visibilities got changed since the last pause. Useful to
426 * determine whether to invoke the task stack change listener after pausing.
427 */
428 boolean mAppVisibilitiesChangedSinceLastPause;
429
430 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100431 * Set of tasks that are in resizing mode during an app transition to fill the "void".
432 */
433 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
434
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700435
436 /**
437 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
438 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
439 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
440 * like the docked stack going empty.
441 */
442 private boolean mAllowDockedStackResize = true;
443
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100444 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700445 * Description of a request to start a new activity, which has been held
446 * due to app switches being disabled.
447 */
448 static class PendingActivityLaunch {
449 final ActivityRecord r;
450 final ActivityRecord sourceRecord;
451 final int startFlags;
452 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700453 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700454
455 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700456 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700457 r = _r;
458 sourceRecord = _sourceRecord;
459 startFlags = _startFlags;
460 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700461 callerApp = _callerApp;
462 }
463
464 void sendErrorResult(String message) {
465 try {
466 if (callerApp.thread != null) {
467 callerApp.thread.scheduleCrash(message);
468 }
469 } catch (RemoteException e) {
470 Slog.e(TAG, "Exception scheduling crash of failed "
471 + "activity launcher sourceRecord=" + sourceRecord, e);
472 }
Craig Mautneree36c772014-07-16 14:56:05 -0700473 }
474 }
475
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800476 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700477 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700478 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800479 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800480 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700481 }
482
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800483 void setRecentTasks(RecentTasks recentTasks) {
484 mRecentTasks = recentTasks;
485 }
486
Jeff Brown2c43c332014-06-12 22:38:59 -0700487 /**
488 * At the time when the constructor runs, the power manager has not yet been
489 * initialized. So we initialize our wakelocks afterwards.
490 */
491 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800492 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700493 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700494 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700495 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700496 }
497
justinzhang5286d3f2014-05-12 17:06:01 -0400498 // This function returns a IStatusBarService. The value is from ServiceManager.
499 // getService and is cached.
500 private IStatusBarService getStatusBarService() {
501 synchronized (mService) {
502 if (mStatusBarService == null) {
503 mStatusBarService = IStatusBarService.Stub.asInterface(
504 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
505 if (mStatusBarService == null) {
506 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
507 }
508 }
509 return mStatusBarService;
510 }
511 }
512
Jason Monk35c62a42014-06-17 10:24:47 -0400513 private IDevicePolicyManager getDevicePolicyManager() {
514 synchronized (mService) {
515 if (mDevicePolicyManager == null) {
516 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
517 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
518 if (mDevicePolicyManager == null) {
519 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
520 }
521 }
522 return mDevicePolicyManager;
523 }
524 }
525
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700526 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800527 synchronized (mService) {
528 mWindowManager = wm;
529
530 mDisplayManager =
531 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
532 mDisplayManager.registerDisplayListener(this, null);
533
534 Display[] displays = mDisplayManager.getDisplays();
535 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
536 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800537 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700538 if (activityDisplay.mDisplay == null) {
539 throw new IllegalStateException("Default Display does not exist");
540 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800541 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700542 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800543 }
544
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800545 mHomeStack = mFocusedStack = mLastFocusedStack =
546 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800547
548 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800549 }
Craig Mautner27084302013-03-25 08:05:25 -0700550 }
551
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200552 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700553 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200554 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700555 }
556
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700557 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800558 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700559 }
560
Craig Mautnerde4ef022013-04-07 19:01:33 -0700561 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800562 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700563 }
564
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700565 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700566 if (stack == null) {
567 return false;
568 }
569
Craig Mautnerdf88d732014-01-27 09:21:32 -0800570 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
571 if (parent != null) {
572 stack = parent.task.stack;
573 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800574 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700575 }
576
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700577 /** The top most stack. */
578 boolean isFrontStack(ActivityStack stack) {
579 if (stack == null) {
580 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800581 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700582
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700583 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
584 if (parent != null) {
585 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800586 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700587 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
588 }
589
Wale Ogunwaled046a012015-12-24 13:05:59 -0800590 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800591 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
592 if (!focusCandidate.isFocusable()) {
593 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
594 focusCandidate = focusCandidate.getNextFocusableStackLocked();
595 }
596
597 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800598 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800599 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800600
Wale Ogunwaled046a012015-12-24 13:05:59 -0800601 EventLogTags.writeAmFocusedStack(
602 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
603 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
604 }
605
606 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800607 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800608 // The focus activity should always be the top activity in the focused stack.
609 // There will be chaos and anarchy if it isn't...
610 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
611 }
Craig Mautnerde313752015-01-22 14:28:03 -0800612
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800613 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800614 if (r != null && r.idle) {
615 checkFinishBootingLocked();
616 }
617 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700618 }
619
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700620 void moveHomeStackToFront(String reason) {
621 mHomeStack.moveToFront(reason);
622 }
623
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700624 /** Returns true if the focus activity was adjusted to the home stack top activity. */
625 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700626 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
627 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700628 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700629 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700630
Craig Mautner84984fa2014-06-19 11:19:20 -0700631 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700632
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700633 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700634 if (top == null) {
635 return false;
636 }
637 mService.setFocusedActivityLocked(top, reason);
638 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700639 }
640
Craig Mautner299f9602015-01-26 09:47:33 -0800641 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700642 if (!mService.mBooting && !mService.mBooted) {
643 // Not ready yet!
644 return false;
645 }
646
Craig Mautner84984fa2014-06-19 11:19:20 -0700647 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
648 mWindowManager.showRecentApps();
649 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700650 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700651
Craig Mautner84984fa2014-06-19 11:19:20 -0700652 if (prev != null) {
653 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
654 }
655
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700656 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
657 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800658 final String myReason = reason + " resumeHomeStackTask";
659
Mark Lua56ea122015-10-08 13:31:01 +0800660 // Only resume home activity if isn't finishing.
661 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800662 mService.setFocusedActivityLocked(r, myReason);
663 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700664 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800665 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700666 }
667
Craig Mautner8d341ef2013-03-26 09:03:27 -0700668 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700669 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700670 }
671
672 /**
673 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
674 * @param id Id of the task we would like returned.
675 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
676 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700677 * @param stackId The stack to restore the task to (default launch stack will be used if
678 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700679 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700680 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800681 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800682 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800683 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800684 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
685 ActivityStack stack = stacks.get(stackNdx);
686 TaskRecord task = stack.taskForIdLocked(id);
687 if (task != null) {
688 return task;
689 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700690 }
691 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800692
693 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700694 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800695 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800696 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700697 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800698 return null;
699 }
700
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700701 if (!restoreFromRecents) {
702 return task;
703 }
704
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700705 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700706 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
707 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800708 return null;
709 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700710 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800711 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700712 }
713
Craig Mautner6170f732013-04-02 13:05:23 -0700714 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800715 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800716 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800717 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800718 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
719 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
720 if (r != null) {
721 return r;
722 }
Craig Mautner6170f732013-04-02 13:05:23 -0700723 }
724 }
725 return null;
726 }
727
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000728 boolean isFocusedUserLockedProfile() {
729 final int userId = mFocusedStack.topRunningActivityLocked().userId;
730 return userId != UserHandle.myUserId()
731 && mService.mUserController.shouldConfirmCredentials(userId);
732 }
733
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800734 void setNextTaskIdForUserLocked(int taskId, int userId) {
735 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
736 if (taskId > currentTaskId) {
737 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700738 }
739 }
740
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800741 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800742 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
743 // for a userId u, a taskId can only be in the range
744 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
745 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
746 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800747 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
748 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
749 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800750 candidateTaskId++;
751 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
752 // Wrap around as there will be smaller task ids that are available now.
753 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700754 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800755 if (candidateTaskId == currentTaskId) {
756 // Something wrong!
757 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
758 throw new IllegalStateException("Cannot get an available task id."
759 + " Reached limit of " + MAX_TASK_IDS_PER_USER
760 + " running tasks per user.");
761 }
762 }
763 mCurTaskIdForUser.put(userId, candidateTaskId);
764 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700765 }
766
Craig Mautnerde4ef022013-04-07 19:01:33 -0700767 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800768 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700769 if (stack == null) {
770 return null;
771 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700772 ActivityRecord resumedActivity = stack.mResumedActivity;
773 if (resumedActivity == null || resumedActivity.app == null) {
774 resumedActivity = stack.mPausingActivity;
775 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700776 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700777 }
778 }
779 return resumedActivity;
780 }
781
Dianne Hackbornff072722014-09-24 10:56:28 -0700782 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700783 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800784 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800785 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
786 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800787 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
788 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700789 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 continue;
791 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700792 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800793 if (hr != null) {
794 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
795 && processName.equals(hr.processName)) {
796 try {
George Mount2c92c972014-03-20 09:38:23 -0700797 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800798 didSomething = true;
799 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700800 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800801 Slog.w(TAG, "Exception in new application when starting activity "
802 + hr.intent.getComponent().flattenToShortString(), e);
803 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700804 }
Craig Mautner20e72272013-04-01 13:45:53 -0700805 }
Craig Mautner20e72272013-04-01 13:45:53 -0700806 }
807 }
808 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700809 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700810 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700811 }
Craig Mautner20e72272013-04-01 13:45:53 -0700812 return didSomething;
813 }
814
815 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800816 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
817 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800818 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
819 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700820 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800821 continue;
822 }
823 final ActivityRecord resumedActivity = stack.mResumedActivity;
824 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700825 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800826 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800827 return false;
828 }
Craig Mautner20e72272013-04-01 13:45:53 -0700829 }
830 }
831 return true;
832 }
833
Craig Mautnerde4ef022013-04-07 19:01:33 -0700834 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800835 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
836 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
838 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700839 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700841 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800842 return false;
843 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700844 }
845 }
846 }
847 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700848 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800849 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
850 mLastFocusedStack + " to=" + mFocusedStack);
851 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700852 return true;
853 }
854
855 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800856 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800857 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
858 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800859 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
860 final ActivityStack stack = stacks.get(stackNdx);
861 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800862 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700863 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800864 return false;
865 }
866 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800867 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700868 }
869 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800870 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700871 }
872
Craig Mautner2acc3892013-09-23 10:28:14 -0700873 /**
874 * Pause all activities in either all of the stacks or just the back stacks.
875 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700876 * @return true if any activity was paused as a result of this call.
877 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700878 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700879 boolean someActivityPaused = false;
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) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700885 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700887 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
888 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700890 }
891 }
892 return someActivityPaused;
893 }
894
Craig Mautnerde4ef022013-04-07 19:01:33 -0700895 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700896 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800897 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
898 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800899 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
900 final ActivityStack stack = stacks.get(stackNdx);
901 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700902 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800903 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700904 Slog.d(TAG_STATES,
905 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800906 pausing = false;
907 } else {
908 return false;
909 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700910 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700911 }
912 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700913 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700914 }
915
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700916 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
917 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500918 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800919 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
920 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
921 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
922 final ActivityStack stack = stacks.get(stackNdx);
923 if (stack.mResumedActivity != null &&
924 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700925 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800926 }
927 }
928 }
929 }
930
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800931 void cancelInitializingActivities() {
932 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
933 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
934 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
935 stacks.get(stackNdx).cancelInitializingActivities();
936 }
937 }
938 }
939
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700940 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700941 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700942 }
943
944 void sendWaitingVisibleReportLocked(ActivityRecord r) {
945 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700946 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700947 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700948 if (w.who == null) {
949 changed = true;
950 w.timeout = false;
951 if (r != null) {
952 w.who = new ComponentName(r.info.packageName, r.info.name);
953 }
954 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
955 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700956 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700957 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700958 if (changed) {
959 mService.notifyAll();
960 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700961 }
962
963 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
964 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700965 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700966 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700967 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700968 if (w.who == null) {
969 changed = true;
970 w.timeout = timeout;
971 if (r != null) {
972 w.who = new ComponentName(r.info.packageName, r.info.name);
973 }
974 w.thisTime = thisTime;
975 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700976 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700977 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700978 if (changed) {
979 mService.notifyAll();
980 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700981 }
982
Craig Mautner29219d92013-04-16 20:19:12 -0700983 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800984 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700985 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700986 if (r != null) {
987 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700988 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700989
Craig Mautner4a1cb222013-12-04 16:14:06 -0800990 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800991 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
993 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800994 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700995 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700996 if (r != null) {
997 return r;
998 }
999 }
1000 }
1001 return null;
1002 }
1003
Dianne Hackborn09233282014-04-30 11:33:59 -07001004 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001005 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001006 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1007 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001008 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001009 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001010 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001011 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1012 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001013 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001014 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001015 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001016 }
1017 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001018
1019 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1020 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1021 while (maxNum > 0) {
1022 long mostRecentActiveTime = Long.MIN_VALUE;
1023 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001024 final int numTaskLists = runningTaskLists.size();
1025 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1026 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001027 if (!stackTaskList.isEmpty()) {
1028 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1029 if (lastActiveTime > mostRecentActiveTime) {
1030 mostRecentActiveTime = lastActiveTime;
1031 selectedStackList = stackTaskList;
1032 }
1033 }
1034 }
1035 if (selectedStackList != null) {
1036 list.add(selectedStackList.remove(0));
1037 --maxNum;
1038 } else {
1039 break;
1040 }
1041 }
Craig Mautner20e72272013-04-01 13:45:53 -07001042 }
1043
Todd Kennedy7440f172015-12-09 14:31:22 -08001044 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1045 ProfilerInfo profilerInfo) {
1046 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001047 if (aInfo != null) {
1048 // Store the found target back into the intent, because now that
1049 // we have it we never want to do this again. For example, if the
1050 // user navigates back to this point in the history, we should
1051 // always restart the exact same activity.
1052 intent.setComponent(new ComponentName(
1053 aInfo.applicationInfo.packageName, aInfo.name));
1054
1055 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001056 if (!aInfo.processName.equals("system")) {
1057 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001058 mService.setDebugApp(aInfo.processName, true, false);
1059 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001060
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001061 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1062 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1063 }
1064
Man Caocfa78b22015-06-11 20:14:34 -07001065 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1066 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1067 }
1068
1069 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001070 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001071 }
1072 }
1073 }
1074 return aInfo;
1075 }
1076
Todd Kennedy7440f172015-12-09 14:31:22 -08001077 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001078 return resolveIntent(intent, resolvedType, userId, 0);
1079 }
1080
1081 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001082 try {
1083 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001084 PackageManager.MATCH_DEFAULT_ONLY | flags
1085 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001086 } catch (RemoteException e) {
1087 }
1088 return null;
1089 }
1090
1091 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1092 ProfilerInfo profilerInfo, int userId) {
1093 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1094 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1095 }
1096
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001097 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1098 boolean andResume, boolean checkConfig) throws RemoteException {
1099
1100 if (!allPausedActivitiesComplete()) {
1101 // While there are activities pausing we skipping starting any new activities until
1102 // pauses are complete. NOTE: that we also do this for activities that are starting in
1103 // the paused state because they will first be resumed then paused on the client side.
1104 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1105 "realStartActivityLocked: Skipping start of r=" + r
1106 + " some activities pausing...");
1107 return false;
1108 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001109
Craig Mautner2568c3a2015-03-26 14:22:34 -07001110 if (andResume) {
1111 r.startFreezingScreenLocked(app, 0);
1112 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001113
Craig Mautner2568c3a2015-03-26 14:22:34 -07001114 // schedule launch ticks to collect information about slow apps.
1115 r.startLaunchTickingLocked();
1116 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001117
1118 // Have the window manager re-evaluate the orientation of
1119 // the screen based on the new activity order. Note that
1120 // as a result of this, it can call back into the activity
1121 // manager with a new orientation. We don't care about that,
1122 // because the activity is not currently running so we are
1123 // just restarting it anyway.
1124 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001125 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001126 mService.mConfiguration,
1127 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001128 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001129 }
1130
1131 r.app = app;
1132 app.waitingToKill = null;
1133 r.launchCount++;
1134 r.lastLaunchTime = SystemClock.uptimeMillis();
1135
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001136 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001137
1138 int idx = app.activities.indexOf(r);
1139 if (idx < 0) {
1140 app.activities.add(r);
1141 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001142 mService.updateLruProcessLocked(app, true, null);
1143 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001144
Craig Mautner15df08a2015-04-01 12:17:18 -07001145 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001146 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1147 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001148 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001149 }
1150
1151 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001152 try {
1153 if (app.thread == null) {
1154 throw new RemoteException();
1155 }
1156 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001157 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001158 if (andResume) {
1159 results = r.results;
1160 newIntents = r.newIntents;
1161 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001162 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1163 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1164 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001165 if (andResume) {
1166 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1167 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001168 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001169 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001170 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001171 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001172 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001173 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001174 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001175 r.sleeping = false;
1176 r.forceNewConfig = false;
1177 mService.showAskCompatModeDialogLocked(r);
1178 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001179 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001180 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1181 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1182 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001183 final String profileFile = mService.mProfileFile;
1184 if (profileFile != null) {
1185 ParcelFileDescriptor profileFd = mService.mProfileFd;
1186 if (profileFd != null) {
1187 try {
1188 profileFd = profileFd.dup();
1189 } catch (IOException e) {
1190 if (profileFd != null) {
1191 try {
1192 profileFd.close();
1193 } catch (IOException o) {
1194 }
1195 profileFd = null;
1196 }
1197 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001198 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001199
1200 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1201 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 }
1203 }
1204 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001205
Craig Mautner2568c3a2015-03-26 14:22:34 -07001206 if (andResume) {
1207 app.hasShownUi = true;
1208 app.pendingUiClean = true;
1209 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001210 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001211 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001212 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001213 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001214 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001215 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001216
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001217 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001218 // This may be a heavy-weight process! Note that the package
1219 // manager will ensure that only activity can run in the main
1220 // process of the .apk, which is the only thing that will be
1221 // considered heavy-weight.
1222 if (app.processName.equals(app.info.packageName)) {
1223 if (mService.mHeavyWeightProcess != null
1224 && mService.mHeavyWeightProcess != app) {
1225 Slog.w(TAG, "Starting new heavy weight process " + app
1226 + " when already running "
1227 + mService.mHeavyWeightProcess);
1228 }
1229 mService.mHeavyWeightProcess = app;
1230 Message msg = mService.mHandler.obtainMessage(
1231 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1232 msg.obj = r;
1233 mService.mHandler.sendMessage(msg);
1234 }
1235 }
1236
1237 } catch (RemoteException e) {
1238 if (r.launchFailed) {
1239 // This is the second time we failed -- finish activity
1240 // and give up.
1241 Slog.e(TAG, "Second failure launching "
1242 + r.intent.getComponent().flattenToShortString()
1243 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001244 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001245 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1246 "2nd-crash", false);
1247 return false;
1248 }
1249
1250 // This is the first time we failed -- restart process and
1251 // retry.
1252 app.activities.remove(r);
1253 throw e;
1254 }
1255
1256 r.launchFailed = false;
1257 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001258 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001259 }
1260
1261 if (andResume) {
1262 // As part of the process of launching, ActivityThread also performs
1263 // a resume.
1264 stack.minimalResumeActivityLocked(r);
1265 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001266 // This activity is not starting in the resumed state... which should look like we asked
1267 // it to pause+stop (but remain visible), and it has done so and reported back the
1268 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001269 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001270 "Moving to PAUSED: " + r + " (starting in paused state)");
1271 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001272 }
1273
1274 // Launch the new version setup screen if needed. We do this -after-
1275 // launching the initial activity (that is, home), so that it can have
1276 // a chance to initialize itself while in the background, making the
1277 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001278 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001279 mService.startSetupActivityLocked();
1280 }
1281
Dianne Hackborn465fa392014-09-14 14:21:18 -07001282 // Update any services we are bound to that might care about whether
1283 // their client may have activities.
1284 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1285
Craig Mautner2420ead2013-04-01 17:13:20 -07001286 return true;
1287 }
1288
Craig Mautnere79d42682013-04-01 19:01:53 -07001289 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001290 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001291 // Is this activity's application already running?
1292 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001293 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001294
1295 r.task.stack.setLaunchTime(r);
1296
1297 if (app != null && app.thread != null) {
1298 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001299 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1300 || !"android".equals(r.info.packageName)) {
1301 // Don't add this if it is a platform component that is marked
1302 // to run in multiple processes, because this is actually
1303 // part of the framework so doesn't make sense to track as a
1304 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001305 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1306 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001307 }
George Mount2c92c972014-03-20 09:38:23 -07001308 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001309 return;
1310 } catch (RemoteException e) {
1311 Slog.w(TAG, "Exception when starting activity "
1312 + r.intent.getComponent().flattenToShortString(), e);
1313 }
1314
1315 // If a dead object exception was thrown -- fall through to
1316 // restart the application.
1317 }
1318
1319 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001320 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001321 }
1322
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001323 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001324 String resultWho, int requestCode, int callingPid, int callingUid,
1325 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001326 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001327 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1328 callingUid);
1329 if (startAnyPerm == PERMISSION_GRANTED) {
1330 return true;
1331 }
1332 final int componentRestriction = getComponentRestrictionForCallingPackage(
1333 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1334 final int actionRestriction = getActionRestrictionForCallingPackage(
1335 intent.getAction(), callingPackage, callingPid, callingUid);
1336 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1337 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1338 if (resultRecord != null) {
1339 resultStack.sendActivityResultLocked(-1,
1340 resultRecord, resultWho, requestCode,
1341 Activity.RESULT_CANCELED, null);
1342 }
1343 final String msg;
1344 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1345 msg = "Permission Denial: starting " + intent.toString()
1346 + " from " + callerApp + " (pid=" + callingPid
1347 + ", uid=" + callingUid + ")" + " with revoked permission "
1348 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1349 } else if (!aInfo.exported) {
1350 msg = "Permission Denial: starting " + intent.toString()
1351 + " from " + callerApp + " (pid=" + callingPid
1352 + ", uid=" + callingUid + ")"
1353 + " not exported from uid " + aInfo.applicationInfo.uid;
1354 } else {
1355 msg = "Permission Denial: starting " + intent.toString()
1356 + " from " + callerApp + " (pid=" + callingPid
1357 + ", uid=" + callingUid + ")"
1358 + " requires " + aInfo.permission;
1359 }
1360 Slog.w(TAG, msg);
1361 throw new SecurityException(msg);
1362 }
1363
1364 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1365 final String message = "Appop Denial: starting " + intent.toString()
1366 + " from " + callerApp + " (pid=" + callingPid
1367 + ", uid=" + callingUid + ")"
1368 + " requires " + AppOpsManager.permissionToOp(
1369 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1370 Slog.w(TAG, message);
1371 return false;
1372 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1373 final String message = "Appop Denial: starting " + intent.toString()
1374 + " from " + callerApp + " (pid=" + callingPid
1375 + ", uid=" + callingUid + ")"
1376 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1377 Slog.w(TAG, message);
1378 return false;
1379 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001380 if (options != null && options.getLaunchTaskId() != -1) {
1381 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1382 callingPid, callingUid);
1383 if (startInTaskPerm != PERMISSION_GRANTED) {
1384 final String msg = "Permission Denial: starting " + intent.toString()
1385 + " from " + callerApp + " (pid=" + callingPid
1386 + ", uid=" + callingUid + ") with launchTaskId="
1387 + options.getLaunchTaskId();
1388 Slog.w(TAG, msg);
1389 throw new SecurityException(msg);
1390 }
1391 }
1392
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001393 return true;
1394 }
1395
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001396 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001397 final long identity = Binder.clearCallingIdentity();
1398 try {
1399 return UserManager.get(mService.mContext).getUserInfo(userId);
1400 } finally {
1401 Binder.restoreCallingIdentity(identity);
1402 }
1403 }
1404
Svet Ganov99b60432015-06-27 13:15:22 -07001405 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001406 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001407 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1408 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001409 == PackageManager.PERMISSION_DENIED) {
1410 return ACTIVITY_RESTRICTION_PERMISSION;
1411 }
1412
Christopher Tateff7add02015-08-17 10:23:22 -07001413 if (activityInfo.permission == null) {
1414 return ACTIVITY_RESTRICTION_NONE;
1415 }
1416
Svet Ganov99b60432015-06-27 13:15:22 -07001417 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1418 if (opCode == AppOpsManager.OP_NONE) {
1419 return ACTIVITY_RESTRICTION_NONE;
1420 }
1421
1422 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1423 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001424 if (!ignoreTargetSecurity) {
1425 return ACTIVITY_RESTRICTION_APPOP;
1426 }
Svet Ganov99b60432015-06-27 13:15:22 -07001427 }
1428
1429 return ACTIVITY_RESTRICTION_NONE;
1430 }
1431
Svetoslav7008b512015-06-24 18:47:07 -07001432 private int getActionRestrictionForCallingPackage(String action,
1433 String callingPackage, int callingPid, int callingUid) {
1434 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001435 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001436 }
1437
1438 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1439 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001440 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001441 }
1442
1443 final PackageInfo packageInfo;
1444 try {
1445 packageInfo = mService.mContext.getPackageManager()
1446 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1447 } catch (PackageManager.NameNotFoundException e) {
1448 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001449 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001450 }
1451
1452 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001453 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001454 }
1455
1456 if (mService.checkPermission(permission, callingPid, callingUid) ==
1457 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001458 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001459 }
1460
1461 final int opCode = AppOpsManager.permissionToOpCode(permission);
1462 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001463 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001464 }
1465
1466 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1467 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001468 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001469 }
1470
Svet Ganov99b60432015-06-27 13:15:22 -07001471 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001472 }
1473
Wale Ogunwaled046a012015-12-24 13:05:59 -08001474 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001475 if (r == null) {
1476 // Not sure what you are trying to do, but it is not going to work...
1477 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001478 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001479 final TaskRecord task = r.task;
1480 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001481 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001482 return false;
1483 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001484 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001485 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001486 }
1487
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001488 void setLaunchSource(int uid) {
1489 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1490 }
1491
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001492 void acquireLaunchWakelock() {
1493 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1494 throw new IllegalStateException("Calling must be system uid");
1495 }
1496 mLaunchingActivity.acquire();
1497 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1498 // To be safe, don't allow the wake lock to be held for too long.
1499 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1500 }
1501 }
1502
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001503 /**
1504 * Called when the frontmost task is idle.
1505 * @return the state of mService.mBooting before this was called.
1506 */
1507 private boolean checkFinishBootingLocked() {
1508 final boolean booting = mService.mBooting;
1509 boolean enableScreen = false;
1510 mService.mBooting = false;
1511 if (!mService.mBooted) {
1512 mService.mBooted = true;
1513 enableScreen = true;
1514 }
1515 if (booting || enableScreen) {
1516 mService.postFinishBooting(booting, enableScreen);
1517 }
1518 return booting;
1519 }
1520
Craig Mautnerf3333272013-04-22 10:55:53 -07001521 // Checked.
1522 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1523 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001524 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001525
Craig Mautnerf3333272013-04-22 10:55:53 -07001526 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001527 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001528 int NS = 0;
1529 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001530 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001531 boolean activityRemoved = false;
1532
Wale Ogunwale7d701172015-03-11 15:36:30 -07001533 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001534 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001535 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1536 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001537 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1538 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001539 if (fromTimeout) {
1540 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001541 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001542
1543 // This is a hack to semi-deal with a race condition
1544 // in the client where it can be constructed with a
1545 // newer configuration from when we asked it to launch.
1546 // We'll update with whatever configuration it now says
1547 // it used to launch.
1548 if (config != null) {
1549 r.configuration = config;
1550 }
1551
1552 // We are now idle. If someone is waiting for a thumbnail from
1553 // us, we can now deliver.
1554 r.idle = true;
1555
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001556 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001557 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001558 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001559 }
1560 }
1561
1562 if (allResumedActivitiesIdle()) {
1563 if (r != null) {
1564 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001565 }
1566
1567 if (mLaunchingActivity.isHeld()) {
1568 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1569 if (VALIDATE_WAKE_LOCK_CALLER &&
1570 Binder.getCallingUid() != Process.myUid()) {
1571 throw new IllegalStateException("Calling must be system uid");
1572 }
1573 mLaunchingActivity.release();
1574 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001575 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001576 }
1577
1578 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001579 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001580 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001581 if ((NF = mFinishingActivities.size()) > 0) {
1582 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001583 mFinishingActivities.clear();
1584 }
1585
Craig Mautnerf3333272013-04-22 10:55:53 -07001586 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001587 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001588 mStartingUsers.clear();
1589 }
1590
Craig Mautnerf3333272013-04-22 10:55:53 -07001591 // Stop any activities that are scheduled to do so but have been
1592 // waiting for the next one to start.
1593 for (int i = 0; i < NS; i++) {
1594 r = stops.get(i);
1595 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001596 if (stack != null) {
1597 if (r.finishing) {
1598 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1599 } else {
1600 stack.stopActivityLocked(r);
1601 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001602 }
1603 }
1604
1605 // Finish any activities that are scheduled to do so but have been
1606 // waiting for the next one to start.
1607 for (int i = 0; i < NF; i++) {
1608 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001609 final ActivityStack stack = r.task.stack;
1610 if (stack != null) {
1611 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1612 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001613 }
1614
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001615 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001616 // Complete user switch
1617 if (startingUsers != null) {
1618 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001619 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001620 }
1621 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001622 }
1623
1624 mService.trimApplications();
1625 //dump();
1626 //mWindowManager.dump();
1627
Craig Mautnerf3333272013-04-22 10:55:53 -07001628 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001629 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001630 }
1631
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001632 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001633 }
1634
Craig Mautner8e569572013-10-11 17:36:59 -07001635 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001636 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001637 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1638 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001639 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1640 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1641 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001642 }
Craig Mautner19091252013-10-05 00:03:53 -07001643 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001644 }
1645
1646 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001647 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1648 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001649 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1650 stacks.get(stackNdx).closeSystemDialogsLocked();
1651 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001652 }
1653 }
1654
Craig Mautner93529a42013-10-04 15:03:13 -07001655 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001656 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001657 }
1658
Craig Mautner8d341ef2013-03-26 09:03:27 -07001659 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001660 * Update the last used stack id for non-current user (current user's last
1661 * used stack is the focused stack)
1662 */
1663 void updateUserStackLocked(int userId, ActivityStack stack) {
1664 if (userId != mCurrentUser) {
1665 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1666 }
1667 }
1668
1669 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001670 * @return true if some activity was finished (or would have finished if doit were true).
1671 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001672 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1673 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001674 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001675 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1676 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001677 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001678 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001679 if (stack.finishDisabledPackageActivitiesLocked(
1680 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001681 didSomething = true;
1682 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001683 }
1684 }
1685 return didSomething;
1686 }
1687
Dianne Hackborna413dc02013-07-12 12:02:55 -07001688 void updatePreviousProcessLocked(ActivityRecord r) {
1689 // Now that this process has stopped, we may want to consider
1690 // it to be the previous app to try to keep around in case
1691 // the user wants to return to it.
1692
1693 // First, found out what is currently the foreground app, so that
1694 // we don't blow away the previous app if this activity is being
1695 // hosted by the process that is actually still the foreground.
1696 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001697 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1698 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001699 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1700 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001701 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001702 if (stack.mResumedActivity != null) {
1703 fgApp = stack.mResumedActivity.app;
1704 } else if (stack.mPausingActivity != null) {
1705 fgApp = stack.mPausingActivity.app;
1706 }
1707 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001708 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001709 }
1710 }
1711
1712 // Now set this one as the previous process, only if that really
1713 // makes sense to.
1714 if (r.app != null && fgApp != null && r.app != fgApp
1715 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001716 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001717 mService.mPreviousProcess = r.app;
1718 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1719 }
1720 }
1721
Wale Ogunwaled046a012015-12-24 13:05:59 -08001722 boolean resumeFocusedStackTopActivityLocked() {
1723 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001724 }
1725
Wale Ogunwaled046a012015-12-24 13:05:59 -08001726 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001727 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001728 if (targetStack != null && isFocusedStack(targetStack)) {
1729 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001730 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001731 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1732 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001733 }
1734
Todd Kennedy39bfee52016-02-24 10:28:21 -08001735 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1736 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1737 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1738 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1739 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1740 }
1741 }
1742 }
1743
Adrian Roos20d7df32016-01-12 18:59:43 +01001744 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1745 TaskRecord finishedTask = null;
1746 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001747 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1748 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001749 final int numStacks = stacks.size();
1750 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1751 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001752 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1753 if (stack == focusedStack || finishedTask == null) {
1754 finishedTask = t;
1755 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001756 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001757 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001758 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001759 }
1760
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001761 void finishVoiceTask(IVoiceInteractionSession session) {
1762 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1763 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1764 final int numStacks = stacks.size();
1765 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1766 final ActivityStack stack = stacks.get(stackNdx);
1767 stack.finishVoiceTask(session);
1768 }
1769 }
1770 }
1771
Andrii Kulianc27916642016-04-12 17:59:27 -07001772 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1773 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001774 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1775 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001776 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001777 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1778 // Caller wants the home activity moved with it. To accomplish this,
1779 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001780 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001781 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001782 if (task.stack == null) {
1783 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1784 + task + " to front. Stack is null");
1785 return;
1786 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001787
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001788 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001789 int stackId = options.getLaunchStackId();
1790 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001791 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001792 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001793 if (stackId == INVALID_STACK_ID) {
1794 stackId = task.getLaunchStackId();
1795 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001796 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001797 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1798 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1799 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001800 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1801 // still need moveTaskToFrontLocked() below for any transition settings.
1802 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001803 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1804 resizeStackLocked(stackId, bounds,
1805 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001806 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001807 } else {
1808 // WM resizeTask must be done after the task is moved to the correct stack,
1809 // because Task's setBounds() also updates dim layer's bounds, but that has
1810 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001811 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001812 false /* relayout */, false /* forced */);
1813 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001814 }
1815 }
1816
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001817 final ActivityRecord r = task.getTopActivity();
1818 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1819 r == null ? null : r.appTimeTracker, reason);
1820
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001821 if (DEBUG_STACK) Slog.d(TAG_STACK,
1822 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001823
Andrii Kulianc27916642016-04-12 17:59:27 -07001824 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1825 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001826 }
1827
Wale Ogunwale854809c2015-12-27 16:18:19 -08001828 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001829 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001830 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001831 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001832 return false;
1833 }
1834 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1835 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001836 }
1837
Craig Mautner967212c2013-04-13 21:10:58 -07001838 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001839 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001840 }
1841
1842 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001843 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1844 if (activityContainer != null) {
1845 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001846 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001847 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001848 return null;
1849 }
1850 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001851 }
1852
Craig Mautner967212c2013-04-13 21:10:58 -07001853 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001854 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001855 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1856 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001857 }
1858 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001859 }
1860
Craig Mautner4a1cb222013-12-04 16:14:06 -08001861 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001862 ActivityRecord homeActivity = getHomeActivity();
1863 if (homeActivity != null) {
1864 return homeActivity.appToken;
1865 }
1866 return null;
1867 }
1868
1869 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001870 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001871 }
1872
1873 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001874 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1875 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1876 final TaskRecord task = tasks.get(taskNdx);
1877 if (task.isHomeTask()) {
1878 final ArrayList<ActivityRecord> activities = task.mActivities;
1879 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1880 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001881 if (r.isHomeActivity()
1882 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001883 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001884 }
1885 }
1886 }
1887 }
1888 return null;
1889 }
1890
Chong Zhangb15758a2015-11-17 12:12:03 -08001891 /**
1892 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1893 * the docked stack itself, or if it's side-by-side to the docked stack.
1894 */
1895 boolean isStackDockedInEffect(int stackId) {
1896 return stackId == DOCKED_STACK_ID ||
1897 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1898 }
1899
Todd Kennedyca4d8422015-01-15 15:19:22 -08001900 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001901 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001902 ActivityContainer activityContainer =
1903 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001904 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001905 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1906 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001907 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001908 return activityContainer;
1909 }
1910
Craig Mautner34b73df2014-01-12 21:11:08 -08001911 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001912 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1913 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1914 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001915 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1916 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001917 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001918 }
1919 }
1920
Craig Mautner95da1082014-02-24 17:54:35 -08001921 void deleteActivityContainer(IActivityContainer container) {
1922 ActivityContainer activityContainer = (ActivityContainer)container;
1923 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001924 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1925 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001926 final int stackId = activityContainer.mStackId;
1927 mActivityContainers.remove(stackId);
1928 mWindowManager.removeStack(stackId);
1929 }
1930 }
1931
Jorim Jaggidc249c42015-12-15 14:57:31 -08001932 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001933 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001934 if (stackId == DOCKED_STACK_ID) {
1935 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1936 preserveWindows);
1937 return;
1938 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001939 final ActivityStack stack = getStack(stackId);
1940 if (stack == null) {
1941 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1942 return;
1943 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001944
Jorim Jaggidc249c42015-12-15 14:57:31 -08001945 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001946 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1947 // stack size changing so things don't get out of sync.
1948 return;
1949 }
1950
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001951 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001952 mWindowManager.deferSurfaceLayout();
1953 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001954 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001955 if (!deferResume) {
1956 ensureConfigurationAndResume(
1957 stack, stack.topRunningActivityLocked(), preserveWindows);
1958 }
Jorim Jaggic4025202015-10-22 16:43:34 +02001959 } finally {
1960 mWindowManager.continueSurfaceLayout();
1961 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001962 }
1963 }
1964
Jorim Jaggi192086e2016-03-11 17:17:03 +01001965 void deferUpdateBounds(int stackId) {
1966 final ActivityStack stack = getStack(stackId);
1967 if (stack != null) {
1968 stack.deferUpdateBounds();
1969 }
1970 }
1971
1972 void continueUpdateBounds(int stackId) {
1973 final ActivityStack stack = getStack(stackId);
1974 if (stack != null) {
1975 stack.continueUpdateBounds();
1976 }
1977 }
1978
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001979 void notifyAppTransitionDone() {
1980 continueUpdateBounds(HOME_STACK_ID);
1981 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
1982 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
1983 if (anyTaskForIdLocked(taskId) != null) {
1984 mWindowManager.setTaskDockedResizing(taskId, false);
1985 }
1986 }
1987 mResizingTasksDuringAnimation.clear();
1988 }
1989
Jorim Jaggi192086e2016-03-11 17:17:03 +01001990 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08001991 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001992 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001993
Jorim Jaggi192086e2016-03-11 17:17:03 +01001994 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
1995 return;
1996 }
1997
Jorim Jaggidc249c42015-12-15 14:57:31 -08001998 mTmpBounds.clear();
1999 mTmpConfigs.clear();
2000 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002001 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002002 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2003 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002004 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002005 final TaskRecord task = tasks.get(i);
2006 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002007 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2008 // For freeform stack we don't adjust the size of the tasks to match that
2009 // of the stack, but we do try to make sure the tasks are still contained
2010 // with the bounds of the stack.
2011 tempRect2.set(task.mBounds);
2012 fitWithinBounds(tempRect2, bounds);
2013 task.updateOverrideConfiguration(tempRect2);
2014 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002015 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002016 }
2017 }
2018
2019 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2020 mTmpBounds.put(task.taskId, task.mBounds);
2021 if (tempTaskInsetBounds != null) {
2022 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2023 }
2024 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002025
2026 // We might trigger a configuration change. Save the current task bounds for freezing.
2027 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002028 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2029 mTmpBounds, mTmpInsetBounds);
2030 stack.setBounds(bounds);
2031 }
2032
2033 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
2034 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08002035 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002036 return;
2037 }
2038 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
2039 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002040 if (!updated) {
2041 resumeFocusedStackTopActivityLocked();
2042 }
2043 }
2044
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002045 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2046 final ActivityStack stack = getStack(fromStackId);
2047 if (stack == null) {
2048 return;
2049 }
2050
2051 mWindowManager.deferSurfaceLayout();
2052 try {
2053 if (fromStackId == DOCKED_STACK_ID) {
2054
2055 // We are moving all tasks from the docked stack to the fullscreen stack,
2056 // which is dismissing the docked stack, so resize all other stacks to
2057 // fullscreen here already so we don't end up with resize trashing.
2058 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2059 if (StackId.isResizeableByDockedStack(i)) {
2060 ActivityStack otherStack = getStack(i);
2061 if (otherStack != null) {
2062 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2063 true /* allowResizeInDockedMode */, DEFER_RESUME);
2064 }
2065 }
2066 }
2067
2068 // Also disable docked stack resizing since we have manually adjusted the
2069 // size of other stacks above and we don't want to trigger a docked stack
2070 // resize when we remove task from it below and it is detached from the
2071 // display because it no longer contains any tasks.
2072 mAllowDockedStackResize = false;
2073 }
2074 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2075 final int size = tasks.size();
2076 if (onTop) {
2077 for (int i = 0; i < size; i++) {
2078 moveTaskToStackLocked(tasks.get(i).taskId,
2079 FULLSCREEN_WORKSPACE_STACK_ID, onTop, !FORCE_FOCUS,
2080 "moveTasksToFullscreenStack", ANIMATE);
2081 }
2082 } else {
2083 for (int i = size - 1; i >= 0; i--) {
2084 positionTaskInStackLocked(tasks.get(i).taskId,
2085 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2086 }
2087 }
2088 } finally {
2089 mAllowDockedStackResize = true;
2090 mWindowManager.continueSurfaceLayout();
2091 }
2092 }
2093
Jorim Jaggidc249c42015-12-15 14:57:31 -08002094 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2095 Rect tempDockedTaskInsetBounds,
2096 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002097
2098 if (!mAllowDockedStackResize) {
2099 // Docked stack resize currently disabled.
2100 return;
2101 }
2102
Jorim Jaggidc249c42015-12-15 14:57:31 -08002103 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2104 if (stack == null) {
2105 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2106 return;
2107 }
2108
2109 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2110 mWindowManager.deferSurfaceLayout();
2111 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002112 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2113 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002114 ActivityRecord r = stack.topRunningActivityLocked();
2115 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2116 tempDockedTaskInsetBounds);
2117
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002118 // TODO: Checking for isAttached might not be needed as if the user passes in null
2119 // dockedBounds then they want the docked stack to be dismissed.
2120 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2121 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002122 // In this case we make all other static stacks fullscreen and move all
2123 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002124 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002125
2126 // stack shouldn't contain anymore activities, so nothing to resume.
2127 r = null;
2128 } else {
2129 // Docked stacks occupy a dedicated region on screen so the size of all other
2130 // static stacks need to be adjusted so they don't overlap with the docked stack.
2131 // We get the bounds to use from window manager which has been adjusted for any
2132 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002133 mWindowManager.getStackDockedModeBounds(
2134 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002135 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002136 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2137 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2138 tempOtherTaskInsetBounds, preserveWindows,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002139 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002140 }
2141 }
2142 }
2143 ensureConfigurationAndResume(stack, r, preserveWindows);
2144 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002145 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002146 mWindowManager.continueSurfaceLayout();
2147 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2148 }
2149
2150 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2151 tempDockedTaskBounds != null
2152 || tempDockedTaskInsetBounds != null
2153 || tempOtherTaskBounds != null
2154 || tempOtherTaskInsetBounds != null);
2155 }
2156
Robert Carr0d00c2e2016-02-29 17:45:02 -08002157 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2158 final ActivityStack stack = getStack(PINNED_STACK_ID);
2159 if (stack == null) {
2160 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2161 return;
2162 }
2163 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2164 mWindowManager.deferSurfaceLayout();
2165 try {
2166 ActivityRecord r = stack.topRunningActivityLocked();
2167 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2168 null);
2169 ensureConfigurationAndResume(stack, r, false);
2170 } finally {
2171 mWindowManager.continueSurfaceLayout();
2172 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2173 }
2174 }
2175
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002176 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2177 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002178 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002179 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002180 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002181 }
2182
Chong Zhang87b21722015-09-21 15:39:51 -07002183 // If this is a forced resize, let it go through even if the bounds is not changing,
2184 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002185 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002186 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002187 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002188 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002189 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002190 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002191
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002192 if (!mWindowManager.isValidTaskId(task.taskId)) {
2193 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002194 // All we can do for now is update the bounds so it can be used when the task is
2195 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002196 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002197 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2198 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002199 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002200 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002201 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002202 }
2203
Chong Zhang6de2ae82015-09-30 18:25:21 -07002204 // Do not move the task to another stack here.
2205 // This method assumes that the task is already placed in the right stack.
2206 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002207
Chong Zhang6de2ae82015-09-30 18:25:21 -07002208 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002209
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002210 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002211 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002212 // way and the activity was kept the way it was. If it's false, it means the activity had
2213 // to be relaunched due to configuration change.
2214 boolean kept = true;
2215 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002216 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002217 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002218 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002219 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002220
2221 if (!deferResume) {
2222
2223 // All other activities must be made visible with their correct configuration.
2224 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2225 if (!kept) {
2226 resumeFocusedStackTopActivityLocked();
2227 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002228 }
2229 }
2230 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002231 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002232
2233 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002234 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002235 }
2236
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002237 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002238 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2239 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002240 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002241 }
2242
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002243 ActivityContainer activityContainer = new ActivityContainer(stackId);
2244 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002245 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002246 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002247 }
2248
2249 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002250 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002251 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2252 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002253 break;
2254 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002255 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002256 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002257 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002258 }
2259
Chong Zhang5dcb2752015-08-18 13:50:26 -07002260 /**
2261 * Restores a recent task to a stack
2262 * @param task The recent task to be restored.
2263 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002264 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002265 * @return true if the task has been restored successfully.
2266 */
2267 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2268 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002269 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002270 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2271 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2272 // Put it in the fullscreen stack.
2273 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002274 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002275
Wale Ogunwale706ed792015-08-02 10:29:44 -07002276 if (task.stack != null) {
2277 // Task has already been restored once. See if we need to do anything more
2278 if (task.stack.mStackId == stackId) {
2279 // Nothing else to do since it is already restored in the right stack.
2280 return true;
2281 }
2282 // Remove current stack association, so we can re-associate the task with the
2283 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002284 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002285 }
2286
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002287 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002288 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002289
2290 if (stack == null) {
2291 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002292 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2293 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002294 return false;
2295 }
2296
Wale Ogunwale5f986092015-12-04 15:35:38 -08002297 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002298 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2299 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002300 final ArrayList<ActivityRecord> activities = task.mActivities;
2301 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002302 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002303 }
2304 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002305 }
2306
Wale Ogunwale040b4702015-08-06 18:10:50 -07002307 /**
2308 * Moves the specified task record to the input stack id.
2309 * WARNING: This method performs an unchecked/raw move of the task and
2310 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002311 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002312 * @param task Task to move.
2313 * @param stackId Id of stack to move task to.
2314 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002315 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002316 * @param reason Reason the task is been moved.
2317 * @return The stack the task was moved to.
2318 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002319 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002320 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002321
2322 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2323 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2324 + "support multi-window task=" + task + " to stackId=" + stackId);
2325 }
2326
Chong Zhang02898352015-08-21 17:27:14 -07002327 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002328 final ActivityStack prevStack = task.stack;
2329 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2330 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2331 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2332 // Whenever we are moving the top activity from the front stack we want to make sure to move
2333 // the stack to the front.
2334 final boolean wasFront = isFrontStack(prevStack)
2335 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002336
Chong Zhangd545dce2016-02-29 18:09:17 -08002337 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002338 // We don't allow moving a unresizeable task to the docked stack since the docked
2339 // stack is used for split-screen mode and will cause things like the docked divider to
2340 // show up. We instead leave the task in its current stack or move it to the fullscreen
2341 // stack if it isn't currently in a stack.
2342 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002343 Slog.w(TAG, "Can not move unresizeable task=" + task
2344 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2345 }
2346
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002347 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2348 // if a docked stack is created below which will lead to the stack we are moving from and
2349 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002350 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002351 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002352 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002353 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002354 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002355
2356 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002357 // move focus to the new stack by moving the stack to the front.
2358 stack.moveToFrontAndResumeStateIfNeeded(
2359 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002360
Wale Ogunwale040b4702015-08-06 18:10:50 -07002361 return stack;
2362 }
2363
Chong Zhange4fbd322016-03-01 14:44:03 -08002364 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002365 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002366 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2367 false /* deferResume */);
2368 }
2369
2370 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2371 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002372 final TaskRecord task = anyTaskForIdLocked(taskId);
2373 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002374 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002375 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002376 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002377
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002378 if (task.stack != null && task.stack.mStackId == stackId) {
2379 // You are already in the right stack silly...
2380 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002381 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002382 }
2383
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002384 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2385 throw new IllegalArgumentException("moveTaskToStack:"
2386 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2387 }
2388
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002389 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002390 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002391 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002392 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002393 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002394 // We are about to relaunch the activity because its configuration changed due to
2395 // being maximized, i.e. size change. The activity will first remove the old window
2396 // and then add a new one. This call will tell window manager about this, so it can
2397 // preserve the old window until the new one is drawn. This prevents having a gap
2398 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002399 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002400 // Note here we always set the replacing window first, as the flags might be needed
2401 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002402 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002403 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002404
2405 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002406 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002407 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002408 try {
2409 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002410 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002411 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002412
Wale Ogunwale961f4852016-02-01 20:25:54 -08002413 if (!animate) {
2414 stack.mNoAnimActivities.add(topActivity);
2415 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002416
Wale Ogunwale961f4852016-02-01 20:25:54 -08002417 // We might trigger a configuration change. Save the current task bounds for freezing.
2418 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2419
2420 // Make sure the task has the appropriate bounds/size for the stack it is in.
2421 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002422 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2423 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002424 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2425 Rect bounds = task.getLaunchBounds();
2426 if (bounds == null) {
2427 stack.layoutTaskInStack(task, null);
2428 bounds = task.mBounds;
2429 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002430 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2431 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002432 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002433 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2434 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002435 }
2436 } finally {
2437 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002438 }
2439
2440 if (mightReplaceWindow) {
2441 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2442 // window), we need to clear the replace window settings. Otherwise, we schedule a
2443 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002444 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002445 }
2446
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002447 if (!deferResume) {
2448
2449 // The task might have already been running and its visibility needs to be synchronized with
2450 // the visibility of the stack / windows.
2451 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2452 resumeFocusedStackTopActivityLocked();
2453 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002454
Jorim Jaggid53f0922016-04-06 22:16:23 -07002455 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002456
2457 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002458 }
2459
Wale Ogunwale079a0042015-10-24 11:44:07 -07002460 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2461 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2462 if (stack == null) {
2463 throw new IllegalArgumentException(
2464 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2465 }
2466
2467 final ActivityRecord r = stack.topRunningActivityLocked();
2468 if (r == null) {
2469 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2470 + " in stack=" + stack);
2471 return false;
2472 }
2473
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002474 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002475 Slog.w(TAG,
2476 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002477 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002478 return false;
2479 }
2480
Wale Ogunwale480dca02016-02-06 13:58:29 -08002481 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002482 return true;
2483 }
2484
Wale Ogunwale480dca02016-02-06 13:58:29 -08002485 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2486 mWindowManager.deferSurfaceLayout();
2487 try {
2488 final TaskRecord task = r.task;
2489
2490 // Need to make sure the pinned stack exist so we can resize it below...
2491 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2492
2493 // Resize the pinned stack to match the current size of the task the activity we are
2494 // going to be moving is currently contained in. We do this to have the right starting
2495 // animation bounds for the pinned stack to the desired bounds the caller wants.
2496 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2497 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002498 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002499
2500 if (task.mActivities.size() == 1) {
2501 // There is only one activity in the task. So, we can just move the task over to
2502 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002503 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2504 // Move the home stack forward if the task we just moved to the pinned stack
2505 // was launched from home so home should be visible behind it.
2506 moveHomeStackToFront(reason);
2507 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002508 moveTaskToStackLocked(
2509 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2510 } else {
2511 stack.moveActivityToStack(r);
2512 }
2513 } finally {
2514 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002515 }
2516
Wale Ogunwale480dca02016-02-06 13:58:29 -08002517 // The task might have already been running and its visibility needs to be synchronized
2518 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002519 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002520 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002521
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002522 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002523 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002524 }
2525
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002526 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2527 final TaskRecord task = anyTaskForIdLocked(taskId);
2528 if (task == null) {
2529 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2530 return;
2531 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002532 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2533
2534 task.updateOverrideConfigurationForStack(stack);
2535
2536 mWindowManager.positionTaskInStack(
2537 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002538 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002539 // The task might have already been running and its visibility needs to be synchronized with
2540 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002541 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002542 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002543 }
2544
Craig Mautnerac6f8432013-07-17 13:24:59 -07002545 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002546 mTmpFindTaskResult.r = null;
2547 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002548 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002549 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2550 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002551 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2552 final ActivityStack stack = stacks.get(stackNdx);
2553 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002554 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002555 continue;
2556 }
2557 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002558 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2559 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002560 continue;
2561 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002562 stack.findTaskLocked(r, mTmpFindTaskResult);
2563 // It is possible to have task in multiple stacks with the same root affinity.
2564 // If the match we found was based on root affinity we keep on looking to see if
2565 // there is a better match in another stack. We eventually return the match based
2566 // on root affinity if we don't find a better match.
2567 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2568 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002569 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002570 }
2571 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002572 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2573 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002574 }
2575
2576 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002577 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2578 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002579 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2580 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2581 if (ar != null) {
2582 return ar;
2583 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002584 }
2585 }
2586 return null;
2587 }
2588
Craig Mautner8d341ef2013-03-26 09:03:27 -07002589 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002590 scheduleSleepTimeout();
2591 if (!mGoingToSleep.isHeld()) {
2592 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002593 if (mLaunchingActivity.isHeld()) {
2594 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2595 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002596 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002597 mLaunchingActivity.release();
2598 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002599 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002600 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002601 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002602 }
2603
2604 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002605 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002606
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002607 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002608 final long endTime = System.currentTimeMillis() + timeout;
2609 while (true) {
2610 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002611 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2612 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002613 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2614 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2615 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002616 }
2617 if (cantShutdown) {
2618 long timeRemaining = endTime - System.currentTimeMillis();
2619 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002620 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002621 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002622 } catch (InterruptedException e) {
2623 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002624 } else {
2625 Slog.w(TAG, "Activity manager shutdown timed out");
2626 timedout = true;
2627 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002628 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002629 } else {
2630 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002631 }
2632 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002633
2634 // Force checkReadyForSleep to complete.
2635 mSleepTimeout = true;
2636 checkReadyForSleepLocked();
2637
Craig Mautner8d341ef2013-03-26 09:03:27 -07002638 return timedout;
2639 }
2640
2641 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002642 removeSleepTimeouts();
2643 if (mGoingToSleep.isHeld()) {
2644 mGoingToSleep.release();
2645 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002646 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2647 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002648 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2649 final ActivityStack stack = stacks.get(stackNdx);
2650 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002651 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002652 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002653 }
Craig Mautner5314a402013-09-26 12:40:16 -07002654 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002655 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002656 mGoingToSleepActivities.clear();
2657 }
2658
2659 void activitySleptLocked(ActivityRecord r) {
2660 mGoingToSleepActivities.remove(r);
2661 checkReadyForSleepLocked();
2662 }
2663
2664 void checkReadyForSleepLocked() {
2665 if (!mService.isSleepingOrShuttingDown()) {
2666 // Do not care.
2667 return;
2668 }
2669
2670 if (!mSleepTimeout) {
2671 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002672 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2673 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002674 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2675 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2676 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002677 }
2678
2679 if (mStoppingActivities.size() > 0) {
2680 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002681 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002682 + mStoppingActivities.size() + " activities");
2683 scheduleIdleLocked();
2684 dontSleep = true;
2685 }
2686
2687 if (mGoingToSleepActivities.size() > 0) {
2688 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002689 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002690 + mGoingToSleepActivities.size() + " activities");
2691 dontSleep = true;
2692 }
2693
2694 if (dontSleep) {
2695 return;
2696 }
2697 }
2698
Craig Mautnere0a38842013-12-16 16:14:02 -08002699 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2700 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002701 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2702 stacks.get(stackNdx).goToSleep();
2703 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002704 }
2705
2706 removeSleepTimeouts();
2707
2708 if (mGoingToSleep.isHeld()) {
2709 mGoingToSleep.release();
2710 }
2711 if (mService.mShuttingDown) {
2712 mService.notifyAll();
2713 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002714 }
2715
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002716 boolean reportResumedActivityLocked(ActivityRecord r) {
2717 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002718 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002719 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002720 }
2721 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002722 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002723 mWindowManager.executeAppTransition();
2724 return true;
2725 }
2726 return false;
2727 }
2728
Craig Mautner8d341ef2013-03-26 09:03:27 -07002729 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002730 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2731 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002732 int stackNdx = stacks.size() - 1;
2733 while (stackNdx >= 0) {
2734 stacks.get(stackNdx).handleAppCrashLocked(app);
2735 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002736 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002737 }
2738 }
2739
Jose Lima4b6c6692014-08-12 17:41:12 -07002740 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002741 final ActivityStack stack = r.task.stack;
2742 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002743 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2744 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002745 return false;
2746 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002747 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002748 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2749 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002750
2751 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002752 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002753 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002754 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002755 return true;
2756 }
2757
2758 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002759 if (visible && top.fullscreen) {
2760 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002761 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2762 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2763 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2764 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002765 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002766 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2767 // Only the activity set as currently visible behind should actively reset its
2768 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002769 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2770 "requestVisibleBehind: returning visible=" + visible
2771 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2772 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002773 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002774 }
2775
Jose Lima4b6c6692014-08-12 17:41:12 -07002776 stack.setVisibleBehindActivity(visible ? r : null);
2777 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002778 // If there is a translucent home activity, we need to force it stop being translucent,
2779 // because we can't depend on the application to necessarily perform that operation.
2780 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002781 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002782 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002783 mService.convertFromTranslucent(next.appToken);
2784 }
2785 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002786 if (top.app != null && top.app.thread != null) {
2787 // Notify the top app of the change.
2788 try {
2789 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2790 } catch (RemoteException e) {
2791 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002792 }
2793 return true;
2794 }
2795
Craig Mautnerbb742462014-07-07 15:28:55 -07002796 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2797 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002798 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002799 final ActivityStack stack = task.stack;
2800
2801 r.mLaunchTaskBehind = false;
2802 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002803 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002804 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002805 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002806
2807 // When launching tasks behind, update the last active time of the top task after the new
2808 // task has been shown briefly
2809 final ActivityRecord top = stack.topActivity();
2810 if (top != null) {
2811 top.task.touchActiveTime();
2812 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002813 }
2814
2815 void scheduleLaunchTaskBehindComplete(IBinder token) {
2816 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2817 }
2818
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002819 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2820 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002821 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002822 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2823 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002824 final int topStackNdx = stacks.size() - 1;
2825 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2826 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002827 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002828 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002829 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002830 }
2831
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002832 void invalidateTaskLayers() {
2833 mTaskLayersChanged = true;
2834 }
2835
2836 void rankTaskLayersIfNeeded() {
2837 if (!mTaskLayersChanged) {
2838 return;
2839 }
2840 mTaskLayersChanged = false;
2841 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2842 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2843 int baseLayer = 0;
2844 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2845 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2846 }
2847 }
2848 }
2849
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002850 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2851 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2852 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2853 final int topStackNdx = stacks.size() - 1;
2854 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2855 final ActivityStack stack = stacks.get(stackNdx);
2856 stack.clearOtherAppTimeTrackers(except);
2857 }
2858 }
2859 }
2860
Craig Mautner8d341ef2013-03-26 09:03:27 -07002861 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002862 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2863 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002864 final int numStacks = stacks.size();
2865 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2866 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002867 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002868 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002869 }
2870 }
2871
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002872 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2873 // Examine all activities currently running in the process.
2874 TaskRecord firstTask = null;
2875 // Tasks is non-null only if two or more tasks are found.
2876 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002877 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2878 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002879 ActivityRecord r = app.activities.get(i);
2880 // First, if we find an activity that is in the process of being destroyed,
2881 // then we just aren't going to do anything for now; we want things to settle
2882 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002883 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002884 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002885 return;
2886 }
2887 // Don't consider any activies that are currently not in a state where they
2888 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002889 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2890 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002891 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002892 continue;
2893 }
2894 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002895 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002896 + " from " + r);
2897 if (firstTask == null) {
2898 firstTask = r.task;
2899 } else if (firstTask != r.task) {
2900 if (tasks == null) {
2901 tasks = new ArraySet<>();
2902 tasks.add(firstTask);
2903 }
2904 tasks.add(r.task);
2905 }
2906 }
2907 }
2908 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002909 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002910 return;
2911 }
2912 // If we have activities in multiple tasks that are in a position to be destroyed,
2913 // let's iterate through the tasks and release the oldest one.
2914 final int numDisplays = mActivityDisplays.size();
2915 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2916 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2917 // Step through all stacks starting from behind, to hit the oldest things first.
2918 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2919 final ActivityStack stack = stacks.get(stackNdx);
2920 // Try to release activities in this stack; if we manage to, we are done.
2921 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2922 return;
2923 }
2924 }
2925 }
2926 }
2927
Amith Yamasani37a40c22015-06-17 13:25:42 -07002928 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002929 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002930 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002931 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002932
Craig Mautner858d8a62013-04-23 17:08:34 -07002933 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002934 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2935 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002936 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002937 final ActivityStack stack = stacks.get(stackNdx);
2938 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002939 TaskRecord task = stack.topTask();
2940 if (task != null) {
2941 mWindowManager.moveTaskToTop(task.taskId);
2942 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002943 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002944 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002945
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002946 ActivityStack stack = getStack(restoreStackId);
2947 if (stack == null) {
2948 stack = mHomeStack;
2949 }
2950 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002951 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002952 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002953 } else {
2954 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002955 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002956 }
Craig Mautner93529a42013-10-04 15:03:13 -07002957 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002958 }
2959
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002960 /** Checks whether the userid is a profile of the current user. */
2961 boolean isCurrentProfileLocked(int userId) {
2962 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002963 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002964 }
2965
Chong Zhang45c25ce2015-08-10 22:18:26 -07002966 /** Checks whether the activity should be shown for current user. */
2967 boolean okToShowLocked(ActivityRecord r) {
2968 return r != null && (isCurrentProfileLocked(r.userId)
2969 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2970 }
2971
Craig Mautnerde4ef022013-04-07 19:01:33 -07002972 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002973 ArrayList<ActivityRecord> stops = null;
2974
2975 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002976 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2977 ActivityRecord s = mStoppingActivities.get(activityNdx);
2978 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002979 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002980 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2981 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002982 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002983 if (s.finishing) {
2984 // If this activity is finishing, it is sitting on top of
2985 // everyone else but we now know it is no longer needed...
2986 // so get rid of it. Otherwise, we need to go through the
2987 // normal flow and hide it once we determine that it is
2988 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002989 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002990 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002991 }
2992 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002993 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002994 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002995 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002996 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002997 }
2998 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002999 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003000 }
3001 }
3002
3003 return stops;
3004 }
3005
Craig Mautnercf910b02013-04-23 11:23:27 -07003006 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003007 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3008 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3009 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3010 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003011 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003012 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003013 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003014 if (r == null) Slog.e(TAG,
3015 "validateTop...: null top activity, stack=" + stack);
3016 else {
3017 final ActivityRecord pausing = stack.mPausingActivity;
3018 if (pausing != null && pausing == r) Slog.e(TAG,
3019 "validateTop...: top stack has pausing activity r=" + r
3020 + " state=" + state);
3021 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3022 "validateTop...: activity in front not resumed r=" + r
3023 + " state=" + state);
3024 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003025 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003026 final ActivityRecord resumed = stack.mResumedActivity;
3027 if (resumed != null && resumed == r) Slog.e(TAG,
3028 "validateTop...: back stack has resumed activity r=" + r
3029 + " state=" + state);
3030 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3031 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003032 }
3033 }
3034 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003035 }
3036
Craig Mautnere0570202015-05-13 13:06:11 -07003037 private String lockTaskModeToString() {
3038 switch (mLockTaskModeState) {
3039 case LOCK_TASK_MODE_LOCKED:
3040 return "LOCKED";
3041 case LOCK_TASK_MODE_PINNED:
3042 return "PINNED";
3043 case LOCK_TASK_MODE_NONE:
3044 return "NONE";
3045 default: return "unknown=" + mLockTaskModeState;
3046 }
3047 }
3048
Craig Mautner27084302013-03-25 08:05:25 -07003049 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003050 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003051 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003052 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003053 pw.print(prefix);
3054 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003055 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003056 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003057 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3058 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3059 if (packages.size() > 0) {
3060 pw.println(" mLockTaskPackages (userId:packages)=");
3061 for (int i = 0; i < packages.size(); ++i) {
3062 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3063 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3064 }
3065 }
3066 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003067 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003068
Craig Mautner20e72272013-04-01 13:45:53 -07003069 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003070 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003071 }
3072
Dianne Hackborn390517b2013-05-30 15:03:32 -07003073 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3074 boolean needSep, String prefix) {
3075 if (activity != null) {
3076 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3077 if (needSep) {
3078 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003079 }
3080 pw.print(prefix);
3081 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003082 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003083 }
3084 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003085 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003086 }
3087
Craig Mautner8d341ef2013-03-26 09:03:27 -07003088 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3089 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003090 boolean printed = false;
3091 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003092 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3093 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003094 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003095 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003096 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003097 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003098 final ActivityStack stack = stacks.get(stackNdx);
3099 StringBuilder stackHeader = new StringBuilder(128);
3100 stackHeader.append(" Stack #");
3101 stackHeader.append(stack.mStackId);
3102 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003103 stackHeader.append("\n");
3104 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3105 stackHeader.append("\n");
3106 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003107 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3108 needSep, stackHeader.toString());
3109 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3110 !dumpAll, false, dumpPackage, true,
3111 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003112
Craig Mautner4a1cb222013-12-04 16:14:06 -08003113 needSep = printed;
3114 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3115 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003116 if (pr) {
3117 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003118 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003119 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003120 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3121 " mResumedActivity: ");
3122 if (pr) {
3123 printed = true;
3124 needSep = false;
3125 }
3126 if (dumpAll) {
3127 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3128 " mLastPausedActivity: ");
3129 if (pr) {
3130 printed = true;
3131 needSep = true;
3132 }
3133 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3134 needSep, " mLastNoHistoryActivity: ");
3135 }
3136 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003137 }
3138 }
3139
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003140 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3141 false, dumpPackage, true, " Activities waiting to finish:", null);
3142 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3143 false, dumpPackage, true, " Activities waiting to stop:", null);
3144 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3145 false, dumpPackage, true, " Activities waiting for another to become visible:",
3146 null);
3147 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3148 false, dumpPackage, true, " Activities waiting to sleep:", null);
3149 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3150 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003151
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003152 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003153 }
3154
Dianne Hackborn390517b2013-05-30 15:03:32 -07003155 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003156 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003157 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003158 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003159 String innerPrefix = null;
3160 String[] args = null;
3161 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003162 for (int i=list.size()-1; i>=0; i--) {
3163 final ActivityRecord r = list.get(i);
3164 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3165 continue;
3166 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003167 if (innerPrefix == null) {
3168 innerPrefix = prefix + " ";
3169 args = new String[0];
3170 }
3171 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003172 final boolean full = !brief && (complete || !r.isInHistory());
3173 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003174 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003175 needNL = false;
3176 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003177 if (header1 != null) {
3178 pw.println(header1);
3179 header1 = null;
3180 }
3181 if (header2 != null) {
3182 pw.println(header2);
3183 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003184 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003185 if (lastTask != r.task) {
3186 lastTask = r.task;
3187 pw.print(prefix);
3188 pw.print(full ? "* " : " ");
3189 pw.println(lastTask);
3190 if (full) {
3191 lastTask.dump(pw, prefix + " ");
3192 } else if (complete) {
3193 // Complete + brief == give a summary. Isn't that obvious?!?
3194 if (lastTask.intent != null) {
3195 pw.print(prefix); pw.print(" ");
3196 pw.println(lastTask.intent.toInsecureStringWithClip());
3197 }
3198 }
3199 }
3200 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3201 pw.print(" #"); pw.print(i); pw.print(": ");
3202 pw.println(r);
3203 if (full) {
3204 r.dump(pw, innerPrefix);
3205 } else if (complete) {
3206 // Complete + brief == give a summary. Isn't that obvious?!?
3207 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3208 if (r.app != null) {
3209 pw.print(innerPrefix); pw.println(r.app);
3210 }
3211 }
3212 if (client && r.app != null && r.app.thread != null) {
3213 // flush anything that is already in the PrintWriter since the thread is going
3214 // to write to the file descriptor directly
3215 pw.flush();
3216 try {
3217 TransferPipe tp = new TransferPipe();
3218 try {
3219 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3220 r.appToken, innerPrefix, args);
3221 // Short timeout, since blocking here can
3222 // deadlock with the application.
3223 tp.go(fd, 2000);
3224 } finally {
3225 tp.kill();
3226 }
3227 } catch (IOException e) {
3228 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3229 } catch (RemoteException e) {
3230 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3231 }
3232 needNL = true;
3233 }
3234 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003235 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003236 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003237
Craig Mautnerf3333272013-04-22 10:55:53 -07003238 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003239 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3240 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003241 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3242 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003243 }
3244
3245 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003246 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003247 }
3248
3249 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003250 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3251 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003252 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3253 }
3254
Craig Mautner05d29032013-05-03 13:40:13 -07003255 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003256 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3257 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3258 }
Craig Mautner05d29032013-05-03 13:40:13 -07003259 }
3260
Craig Mautner0eea92c2013-05-16 13:35:39 -07003261 void removeSleepTimeouts() {
3262 mSleepTimeout = false;
3263 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3264 }
3265
3266 final void scheduleSleepTimeout() {
3267 removeSleepTimeouts();
3268 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3269 }
3270
Craig Mautner4a1cb222013-12-04 16:14:06 -08003271 @Override
3272 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003273 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003274 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3275 }
3276
3277 @Override
3278 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003279 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003280 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3281 }
3282
3283 @Override
3284 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003285 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003286 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3287 }
3288
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003289 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003290 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003291 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003292 newDisplay = mActivityDisplays.get(displayId) == null;
3293 if (newDisplay) {
3294 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003295 if (activityDisplay.mDisplay == null) {
3296 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3297 return;
3298 }
Craig Mautner4504de52013-12-20 09:06:56 -08003299 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003300 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003301 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003302 }
Craig Mautner4504de52013-12-20 09:06:56 -08003303 if (newDisplay) {
3304 mWindowManager.onDisplayAdded(displayId);
3305 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003306 }
3307
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003308 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003309 mDefaultMinimalSizeOfResizeableTask =
3310 mService.mContext.getResources().getDimensionPixelSize(
3311 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003312 }
3313
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003314 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003315 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003316 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3317 if (activityDisplay != null) {
3318 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003319 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003320 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003321 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003322 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003323 }
3324 }
3325 mWindowManager.onDisplayRemoved(displayId);
3326 }
3327
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003328 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003329 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003330 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3331 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003332 // TODO: Update the bounds.
3333 }
3334 }
3335 mWindowManager.onDisplayChanged(displayId);
3336 }
3337
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003338 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003339 StackInfo info = new StackInfo();
3340 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3341 info.displayId = Display.DEFAULT_DISPLAY;
3342 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003343 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003344
3345 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3346 final int numTasks = tasks.size();
3347 int[] taskIds = new int[numTasks];
3348 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003349 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003350 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003351 for (int i = 0; i < numTasks; ++i) {
3352 final TaskRecord task = tasks.get(i);
3353 taskIds[i] = task.taskId;
3354 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3355 : task.realActivity != null ? task.realActivity.flattenToString()
3356 : task.getTopActivity() != null ? task.getTopActivity().packageName
3357 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003358 taskBounds[i] = new Rect();
3359 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003360 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003361 }
3362 info.taskIds = taskIds;
3363 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003364 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003365 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003366 return info;
3367 }
3368
3369 StackInfo getStackInfoLocked(int stackId) {
3370 ActivityStack stack = getStack(stackId);
3371 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003372 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003373 }
3374 return null;
3375 }
3376
3377 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003378 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003379 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3380 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003381 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003382 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003383 }
3384 }
3385 return list;
3386 }
3387
Craig Mautner15df08a2015-04-01 12:17:18 -07003388 TaskRecord getLockedTaskLocked() {
3389 final int top = mLockTaskModeTasks.size() - 1;
3390 if (top >= 0) {
3391 return mLockTaskModeTasks.get(top);
3392 }
3393 return null;
3394 }
3395
3396 boolean isLockedTask(TaskRecord task) {
3397 return mLockTaskModeTasks.contains(task);
3398 }
3399
3400 boolean isLastLockedTask(TaskRecord task) {
3401 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3402 }
3403
3404 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003405 if (!mLockTaskModeTasks.remove(task)) {
3406 return;
3407 }
3408 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3409 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003410 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003411 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3412 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003413 final Message lockTaskMsg = Message.obtain();
3414 lockTaskMsg.arg1 = task.userId;
3415 lockTaskMsg.what = LOCK_TASK_END_MSG;
3416 mHandler.sendMessage(lockTaskMsg);
3417 }
3418 }
3419
Andrii Kulianc27916642016-04-12 17:59:27 -07003420 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3421 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3422 false /* forceNonResizable */);
3423 }
3424
Jorim Jaggid53f0922016-04-06 22:16:23 -07003425 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003426 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003427 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3428 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003429 return;
3430 }
3431
Andrii Kulianc27916642016-04-12 17:59:27 -07003432 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003433 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003434 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3435
Andrii Kulianc27916642016-04-12 17:59:27 -07003436 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3437 // we need to move it to top of fullscreen stack, otherwise it will be covered.
3438 mService.moveTasksToFullscreenStack(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003439 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3440 String packageName = task.getTopActivity() != null
3441 ? task.getTopActivity().appInfo.packageName : null;
3442 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3443 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003444 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003445 }
3446
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003447 void showLockTaskToast() {
3448 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003449 }
3450
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003451 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3452 if (mLockTaskModeTasks.contains(task)) {
3453 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3454 }
3455 }
3456
Craig Mautner432f64e2015-05-20 14:59:57 -07003457 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3458 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003459 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003460 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003461 final TaskRecord lockedTask = getLockedTaskLocked();
3462 if (lockedTask != null) {
3463 removeLockedTaskLocked(lockedTask);
3464 if (!mLockTaskModeTasks.isEmpty()) {
3465 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003466 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3467 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003468 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003469 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003470 return;
3471 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003472 }
Craig Mautnere0570202015-05-13 13:06:11 -07003473 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3474 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003475 return;
3476 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003477
3478 // Should have already been checked, but do it again.
3479 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003480 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3481 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003482 return;
3483 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003484 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003485 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003486 return;
3487 }
3488
3489 if (mLockTaskModeTasks.isEmpty()) {
3490 // First locktask.
3491 final Message lockTaskMsg = Message.obtain();
3492 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3493 lockTaskMsg.arg1 = task.userId;
3494 lockTaskMsg.what = LOCK_TASK_START_MSG;
3495 lockTaskMsg.arg2 = lockTaskModeState;
3496 mHandler.sendMessage(lockTaskMsg);
3497 }
3498 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003499 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3500 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003501 mLockTaskModeTasks.remove(task);
3502 mLockTaskModeTasks.add(task);
3503
3504 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003505 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003506 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003507
3508 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003509 findTaskToMoveToFrontLocked(task, 0, null, reason,
3510 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003511 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003512 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3513 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3514 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003515 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003516 }
3517
3518 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003519 return isLockTaskModeViolation(task, false);
3520 }
3521
3522 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3523 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003524 return false;
3525 }
3526 final int lockTaskAuth = task.mLockTaskAuth;
3527 switch (lockTaskAuth) {
3528 case LOCK_TASK_AUTH_DONT_LOCK:
3529 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003530 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003531 case LOCK_TASK_AUTH_LAUNCHABLE:
3532 case LOCK_TASK_AUTH_WHITELISTED:
3533 return false;
3534 case LOCK_TASK_AUTH_PINNABLE:
3535 // Pinnable tasks can't be launched on top of locktask tasks.
3536 return !mLockTaskModeTasks.isEmpty();
3537 default:
3538 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3539 return true;
3540 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003541 }
3542
Craig Mautner15df08a2015-04-01 12:17:18 -07003543 void onLockTaskPackagesUpdatedLocked() {
3544 boolean didSomething = false;
3545 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3546 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003547 final boolean wasWhitelisted =
3548 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3549 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003550 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003551 final boolean isWhitelisted =
3552 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3553 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3554 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003555 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003556 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3557 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003558 removeLockedTaskLocked(lockedTask);
3559 lockedTask.performClearTaskLocked();
3560 didSomething = true;
3561 }
3562 }
3563 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3564 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3565 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3566 final ActivityStack stack = stacks.get(stackNdx);
3567 stack.onLockTaskPackagesUpdatedLocked();
3568 }
3569 }
Craig Mautnere0570202015-05-13 13:06:11 -07003570 final ActivityRecord r = topRunningActivityLocked();
3571 final TaskRecord task = r != null ? r.task : null;
3572 if (mLockTaskModeTasks.isEmpty() && task != null
3573 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3574 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003575 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3576 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3577 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3578 false);
3579 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003580 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003581 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003582 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003583 }
3584 }
3585
Benjamin Franz43261142015-02-11 15:59:44 +00003586 int getLockTaskModeState() {
3587 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003588 }
3589
Jorim Jaggife89d122015-12-22 16:28:44 +01003590 void activityRelaunchedLocked(IBinder token) {
3591 mWindowManager.notifyAppRelaunchingFinished(token);
3592 }
3593
3594 void activityRelaunchingLocked(ActivityRecord r) {
3595 mWindowManager.notifyAppRelaunching(r.appToken);
3596 }
3597
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003598 void logStackState() {
3599 mActivityMetricsLogger.logWindowState();
3600 }
3601
Andrii Kulian933076d2016-03-29 17:04:42 -07003602 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003603 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3604 final ActivityRecord r = task.mActivities.get(i);
3605 if (r.app != null && r.app.thread != null) {
3606 mMultiWindowModeChangedActivities.add(r);
3607 }
3608 }
3609
3610 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3611 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3612 }
3613 }
3614
Andrii Kulian933076d2016-03-29 17:04:42 -07003615 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003616 final ActivityStack stack = task.stack;
3617 if (prevStack == null || prevStack == stack
3618 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3619 return;
3620 }
3621
3622 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3623 final ActivityRecord r = task.mActivities.get(i);
3624 if (r.app != null && r.app.thread != null) {
3625 mPipModeChangedActivities.add(r);
3626 }
3627 }
3628
3629 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3630 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3631 }
3632 }
3633
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003634 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003635
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003636 public ActivityStackSupervisorHandler(Looper looper) {
3637 super(looper);
3638 }
3639
Craig Mautnerf3333272013-04-22 10:55:53 -07003640 void activityIdleInternal(ActivityRecord r) {
3641 synchronized (mService) {
3642 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3643 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003644 }
3645
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003646 @Override
3647 public void handleMessage(Message msg) {
3648 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003649 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3650 synchronized (mService) {
3651 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3652 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003653 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003654 }
3655 }
3656 } break;
3657 case REPORT_PIP_MODE_CHANGED_MSG: {
3658 synchronized (mService) {
3659 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3660 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003661 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003662 }
3663 }
3664 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003665 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003666 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3667 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003668 if (mService.mDidDexOpt) {
3669 mService.mDidDexOpt = false;
3670 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3671 nmsg.obj = msg.obj;
3672 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3673 return;
3674 }
3675 // We don't at this point know if the activity is fullscreen,
3676 // so we need to be conservative and assume it isn't.
3677 activityIdleInternal((ActivityRecord)msg.obj);
3678 } break;
3679 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003680 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003681 activityIdleInternal((ActivityRecord)msg.obj);
3682 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003683 case RESUME_TOP_ACTIVITY_MSG: {
3684 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003685 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003686 }
3687 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003688 case SLEEP_TIMEOUT_MSG: {
3689 synchronized (mService) {
3690 if (mService.isSleepingOrShuttingDown()) {
3691 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3692 mSleepTimeout = true;
3693 checkReadyForSleepLocked();
3694 }
3695 }
3696 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003697 case LAUNCH_TIMEOUT_MSG: {
3698 if (mService.mDidDexOpt) {
3699 mService.mDidDexOpt = false;
3700 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3701 return;
3702 }
3703 synchronized (mService) {
3704 if (mLaunchingActivity.isHeld()) {
3705 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3706 if (VALIDATE_WAKE_LOCK_CALLER
3707 && Binder.getCallingUid() != Process.myUid()) {
3708 throw new IllegalStateException("Calling must be system uid");
3709 }
3710 mLaunchingActivity.release();
3711 }
3712 }
3713 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003714 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003715 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003716 } break;
3717 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003718 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003719 } break;
3720 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003721 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003722 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003723 case CONTAINER_CALLBACK_VISIBILITY: {
3724 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003725 final IActivityContainerCallback callback = container.mCallback;
3726 if (callback != null) {
3727 try {
3728 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3729 } catch (RemoteException e) {
3730 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003731 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003732 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003733 case LOCK_TASK_START_MSG: {
3734 // When lock task starts, we disable the status bars.
3735 try {
Jason Monk62515be2014-05-21 16:06:19 -04003736 if (mLockTaskNotify == null) {
3737 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003738 }
Jason Monk62515be2014-05-21 16:06:19 -04003739 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003740 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003741 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003742 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003743 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003744 flags = StatusBarManager.DISABLE_MASK
3745 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003746 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003747 flags = StatusBarManager.DISABLE_MASK
3748 & (~StatusBarManager.DISABLE_BACK)
3749 & (~StatusBarManager.DISABLE_HOME)
3750 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003751 }
3752 getStatusBarService().disable(flags, mToken,
3753 mService.mContext.getPackageName());
3754 }
3755 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003756 if (getDevicePolicyManager() != null) {
3757 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003758 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003759 }
justinzhang5286d3f2014-05-12 17:06:01 -04003760 } catch (RemoteException ex) {
3761 throw new RuntimeException(ex);
3762 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003763 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003764 case LOCK_TASK_END_MSG: {
3765 // When lock task ends, we enable the status bars.
3766 try {
Jason Monk62515be2014-05-21 16:06:19 -04003767 if (getStatusBarService() != null) {
3768 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3769 mService.mContext.getPackageName());
3770 }
3771 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003772 if (getDevicePolicyManager() != null) {
3773 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3774 msg.arg1);
3775 }
Jason Monk62515be2014-05-21 16:06:19 -04003776 if (mLockTaskNotify == null) {
3777 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3778 }
3779 mLockTaskNotify.show(false);
3780 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003781 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003782 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003783 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003784 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003785 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003786 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003787 new LockPatternUtils(mService.mContext)
3788 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003789 }
3790 } catch (SettingNotFoundException e) {
3791 // No setting, don't lock.
3792 }
justinzhang5286d3f2014-05-12 17:06:01 -04003793 } catch (RemoteException ex) {
3794 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003795 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003796 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003797 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003798 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003799 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3800 if (mLockTaskNotify == null) {
3801 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3802 }
3803 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3804 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003805 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3806 final ActivityContainer container = (ActivityContainer) msg.obj;
3807 final IActivityContainerCallback callback = container.mCallback;
3808 if (callback != null) {
3809 try {
3810 callback.onAllActivitiesComplete(container.asBinder());
3811 } catch (RemoteException e) {
3812 }
3813 }
3814 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003815 case LAUNCH_TASK_BEHIND_COMPLETE: {
3816 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003817 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003818 if (r != null) {
3819 handleLaunchTaskBehindCompleteLocked(r);
3820 }
3821 }
3822 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003823
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003824 }
3825 }
3826 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003827
Ying Wangb081a592014-04-22 15:20:16 -07003828 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003829 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3830 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003832 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003833 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003834 ActivityRecord mParentActivity = null;
3835 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003836
Craig Mautnere3a00d72014-04-16 08:31:19 -07003837 boolean mVisible = true;
3838
Craig Mautner4a1cb222013-12-04 16:14:06 -08003839 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003840 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003842 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3843 final static int CONTAINER_STATE_NO_SURFACE = 1;
3844 final static int CONTAINER_STATE_FINISHING = 2;
3845 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3846
3847 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 synchronized (mService) {
3849 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003850 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003851 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003852 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003853 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003854 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003855
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003856 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003857 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003858 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003859 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003860 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003861 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003862 }
3863
3864 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003865 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003866 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003867 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3868 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003869 return;
3870 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003871 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003872 }
3873 }
3874
3875 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003876 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003877 synchronized (mService) {
3878 if (mActivityDisplay != null) {
3879 return mActivityDisplay.mDisplayId;
3880 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003881 }
3882 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003883 }
3884
Jeff Brownca9bc702014-02-11 14:32:56 -08003885 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003886 public int getStackId() {
3887 synchronized (mService) {
3888 return mStackId;
3889 }
3890 }
3891
3892 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003893 public boolean injectEvent(InputEvent event) {
3894 final long origId = Binder.clearCallingIdentity();
3895 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003896 synchronized (mService) {
3897 if (mActivityDisplay != null) {
3898 return mInputManagerInternal.injectInputEvent(event,
3899 mActivityDisplay.mDisplayId,
3900 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3901 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003902 }
3903 return false;
3904 } finally {
3905 Binder.restoreCallingIdentity(origId);
3906 }
3907 }
3908
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003909 @Override
3910 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003911 synchronized (mService) {
3912 if (mContainerState == CONTAINER_STATE_FINISHING) {
3913 return;
3914 }
3915 mContainerState = CONTAINER_STATE_FINISHING;
3916
Craig Mautnerd163e752014-06-13 17:18:47 -07003917 long origId = Binder.clearCallingIdentity();
3918 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003919 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003920 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003921 } finally {
3922 Binder.restoreCallingIdentity(origId);
3923 }
3924 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003925 }
3926
Craig Mautner60257702014-09-17 15:02:33 -07003927 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003928 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003929 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003930 if (mActivityDisplay != null) {
3931 mActivityDisplay.detachActivitiesLocked(mStack);
3932 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003933 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 }
3935 }
3936
3937 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003938 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003939 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003940 }
3941
3942 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003943 public final int startActivityIntentSender(IIntentSender intentSender)
3944 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003945 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3946
3947 if (!(intentSender instanceof PendingIntentRecord)) {
3948 throw new IllegalArgumentException("Bad PendingIntent object");
3949 }
3950
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003951 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003952 Binder.getCallingUid(), mCurrentUser, false,
3953 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003954
3955 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3956 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3957 pendingIntent.key.requestResolvedType);
3958
3959 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3960 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3961 }
3962
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003963 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003964 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003965 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003966 throw new SecurityException(
3967 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3968 }
3969 }
3970
Craig Mautnerdf88d732014-01-27 09:21:32 -08003971 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003972 public IBinder asBinder() {
3973 return this;
3974 }
3975
Craig Mautner4504de52013-12-20 09:06:56 -08003976 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003977 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003978 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003979 }
3980
Craig Mautner4a1cb222013-12-04 16:14:06 -08003981 ActivityStackSupervisor getOuter() {
3982 return ActivityStackSupervisor.this;
3983 }
3984
Craig Mautnerd163e752014-06-13 17:18:47 -07003985 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003986 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003987 }
3988
Craig Mautner6985bad2014-04-21 15:22:06 -07003989 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003990 void setVisible(boolean visible) {
3991 if (mVisible != visible) {
3992 mVisible = visible;
3993 if (mCallback != null) {
3994 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3995 0 /* unused */, this).sendToTarget();
3996 }
3997 }
3998 }
3999
Craig Mautner6985bad2014-04-21 15:22:06 -07004000 void setDrawn() {
4001 }
4002
Craig Mautner1b4bf852014-05-26 15:06:32 -07004003 // You can always start a new task on a regular ActivityStack.
4004 boolean isEligibleForNewTasks() {
4005 return true;
4006 }
4007
Craig Mautnerd163e752014-06-13 17:18:47 -07004008 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004009 detachLocked();
4010 deleteActivityContainer(this);
4011 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004012 }
4013
Craig Mautner34b73df2014-01-12 21:11:08 -08004014 @Override
4015 public String toString() {
4016 return mIdString + (mActivityDisplay == null ? "N" : "A");
4017 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004018 }
4019
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004020 private class VirtualActivityContainer extends ActivityContainer {
4021 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004022 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004023
4024 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4025 super(getNextStackId());
4026 mParentActivity = parent;
4027 mCallback = callback;
4028 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004029 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004030 }
4031
4032 @Override
4033 public void setSurface(Surface surface, int width, int height, int density) {
4034 super.setSurface(surface, width, height, density);
4035
4036 synchronized (mService) {
4037 final long origId = Binder.clearCallingIdentity();
4038 try {
4039 setSurfaceLocked(surface, width, height, density);
4040 } finally {
4041 Binder.restoreCallingIdentity(origId);
4042 }
4043 }
4044 }
4045
4046 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4047 if (mContainerState == CONTAINER_STATE_FINISHING) {
4048 return;
4049 }
4050 VirtualActivityDisplay virtualActivityDisplay =
4051 (VirtualActivityDisplay) mActivityDisplay;
4052 if (virtualActivityDisplay == null) {
4053 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004054 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004055 mActivityDisplay = virtualActivityDisplay;
4056 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004057 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004058 }
4059
4060 if (mSurface != null) {
4061 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004062 }
4063
Craig Mautner6985bad2014-04-21 15:22:06 -07004064 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004065 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004066 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004067 } else {
4068 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004069 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004070 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004071 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004072 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004073 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004074
Craig Mautnerd163e752014-06-13 17:18:47 -07004075 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004076
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004077 if (DEBUG_STACK) Slog.d(TAG_STACK,
4078 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004079 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004080
Craig Mautner6985bad2014-04-21 15:22:06 -07004081 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004082 boolean isAttachedLocked() {
4083 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004084 }
4085
4086 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004087 void setDrawn() {
4088 synchronized (mService) {
4089 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004090 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004091 }
4092 }
4093
Craig Mautner1b4bf852014-05-26 15:06:32 -07004094 // Never start a new task on an ActivityView if it isn't explicitly specified.
4095 @Override
4096 boolean isEligibleForNewTasks() {
4097 return false;
4098 }
4099
Craig Mautnerd163e752014-06-13 17:18:47 -07004100 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004101 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004102 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4103 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4104 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4105 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4106 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004107 }
4108 }
4109
Craig Mautner4a1cb222013-12-04 16:14:06 -08004110 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4111 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004112 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004113 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004114 int mDisplayId;
4115 Display mDisplay;
4116 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004117
Craig Mautner4a1cb222013-12-04 16:14:06 -08004118 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4119 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004120 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004121
Jose Lima4b6c6692014-08-12 17:41:12 -07004122 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004123
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004124 ActivityDisplay() {
4125 }
Craig Mautner4504de52013-12-20 09:06:56 -08004126
Craig Mautner1a70a162014-09-13 12:09:31 -07004127 // After instantiation, check that mDisplay is not null before using this. The alternative
4128 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004129 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004130 final Display display = mDisplayManager.getDisplay(displayId);
4131 if (display == null) {
4132 return;
4133 }
4134 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004135 }
4136
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004137 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004138 mDisplay = display;
4139 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004140 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004141 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004142
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004143 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004144 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004145 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4146 + " onTop=" + onTop);
4147 if (onTop) {
4148 mStacks.add(stack);
4149 } else {
4150 mStacks.add(0, stack);
4151 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004152 }
4153
Craig Mautnere0a38842013-12-16 16:14:02 -08004154 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004155 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004156 + " from displayId=" + mDisplayId);
4157 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004158 }
4159
Jose Lima4b6c6692014-08-12 17:41:12 -07004160 void setVisibleBehindActivity(ActivityRecord r) {
4161 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004162 }
4163
Jose Lima4b6c6692014-08-12 17:41:12 -07004164 boolean hasVisibleBehindActivity() {
4165 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004166 }
4167
Craig Mautner34b73df2014-01-12 21:11:08 -08004168 @Override
4169 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004170 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4171 }
4172 }
4173
4174 class VirtualActivityDisplay extends ActivityDisplay {
4175 VirtualDisplay mVirtualDisplay;
4176
Craig Mautner6985bad2014-04-21 15:22:06 -07004177 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004178 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004179 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4180 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4181 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4182 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004183
4184 init(mVirtualDisplay.getDisplay());
4185
4186 mWindowManager.handleDisplayAdded(mDisplayId);
4187 }
4188
4189 void setSurface(Surface surface) {
4190 if (mVirtualDisplay != null) {
4191 mVirtualDisplay.setSurface(surface);
4192 }
4193 }
4194
4195 @Override
4196 void detachActivitiesLocked(ActivityStack stack) {
4197 super.detachActivitiesLocked(stack);
4198 if (mVirtualDisplay != null) {
4199 mVirtualDisplay.release();
4200 mVirtualDisplay = null;
4201 }
4202 }
4203
4204 @Override
4205 public String toString() {
4206 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004207 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004208 }
Jose Lima58e66d62014-05-27 20:00:27 -07004209
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004210 /**
4211 * Adjust bounds to stay within stack bounds.
4212 *
4213 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4214 * that keep them unchanged, but be contained within the stack bounds.
4215 *
4216 * @param bounds Bounds to be adjusted.
4217 * @param stackBounds Bounds within which the other bounds should remain.
4218 */
4219 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4220 if (stackBounds == null || stackBounds.contains(bounds)) {
4221 return;
4222 }
4223
4224 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4225 final int maxRight = stackBounds.right
4226 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4227 int horizontalDiff = stackBounds.left - bounds.left;
4228 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4229 || (bounds.left + horizontalDiff >= maxRight)) {
4230 horizontalDiff = maxRight - bounds.left;
4231 }
4232 bounds.left += horizontalDiff;
4233 bounds.right += horizontalDiff;
4234 }
4235
4236 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4237 final int maxBottom = stackBounds.bottom
4238 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4239 int verticalDiff = stackBounds.top - bounds.top;
4240 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4241 || (bounds.top + verticalDiff >= maxBottom)) {
4242 verticalDiff = maxBottom - bounds.top;
4243 }
4244 bounds.top += verticalDiff;
4245 bounds.bottom += verticalDiff;
4246 }
4247 }
4248
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004249 ActivityStack findStackBehind(ActivityStack stack) {
4250 // TODO(multi-display): We are only looking for stacks on the default display.
4251 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4252 if (display == null) {
4253 return null;
4254 }
4255 final ArrayList<ActivityStack> stacks = display.mStacks;
4256 for (int i = stacks.size() - 1; i >= 0; i--) {
4257 if (stacks.get(i) == stack && i > 0) {
4258 return stacks.get(i - 1);
4259 }
4260 }
4261 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4262 + " in=" + stacks);
4263 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004264
Jorim Jaggic69bd222016-03-15 14:38:37 +01004265 /**
4266 * Puts a task into resizing mode during the next app transition.
4267 *
4268 * @param taskId the id of the task to put into resizing mode
4269 */
4270 private void setResizingDuringAnimation(int taskId) {
4271 mResizingTasksDuringAnimation.add(taskId);
4272 mWindowManager.setTaskDockedResizing(taskId, true);
4273 }
4274
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004275 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4276 final TaskRecord task;
4277 final int callingUid;
4278 final String callingPackage;
4279 final Intent intent;
4280 final int userId;
4281 final ActivityOptions activityOptions = (bOptions != null)
4282 ? new ActivityOptions(bOptions) : null;
4283 final int launchStackId = (activityOptions != null)
4284 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
4285
4286 if (launchStackId == HOME_STACK_ID) {
4287 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4288 + taskId + " can't be launch in the home stack.");
4289 }
4290 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4291 if (task == null) {
4292 throw new IllegalArgumentException(
4293 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4294 }
4295
4296 if (launchStackId != INVALID_STACK_ID) {
4297 if (launchStackId == DOCKED_STACK_ID) {
4298 mWindowManager.setDockedStackCreateState(
4299 activityOptions.getDockCreateMode(), null /* initialBounds */);
4300
4301 // Defer updating the stack in which recents is until the app transition is done, to
4302 // not run into issues where we still need to draw the task in recents but the
4303 // docked stack is already created.
4304 deferUpdateBounds(HOME_STACK_ID);
4305 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4306 }
4307 if (task.stack.mStackId != launchStackId) {
4308 moveTaskToStackLocked(
4309 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4310 ANIMATE);
4311 }
4312 }
4313
4314 // If the user must confirm credentials (e.g. when first launching a work app and the
4315 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4316 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4317 && task.getRootActivity() != null) {
Jorim Jaggi09c49542016-04-09 01:39:40 -07004318 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004319 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
4320
4321 // If we are launching the task in the docked stack, put it into resizing mode so
4322 // the window renders full-screen with the background filling the void. Also only
4323 // call this at the end to make sure that tasks exists on the window manager side.
4324 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004325 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004326 }
4327 return ActivityManager.START_TASK_TO_FRONT;
4328 }
4329 callingUid = task.mCallingUid;
4330 callingPackage = task.mCallingPackage;
4331 intent = task.intent;
4332 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4333 userId = task.userId;
Jorim Jaggic69bd222016-03-15 14:38:37 +01004334 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4335 null, null, 0, 0, bOptions, userId, null, task);
4336 if (launchStackId == DOCKED_STACK_ID) {
4337 setResizingDuringAnimation(task.taskId);
4338 }
4339 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004340 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004341
4342 /**
4343 * @return a list of activities which are the top ones in each visible stack. The first
4344 * entry will be the focused activity.
4345 */
4346 public List<IBinder> getTopVisibleActivities() {
4347 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4348 if (display == null) {
4349 return Collections.EMPTY_LIST;
4350 }
4351 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4352 final ArrayList<ActivityStack> stacks = display.mStacks;
4353 for (int i = stacks.size() - 1; i >= 0; i--) {
4354 ActivityStack stack = stacks.get(i);
4355 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4356 ActivityRecord top = stack.topActivity();
4357 if (top != null) {
4358 if (stack == mFocusedStack) {
4359 topActivityTokens.add(0, top.appToken);
4360 } else {
4361 topActivityTokens.add(top.appToken);
4362 }
4363 }
4364 }
4365 }
4366 return topActivityTokens;
4367 }
Craig Mautner27084302013-03-25 08:05:25 -07004368}